If you are porting an M1 scalper from MQL4 to MQL5, or testing a new EA on a live MT5 account, you have likely run into the dreaded OrderSend Error 130.In MT4, this was known as ERR_INVALID_STOPS. In the MQL5 environment, it shows up as trade server return code 10016 (TRADE_RETCODE_INVALID_STOPS). This error means the trade server rejected your OrderSend request because your Stop Loss (SL) or Take Profit (TP) parameters violated broker constraints. When scalping on the M1 timeframe, you are hunting for micro-movements, which means your stops are incredibly tight. Here is how to programmatically solve this issue and ensure your EA executes flawlessly.
1. Account for SYMBOL_TRADE_STOPS_LEVEL
Brokers require a minimum distance (in points) between the current market price and any resting SL/TP. If you place a stop within this zone, the server rejects it. The MQL5 Fix:Always query the stop level dynamically. Since spreads widen wildly during M1 volatility, a robust scalping EA should use the spread as a safety multiplier if the broker returns a stop level of zero.
Code: Select all
long stopLevel = SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL);
long spread = SymbolInfoInteger(_Symbol, SYMBOL_SPREAD);
// Some brokers return 0 for stopLevel but secretly enforce limits based on spread
long actualStopLevel = MathMax(stopLevel, spread * 2);
double minStopDist = actualStopLevel * SymbolInfoDouble(_Symbol, SYMBOL_POINT);A very common logic mistake is calculating a Buy order's Stop Loss from the Ask price instead of Bid, which pushes it too close to the invalid zone.
Buy Orders: Entry happens at Ask. The SL must be validated relative to Bid (i.e., SL < Bid - minStopDist).
Sell Orders: Entry happens at Bid. The SL must be validated relative to Ask (i.e., SL > Ask + minStopDist).
3. Normalize Your Doubles
Floating-point precision errors will instantly trigger a 10016 invalid stops error. Never send raw calculated variables directly to OrderSend. You must wrap your final SL and TP prices in NormalizeDouble() so they match the exact decimal structure of the symbol.