Loading…

Margin Formula Walkthrough

"How much margin does this order need" has three different correct answers depending on MODE_MARGINCALCMODE, and a script that hardcodes one formula for every symbol gets the others quietly wrong. Forex mode divides contract value by account leverage. CFD mode multiplies contract size by the instrument's own price before dividing by leverage, because a CFD is not quoted as a currency pair. Futures mode ignores leverage entirely and uses a flat per-contract figure set by the exchange - a script built and tested only against forex pairs will silently apply the wrong shape of formula the first time it is pointed at an index or commodity futures contract. This script reads the symbol's actual calc mode and derives its margin requirement using only that mode's real formula, built entirely from live MarketInfo() figures - contract size, account leverage, MODE_MARGININIT, MODE_MARGINMAINTENANCE - never a hardcoded contract size or leverage value. The working-out is printed in full, not just the answer. The derived figure is then checked two ways. First, against the broker's own precomputed MODE_MARGINREQUIRED per-lot figure where the broker publishes one - the most direct ground truth available. Second, against AccountFreeMarginCheck(), which asks the broker's margin engine directly through a completely separate code path; this second figure reflects the WHOLE account (existing positions, any hedging credit) at the moment of the call, so it is a cross-check rather than a clean mathematical twin of the isolated per-lot derivation, and the script says so rather than treating a difference between them as an error. Notes and limitations The Forex-mode leverage formula computes margin in the symbol's own margin currency. Where that differs from the account's deposit currency, the broker applies an FX conversion internally that this script has no second quote to replicate - the script says so explicitly whenever it falls into that branch, rather than presenting an unconverted figure as final. MODE_MARGINHEDGED, when set below the full contract size, means the broker reduces required margin against an existing opposing position on the same symbol - the derivation in this script assumes a single new position with no offsetting exposure already open, and prints a note when it detects a hedged discount is available but not modelled. Calc modes outside the three documented core values (0 Forex, 1 CFD, 2 Futures) fall back to a best-effort approximation and are flagged as such in the output - treat those with more caution than the three named modes.

Script · MT5 · v1.0 · Free

About Margin Formula Walkthrough

Margin Formula Walkthrough is a script for MetaTrader 5. It can alert you with a pop-up, sound, email or push message. It has 2 settings, such as Symbol and Lots. MQL source code included.

Files in the download: MarginFormulaWalkthrough.ex5, MarginFormulaWalkthrough.mq5, MarginFormulaWalkthrough.txt.

Settings

SettingDefaultWhat it does
SymbolSymbol to analyse (blank = current chart symbol)
Lots1.0Position size used for every figure below

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: Alerts · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria