Skip to main content

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:

  1. Guessing ranges - What SL should I test? 1%? 5%? 10%?
  2. Running grid searches - Test hundreds of combinations
  3. Analyzing results - Compare metrics across parameters
  4. Avoiding overfitting - Hope your results aren't curve-fitted

The AI Solution

ApudFlow's AI optimization:

  1. Analyzes your data's volatility automatically
  2. Determines appropriate ranges based on actual price behavior
  3. Tests all combinations intelligently
  4. Performs recursive search if results are poor
  5. Provides actionable recommendations

Enabling AI Optimization

Basic Setup

  1. Configure only essential parameters:

    • data: Your OHLC data
    • signals: Trading signals from Signal Generator
    • Field mappings (time, open, high, low, close)
    • initial_capital, commission, slippage
  2. Check "🤖 AI Find Best Strategy"

  3. Select optimization target (sharpe_ratio recommended)

  4. 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:

TypeDescriptionBest For
percent% of entry priceStocks, ETFs, indices
fixedPrice units/pipsForex, commodities
atrATR multiplesVolatile instruments

Example:

  • percent with SL=2.0 → Stop 2% below entry
  • fixed with SL=50 → Stop 50 pips below entry
  • atr with 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:

ParameterHow Range is Calculated
Stop Loss0.5x to 3x average bar change
Take Profit1x to 5x average bar change
Position Size5% - 25% of capital
Trailing StopDisabled, tight, normal, wide
Max HoldBased 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

If the best result is unprofitable or poor, AI doesn't give up:

AttemptRange ExpansionTest Values
Initial1.0x (baseline)5-7 values
Retry 11.5x wider + tighter7-9 values
Retry 22.0x wider + tighter9-11 values
Retry 32.5x wider + tighter11-13 values

Trigger conditions:

  • total_return < 0% (losing money)
  • sharpe_ratio < 0 (negative risk-adjusted return)
  • sharpe_ratio < 0.3 AND return < 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

TargetDescriptionWhen to Use
sharpe_ratioRisk-adjusted returnRecommended - balances return vs risk
total_returnMaximum profitAggressive, accepts higher drawdowns
profit_factorGross profit / gross lossConsistent profitability
win_rate% winning tradesPsychological comfort
sortino_ratioDownside-only riskFocus on losses
expectancyExpected value per tradeProbability-based

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": {
"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 result
  • trades_summary: Breakdown by exit reason
  • equity_curve: Equity over time
  • drawdown_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

ExitDescription
take_profitPrice hit TP level
stop_lossPrice hit SL level
trailing_stopTrailing stop triggered
signalOpposite signal or close signal
max_durationHeld too long
time_closeDaily 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

ScoreRatingInterpretation
80-100⭐ ExcellentReliable across all periods
60-80✅ GoodMinor variations, generally reliable
40-60⚠️ ModerateReview needed, possible overfit
20-40❌ PoorLikely overfitted
0-20🚫 Very PoorStrategy 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

TypeDescriptionExample
percent% of capital per trade0.1 = 10%
fixedFixed dollar amount1000 = $1000
risk_basedBased on stop loss distance0.02 = risk 2%

Risk-based sizing is professional:

Formula: Position Size = (Capital × Risk %) / Stop Loss Distance

Stop Loss Types

TypeDescriptionExample
percent% of entry price2.0 = 2%
atrATR multiple1.5 = 1.5×ATR
fixedPrice distance5.0 = $5

Take Profit Types

TypeDescriptionExample
percent% of entry price4.0 = 4%
atrATR multiple3.0 = 3×ATR
rr_ratioRisk:Reward ratio3.0 = 1:3
fixedPrice distance10.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

FormatExampleResult
Single2[2.0]
List1,2,3,4[1, 2, 3, 4]
Range1-5[1, 2, 3, 4, 5]
Step1-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

  1. Check if signal logic makes sense
  2. Try different ai_sl_tp_type
  3. Review data quality (gaps, outliers)
  4. Consider different optimization target

Too Many Trades

  1. Signal Generator may need filtering
  2. Add cooldown or signal_mode: first
  3. Check for duplicate signals

Inconsistent Block Performance

  1. Strategy may be overfitted
  2. Try simpler conditions
  3. Increase sample size
  4. 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.