Search a small grid of sensible values, choose a setting that sits in a broad region of good results instead of the single best point, require enough trades, include costs, and confirm the choice on data the search never saw. Fewer parameters and coarser grids overfit less.
Why optimisation overfits
Every extra parameter and every extra value you try is another chance to find a setting that fitted the noise. Try a thousand combinations and some will look excellent by luck alone. The best result of a large search is biased upward, often by a lot, and the more you searched the larger that bias.
A safer process
- Fix the parameters that do not need tuning. Use standard values where they exist (a 14-bar ATR, a daily rebalance) and tune only what matters.
- Use coarse, sensible grids. Moving averages of 10, 20, 50 and 100 bars tell you as much as every value from 10 to 100, with a tenth of the chances to fit noise.
- Set a minimum trade count. Drop any setting with too few trades to judge, often fewer than 30 to 50.
- Include realistic costs in the search. A setting that trades more often looks better without costs and worse with them, so the winner changes.
- Pick a plateau, not a peak. Look at the results around the best point. If neighbouring values are nearly as good, the edge is broad. If the best value is surrounded by losses, it is noise.
- Choose by a rule fixed in advance. For example, the median Sharpe ratio across a parameter and its neighbours, rather than the single highest figure.
- Test the winner out of sample, once, on data the search did not use. Walk-forward testing repeats this for every period.
Reading a parameter grid
Plot or print the results as a table of one parameter against another. What you want to see is a smooth area of reasonable results. Warning signs:
- the best cell is isolated, with much worse results on every side;
- good and bad cells alternate with no pattern;
- the best setting is at the edge of the grid, which means the real optimum may lie outside what you tested;
- results improve steadily as the strategy trades less, until only a handful of trades remain.
How Reamer Research handles it
Reamer Research runs one parameter set per call, so a grid is an ordinary Python loop. Passing a configured object keeps each parameter set with its strategy:
import itertools
import reamer_research as rr
costs = dict(commission_per_unit=0.005, spread=0.01, slippage=0.01)
grid = {}
for fast, slow in itertools.product([10, 20, 50], [50, 100, 200]):
if fast >= slow:
continue
r = rr.backtest(Crossover(fast, slow), 'SPY.bin', end=split, **costs)
if r.closed_trades_count >= 30:
grid[fast, slow] = r.sharpe_ratio
The same rule runs as a function on_bar(data) or a class as well; a configured object suits a search. Each run is byte-identical at a fixed seed, so two cells differ only because their parameters differ. Every cost defaults to zero, so the costs you set are the costs the search used, and they are visible in the code. Each result carries the Sharpe, Sortino and Calmar ratios, profit factor, drawdown and trade count, so the selection rule can use any of them.
Runs are independent, so a grid can be spread across processes with Python's multiprocessing or concurrent.futures, one run per worker.
Limits to know
- There is no built-in optimiser. The search is your loop, which keeps the choice of grid and selection rule visible in your own code.
- Grids over many tickers or long histories need a licence. Evaluation Mode runs one ticker over at most 30 days of bars.
A $20 month includes the full kit, so you can run your own grid with costs and cancel if it does not fit.