Optimize

Optimizing a strategy

Tune a strategy's parameters with Bayesian search, score on walk-forward validation, and save the best variant.

5 min readUpdated Jun 19, 2026

The optimizer searches for parameter values that improve your strategy, using Bayesian optimization (Optuna's TPE sampler). Instead of trying every combination, it learns from each trial which regions of the search space look promising, so it finds a good configuration in tens of trials rather than thousands.

How a run works

  1. You pick which parameters to tune and the range to search for each.
  2. The optimizer loads market data once, then runs one backtest per trial, choosing the next parameter combination from what it learned so far.
  3. Each trial is scored on your chosen fitness metric. Trials that break a hard constraint (too few trades, blown drawdown, negative validation return) are rejected, not ranked.
  4. When the run finishes, the best non-rejected trial is highlighted. You can save any trial as a new strategy - it never overwrites the original.

Walk-forward validation

This is the safeguard against curve-fitting. With it on, the date range is split into a training window (the first 70%) and a validation window (the most recent 30%). The optimizer fits parameters on training but scores them on validation - data it didn't tune against. A parameter set that only memorized the training data scores poorly on validation and loses.

Keep walk-forward on for any result you plan to trade. Turn it off only for a quick smoke test.

A good first run

Don't tune everything at once. To confirm the pipeline and get a feel for it:

  • Enable one parameter (e.g. a lookback or period).
  • Set Max trials to 15-20.
  • Use about a year of data past the strategy's warm-up.

Then move on to the real workflow below.

The workflow: optimizing without fooling yourself

An optimizer is a machine for finding parameters that look great on the past. Run it carelessly and it hands you a lottery ticket: a config that won by luck and will not repeat. The steps below keep the search honest.

1. Reserve a final exam before you start

Set the optimization end date about 12 months back from today, and do not touch the most recent year with the optimizer at all. That untouched window is your final exam. It only proves anything if the optimizer never saw it - and it stops proving anything the moment you start iterating against it, so you get to use it once per strategy.

2. Fix structure before parameters

Look at the rejection reasons from your last run. If a large share of trials were rejected for validation max drawdown, the problem is not the parameter values - it is that the strategy's logic does not fit the recent market. No parameter search fixes that. Edit the strategy first (add a market-regime condition, reduce leverage in ranging markets), run one normal backtest to confirm the drawdown is tamed, then come back.

3. Decide what "pass" means before you run

Write down your bar before launching, for example: validation expectancy of at least +3% per trade, on at least 20 validation trades, with the train number no more than a few times the validation number. If you pick the bar after seeing results, every result will look like progress - that is hand curve-fitting with extra steps.

4. Tune 2-3 parameters per run, in groups

Each added parameter multiplies the search space and the luck of the winner, and a typical validation window only yields 20-25 trades - enough evidence for 2-3 knobs, not 6. Group by function and run the groups in order, each pass starting from the previous winner (save the variant, then optimize the variant):

  1. Signal - the entry-rule indicator periods. These decide which trades exist, so they go first.
  2. Filters - threshold conditions and regime gates. These prune the trade set the signal created.
  3. Exits - stop and target multipliers, trailing settings. These change the outcome of each trade, not which trades happen.

One full loop through the groups is the budget. Looping again and again against the same validation window slowly turns it into training data.

5. Pick from plateaus, not peaks

Before saving, scan the top ten trials. The trustworthy winner is a cluster: several trials with similar parameter values all showing similar validation expectancy. A lone trial whose result collapses when a parameter moves slightly is a fit to noise, even if it has the best score. Prefer the middle of a stable neighborhood over an isolated peak.

6. Sit the final exam - once

Run one normal backtest of the finished variant over the reserved recent window from step 1. You are checking one thing: does the per-trade expectancy keep its sign on data nothing was ever fit to?

  • It holds: trust the strategy at modest size, and set expectations from the validation expectancy, never from the full-window return.
  • It fails: the edge is not there. Going back to re-tune parameters and "re-take" the exam burns the holdout - wait for new market data before judging this strategy again.

Reading the results

The run view ranks trials by score, with the baseline (your current configuration) pinned at the top so you can see whether any trial actually beat it.

The two columns to read first on a walk-forward run:

  • Validation / Trade - average profit per trade on the held-out window, with the trade count in parentheses. This is the most honest forward estimate on the page. +5% over 25 trades beats +15% over 8.
  • Train / Trade - the same number on the window the parameters were fit to. It is always flattered, because the optimizer chose these parameters precisely for shining there. A large train-to-validation gap is the overfitting signature.

The full-window Return column is the least meaningful number on the page: it is compounded, leverage-amplified, and usually dominated by a few old outlier years. Never size positions from it.

Rejected trials are information, not failures. "Too few trades" means the config does not trade often enough on this range to be judged - use a longer date range, not a lower bar. A wall of "validation max drawdown" rejections means the strategy structure fights the recent market (see step 2 above).

Save the winner only if it clears the baseline on the validation window by a meaningful margin - a tiny edge is usually noise.

Optimizing a strategy | Help Center | LucraX · LucraX