Skip to content

Optimizing Strategy Parameters

Guide to hyperparameter optimization using Freqtrade's hyperopt engine.

Overview

TradAI integrates with Freqtrade's hyperopt for automated parameter optimization:

  • Hyperopt: Bayesian optimization for strategy parameters
  • Loss Functions: Sharpe, Sortino, Calmar, and more
  • Walk-Forward Analysis: Detect overfitting with train/test splits
  • MLflow Tracking: Automatic logging of optimization runs

Quick Start

# Basic optimization with Sharpe loss
tradai strategy optimize MomentumV2 --epochs 100 --loss sharpe

# With date range
tradai strategy optimize MomentumV2 \
    --epochs 200 \
    --loss sortino \
    --start 2024-01-01 \
    --end 2024-06-01

# Apply best params to strategy config
tradai strategy optimize MomentumV2 --epochs 100 --apply

Optimization Commands

tradai strategy optimize

Full-featured hyperparameter optimization.

tradai strategy optimize STRATEGY [OPTIONS]
Option Description
--epochs, -e Number of optimization epochs (default: 100)
--loss, -l Loss function: sharpe, sortino, calmar, profit, drawdown, winrate
--start Start date (YYYY-MM-DD)
--end End date (YYYY-MM-DD)
--symbol, -s Trading pair (repeatable)
--space Parameter space: buy, sell, roi, stoploss, trailing, all
--jobs, -j Parallel jobs (-1 for all cores)
--min-trades Minimum trades for valid result (default: 10)
--apply Apply best params to strategy config
--walk-forward Run walk-forward analysis
--wf-periods Number of walk-forward periods (default: 4)
--local Run locally instead of via backend

tradai strategy optimize-preset

Run optimization with predefined configurations.

tradai strategy optimize-preset STRATEGY PRESET [OPTIONS]

Available Presets:

Preset Epochs Spaces Use Case
quick 50 buy Fast iteration during development
standard 200 buy, sell Standard parameter tuning
thorough 500 buy, sell, roi, stoploss Complete optimization
production 1000 all Production-ready with walk-forward
# Quick test
tradai strategy optimize-preset MomentumV2 quick

# Production optimization
tradai strategy optimize-preset MomentumV2 production \
    --start 2024-01-01 \
    --end 2024-06-01 \
    --apply

tradai strategy optimize-results

View past optimization runs from MLflow.

tradai strategy optimize-results [OPTIONS]
Option Description
--strategy, -s Filter by strategy name
--limit, -n Number of results (default: 10)
--mlflow MLflow tracking URI

Loss Functions

Choose the loss function that matches your optimization goal:

Loss Function Optimizes For Use When
sharpe Risk-adjusted returns General purpose (recommended)
sortino Downside risk-adjusted returns Focus on downside protection
calmar Return/max drawdown ratio Minimize drawdowns
profit Total profit Maximize absolute returns
drawdown Maximum drawdown Conservative strategies
winrate Win rate with profit High win rate preference
# Optimize for Sharpe ratio (default)
tradai strategy optimize MomentumV2 --loss sharpe

# Optimize for minimum drawdown
tradai strategy optimize MomentumV2 --loss drawdown

# Optimize for Sortino (focus on downside)
tradai strategy optimize MomentumV2 --loss sortino

Parameter Spaces

Control which parameters to optimize:

Space Parameters Impact
buy Entry signal parameters When to enter trades
sell Exit signal parameters When to exit trades
roi Return-on-investment targets Profit-taking levels
stoploss Stop loss threshold Risk per trade
trailing Trailing stop settings Dynamic stop loss
all All of the above Full optimization
# Only optimize buy signals (fastest)
tradai strategy optimize MomentumV2 --space buy

# Optimize entry and exit
tradai strategy optimize MomentumV2 --space buy --space sell

# Full optimization (slowest)
tradai strategy optimize MomentumV2 --space all

Walk-Forward Analysis

Walk-forward analysis detects overfitting by validating on out-of-sample data.

How It Works

  1. Split date range into multiple windows
  2. For each window:
  3. Optimize on training period
  4. Validate on test period
  5. Compare train vs test performance
  6. Flag potential overfitting

Running Walk-Forward

tradai strategy optimize MomentumV2 \
    --epochs 200 \
    --walk-forward \
    --wf-periods 4 \
    --start 2024-01-01 \
    --end 2024-06-01

Understanding Results

Walk-Forward Analysis (4 periods)

Window Results
┏━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━┓
┃ Window ┃ Train Period                 ┃ Train Sharpe  ┃ Test Period                ┃ Test Sharpe  ┃ Ratio ┃
┡━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━━━┩
│   1    │ 2024-01-01 to 2024-02-15    │ 2.45          │ 2024-02-15 to 2024-03-01   │ 1.82         │ 1.35  │
│   2    │ 2024-03-01 to 2024-04-15    │ 3.12          │ 2024-04-15 to 2024-05-01   │ 0.45         │ 6.93! │
│   3    │ 2024-05-01 to 2024-06-15    │ 2.78          │ 2024-06-15 to 2024-07-01   │ 2.15         │ 1.29  │
└────────┴──────────────────────────────┴───────────────┴────────────────────────────┴──────────────┴───────┘

Summary
┃ Avg Train Sharpe: 2.78                                                               ┃
┃ Avg Test Sharpe:  1.47                                                               ┃
┃ Overfit Ratio:    1.89                                                               ┃

! High overfit ratio - train performance significantly better than test

Overfitting Indicators

Indicator Warning Threshold Meaning
Overfit Ratio > 2.0 Train performance much better than test
Negative Test Sharpe < 0 Strategy fails on unseen data
High Ratio Variance Window-to-window Inconsistent performance

