Empirical Margin Prober
MarketInfo(symbol, MODE_MARGINREQUIRED) is the margin figure a broker publishes, and most of the time it is accurate - but it is still just a number the broker chose to report, not something the platform measures independently. A broker whose published figure is optimistic (lower than what is actually reserved) can leave a trader surprised by a margin call that "shouldn't" have been possible on paper. The only way to know for certain is to watch the account's own margin figure change in real time around an actual fill, which is what this script does: it records AccountMargin() immediately before opening a minimum-lot market order, opens it, records AccountMargin() again immediately after the fill, and reports the difference against the published per-lot figure. The order is closed again immediately after the reading is taken, and the lot size is always the broker's own minimum for the symbol - never user-adjustable - because a margin probe has no legitimate reason to risk more than that. SAFETY. This script sends a real order to the broker's server; there is no way to measure real margin usage without one. It refuses outright to run on a LIVE account unless InpIConfirmThisOpensARealOrder is explicitly set to true, checked before any other logic runs. Even then - and on a demo account too - a MessageBox states the account type, account number and broker plainly and must be accepted before anything is sent. If the closing leg fails after every retry, the script does not fail silently: it Alerts loudly that a position may still be open and needs manual closing, and keeps retrying the close on every kind of error, including ones this catalogue's other scripts would normally treat as permanent - leaving this probe's own position open is not an acceptable outcome to give up on. Notes and limitations - This is a real trade with real spread cost, even at minimum size and even closed instantly - the round-trip cost (spread plus any swap or commission) is reported alongside the margin comparison so the cost of running the probe is never hidden. - AccountMargin() reflects the WHOLE account's used margin, not this one position specifically. Run this probe on an account with no other concurrent margin changes in flight (no other EA opening or closing trades at the same moment) for the before/after delta to isolate this trade's margin cleanly. - If InpMaxRetries attempts to close the order all fail, the script keeps retrying the close regardless of the error type rather than stopping at InpMaxRetries, and Alerts loudly that a position may still be open. Check the Experts log and close the position manually if the Alert fires. - Refuses to run if automated trading is switched off or the terminal is not connected to the trade server, before any order is considered. - Margin requirements can themselves change with volatility, session or broker policy - one probe is a single data point for the moment it was run, not a permanent guarantee of what margin will be required next time.
Script · MT4 · v1.0 · Free
About Empirical Margin Prober
Empirical Margin Prober is an expert advisor (trading robot) for MetaTrader 4. It can size trades from your balance and risk percent. It has 5 settings, such as Symbol, I confirm this opens a real order and Slippage pips. MQL source code included.
Files in the download: EmpiricalMarginProber.mq4, EmpiricalMarginProber.txt.
Trading robots can lose money. Check every setting and test on a demo account before using real funds.
Settings
| Setting | Default | What it does |
|---|---|---|
| Symbol | Symbol to probe ("" = this chart's symbol) | |
| I confirm this opens a real order | false | Must be TRUE to run on a LIVE account - see the .txt |
| Slippage pips | 5 | Slippage tolerance, pips |
| Max retries | 5 | Attempts before giving up on open or close |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Experts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
- Test it first on a demo account or in the Strategy Tester.
Tags: Risk & Lot Size · MT4 Script · MQL4 Source Code