EGY-AI Logo
EGY-AI
Live GA trading engine
⚙️Advanced10 min read

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:

1
Data layer

Connects to Binance's public WebSocket streams and REST API to get real-time price, order book, and historical candle data.

2
Strategy layer

The genetic algorithm engine. Evolves a population of trading strategies on real candle data, evaluating each one for fitness and breeding better ones.

3
Presentation layer

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:

btcusdt@tradeEvery individual trade — price + quantity + timestamp. Updates multiple times per second.
btcusdt@depth20@100msTop 20 bids and asks, refreshed every 100ms. Powers the live order book.
btcusdt@ticker24-hour statistics — volume, % change, high, low. Updates continuously.
btcusdt@kline_1m1-minute candles as they form. The GA engine uses these for strategy evaluation.

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:

fastMa3–24Short moving average window. Tracks recent price trend.
slowMafastMa+5 to 80Long moving average window. Tracks longer-term trend.
rsiPeriod5–30How many candles to calculate RSI over.
rsiLow10–45RSI threshold below which the strategy won't go short.
rsiHigh55–90RSI threshold above which the strategy won't go long.

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:

fitness = net_equity − (max_drawdown × 2.0)
net_equity = final portfolio value after all fees deducted
max_drawdown × 2.0 = worst peak-to-trough loss, penalised heavily

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.

Transaction costs (0.10% per trade, matching Binance's taker fee) are deducted from every strategy's backtest result. This prevents the GA from evolving high-frequency strategies that look profitable on paper but lose money to fees in practice.

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:

Tournament selection

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.

Arithmetic crossover

The two parents' parameters are mixed using a random blend factor α. Each child parameter = parentA × α + parentB × (1−α). The child inherits from both parents proportionally.

Mutation

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:

BUYfastMA > slowMA AND RSI < rsiHigh — upward momentum, not yet overbought
SELLfastMA < slowMA — downward momentum confirmed
HOLDNo clear directional signal from the current best genome

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.