Loading…

Monte Carlo Trade-Order Simulator

A backtest report shows exactly one equity curve - the one produced by the specific order the trades happened to close in. That order is a single sample out of a much larger space of equally valid orderings of the same trades. Two strategies with an identical trade list can show wildly different reported drawdowns purely because of when the losing streak happened to land. This library answers the question directly: take the closed-trade profit and loss values exactly as they are, reshuffle their order thousands of times, and replay each shuffled sequence as its own equity curve. The trades never change, only their order does, which isolates sequence risk from the edge itself. A strategy whose 5th and 95th percentile outcomes sit close together survived nearly every reordering; one where the band is wide got a flattering draw in the actual backtest and should not be trusted on that curve alone. This is deliberately a permutation test, not a bootstrap. Resampling the same trades WITH replacement - drawing some trades twice and others not at all - answers a different question about the population the trades were drawn from, and belongs in a separate library (Bootstrap.mqh) rather than folded in here with a mode flag. The random generator combines two calls to MathRand() to cover trade counts into the hundreds of thousands without pulling in an external source - MathRand() alone only spans 0..32767 on both platforms, which quietly under- samples any history longer than that if used directly. Every function here works on plain double arrays and calls only MathRand, MathSrand, ArraySort, ArrayCopy, GetTickCount and StringFormat - none of which changed between MQL4 and MQL5 - so this library ports byte-for-byte. The 5000-trade balance and drawdown history a real EA would feed in comes from OrderProfit()-style reads on MQL4 or HistoryDealGetDouble()-style reads on MQL5, but that history-gathering step lives in the caller, not in this library, which is why the port needed no changes at all. Notes and limitations - This measures sequence risk within a FIXED set of trades. It says nothing about whether the edge itself is real - a consistently losing strategy will simply show a tight band of consistently losing outcomes. - Drawdown is measured in percent of the running peak balance, matching how most brokers and prop-firm rules define it. - The synthetic trade generator in the demo is for illustration only. Feed the library your actual closed-trade profit series (read via OrderProfit() on MQL4 or the HistorySelect/HistoryDealGetDouble path on MQL5) for a meaningful result. - MonteCarloRun keeps a full copy of the trade array per simulation internally for the shuffle; very large trade counts times very large simulation counts will simply take longer, there is no hidden limit besides available memory.

Script · MT4 · v1.0 · Free

About Monte Carlo Trade-Order Simulator

Monte Carlo Trade-Order Simulator is a script for MetaTrader 4. It has 7 settings, such as Trade count, Win rate percent and Average win. MQL source code included.

Files in the download: MonteCarlo.mqh, MonteCarlo.txt, MonteCarloDemo.mq4.

Settings

SettingDefaultWhat it does
Trade count120Number of synthetic trades to simulate
Win rate percent45.0Percentage of synthetic trades that win
Average win180.0Average profit of a winning trade
Average loss100.0Average loss of a losing trade
Starting balance10000.0Starting account balance for the simulation
Simulations5000Number of reshuffles to run
Seed42Random seed, 0 = non-reproducible

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: MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria