DEV Community

Nexus Intelligence Research
Nexus Intelligence Research

Posted on

Evolutionary Trading Strategies with Genetic Algorithms in Python

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
Enter fullscreen mode Exit fullscreen mode

Evolution Process

  1. Initialization — Random population of 100 genomes
  2. Evaluation — Backtest each genome on historical data
  3. Selection — Tournament selection (best genomes survive)
  4. Crossover — Combine traits of top performers
  5. Mutation — Random changes for exploration
  6. Elitism — Keep top 5% unchanged
  7. 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
Enter fullscreen mode Exit fullscreen mode

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:

  1. Fetch real-time candles from Binance/Bybit/Kraken
  2. Detect regime changes (trending vs ranging)
  3. Re-evaluate top genomes on real-time data
  4. 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"},
]
Enter fullscreen mode Exit fullscreen mode

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)