Margin Calculator
MQL4's MODE_MARGINREQUIRED gives the broker's own per-lot margin figure directly in account currency, and the MQL4 version of this header uses it first because it already accounts for the symbol's margin calculation mode - forex, CFD, futures, CFD-index and CFD-with-leverage all compute required margin differently, and getting that formula wrong for a CFD-index symbol is a common source of EAs that look correct on a forex demo and then miscalculate margin the moment someone runs them on an index CFD. MQL5 has no static per-lot margin lookup at all. The MQL5 version uses OrderCalcMargin() instead - a real broker-side calculation for a hypothetical order at the current market, which prices through whatever margin mode the symbol actually uses rather than looking up a stored figure. Both approaches answer the same question; see the differences section below for exactly which call each platform uses and when each one falls back to a manual estimate. Both platforms fall back to a plain contractSize * price / leverage estimate when the primary call reports nothing usable. That fallback is accurate for ordinary forex margining and only approximate for the other calculation modes - the header says so plainly rather than presenting a guess as a certainty, and McMarginCalcModeToString is provided specifically so a caller can see which mode is in play and judge how much to trust the fallback. Contract size is always read live from the symbol (MODE_LOTSIZE on MQL4, SYMBOL_TRADE_CONTRACT_SIZE on MQL5) - never hardcoded - since it differs per instrument and a hardcoded contract size is exactly the kind of mistake that looks fine on forex and quietly miscalculates margin on gold or an index. McMaxVolumeForBudget answers "how big a position can this margin afford" without also trying to be a lot-size normalizer - it deliberately leaves lot-step rounding to a dedicated header such as LotNormalize, because mixing the two concerns in one function makes both harder to test and harder to trust independently. McFreeMarginAfterTrade and McHasEnoughMargin answer "how much free margin is left, or is there enough, after this hypothetical trade" - see the differences section for how the two platforms compute this, since MQL5 has no equivalent of MQL4's AccountFreeMarginCheck(). Notes and limitations - The manual fallback formula (contract size * price / leverage) assumes plain forex margining on both platforms. On a CFD-index or CFD-leverage symbol where the primary call reports nothing usable, the fallback will be wrong - check McMarginCalcMode first and treat the fallback as a rough estimate on any non-Forex mode. - McMaxVolumeForBudget ignores lot step, minimum and maximum entirely. Run its result through a lot-normalizing header before using it as an order volume. - Free margin figures depend on the whole account's open exposure at the moment of the call, not just the symbol being checked - they are a snapshot, not a guarantee that survives until the order actually reaches the server. - On MQL5, McFreeMarginAfterTrade is an estimate built from OrderCalcMargin and current free margin, not a server-computed figure - see the differences section above for exactly when it can diverge from what the broker would actually allow. - No leverage or margin-mode caching; every call re-reads live symbol and account state.
Script · MT5 · v1.0 · Free
About Margin Calculator
Margin Calculator is a script for MetaTrader 5. It has 2 settings, such as Margin budget and Trade volume. MQL source code included.
Files in the download: MarginCalc.mq5.mqh, MarginCalc.txt, MarginCalcDemo.ex5, MarginCalcDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Margin budget | 500.0 | Margin budget for the worked example |
| Trade volume | 0.50 | Sample volume for the free-margin check |
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: Volume · MT5 Script · MQL5 Source Code