Loading…

Margin What-If Simulator

Working out whether a trade will leave enough breathing room is normally done by opening the order and looking, which is a strange way to find out something you wanted to know before committing. This script answers the question first. The trap in a homemade version of this calculation is the margin figure itself. Multiplying a static per-lot margin constant by the intended volume looks reasonable, but that value can be zero on brokers that price margin unusually, or quoted in a currency other than the account currency on some CFD and index symbols. A script that trusts it blindly prints a confident number that a real order would not agree with. This script instead calls the platform's own margin-calculation engine with the exact symbol, direction and volume requested - the same engine that would accept or reject a real order - so the simulation cannot be wrong in a way that matters on a flat book: if it says the order fits, a real order with the same parameters will fit too, on this account, right now. The volume is floored to the broker's lot step rather than rounded, so the simulation never tests a larger position than was actually typed in. Notes and limitations - No order is ever sent. This is read-only. - The "after" equity figure ignores the entry spread cost - a real fill shows a small extra floating loss the instant it opens, which this script does not add in. - The money-mode stop-out interpretation (equity minus margin falling to the configured money level) is the common convention, but brokers do not all document their exact formula identically. Percent-mode stop-out is exact. - Only one hypothetical order is tested per run. Basket or multi-symbol what-ifs are outside this script's scope. - On MQL5, an opposite-direction position already open on the tested symbol makes the required-margin figure a conservative overstatement on a netting account - see the differences section above.

Script · MT5 · v1.0 · Free

About Margin What-If Simulator

Margin What-If Simulator is a script for MetaTrader 5. It can alert you with a pop-up, sound, email or push message. It has 3 settings, such as Symbol, Side and Volume. MQL source code included.

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

Settings

SettingDefaultWhat it does
SymbolSymbol ("" = this chart's symbol)
SideWhatIfBuyHypothetical order direction
Volume0.10Hypothetical order volume, lots

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

MQL4 & MQL5 scripts · PolyMaria