Backtest Strategy - AI-Powered Parameter Optimization
The Backtest Strategy worker is a high-performance backtesting engine with a unique advantage: AI-powered parameter optimization that automatically analyzes your data and finds optimal trading parameters. This guide focuses on leveraging AI optimization for professional-grade strategy development.
Why AI Optimization?
The Traditional Problem
Manual parameter optimization requires:
- Guessing ranges - What SL should I test? 1%? 5%? 10%?
- Running grid searches - Test hundreds of combinations
- Analyzing results - Compare metrics across parameters
- Avoiding overfitting - Hope your results aren't curve-fitted
The AI Solution
ApudFlow's AI optimization:
- Analyzes your data's volatility automatically
- Determines appropriate ranges based on actual price behavior
- Tests all combinations intelligently
- Performs recursive search if results are poor
- Provides actionable recommendations
Enabling AI Optimization
Basic Setup
-
Configure only essential parameters:
data: Your OHLC datasignals: Trading signals from Signal Generator- Field mappings (time, open, high, low, close)
initial_capital,commission,slippage
-
Check "🤖 AI Find Best Strategy"
-
Select optimization target (sharpe_ratio recommended)
-
Run the backtest
All other parameters are hidden - AI determines them automatically!
SL/TP Type Selection
Choose how AI interprets stop loss and take profit values:
| Type | Description | Best For |
|---|---|---|
percent | % of entry price | Stocks, ETFs, indices |
fixed | Price units/pips | Forex, commodities |
atr | ATR multiples | Volatile instruments |
Example:
percentwith SL=2.0 → Stop 2% below entryfixedwith SL=50 → Stop 50 pips below entryatrwith SL=1.5 → Stop 1.5×ATR below entry
How AI Optimization Works
Step 1: Volatility Analysis
AI analyzes your data to understand:
- Average bar price change - Typical movement per bar
- 95th percentile change - Maximum expected movement
- Timeframe detection - Is this tick, intraday, hourly, daily?
- Price characteristics - Range, distribution, patterns
Step 2: Range Generation
Based on volatility, AI calculates appropriate ranges:
| Parameter | How Range is Calculated |
|---|---|
| Stop Loss | 0.5x to 3x average bar change |
| Take Profit | 1x to 5x average bar change |
| Position Size | 5% - 25% of capital |
| Trailing Stop | Disabled, tight, normal, wide |
| Max Hold | Based on detected timeframe |
Example for daily data with 1.5% average move:
- SL range: 0.75% to 4.5%
- TP range: 1.5% to 7.5%
- Position size: 5% to 25%
Step 3: Combination Testing
AI tests all parameter combinations (up to ~600):
- 5-7 SL values × 5-7 TP values × 3-5 size values
- With and without trailing stop
- Multiple trailing distances
Step 4: Recursive Search
If the best result is unprofitable or poor, AI doesn't give up:
| Attempt | Range Expansion | Test Values |
|---|---|---|
| Initial | 1.0x (baseline) | 5-7 values |
| Retry 1 | 1.5x wider + tighter | 7-9 values |
| Retry 2 | 2.0x wider + tighter | 9-11 values |
| Retry 3 | 2.5x wider + tighter | 11-13 values |
Trigger conditions:
total_return < 0%(losing money)sharpe_ratio < 0(negative risk-adjusted return)sharpe_ratio < 0.3ANDreturn < 2%(poor results)
Step 5: Best Result Selection
AI selects the best parameters based on your optimization target and provides:
- Best parameters ready to copy
- Top 10 alternatives to compare
- Full trade list for visualization
- Recommendations for improvement
Optimization Targets
| Target | Description | When to Use |
|---|---|---|
sharpe_ratio | Risk-adjusted return | Recommended - balances return vs risk |
total_return | Maximum profit | Aggressive, accepts higher drawdowns |
profit_factor | Gross profit / gross loss | Consistent profitability |
win_rate | % winning trades | Psychological comfort |
sortino_ratio | Downside-only risk | Focus on losses |
expectancy | Expected value per trade | Probability-based |
Sharpe Ratio (Recommended)
Formula: Sharpe = (Portfolio Return - Risk-Free Rate) / Portfolio Standard Deviation
Where:
- Portfolio return = Total strategy return
- Risk-free rate = Baseline return (e.g., treasury bonds)
- Standard deviation = Measure of volatility/risk
Good Sharpe values:
- < 0: Losing money or high risk
- 0-1: Below average
- 1-2: Good
- 2-3: Very good
-
3: Excellent
AI Output Structure
data_analysis
data_analysis:
bars_analyzed: 5000
avg_bar_change_pct: 1.2
volatility_95th_pct: 3.5
detected_timeframe: "daily"
price_range: min=3500, max=4200
auto_determined_types
{
"auto_determined_types": {
"stop_loss_type": "percent",
"take_profit_type": "percent",
"position_size_type": "percent",
"explanation": "Percent types chosen for universal applicability across price ranges"
}
}
best_parameters
{
"best_parameters": {
"stop_loss_value": 1.8,
"take_profit_value": 4.5,
"position_size": 0.15,
"trailing_stop": true,
"trailing_stop_value": 1.2,
"max_hold_duration": "5d"
}
}
best_results
{
"best_results": {
"initial_capital": 10000,
"final_capital": 14730,
"total_return_pct": 47.3,
"total_return_value": 4730,
"max_drawdown_pct": 12.4,
"max_drawdown_value": 1475,
"sharpe_ratio": 1.85,
"win_rate": 58.2,
"profit_factor": 2.1,
"total_trades": 156
}
}
recursive_search
{
"recursive_search": {
"performed": true,
"depth": 2,
"max_depth": 3,
"reason": "Initial search found negative returns, expanded ranges",
"improved": true
}
}
recommendations
{
"recommendations": [
"✅ Strategy shows strong risk-adjusted returns (Sharpe > 1.5)",
"✅ Profit factor of 2.1 indicates consistent profitability",
"⚠️ Win rate of 58% suggests room for entry refinement",
"💡 Consider tighter trailing stop for momentum capture"
]
}
top_10
Array of 10 best parameter combinations with full metrics:
{
"top_10": [
{
"rank": 1,
"stop_loss": 1.8,
"take_profit": 4.5,
"position_size": 0.15,
"trailing_stop": true,
"results": {
"sharpe_ratio": 1.85,
"total_return_pct": 47.3,
"max_drawdown_pct": 12.4
}
}
]
}
Verifying Top Results
Use ai_show_trades_for to see detailed trades for any top result:
Default (Best Result)
{
"ai_show_trades_for": 1
}
Verify 5th Best
{
"ai_show_trades_for": 5
}
What You Get
When you change ai_show_trades_for:
trades: Full list of trades for that specific resulttrades_summary: Breakdown by exit reasonequity_curve: Equity over timedrawdown_curve: Drawdown over time
Use Cases
- ✅ Verify calculations are correct
- ✅ Compare equity curves between different parameters
- ✅ Visualize trades on chart
- ✅ Understand why certain parameters rank higher
Trade Details for Charting
Each trade includes complete information:
{
"trades": [
{
"entry_time": "2024-01-15T09:30:00+00:00",
"exit_time": "2024-01-15T14:22:00+00:00",
"direction": "long",
"entry_price": 150.25,
"exit_price": 154.31,
"stop_loss": 148.90,
"take_profit": 154.31,
"size": 66,
"profit": 267.96,
"profit_pct": 2.7,
"exit_reason": "take_profit",
"bars_held": 58,
"commission": 2.15
}
]
}
Exit Reasons
| Exit | Description |
|---|---|
take_profit | Price hit TP level |
stop_loss | Price hit SL level |
trailing_stop | Trailing stop triggered |
signal | Opposite signal or close signal |
max_duration | Held too long |
time_close | Daily close time reached |
Walk-Forward Validation
Don't trust overall results alone! Use block analysis:
{
"analysis_blocks": 6
}
How It Works
Your data is split into N equal periods, and the strategy is tested independently on each.
Consistency Score
| Score | Rating | Interpretation |
|---|---|---|
| 80-100 | ⭐ Excellent | Reliable across all periods |
| 60-80 | ✅ Good | Minor variations, generally reliable |
| 40-60 | ⚠️ Moderate | Review needed, possible overfit |
| 20-40 | ❌ Poor | Likely overfitted |
| 0-20 | 🚫 Very Poor | Strategy fails multiple periods |
Block Output
{
"block_analysis": {
"consistency_score": 78,
"blocks": [
{
"block": 1,
"start": "2023-01-01",
"end": "2023-02-28",
"return_pct": 12.3,
"sharpe": 1.5,
"trades": 28
}
]
}
}
Risk Management Features
Position Sizing Types
| Type | Description | Example |
|---|---|---|
percent | % of capital per trade | 0.1 = 10% |
fixed | Fixed dollar amount | 1000 = $1000 |
risk_based | Based on stop loss distance | 0.02 = risk 2% |
Risk-based sizing is professional:
Formula: Position Size = (Capital × Risk %) / Stop Loss Distance
Stop Loss Types
| Type | Description | Example |
|---|---|---|
percent | % of entry price | 2.0 = 2% |
atr | ATR multiple | 1.5 = 1.5×ATR |
fixed | Price distance | 5.0 = $5 |
Take Profit Types
| Type | Description | Example |
|---|---|---|
percent | % of entry price | 4.0 = 4% |
atr | ATR multiple | 3.0 = 3×ATR |
rr_ratio | Risk:Reward ratio | 3.0 = 1:3 |
fixed | Price distance | 10.0 = $10 |
Trailing Stop
When enabled:
- Long: Stop moves UP as price rises (never down)
- Short: Stop moves DOWN as price falls (never up)
- Distance: Percentage from current price
Time-Based Rules
Max Hold Duration
{
"max_hold_duration": "2h"
}
Close positions after 2 hours regardless of profit.
Formats: 30m, 2h, 1d, 1w
Close at Time
{
"close_at_time": "16:00",
"close_at_time_offset": -5
}
Close all positions at 4 PM EST daily.
Trading Window
{
"trading_window_start": "9:30",
"trading_window_end": "15:30",
"close_at_time_offset": -5
}
Only open positions during US market hours.
Manual Optimization
For advanced users who want full control:
Range Format
| Format | Example | Result |
|---|---|---|
| Single | 2 | [2.0] |
| List | 1,2,3,4 | [1, 2, 3, 4] |
| Range | 1-5 | [1, 2, 3, 4, 5] |
| Step | 1-5:0.5 | [1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5] |
Example
{
"optimize_sl": "1-3:0.5",
"optimize_tp": "2-6:1",
"optimize_size": "0.05,0.1,0.15,0.2",
"optimize_target": "sharpe_ratio"
}
Tests: 5 × 5 × 4 = 100 combinations
Complete AI Optimization Example
Input Configuration
{
"data": "{{workers[0].results}}",
"signals": "{{workers[1].signals}}",
"initial_capital": 10000,
"commission": 0.001,
"slippage": 0.0005,
"ai_optimize": true,
"ai_optimize_target": "sharpe_ratio",
"ai_sl_tp_type": "percent"
}
Output Summary
{
"ai_optimization": true,
"message": "🤖 AI analyzed 280 combinations based on your data's volatility. Best parameters found after 1 recursive search.",
"data_analysis": {
"avg_bar_change_pct": 0.85,
"detected_timeframe": "hourly"
},
"best_parameters": {
"stop_loss_value": 1.2,
"take_profit_value": 3.6,
"position_size": 0.12,
"trailing_stop": true,
"trailing_stop_value": 0.8
},
"best_results": {
"total_return_pct": 34.5,
"sharpe_ratio": 1.72,
"max_drawdown_pct": 8.9,
"win_rate": 55.3,
"total_trades": 187
},
"recommendations": [
"✅ Strong Sharpe ratio indicates good risk-adjusted returns",
"✅ Trailing stop captures additional profit",
"💡 Drawdown of 8.9% is within acceptable range"
]
}
Comparing AI vs Manual Optimization
AI Optimization
Pros:
- ✅ Zero configuration needed
- ✅ Automatically adapts to your data
- ✅ Recursive search for better results
- ✅ Tests trailing stops automatically
- ✅ Includes time-based exits
Cons:
- ⚠️ May miss extreme parameter values
- ⚠️ Fixed optimization targets
Manual Optimization
Pros:
- ✅ Full control over ranges
- ✅ Test specific values you want
- ✅ Combine with block analysis
Cons:
- ⚠️ Requires guessing appropriate ranges
- ⚠️ Risk of testing irrelevant values
- ⚠️ More time-consuming
Recommendation
Start with AI optimization, review results, then use manual optimization for fine-tuning if needed.
Best Practices
1. Use Enough Data
At least 1000 bars for reliable results, 5000+ for AI optimization.
2. Validate with Block Analysis
Always check consistency score before trusting results.
3. Be Suspicious of Perfect Results
Sharpe > 3 or win rate > 70% may indicate overfitting.
4. Compare Multiple Top Results
Don't just take #1 - compare top 5 for robustness.
5. Test Different SL/TP Types
If percent doesn't work well, try ATR-based.
6. Review Trade List
Look at individual trades to understand strategy behavior.
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 Many Trades
- Signal Generator may need filtering
- Add cooldown or
signal_mode: first - Check for duplicate signals
Inconsistent Block Performance
- Strategy may be overfitted
- Try simpler conditions
- Increase sample size
- Review for market regime sensitivity
AI optimization transforms backtesting from art to science. Instead of guessing parameters, let the data tell you what works. Combined with walk-forward validation, you can develop strategies with confidence that they'll perform in live markets.