How to Use AI Optimization in Backtest Strategy
AI optimization automatically finds the best parameters for your trading strategy. Instead of manually testing hundreds of combinations, enable one checkbox and let AI do the work.
Quick Start (3 Steps)β
Step 1: Configure Basic Settingsβ
Set only the essential parameters:
- data: Connect your OHLC data
- signals: Connect signals from Signal Generator
- initial_capital: Starting capital (e.g., 10000)
- commission: Trading commission (e.g., 0.001 = 0.1%)
- slippage: Expected slippage (e.g., 0.0005 = 0.05%)
Step 2: Enable AI Optimizationβ
Check the "π€ AI Find Best Strategy" checkbox.
All manual parameters (stop loss, take profit, position size) will be hidden - AI determines them automatically.
Step 3: Select Optimization Targetβ
Choose what metric AI should optimize for:
| Target | Best For |
|---|---|
sharpe_ratio | Balanced risk-adjusted returns (recommended) |
total_return | Maximum profit |
profit_factor | Consistent profitability |
sortino_ratio | Minimize downside risk |
win_rate | Highest winning percentage |
Configuration Optionsβ
SL/TP Type Selectionβ
Choose how stop loss and take profit values are calculated:
| Type | Description | Example |
|---|---|---|
percent | Percentage of entry price | 2.0 = 2% |
fixed | Price units/pips | 50 = 50 pips |
atr | ATR multiples | 1.5 = 1.5ΓATR |
Recommendation:
- Stocks/ETFs: Use
percent - Forex: Use
fixedoratr - Crypto: Use
percentoratr
Top Results Countβ
Set ai_top_results_count to control how many results to include (1-50).
Default: 10 best parameter combinations.
Show Trades for Specific Resultβ
Set ai_show_trades_for to see detailed trades for any top result:
1= Best result (default)5= 5th best result10= 10th best result
Use this to verify calculations and compare equity curves.
Understanding AI Outputβ
Best Parametersβ
Ready-to-use optimal settings:
{
"stop_loss_value": 1.8,
"take_profit_value": 4.5,
"position_size": 0.15,
"trailing_stop": true,
"trailing_stop_value": 1.2
}
Best Resultsβ
Performance metrics for optimal configuration:
{
"total_return_pct": 47.3,
"sharpe_ratio": 1.85,
"max_drawdown_pct": 12.4,
"win_rate": 58.2,
"profit_factor": 2.1,
"total_trades": 156
}
Data Analysisβ
AI's volatility analysis of your data:
{
"avg_bar_change_pct": 1.2,
"volatility_95th_pct": 3.5,
"detected_timeframe": "daily",
"bars_analyzed": 5000
}
Recommendationsβ
Actionable insights about your strategy:
- "β Strong Sharpe ratio indicates good risk-adjusted returns"
- "β οΈ Win rate below 50% - relies on large winners"
- "π‘ Consider tighter trailing stop for momentum capture"
Top 10 Resultsβ
Compare alternative parameter combinations:
{
"top_10": [
{"rank": 1, "sharpe": 1.85, "return": 47.3, "drawdown": 12.4},
{"rank": 2, "sharpe": 1.72, "return": 42.1, "drawdown": 10.2},
...
]
}
Recursive Searchβ
If initial results are poor (losing money or low Sharpe), AI automatically expands the search:
| Attempt | What Happens |
|---|---|
| Initial | Test baseline ranges based on volatility |
| Retry 1 | Expand ranges 1.5x wider and tighter |
| Retry 2 | Expand ranges 2.0x wider and tighter |
| Retry 3 | Expand ranges 2.5x wider and tighter |
Trigger conditions:
- Total return < 0%
- Sharpe ratio < 0
- Sharpe < 0.3 AND return < 2%
Output shows recursive search info:
{
"recursive_search": {
"performed": true,
"depth": 2,
"reason": "Expanded ranges to find profitable strategy",
"improved": true
}
}
Interpreting Sharpe Ratioβ
| Sharpe | Rating | Meaning |
|---|---|---|
| < 0 | Poor | Losing money or too risky |
| 0-1 | Below average | Consider revising strategy |
| 1-2 | Good | Solid risk-adjusted returns |
| 2-3 | Very good | Strong performance |
| > 3 | Excellent | Verify not overfitted |
Example Workflowβ
Inputβ
{
"data": "{{workers[1].results}}",
"signals": "{{workers[2].signals}}",
"initial_capital": 10000,
"commission": 0.001,
"ai_optimize": true,
"ai_optimize_target": "sharpe_ratio",
"ai_sl_tp_type": "percent"
}
Output Summaryβ
π€ AI analyzed 280 combinations based on your data's volatility.
Best strategy found with Sharpe ratio of 1.72.
Best Parameters:
- Stop Loss: 1.8%
- Take Profit: 4.5%
- Position Size: 15%
- Trailing Stop: Enabled (1.2%)
Performance:
- Total Return: 34.5%
- Max Drawdown: 8.9%
- Win Rate: 55.3%
- Total Trades: 187
Tips for Better Resultsβ
1. Provide Enough Dataβ
- Minimum: 500 bars
- Recommended: 2000+ bars
- Best: 5000+ bars
2. Clean Your Signalsβ
- Remove duplicate signals
- Use
signal_mode: firstin Signal Generator - Check signal count before backtest
3. Validate with Block Analysisβ
After AI finds good parameters, enable:
{
"analysis_blocks": 6
}
Check consistency score - should be 60+ for reliable strategy.
4. Compare Top Resultsβ
Don't just take #1. Compare top 5:
- Similar performance = robust
- Widely different = possibly overfitted
5. Test Different SL/TP Typesβ
If percent doesn't work well, try atr:
{
"ai_sl_tp_type": "atr"
}
Troubleshootingβ
AI Returns Poor Resultsβ
- Check if signal logic makes sense
- Try different
ai_sl_tp_type - Review data quality (gaps, outliers)
- Consider different optimization target
Too Few Tradesβ
- Strategy may be too restrictive
- Check signal count from Signal Generator
- Consider longer data period
Inconsistent Block Performanceβ
- Strategy may be overfitted
- Try simpler signal conditions
- Increase data sample size
AI optimization transforms backtesting from guesswork to science. Enable it with one click, and let the data tell you what works.