Drawdown Calculator
Most published "max drawdown" snippets return one percentage and stop there. That number alone hides the part a trader actually has to live through: how long the account stayed underwater. A 20% drawdown recovered in a week and a 20% drawdown that took eight months to climb back out of report an identical peak-to-trough percentage and are not remotely the same risk to sit through. This header treats duration as a first-class result, not an afterthought. AnalyzeDrawdown does a single forward pass tracking the running peak, keeps the deepest (peak, trough) pair it has seen, and then searches forward from that trough for the first sample that regains the pre-drawdown peak. When the curve never gets back there by the end of the array, recoveryIndex comes back as -1 - a caller who forgets to check that gets a clearly invalid index, not a plausible-looking wrong number. Depth and duration do not always pick the same episode. The included demo deliberately contains a shallow drawdown that lasts a long time and a deep drawdown that resolves quickly, and shows AnalyzeDrawdown correctly selecting the deeper one as "the" maximum drawdown by percentage, while LongestUnderwaterDuration reports the shallow one as the longest continuous stretch below a prior peak - two different, both useful, numbers. Also included: UnderwaterCurve, the full below-peak percentage at every sample for plotting the classic drawdown chart under an equity curve, and CurrentDrawdownPct, a lightweight "where does the curve sit right now" reading distinct from the historical worst. This header intentionally covers drawdown only - broader return-series shape statistics and risk-ratio calculations are out of scope here. Self-contained: no dependencies beyond the standard library, no DLLs, and no trade, position or account calls at all - every function operates purely on the plain double array the caller supplies, so this library ported to MQL5 with no algorithmic changes whatsoever. Notes and limitations - Indices in DrawdownInfo are ARRAY POSITIONS, not bar shifts or timestamps. Map them back to your own time axis if the caller needs a date or bar. - recoveryIndex of -1 means "not yet recovered by the last sample in the array you passed", not "never will recover". Re-run as new samples arrive. - AnalyzeDrawdown reports the single deepest episode. A curve can have several distinct large drawdowns; call it again on a sliced sub-array if every episode individually is needed, not just the worst one. - No thread or state concerns: every function is pure and operates only on the array passed in, in one forward pass.
Script · MT5 · v1.0 · Free
About Drawdown Calculator
Drawdown Calculator is a script for MetaTrader 5. MQL source code included.
Files in the download: DrawdownCalc.mq5.mqh, DrawdownCalc.txt, DrawdownCalcDemo.ex5, DrawdownCalcDemo.mq5.
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT5 Script · MQL5 Source Code