Stop Level Validator
Error 130 (MQL4) / TRADE_RETCODE_INVALID_STOPS (MQL5) means a price was placed closer to the market than the broker's minimum distance allows. The rule sounds simple and most home-grown checks get the reference price wrong, because it is not the same for every order type. A market order's SL/TP is measured from the price the position would close at, not the price it opened at - a buy closes at Bid, so its stops are measured from Bid, and a sell closes at Ask, so its stops are measured from Ask. A pending order is measured differently again: its entry price is checked against the current tradeable price on its side, Ask for a buy-side pending and Bid for a sell-side one, and then its OWN SL/TP is checked against that entry price, not against Bid/Ask. Skipping that second check is the most common gap - an entry price can be perfectly valid while its stop loss is still too tight relative to it. Freeze level is a separate rule with a separate purpose: it blocks modifying or deleting an order whose price already sits close to the current market, to stop a trade being adjusted in the instant before it triggers. It has nothing to do with placing a new order, so this header keeps it as its own function, IsOrderFrozen, rather than folding it into the placement checks. Every validation function returns a plain bool for pass/fail and also fills out-parameters with the nearest price that would pass, so a caller can use the corrected value unconditionally - it simply equals the input when the input was already fine. The header is self-contained. No dependencies, no DLLs, no classes - plain functions callable from a script, an indicator or an EA. It never touches money-per-point arithmetic - only broker-reported distances. Notes and limitations - Many brokers, particularly on ECN/instant-execution accounts, report the stop level as 0. A distance of 0.0 from this header means "no minimum", not "always invalid" - do not treat it as an error case. True on both platforms. - This header validates distance only. It does not snap a price onto the broker's tick grid - see PriceNormalize for that, and normalise the price before running it through this header, since a stop can be the correct distance away and still be off-grid. - IsOrderFrozen tells you whether an order is CURRENTLY frozen. Because Bid and Ask move, the answer can change between the check and the modify/delete call a moment later on a fast-moving symbol. Treat a false result as "was not frozen when checked", not a guarantee. - No thread or state concerns: every function is pure and reads only broker properties. Identical on both platforms.
Script · MT4 · v1.0 · Free
About Stop Level Validator
Stop Level Validator is a script for MetaTrader 4. It has 2 settings, such as Too close offset and Safe offset. MQL source code included.
Files in the download: StopLevelCheck.mqh, StopLevelCheck.txt, StopLevelCheckDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Too close offset | 1 | Points from market used to build a deliberately invalid stop |
| Safe offset | 50 | Points from market used to build a comfortably valid stop |
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 → Scripts.
- 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 script onto a chart to run it once.
Tags: MT4 Script · MQL4 Source Code