Order Preflight Validator
Discovering what is wrong with an order by calling OrderSend() means learning about exactly one problem per attempt: fix the stop distance, resend, discover the lot size was also wrong, resend again, discover margin was also insufficient. Each round trip costs a live broker call, and on a fast-moving market the price used in the first attempt may no longer be current by the time all the problems are fixed one at a time. OrderPreflightValidator checks a proposed order against every constraint in a single pass: trading permission (AutoTrading, connection status, and the symbol's own trade-allowed flag), volume against the symbol's minimum, maximum and lot step, stop loss and take profit distance against the broker's stop level and freeze level, which side of the price the stop and target sit on, and finally margin, via AccountFreeMarginCheck() - the built-in function that reports what free margin would remain after the trade, without actually sending it. Every violation found is appended to a string array and returned together, so a human reading the Experts log or a calling EA deciding whether to proceed sees the complete picture in one call. Notes and limitations - This function only VALIDATES - it never calls OrderSend(), OrderModify() or any other trading function itself. - The margin check relies on AccountFreeMarginCheck(), which prices the hypothetical order at current market conditions; if price moves materially between validation and the real OrderSend() call, the margin picture can have moved too - validate immediately before sending, not minutes in advance. - Stop/freeze distance checks use MODE_STOPLEVEL and MODE_FREEZELEVEL as reported by the broker right now; some brokers widen these dynamically around news, so a proposal that passes now can still be rejected later if conditions changed between the check and the send. - No contract size, point value or lot size is hardcoded anywhere - every limit is read per symbol via MarketInfo(), so gold, indices and JPY crosses are handled correctly without special-casing.
Script · MT5 · v1.0 · Free
About Order Preflight Validator
Order Preflight Validator is a script for MetaTrader 5. MQL source code included.
Files in the download: OrderPreflightValidator.mq5.mqh, OrderPreflightValidator.txt, OrderPreflightValidatorDemo.ex5, OrderPreflightValidatorDemo.mq5.
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 · Risk & Lot Size · MT5 Script · MQL5 Source Code