Loading…

Post Order Margin Forecaster

The naive way to forecast post-trade margin is MODE_MARGINREQUIRED * lots added straight onto the account's current used margin. That overstates the true result whenever an opposite- direction position already exists on the same symbol and the broker genuinely charges less for a hedged pair than for the sum of both sides - a common arrangement, but not a universal one, and MQL4 exposes no single flag that just says "yes, this broker discounts hedges." This library reads MODE_MARGINHEDGED for the symbol and, when the broker reports a usable figure AND an opposite-direction position already exists, prices the hedged portion of the planned order (the overlap between the planned volume and the existing opposite volume) at the hedged rate, and any excess planned volume beyond that overlap at the normal rate - because volume with nothing left to hedge against is charged in full. When the broker reports no usable MODE_MARGINHEDGED figure, or there is no opposite-direction position to hedge against, the forecast falls back to the plain summed calculation and says so explicitly in the result (hedgingOffsetAvailable = false), rather than silently pretending a discount was applied when it was not. The exposure scan looks at every open order on the symbol regardless of magic number, because a broker's margin engine has no concept of "whose" position it is - a full and honest margin forecast has to account for all live exposure on the symbol, including trades opened by other EAs or manually. Notes and limitations - This is a snapshot forecast against current price, leverage and margin data at the moment it runs. It is not a guarantee of the exact margin the broker will reserve when a real order actually fills - spread, slippage and any margin requirement change between the forecast and the fill are not accounted for. - The hedging-offset formula (hedged portion at the MODE_MARGINHEDGED rate, any excess at the full rate) is the commonly documented approach for brokers that support it, but MQL4 exposes no way to confirm a broker's exact internal hedged-margin algorithm - treat the projected figure as a close estimate, not an exact one, particularly on brokers with unusual or tiered margin schedules. - When MODE_MARGINHEDGED returns 0 or there is no opposite-direction position open, the forecast falls back to plain summed margin with no discount, and hedgingOffsetAvailable is set to false so the caller can tell the two cases apart. - PostOrderMarginForecastCompute() only handles market orders (OP_BUY / OP_SELL) - pending orders reserve margin differently on most brokers and are out of scope for this forecast. - Requires the symbol to be present in Market Watch (or already selected) so MarketInfo() returns real figures rather than 0 - the function returns false rather than a silently wrong forecast when MODE_MARGINREQUIRED comes back as 0.

Script · MT5 · v1.0 · Free

About Post Order Margin Forecaster

Post Order Margin Forecaster is a script for MetaTrader 5. It has 2 settings, such as Planned lots and Minimum acceptable level. MQL source code included.

Files in the download: PostOrderMarginForecast.mq5.mqh, PostOrderMarginForecast.txt, PostOrderMarginForecastDemo.ex5, PostOrderMarginForecastDemo.mq5.

Settings

SettingDefaultWhat it does
Planned lots0.10lot size of the hypothetical planned order
Minimum acceptable level150.0margin level (%) below which the demo flags a warning

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

MQL4 & MQL5 scripts · PolyMaria