Loading…

Equity Guard

A drawdown limit only means anything relative to a peak, and neither platform hands you that peak - it has to be tracked across ticks by something. Most equity-guard snippets keep that peak in a global variable, which quietly breaks the moment you want a second guard running with a different reset schedule, or you want to reset one guard without disturbing another. This header takes the peak as a reference parameter instead. The caller declares a double, passes it by reference, and owns exactly when it resets - new day, new session, new backtest run, or never. UpdatePeakEquity advances it; DrawdownFromPeak reads the gap; IsMaxDrawdownBreached does both and returns a bool for the calling EA to act on. Three checks are covered because they answer different questions. Drawdown from a tracked peak answers "how far have we fallen from our best point." An absolute equity floor answers "are we below a hard line regardless of history" - the shape a prop-firm rule or a personal stop-trading threshold usually takes. A session loss check answers "how much have we given back since a known starting balance," which is deliberately left for the caller to supply rather than guessed, because what counts as the start of a session varies by broker and timezone and a wrong guess here is worse than an explicit input. Every percentage function treats a zero or negative reference as "no drawdown" rather than dividing by it, and drawdown never reports negative when equity is above the reference. Notes and limitations - The peak is only as good as the caller's discipline in updating it every tick and resetting it at the right moments. This header provides the arithmetic, not a scheduler. - Live equity does not move during a script run, so the demo exercises every function with a spread of hand-picked reference and peak values around the real account equity rather than pretending equity itself changed. - No margin awareness. A guard that only watches equity can still let an account get margin-called between checks if positions are large relative to free margin - pair this with a margin check, not instead of one. - Nothing here closes positions or disables trading; every function returns information for the caller's own EA to act on. - No money-per-point or contract-size calculation appears anywhere in this header - it operates purely on the account's own equity/balance figures, so the tick-value/contract-size scaling this catalogue's house style calls out for money calculations does not apply here.

Script · MT5 · v1.0 · Free

About Equity Guard

Equity Guard is a script for MetaTrader 5. It has 3 settings, such as Max drawdown percent, Floor amount and Session loss percent. MQL source code included.

Files in the download: EquityGuard.mq5.mqh, EquityGuard.txt, EquityGuardDemo.ex5, EquityGuardDemo.mq5.

Settings

SettingDefaultWhat it does
Max drawdown percent10.0Drawdown-from-peak limit for the demo
Floor amount100.0Absolute equity floor for the demo
Session loss percent5.0Session loss limit for the demo

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