Take the per-trade returns from your backtest, draw them at random with replacement to build thousands of alternative trade sequences, and compound each one from your starting capital. The spread of final equity and maximum drawdown across those sequences shows how much of your single result was the order the trades happened to come in.
What it answers
A backtest gives one path: one order of wins and losses, one maximum drawdown. The same trades in a different order can give a very different drawdown, because a run of losses early in a compounding account does more damage than the same losses spread out. Monte Carlo resampling asks: if the trades were drawn from the same distribution again, what range of outcomes would I see?
It is most useful for:
- Drawdown planning. The 95th-percentile drawdown across simulations is a better guide to what you should be prepared for than the single drawdown your backtest showed.
- Risk of ruin. The share of simulations that lose half the account, or end below the starting capital.
- Position size. Running it at several sizes shows where the bad tail becomes unacceptable.
Doing it yourself
import numpy as np
def monte_carlo(returns, capital=10_000, sims=10_000, seed=0):
rng = np.random.default_rng(seed)
draws = rng.choice(returns, size=(sims, len(returns)), replace=True)
equity = capital * np.cumprod(1 + draws, axis=1)
peaks = np.maximum.accumulate(equity, axis=1)
max_dd = ((peaks - equity) / peaks).max(axis=1)
return np.percentile(equity[:, -1], [5, 50, 95]), np.percentile(max_dd, [5, 50, 95])
Here returns is each trade's profit as a fraction of account equity when the trade opened. Using return on the position instead overstates the risk of a small position and understates a leveraged one.
What it does not tell you
- It assumes trades are independent. Resampling with replacement breaks up streaks and regimes. If your losses cluster in particular markets, the real risk is higher than the simulation shows.
- It cannot fix a biased backtest. If the trades came from an overfit or look-ahead-biased test, resampling them gives a confident range around a wrong answer.
- It needs enough trades. Twenty trades resampled ten thousand times are still twenty trades.
How Reamer Research handles it
Reamer Research runs Monte Carlo resampling on any finished backtest with one call:
import reamer_research as rr
result = rr.backtest(MyStrategy(), 'SPY.bin', spread=0.01, commission_per_unit=0.005)
mc = result.monte_carlo(simulations=10_000, seed=7)
print(mc['final_equity_p5'], mc['max_drawdown_p95'], mc['probability_of_loss'])
MyStrategy can be a class, a configured object or a function on_bar(data). Each simulation draws the run's per-trade returns with replacement, one draw per closed trade, compounding from the run's initial_capital. The returns are each trade's net profit over account equity at entry, the same series the Sharpe and Sortino ratios use. Equity is floored at zero and a simulation stops there.
The call returns the mean, median, best and worst final equity, the 5th-percentile final equity, the probability of loss, the probability of ending below half the starting capital, and the 5th, 50th and 95th percentile maximum drawdown. A fixed seed repeats the figures exactly; with seed=0 a seed is drawn and returned as effective_seed so you can repeat it later. The work runs on every core, and the figures do not depend on how many.
Limits to know
- It resamples trades, not bars. It treats each closed trade as independent, as described above.
- A run with fewer than two closed trades returns zeros.
A $20 month includes the full kit, so you can resample your own strategy's trades and cancel if it does not fit.