AI Strategy Optimizer
Let AI find optimal parameters for your strategy. Weeks of manual testing compressed into overnight insights.
Prerequisites
The AI Optimizer (Q-EVOLVE) automatically tests thousands of parameter combinations to find what works best. Instead of manually trying RSI thresholds from 20 to 40, let the AI explore while you sleep.
How It Works
- You define a strategy with tunable parameters
- Specify ranges for each parameter to explore
- Q-EVOLVE runs backtests on combinations
- AI learns which directions improve performance
- Best configurations are surfaced with explanations
Setting Up Optimization
Mark parameters you want to optimize with ranges:
# Define parameters with optimization ranges
CONFIG = {
'rsi_period': {
'default': 14,
'min': 7,
'max': 28,
'step': 1
},
'rsi_oversold': {
'default': 30,
'min': 20,
'max': 40,
'step': 5
},
'rsi_overbought': {
'default': 70,
'min': 60,
'max': 80,
'step': 5
},
'stop_loss': {
'default': 0.05,
'min': 0.02,
'max': 0.10,
'step': 0.01
}
}
def run_strategy(rsi_period, rsi_oversold, rsi_overbought, stop_loss):
data = fetch_data('BTC/USD', '2023-01-01', '2024-01-01', '1d')
closes = data['Close']
rsi = vbt.RSI.run(closes, window=rsi_period).rsi
entries = rsi < rsi_oversold
exits = rsi > rsi_overbought
pf = vbt.Portfolio.from_signals(
close=closes,
entries=entries,
exits=exits,
sl_stop=stop_loss,
init_cash=10000
)
return pf.stats()Optimization Targets
Choose what the optimizer should maximize:
- Sharpe Ratio (default): Best risk-adjusted returns
- Total Return: Maximum profit (ignores risk)
- Sortino Ratio: Maximize return per downside risk
- Calmar Ratio: Best return per drawdown
- Custom: Define your own objective function
Understanding Results
The optimizer returns the best configurations found:
Optimization Results
────────────────────────────────────────────
Configurations tested: 2,847
Time elapsed: 4h 23m
Best Sharpe: 1.92 (baseline was 1.45)
Top 3 Configurations:
#1 (Sharpe: 1.92)
rsi_period: 12
rsi_oversold: 25
rsi_overbought: 72
stop_loss: 0.04
#2 (Sharpe: 1.87)
rsi_period: 10
rsi_oversold: 28
rsi_overbought: 68
stop_loss: 0.05
#3 (Sharpe: 1.84)
rsi_period: 14
rsi_oversold: 22
rsi_overbought: 75
stop_loss: 0.03
Insights:
- Shorter RSI periods (10-14) outperform longer ones
- Asymmetric thresholds work better (lower oversold)
- Tighter stops (3-5%) improve risk-adjusted returnsAvoiding Overfitting
The optimizer includes safeguards against overfitting—finding parameters that work perfectly on historical data but fail in live trading.
Walk-Forward Validation
The optimizer automatically tests on out-of-sample data:
Walk-Forward Results:
────────────────────────────────────────────
Training period: 2022-01-01 to 2023-06-30
Best config Sharpe: 2.15
Validation period: 2023-07-01 to 2024-01-01
Same config Sharpe: 1.78
Degradation: -17% (acceptable)
Rule of thumb:
< 20% degradation: Parameters likely robust
20-40% degradation: Moderate overfitting concern
> 40% degradation: Significant overfittingParameter Stability
Good parameters work across a range, not just one magic number:
Parameter Stability Analysis:
────────────────────────────────────────────
rsi_oversold = 25 ± 5
Sharpe at oversold = 20: 1.75
Sharpe at oversold = 25: 1.92 ← optimal
Sharpe at oversold = 30: 1.81
Stable: Small changes don't destroy performance
Compare to unstable parameter:
Sharpe at period = 11: 0.95
Sharpe at period = 12: 1.92 ← optimal
Sharpe at period = 13: 1.02
Unstable: Avoid - likely overfitBest Practices
- Start with wide ranges, then narrow based on results
- Optimize 2-4 parameters at a time, not 10
- Always validate on out-of-sample data
- Prefer stable parameters over slightly better unstable ones
- Re-run optimization periodically as markets change
- Don't optimize on the same data you'll trade
Understanding Backtesting Metrics
Learn to interpret Sharpe ratio, drawdown, win rate, and other institutional-grade performance metrics.
Using Cody: Your AI Trading Assistant
Learn how to use Cody to generate strategies, optimize parameters, and understand your code.
Sample Strategies & Templates
Get started quickly with built-in strategy templates. Learn from working examples you can customize.