MLflow Integration

Optimization results are automatically logged to MLflow when MLFLOW_TRACKING_URI is set.

View Logged Runs

# Set MLflow URI
export MLFLOW_TRACKING_URI=http://localhost:5000

# View recent optimizations
tradai strategy optimize-results

# Filter by strategy
tradai strategy optimize-results --strategy MomentumV2

Logged Metrics

Each optimization run logs:

Metric Description
best_loss Best loss value achieved
epochs_run Total epochs completed
best_epoch Epoch with best result
total_trades Trades in best result
win_rate Win rate of best result
best_profit Profit percentage

Logged Parameters

Parameter Description
strategy Strategy name
epochs Configured epochs
loss_function Loss function used
spaces Parameter spaces
param_* Best parameter values

Applying Optimized Parameters

Auto-Apply

Use --apply to automatically save best parameters:

tradai strategy optimize MomentumV2 --epochs 100 --apply

This saves parameters to: - strategies/momentum-v2/tradai.yaml (if exists) - configs/optimized/momentum_v2_YYYYMMDD_HHMMSS.yaml (otherwise)

Manual Application

Optimization results show the best parameters:

Best Parameters
┏━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┓
┃ Parameter          ┃ Value   ┃
┡━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━┩
│ buy_rsi_threshold  │ 28      │
│ buy_adx_threshold  │ 22      │
│ sell_rsi_threshold │ 72      │
│ stoploss           │ -0.085  │
│ trailing_stop      │ true    │
│ trailing_stop_pos  │ 0.012   │
└────────────────────┴─────────┘

Copy these to your strategy's populate_indicators or config file.

Strategy Preparation

For optimization to work, your strategy must define hyperopt parameters:

from freqtrade.strategy import IntParameter, DecimalParameter

class MomentumV2(TradAIStrategy):
    # Optimizable parameters
    buy_rsi_threshold = IntParameter(20, 40, default=30, space="buy")
    buy_adx_threshold = IntParameter(15, 35, default=25, space="buy")
    sell_rsi_threshold = IntParameter(60, 80, default=70, space="sell")

    def populate_entry_trend(self, dataframe, metadata):
        dataframe.loc[
            (dataframe['rsi'] < self.buy_rsi_threshold.value) &
            (dataframe['adx'] > self.buy_adx_threshold.value),
            'enter_long'] = 1
        return dataframe

    def populate_exit_trend(self, dataframe, metadata):
        dataframe.loc[
            dataframe['rsi'] > self.sell_rsi_threshold.value,
            'exit_long'] = 1
        return dataframe

Parameter Types

Type Use Case Example
IntParameter Integer values RSI threshold, period
DecimalParameter Float values Stop loss, trailing
BoolParameter True/False Enable trailing stop
CategoricalParameter Choices Indicator type

Best Practices

1. Start with Quick Optimization

# First, quick test to verify strategy works
tradai strategy optimize MomentumV2 --epochs 50 --space buy

2. Use Adequate Data

# Minimum 6 months for reliable optimization
tradai strategy optimize MomentumV2 \
    --epochs 200 \
    --start 2024-01-01 \
    --end 2024-06-01

3. Always Run Walk-Forward for Production

# Before deploying, validate with walk-forward
tradai strategy optimize MomentumV2 \
    --epochs 500 \
    --walk-forward \
    --wf-periods 4

4. Set Minimum Trades

# Require at least 20 trades for statistical significance
tradai strategy optimize MomentumV2 --min-trades 20

5. Compare Multiple Loss Functions

# Run with different objectives
tradai strategy optimize MomentumV2 --loss sharpe
tradai strategy optimize MomentumV2 --loss sortino
tradai strategy optimize MomentumV2 --loss calmar

# Compare results in MLflow
tradai strategy optimize-results --strategy MomentumV2

Typical Workflow

# 1. Quick test during development
tradai strategy optimize-preset MomentumV2 quick

# 2. Standard optimization when strategy is stable
tradai strategy optimize-preset MomentumV2 standard \
    --start 2024-01-01 \
    --end 2024-06-01

# 3. Thorough optimization before backtesting
tradai strategy optimize-preset MomentumV2 thorough \
    --start 2024-01-01 \
    --end 2024-06-01 \
    --apply

# 4. Production validation with walk-forward
tradai strategy optimize-preset MomentumV2 production \
    --start 2024-01-01 \
    --end 2024-06-01 \
    --apply

# 5. View optimization history
tradai strategy optimize-results --strategy MomentumV2

Troubleshooting

No Trades Found

Error: Optimization found 0 valid epochs

Solutions: - Increase date range - Lower --min-trades - Check strategy signals generate trades

Freqtrade Not Found

Error: freqtrade command not found

Solution:

uv pip install freqtrade

MLflow Not Logging

Warning: MLFLOW_TRACKING_URI not set

Solution:

export MLFLOW_TRACKING_URI=http://localhost:5000
just up  # Start MLflow service

High Overfit Ratio

! Overfit ratio 3.5 - results may not generalize

Solutions: - Reduce parameter count - Increase min_trades - Use longer training periods - Add regularization to strategy

Quick Reference

Command Description
tradai strategy optimize STRATEGY Run hyperopt optimization
tradai strategy optimize-preset STRATEGY PRESET Use predefined config
tradai strategy optimize-results View past runs
--epochs 100 Set optimization epochs
--loss sharpe Use Sharpe loss function
--walk-forward Enable walk-forward analysis
--apply Save best params to config
--local Run locally (not via backend)

See Also