Member-only story
A Minimal Grid Search With Vectorbt Using MultiIndex Signals
Vectorbt makes parameter optimization straightforward because it can backtest many strategy variants in one run. Instead of looping and running separate backtests, you build a “grid” of signals and parameter series as a single wide object, then let vbt.Portfolio evaluate everything in parallel.
The script below implements exactly that idea: it generates a grid over EMA window length, stop-loss level, and take-profit level (defined as a multiple of stop-loss), runs a single portfolio simulation across the full grid, and then selects the best parameter set by Sharpe ratio.
Strategy idea
The strategy is deliberately simple to keep the optimization mechanics clear.
An entry signal triggers when price is above an EMA, meaning it trades with the trend:
- Long entry when
close > EMA(window) - No explicit exit signal
- Positions are closed by risk management rules:
- Stop-loss (
sl_stop) - Take-profit (
tp_stop), computed astp = sl * ratio
This creates a clean parameter surface to search:
