GAFreqTrade is a Genetic Algorithm system that automatically generates and optimizes trading strategies for Freqtrade. It uses evolutionary algorithms to discover profitable trading strategies through generations of mutation, crossover, and selection.
- Python 3.9+
- Freqtrade installed (optional for mock testing)
- Required packages:
pip install -r requirements.txt
Run a quick test evolution without real backtesting:
python run_evolution.py --generations 5 --population 10This will:
- Generate 10 random strategies
- Evaluate them using mock fitness scores
- Evolve for 5 generations
- Save results in
strategies/generated/andcheckpoints/
Once you have Freqtrade configured with historical data:
python run_evolution.py --no-mock --generations 20 --population 50This will use real Freqtrade backtesting to evaluate strategies.
If evolution was interrupted, resume from the last checkpoint:
python run_evolution.py --resume checkpoints/population_gen_0010.pklCreate your own config and use it:
python run_evolution.py --config config/ga_config.yaml --generations 100--config CONFIG Path to configuration file
--population POPULATION Population size
--generations GENERATIONS Number of generations
--elite ELITE Elite size (preserved best strategies)
--resume CHECKPOINT Resume from checkpoint file
--no-mock Use real Freqtrade backtesting
--checkpoint-interval N Save checkpoint every N generations
--log-level LEVEL Logging level (DEBUG|INFO|WARNING|ERROR)
--quiet Suppress output
GENERATION 5
============================================================
Evaluating generation 5...
Evaluating Gen005_Strat_001...
Fitness: 0.6445, Profit: 15.23%
...
Top 5 Strategies:
1. Gen005_Strat_012: Fitness=0.7234
2. Gen004_Strat_007: Fitness=0.7021
...
FINAL TOP 10 STRATEGIES:
1. Gen050_Strat_034
Fitness: 0.8123
Indicators: rsi, macd, bb, ema
EVOLUTION TREND:
Gen 0: Best=0.6800, Avg=0.4885, Profit=29.86%
Gen 10: Best=0.7234, Avg=0.6012, Profit=35.42%
...
Fitness is calculated from multiple metrics:
- Profit (30%): Total profit percentage
- Sharpe Ratio (10%): Risk-adjusted returns
- Drawdown (25%): Maximum drawdown (lower is better)
- Win Rate (15%): Percentage of winning trades
- Stability (15%): Consistency metrics
- Trade Count (5%): Penalty for too few/many trades
Fitness scores range from 0.0 (worst) to 1.0 (best).
Generated strategies are saved in strategies/generated/:
strategies/generated/
├── Strategy_Gen000_Strat_001.py
├── Strategy_Gen000_Strat_002.py
├── ...
└── Strategy_Gen050_Strat_100.py
Each strategy is a complete Freqtrade strategy that can be:
- Backtested independently
- Hyperoptimized with Freqtrade
- Used for live trading (after thorough validation)
Checkpoints are saved in checkpoints/:
checkpoints/
├── population_gen_0000.pkl # Binary checkpoint
├── population_gen_0000.json # Human-readable summary
├── population_gen_0010.pkl
├── population_gen_0010.json
└── ...
The JSON files show:
- Generation statistics
- Top 10 strategies
- Genealogy information
Edit config/ga_config.yaml to customize:
# Population Parameters
population_size: 100
elite_size: 10
generations: 1000
# Genetic Operators
mutation_rate: 0.20
crossover_rate: 0.70
tournament_size: 5
# Fitness Weights
fitness_weights:
profit: 0.30
sharpe: 0.10
drawdown: 0.25
winrate: 0.15
stability: 0.20
trade_penalty: 0.05- Small (10-20): Fast testing, may converge to local optima
- Medium (50-100): Good balance
- Large (200+): Explores more solutions, slower
- Quick test: 5-10 generations
- Real optimization: 50-100 generations
- Long-term: 500+ generations
- Should be ~5-10% of population
- Too small: May lose good strategies
- Too large: Reduces diversity
- Low (0.05-0.10): Conservative, slow evolution
- Medium (0.15-0.25): Balanced
- High (0.30+): Aggressive, high diversity
# Quick test with mock data
python run_evolution.py --generations 5 --population 10# First real run with moderate population
python run_evolution.py --no-mock --generations 50 --population 50# Extended run with larger population
python run_evolution.py --no-mock --generations 200 --population 100 \
--checkpoint-interval 10# Resume and continue for 50 more generations
python run_evolution.py --resume checkpoints/population_gen_0100.pkl \
--generations 150Watch the logs in real-time:
tail -f logs/evolution_*.logCheck checkpoints:
cat checkpoints/population_gen_0050.json | jq '.top_10'- Install Freqtrade:
pip install freqtrade[all] - Or use mock mode: don't pass
--no-mock
- Increase timeout in config:
backtest_timeout: 600 - Use shorter timeranges
- Reduce data size
- Reduce population size:
--population 50 - Reduce parallel backtests in config
- Use smaller timeranges
- Increase mutation rate for more diversity
- Increase population size
- Run for more generations
- Adjust fitness weights to prioritize different metrics
After running evolution:
- Analyze Results: Review top strategies in checkpoints
- Manual Review: Check generated strategies for logic
- Further Backtesting: Test top strategies on different timeframes
- Hyperopt: Fine-tune parameters with Freqtrade hyperopt
- Paper Trading: Test in dry-run mode before live
- Live Trading: Deploy only after thorough validation
Prioritize low drawdown:
fitness_weights:
profit: 0.20
sharpe: 0.10
drawdown: 0.40 # Increased
winrate: 0.15
stability: 0.15Run multiple populations in parallel and exchange strategies periodically.
Use AI to suggest indicator combinations and conditions.
For issues, questions, or contributions:
- Check the ARCHITECTURE.md for system design
- See IMPLEMENTATION_PLAN.md for development roadmap
- Review TODO.md for planned features
Happy evolving! 🧬📈