Evolutionary Trading Strategies with Genetic Algorithms
Overview
I built an evolutionary trading system that uses genetic algorithms to discover profitable trading strategies automatically. The system evolves populations of strategies over generations, selecting the best performers and combining their traits.
How It Works
Genome Structure
Each trading strategy is a "genome" with these genes:
@dataclass
class Genome:
indicators: List[IndicatorGene] # RSI, MACD, Bollinger, etc.
entry_rules: List[RuleGene] # When to buy
exit_rules: List[RuleGene] # When to sell
sizing_method: str # Position sizing
risk_method: str # Stop loss / take profit
max_hold_bars: int # Max holding period
mutation_rate: float # Evolution rate
Evolution Process
- Initialization — Random population of 100 genomes
- Evaluation — Backtest each genome on historical data
- Selection — Tournament selection (best genomes survive)
- Crossover — Combine traits of top performers
- Mutation — Random changes for exploration
- Elitism — Keep top 5% unchanged
- Speciation — Group similar strategies to protect diversity
Walk-Forward Validation
To avoid overfitting, strategies are validated on out-of-sample data:
def walk_forward_test(genome, candles, train_pct=0.7):
split = int(len(candles) * train_pct)
train = candles[:split]
test = candles[split:]
train_result = backtest_genome(genome, train)
test_result = backtest_genome(genome, test)
# Strategy must perform well on BOTH train and test
return test_result if train_result["win_rate"] > 0.5 else None
Results
| Coin | Win Rate | Trades | Sharpe | Walk-Forward |
|---|---|---|---|---|
| BTC | 67.6% | 44 | 1.26 | ✅ |
| HYPE | 58.8% | 78 | 1.26 | ✅ |
| ETH | 55.8% | 52 | 0.98 | ✅ |
| SOL | 55.8% | 48 | 0.95 | ✅ |
Continual Learning
The system adapts to real-time market conditions every 5 minutes:
- Fetch real-time candles from Binance/Bybit/Kraken
- Detect regime changes (trending vs ranging)
- Re-evaluate top genomes on real-time data
- Adjust fitness weights based on live performance
This bridges the gap between training and live trading.
Multi-Exchange Data
SOURCES = [
{"name": "binance", "url": "https://api.binance.com/api/v3/klines"},
{"name": "bybit", "url": "https://api.bybit.com/v5/market/kline"},
{"name": "kraken", "url": "https://api.kraken.com/0/public/OHLC"},
{"name": "hyperliquid", "url": "https://api.hyperliquid.xyz/info"},
]
Next Steps
- Improve win rate to 80% for leveraged trading
- Add microstructure features (order book imbalance)
- Add regime-aware weight adjustment
- Deploy on VPS for 24/7 operation
This is a project from Nexus Trading — an evolutionary trading system.
Top comments (0)