OnTester Harness Bootstrap
Nearly every EA that wants a smarter optimizer target than raw net profit ends up rewriting the same order-history scan: sum wins and losses for a profit factor, walk the closed trades in order for a drawdown curve, maybe a Sharpe-style ratio, then combine them into one number. This header does that scan once. Deliberately, it is built from OrdersHistoryTotal() rather than the Strategy Tester's own TesterStatistics() function - that means the exact same OnTesterHarnessCollect() call also works when run from a plain Script against a live or demo account's real order history, not only from inside an EA's OnTester(). The demo proves this directly: it runs entirely outside the Strategy Tester and reports real metrics from whatever trade history the account already has. OnTesterHarnessComputeFitness() combines profit factor and drawdown into one score rather than just summing raw numbers: it multiplies profit factor by a drawdown penalty (1 - drawdown% / 100, floored so the penalty never drives the score to zero or negative purely from drawdown), so a system that is only profitable because of one huge drawdown swing does not automatically outrank a steadier system with a lower absolute profit factor. A run with zero closed trades returns a large negative sentinel (-1000), so the optimizer never mistakes "this parameter set did nothing" for a genuinely good result. The drawdown figure is an honest approximation, stated plainly rather than oversold: it is built from the peak-to-trough swing of the cumulative closed-trade profit curve, which can only see drawdown at the granularity of trade closes, not the true tick-by-tick floating equity curve between fills that a live account actually experiences. Notes and limitations - ProfitFactor uses a sentinel value (999.0) when there is gross profit but zero gross loss, rather than dividing by zero or returning an undefined figure - this is called out in OnTesterHarnessReportPrint()'s output directly. - SharpeLike is a per-TRADE mean/stdev ratio, not a per-bar or annualised Sharpe ratio - it needs at least 2 closed trades to be computed at all (0.0 otherwise) and should be read as "how consistent are individual trade outcomes", not compared directly to a textbook Sharpe figure. - MaxDrawdownPercent is built from closed-trade P/L only, at trade- close granularity - it does not see floating drawdown between fills. Treat it as a reasonable, honest proxy, not an exact equity-curve figure. - Only OP_BUY/OP_SELL market orders that have closed are counted; pending orders that never filled carry no realised P/L to score.
Script · MT4 · v1.0 · Free
About OnTester Harness Bootstrap
OnTester Harness Bootstrap is a script for MetaTrader 4. It has 2 settings, such as Magic filter and Starting balance. MQL source code included.
Files in the download: OnTesterHarnessBootstrap.mqh, OnTesterHarnessBootstrap.txt, OnTesterHarnessBootstrapDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Magic filter | 0 | 0 = every magic number, otherwise only this EA's trades |
| Starting balance | 0.0 | 0 = use current AccountBalance() minus net closed profit as an estimate of the starting point |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT4 Script · MQL4 Source Code