Loading…

Margin Stress Tester

The terminal will tell you the current margin level. It will not tell you what that number becomes if the market moves 2%, 5% or 10% against the open book - and a margin level that looks comfortable right now can be one adverse move away from a forced liquidation. This library applies a uniform percentage price shock to every open position, recomputes each one's P&L through its own tick economics (SYMBOL_TRADE_TICK_ VALUE / SYMBOL_TRADE_TICK_SIZE on MQL5, MODE_TICKVALUE / MODE_TICKSIZE on MQL4 - never an assumed contract size), and reports the projected equity and margin level. A search function then steps the shock magnitude outward in a chosen direction until the projected margin level breaches the account's actual stop-out threshold, read from the platform's own stop-out fields rather than a guessed number. Uniform shock is a deliberate, disclosed simplification. Real market crises do not move every symbol by the same percentage - correlated instruments move together, uncorrelated ones do not, and the size of the move differs by instrument. This library does not model that; it answers the simpler and still useful question "what if every open position moved X% against me at once", which is the standard shape of a margin stress test and a reasonable worst-case proxy for a basket of related instruments. It is not a forecast and should not be read as one. Used margin is treated as constant across the shock. Most retail FX/CFD accounts price margin from the position's notional at entry or a fixed per-lot figure, not from the live price, so this holds well enough for the purpose. A broker that recomputes margin requirement continuously from current price is the documented exception - see Notes and Limitations. Notes and limitations - The uniform-shock model does not capture per-symbol correlation during a real crisis. Treat every result as a standardised stress scenario, not a probability-weighted forecast. - Used margin is assumed constant across the shock. A broker that recomputes margin requirement from live price rather than entry notional will see the projected margin level drift from what actually happens - check your broker's margin behaviour if this matters to you. - MstStopOutLevelPct returns -1.0 for brokers whose stop-out is money based rather than percent based; the search function still works in that case using whatever stopOutLevelPct the caller supplies, but the demo cannot compute that figure automatically for a money-based account. - The demo's synthetic fallback position, used only when the account is flat, estimates used margin via a broker lookup or calculation that some brokers report as zero for an unusual symbol state. A real EA with actual open positions does not hit this fallback. - Pure computation only. Nothing here sends an order or closes a position; the caller decides what to do once a stress result looks uncomfortable. This is true on both platforms.

Script · MT5 · v1.0 · Free

About Margin Stress Tester

Margin Stress Tester is a script for MetaTrader 5. It has 5 settings, such as Shock step pct, Shock steps and Search step pct. MQL source code included.

Files in the download: MarginStressTester.mq5.mqh, MarginStressTester.txt, MarginStressTesterDemo.ex5, MarginStressTesterDemo.mq5.

Settings

SettingDefaultWhat it does
Shock step pct1.0Percent step size between scenario rows
Shock steps10Number of scenario rows to print
Search step pct0.5Percent step size for the stop-out search
Search max steps200Maximum steps the stop-out search will take
Magic filter-1Magic number filter, -1 = every open position

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