How EGY-AI Works
A full walkthrough of the architecture — from live tick to trade signal
The big picture
EGY-AI is a complete algorithmic trading system built as a web app. It has three main layers working together:
Connects to Binance's public WebSocket streams and REST API to get real-time price, order book, and historical candle data.
The genetic algorithm engine. Evolves a population of trading strategies on real candle data, evaluating each one for fitness and breeding better ones.
The Next.js web app you're looking at. Renders live charts, the order book, the GA dashboard, bot history, and everything else in real time.
Step 1 — Live data arrives
When the app loads, it opens four simultaneous WebSocket connections to Binance:
Additionally, on startup, 500 completed 1-minute candles are fetched from Binance's REST API to give the GA historical data to train on immediately.
Step 2 — The GA engine initialises
When you click "Start" on the GA Bot tab or GA Dashboard, the engine initialises a population of 24 individuals. Each individual is a Genome — a set of 5 strategy parameters:
Each parameter starts at a random value within its allowed range. No hand-tuning, no human intuition — pure random initialisation.
Step 3 — Fitness evaluation
Every 45 seconds, the engine evaluates each individual by backtesting its strategy against the 500 most recent real candles. The fitness score is not just profit — it's a composite metric:
This means a risky strategy that got lucky once won't score as well as a consistent strategy with smaller but steady returns. The 2x drawdown penalty specifically pushes the GA toward capital preservation over gambling.
Step 4 — Selection and evolution
After evaluation, the top 4 individuals (elites) survive unchanged into the next generation. The remaining 20 spots are filled by children created through:
5 random individuals compete, and the best one is selected as a parent. This is done twice to get two parents. Tournament selection favours good solutions while still giving weaker ones a small chance — maintaining diversity.
The two parents' parameters are mixed using a random blend factor α. Each child parameter = parentA × α + parentB × (1−α). The child inherits from both parents proportionally.
Each parameter has a 25% chance of being nudged randomly. This ensures the GA keeps exploring new territory rather than converging prematurely on one solution.
Step 5 — Trade signal generation
After each generation, the best individual's strategy is evaluated on the most recent candle. The signal is determined by:
When the GA Bot is running on the Trading page, these signals trigger real simulated trades on your balance — buying or selling 100 USDT worth of BTC per signal.
Step 6 — Stop-loss protection
EGY-AI has a built-in circuit breaker. If the bot's trading causes the simulated portfolio to drop more than 8% from its value when the bot started, it pauses automatically and shows a warning.
This reflects real professional practice — no serious algorithmic trading system runs without a maximum drawdown limit. It keeps a single bad run from erasing all previous gains.
The singleton architecture
One architectural detail worth understanding: the GA engine is a global singleton store (using Zustand). This means there is exactly one population, one generation counter, and one evolution loop running — shared by every component in the app.
Whether you're on the Trading page, the GA Dashboard, or any other page — they all read from the same engine. Starting the bot on the Trading page shows up immediately on the GA Dashboard, and vice versa. There's no duplication, no disagreement.