Loading…

Resampled Path Analytics

A single backtest equity curve shows only the one trade order that happened to occur. The trades themselves - their sizes, their win/loss mix - are fixed, but the ORDER they arrived in is one draw out of many possible orders, and a strategy's apparent smoothness (or roughness) can owe more to that one lucky (or unlucky) ordering than to any genuine property of the trades. RpaResample() takes a historical trade P&L series and generates many independent synthetic paths by sampling trades WITH replacement, in a fresh random order per path, walking each one out to a fixed number of trades from a common starting equity. Across all the simulated paths, it reports two things: a percentile envelope (p5/p25/p50/p75/p95) of equity at every step, showing how wide the plausible range of outcomes really is at each point along the way, and a ruin probability - the fraction of simulated paths whose equity ever touched or fell below a caller-specified ruin level at any point, not just at the end. This is a standard trade-sequence bootstrap, not a market-return bootstrap - it reshuffles the ALREADY-REALISED trade outcomes rather than generating new hypothetical trades, so it answers "how much did trade order matter to the curve I got" rather than "what other trades might this strategy have produced under different market conditions". Notes and limitations - The output percentiles[] array is a FLAT 1D array, not a genuine 2D array - MQL4 cannot resize both dimensions of a real 2D array at runtime (only the leftmost dimension is resizable), so this uses row-major manual indexing (step * RPA_BAND_COUNT + bandIndex) instead. Always read it through RpaPercentileAt() rather than indexing the array directly. - This is a trade-sequence bootstrap, not a market bootstrap - see the full description above. It cannot surface a loss pattern that never occurred in the source trades at all, only different ORDERINGS of the trades that did occur. - Ruin is checked at EVERY step along a path, not only at the final step - a path that dips to the ruin level midway and later recovers above it still counts as ruined, matching how a real account would actually be stopped out rather than allowed to keep trading through a margin call. - MathRand() returns a value in 0..32767; sampling uses `MathRand() % tradeCount`, which is a standard, adequate bootstrap sampler for the modest trade counts and path counts this header targets, though it is not a cryptographically uniform sampler (an academic-grade concern, not a practical one here). - Cost is O(pathCount * pathLength) for the simulation and O(pathLength * pathCount * log(pathCount)) for the per-step sort - keep both counts in the low hundreds for interactive use; a few thousand of either will noticeably slow a script run. - seed=0 does not mean "no randomness" - it means "don't reseed", reusing whatever state MathRand()'s internal generator is already in. Pass a genuinely fixed non-zero seed whenever a reproducible result across separate runs is required.

Script · MT4 · v1.0 · Free

About Resampled Path Analytics

Resampled Path Analytics is a script for MetaTrader 4. It has 6 settings, such as Trade count, Path count and Path length. MQL source code included.

Files in the download: ResampledPathAnalytics.mqh, ResampledPathAnalytics.txt, ResampledPathAnalyticsDemo.mq4.

Settings

SettingDefaultWhat it does
Trade count40Number of distinct historical trades to resample from
Path count300Number of synthetic resampled equity paths to simulate
Path length60Number of trades per synthetic path
Starting equity10000.0Starting equity for every synthetic path
Ruin level8000.0Equity level counted as "ruin" if ever reached
Seed20260817Random seed, 0 = use whatever stream is already running

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Channels & Envelopes · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria