Loading…

Edge Decay Tracker

A strategy's expectancy naturally wobbles from trade to trade even when nothing has actually changed. The question worth alerting on is not "is the recent average lower than the historical average" (that is true about half the time on pure noise alone) but "is the recent average lower than sampling variation would plausibly produce" - which is exactly what a two-sample significance test answers. Update() feeds one closed trade's net profit at a time. Internally the tracker cascades the value through two fixed-size FIFO windows: RECENT (the most recently closed trades) and HISTORICAL (the window of trades immediately preceding RECENT). A trade evicted from RECENT enters HISTORICAL; a trade evicted from HISTORICAL simply ages out of the tracker entirely. Both windows keep a running sum and sum-of-squares, so every Update() call is O(1) regardless of window size. TStatistic() computes Welch's t-statistic (the unequal-variance form, appropriate here since a decaying strategy's recent variance has no reason to match its historical variance) between the two windows' means. IsDecaySignificant() flags decay only once both windows are genuinely full, the recent mean is below the historical mean, AND the one-tailed p-value clears the requested confidence threshold - never on a partially-filled window, no matter how extreme the few trades fed so far look. JUDGEMENT CALL, stated plainly: MQL4 has no MathErf/MathErfc (house style section 0.2), so there is no built-in way to evaluate an exact Student's-t p-value for the test's actual degrees of freedom. The p-value here is computed against the STANDARD NORMAL distribution instead, using the same Abramowitz & Stegun tail approximation already used elsewhere in this catalogue. This is the standard large-sample approximation to Welch's test and is accurate once both windows hold a few dozen trades or more; for small windows it is mildly ANTI- CONSERVATIVE (slightly overstates significance) versus the true Student's-t distribution, which has fatter tails at low degrees of freedom. Use windows of 30+ trades each where practical, and treat a borderline p-value on small windows with appropriate scepticism. The demo proves the test discriminates, not just fires: a STABLE run (constant underlying edge throughout) never flags decay, while a DECAYING run (edge collapses partway through) is correctly flagged once the recent window has slid past the break point. Notes and limitations - The p-value is a NORMAL APPROXIMATION to Welch's t-test, not an exact Student's-t calculation - see the judgement call paragraph above. This is a deliberate, documented trade-off forced by MQL4 having no built-in error function, not an oversight. - "Recent" and "historical" are trade counts, not calendar time - the two windows can span very different real-world durations depending on how frequently the strategy trades. - IsDecaySignificant() only ever tests for a DROP in expectancy (one- tailed). It says nothing about a recent IMPROVEMENT in edge; TStatistic() and DecayPValue() are still meaningful in that direction if a caller wants to build that check separately. - Both windows must be genuinely full before any significance conclusion is drawn - IsDecaySignificant() returns false unconditionally until then, and TStatistic()/DecayPValue() are not meaningful either on a partially-filled tracker. - This library measures WHETHER a statistically meaningful drop occurred, not WHY. A flagged decay can reflect a genuine strategy breakdown, a market regime shift, or simply an unlucky short stretch large enough to still clear the significance bar - treat a flag as a prompt to investigate, not an automatic stop-trading signal.

Script · MT5 · v1.0 · Free

About Edge Decay Tracker

Edge Decay Tracker is a script for MetaTrader 5. It has 2 settings, such as Historical window and Recent window. MQL source code included.

Files in the download: EdgeDecayTracker.mq5.mqh, EdgeDecayTracker.txt, EdgeDecayTrackerDemo.ex5, EdgeDecayTrackerDemo.mq5.

Settings

SettingDefaultWhat it does
Historical window40Historical baseline window size (trades)
Recent window40Recent window size (trades)

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 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: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria