Loading…

Underwater Episode Analytics

A single "max drawdown" figure hides everything about HOW a strategy loses and recovers. Two equity curves can share an identical max drawdown percentage while behaving completely differently: one might be a single quick dip that recovered within days, the other a long grind that never fully recovered before the sample ended. Comparing strategies (or reviewing one strategy's history) on max drawdown alone throws that distinction away. UwAnalyze() walks an equity curve once and returns EVERY underwater episode - the stretch from a running peak, through the deepest point reached, to either full recovery back to that peak or the end of the data. Each episode reports its depth as a percentage of the peak it fell from, how many bars it took to reach the trough, how many more bars it took to recover (or -1 if it never did before the data ran out), and a simple shape classification: V-shape if the recovery was no slower than 1.5x the time it took to fall, U-shape if slower, and "unrecovered" if the curve is still underwater when the data ends. UwSummarise() then rolls the whole episode array into the handful of numbers a report script usually actually wants - episode count, how many never recovered, worst depth, average depth, and average recovery time across the episodes that did recover. Notes and limitations - "Bars" here means array index steps in whatever series the caller passes in - one bar per trade, one bar per day, one bar per H1 candle. The header has no concept of actual elapsed time; convert barsToTrough/barsToRecover to real time using the caller's own spacing if needed. - The V/U shape split is a fixed heuristic (recovered within 1.5x the time it took to fall = V, slower = U) rather than a statistically derived boundary. Treat the label as a quick visual sort, not a formal classification. - An episode still open at the end of the data is always reported as UW_SHAPE_UNRECOVERED with recoverIndex and barsToRecover both -1 - this does not mean the strategy will never recover, only that recovery had not happened by the last data point supplied. - Equity values of zero or below at a peak produce a depthPct of 0.0 for that episode rather than a divide-by-zero, since a percentage drawdown from a non-positive peak has no ordinary meaning. - Detection uses strict inequality (equity[i] < peak to start, >= peak to close) - a curve that only ever equals its previous peak exactly, never dips below it, reports zero episodes, which is the intended behaviour for a genuinely flat or monotonically non-decreasing curve.

Script · MT5 · v1.0 · Free

About Underwater Episode Analytics

Underwater Episode Analytics is a script for MetaTrader 5. It has 1 setting, such as Min depth pct. MQL source code included.

Files in the download: UnderwaterEpisodeAnalytics.mq5.mqh, UnderwaterEpisodeAnalytics.txt, UnderwaterEpisodeAnalyticsDemo.ex5, UnderwaterEpisodeAnalyticsDemo.mq5.

Settings

SettingDefaultWhat it does
Min depth pct0.5Ignore episodes shallower than this depth percentage

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