Loading…

Margin Interaction Checker

A common assumption is that opening the opposite side of a position you already hold frees up margin, because the two legs cancel each other's market risk. Some brokers genuinely net hedge margin to nothing. Others margin both legs close to full size, and some apply a partial hedge margining ratio, published nowhere in the terminal, that sits between the two. There is no MarketInfo field that tells you which policy is in effect for a given symbol - the only reliable way to find out is to measure. This script reads the account's actual free margin, then asks the terminal what free margin would be left after the planned order using AccountFreeMarginCheck - the same call a real OrderSend margin rejection is based on, so the number reflects the broker's real hedge policy rather than a guess. Separately, it estimates what the order would cost in isolation using the published per-lot margin requirement, with no hedge credit applied. Comparing the two - implied margin added per the broker's own check, against the isolated estimate - tells you what fraction of the new leg's margin was actually offset by the existing opposite position: none, all of it, or something in between. The result is reported as a plain fraction, not a verdict, because "no credit" and "full credit" are both legitimate broker policies and neither is wrong. What matters is knowing which one your account is under before you plan a hedged position around an assumption that turns out not to hold. No order is ever sent - every figure comes from AccountFreeMarginCheck and MarketInfo, both of which answer without touching the order pool. Notes and limitations - AccountFreeMarginCheck reflects the broker's margin model at the moment the script runs. Margin requirements can change with volatility on some account types, so a check run now is not a guarantee of the margin available seconds later when the real order is sent. - The isolated margin estimate uses MODE_MARGINREQUIRED, the broker's published per-lot figure. A few brokers do not populate this field accurately for every symbol; when it comes back zero or missing, the isolated comparison is skipped and only the account-reported before/after figures are shown. - Pending orders are excluded from the "existing positions" summary - they reserve no margin until filled, so including them would overstate current exposure. - This is a read-only measurement tool. It never calls OrderSend, OrderModify or OrderClose. - The hedge credit fraction is a snapshot, not a documented broker policy. Some brokers change hedge margining rules by symbol class (majors versus exotics, for instance) - re-run per symbol rather than assuming one account-wide answer.

Script · MT4 · v1.0 · Free

About Margin Interaction Checker

Margin Interaction Checker is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 4 settings, such as Symbol, Planned is buy and Planned lots. MQL source code included.

Files in the download: MarginInteractionChecker.mq4, MarginInteractionChecker.txt.

Settings

SettingDefaultWhat it does
SymbolSymbol to test, blank = current chart symbol
Planned is buytruePlanned order direction
Planned lots0.10Planned order volume, lots
Magic filter-1Only show existing positions with this magic (-1 = every magic)

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 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: Hedging · Alerts · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria