Performance Budget Template
An EA that quietly gets slower over time - more symbols, more history, a heavier indicator, a busier order pool - rarely announces the fact. It just starts missing fills, lagging a fast market, or timing out under `IsTradeContextBusy()` pressure, and by the time anyone notices, the actual cause (which stage of OnTick got slow, and when) is long gone. This project is a skeleton, not a finished strategy: it wraps a small set of representative stages - a market-data scan, a signal calculation, a risk check and an order-management pass - each doing genuine, real work (actual `iHigh`/`iLow` reads, a real `iMA` call, a real order-pool walk), timed with `GetMicrosecondCount()` between a `StageBegin`/`StageEnd` pair. Each stage carries its own microsecond budget; a stage that runs over its budget increments an overrun counter and fires a rate-limited alarm (logged always, optionally a modal `Alert()` too) rather than flooding the log every single tick a sustained overrun continues. The whole tick is timed the same way against its own total budget. Swap the four placeholder stages for a real strategy's own logic, keeping the `StageBegin`/`StageEnd` wrapping intact, and the instrumentation - budgets, worst-case tracking, rolling averages, rate-limited alarms, and a periodic CSV performance summary - carries over unchanged. The instrumentation is the genuine, reusable deliverable of this project; the four placeholder stages exist only to give it something real to time. Notes and limitations This template places no trades and manages no positions itself - the four demonstration stages do genuine read-only work (market data reads, an indicator call, an order-pool walk) purely to give the timing harness something real to measure. It is a skeleton to build a strategy's own timing instrumentation into, not a strategy on its own. The stage registry is a fixed-size table capped at 8 simultaneously registered stages (MAX_STAGES in the source). RegisterStage returns -1 and logs a warning if that cap is exceeded; raise the constant and recompile if a real strategy genuinely needs more. `GetMicrosecondCount()` measures wall-clock time on the terminal thread, not CPU time, and includes anything else briefly blocking that thread (e.g. a slow disk write inside a stage). A stage that itself performs file I/O will show that I/O's latency as part of its own measured time, which is usually exactly what you want to know. Overrun budgets in this template (1500us/2000us/800us/1200us per stage, 5000us total) are illustrative starting points attached to the placeholder stages' own workload on the machine this was tested on - they are not tuned figures for any real strategy and will need resetting once real logic replaces the placeholders. The rolling average is a simple total-time-divided-by-call-count figure recomputed from two running counters, not a windowed or decaying average - a strategy that was briefly very slow early in a long session will keep dragging the reported average up for the rest of that session.
Expert advisor · MT4 · v1.0 · Free
About Performance Budget Template
Performance Budget Template is a script for MetaTrader 4 based on Moving Average. It can alert you with a pop-up, sound, email or push message. It has 5 settings, such as Total tick budget us, Alarm cooldown sec and Report interval sec. MQL source code included.
Files in the download: PerformanceBudgetTemplate.ex4, PerformanceBudgetTemplate.mq4, PerformanceBudgetTemplate.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Total tick budget us | 5000 | Whole-OnTick budget, in microseconds, before an overrun alarm fires |
| Alarm cooldown sec | 30 | Minimum seconds between repeated alarms for the SAME stage |
| Report interval sec | 60 | Seconds between periodic performance summaries to the log/file |
| Use alert popup | false | true = also raise a modal Alert() on overrun, not just Print/log |
| Perf log file name | performance_budget_template.csv | File in MQL4\Files\ for the periodic summary |
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: Alerts · Moving Average · MT4 Expert Advisor · MQL4 Source Code