Loading…

Drawdown Duration Analytics

A single "maximum drawdown" number hides how an account actually spent its time. One brutal 30% drawdown and six mild 8% dips strung back-to-back can report a similar peak figure while feeling nothing alike to sit through. This header treats every underwater episode as its own record instead of collapsing the whole history into one worst-case statistic. SegmentDrawdownEpisodes walks the equity curve once, opening a new episode the instant the curve dips below its running peak and closing it out only when the curve genuinely regains that same peak - a series of dips that never quite reclaims the high in between stays correctly grouped as ONE continuous episode, not several. An episode still underwater at the last supplied sample is reported with endIndex -1, explicitly OPEN rather than silently dropped or mistaken for a short recovery. ComputeEpisodeStatistics rolls the episode list up into mean/median depth, mean/median time-to-recovery (computed over RESOLVED episodes only, since an open episode's true length is still unknown), and the fraction of the whole series spent below a prior peak - a number that a single max-drawdown percentage cannot show at all. Self-contained: every function operates on a plain double array of equity samples the caller supplies, in one or two forward passes - no order, position or account calls, no external state. Notes and limitations - Indices in DrawdownEpisode are ARRAY POSITIONS, not bar shifts or timestamps. Map them back to your own time axis if a date or bar number is needed. - lengthToRecovery of -1 means "not yet recovered by the last sample supplied", not "never will recover" - re-run as new samples arrive. - meanTimeUnderwater/medianTimeUnderwater are computed over RESOLVED episodes only; an open episode contributes to fractionSamplesUnderwater but not to those two averages, since its true length is still unknown. - No thread or state concerns: every function is pure and operates only on the array passed in.

Script · MT5 · v1.0 · Free

About Drawdown Duration Analytics

Drawdown Duration Analytics is a script for MetaTrader 5. MQL source code included.

Files in the download: DrawdownDurationAnalytics.mq5.mqh, DrawdownDurationAnalytics.txt, DrawdownDurationAnalyticsDemo.ex5, DrawdownDurationAnalyticsDemo.mq5.

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