See if this function can help you:
//+--------------------------------------------------------------------------------------------------------------------+ //| This function checks the minimum distance in points from current closing price for placing stop orders | //+--------------------------------------------------------------------------------------------------------------------+ bool CheckStopsLevel(double Price, ENUM_ORDER_TYPE Type) { //--- Determines last price for current symbol double BID = SymbolInfoDouble(Symbol(), SYMBOL_BID); double ASK = SymbolInfoDouble(Symbol(), SYMBOL_ASK); //--- Get the SYMBOL_TRADE_STOPS_LEVEL level ulong StopsLevel = SymbolInfoInteger(Symbol(), SYMBOL_TRADE_STOPS_LEVEL); //--- Result of checking bool Check = true; //--- Check the order type switch(Type) { //--- BuyStop pending order case ORDER_TYPE_BUY_STOP: { //--- Check the distance from the current price to the activation price Check = ((Price - ASK) > StopsLevel * Point()); if(!Check) {ERRMSG = "The distance from the current price to the order opening price is invalid.";} } break; //--- SellStop pending order case ORDER_TYPE_SELL_STOP: { //--- Check the distance from the current price to the activation price Check = ((BID - Price) > StopsLevel * Point()); if(!Check) {ERRMSG = "The distance from the current price to the order opening price is invalid.";} } break; } //--- Verification succeeded return(Check); }
//+--------------------------------------------------------------------------------------------------------------------+ //| This function checks the distance from opening price to activation price | //+--------------------------------------------------------------------------------------------------------------------+ bool CheckFreezeLevel(ulong TICKET) { //--- Determines last price for current symbol double BID = SymbolInfoDouble(Symbol(), SYMBOL_BID); double ASK = SymbolInfoDouble(Symbol(), SYMBOL_ASK); //--- Get the SYMBOL_TRADE_FREEZE_LEVEL level ulong FreezeLevel = SymbolInfoInteger(Symbol(), SYMBOL_TRADE_FREEZE_LEVEL); //--- Select order for working if(!OrderSelect(TICKET)) { //--- Failed to select order ERRMSG = StringFormat("Error selecting order to work, ticket ", TICKET); return(false); } //--- Get the order data double Price = OrderGetDouble(ORDER_PRICE_OPEN); ENUM_ORDER_TYPE Type = (ENUM_ORDER_TYPE) OrderGetInteger(ORDER_TYPE); //--- Result of checking bool Check = true; //--- Check the order type switch(Type) { //--- BuyStop pending order case ORDER_TYPE_BUY_STOP: { //--- Check the distance from the opening price to the activation price Check = ((Price - ASK) > FreezeLevel * Point()); if(!Check) {ERRMSG = StringFormat("Order %s #%d cannot be modified: Ask-Open=%d points < SYMBOL_TRADE_FREEZE_LEVEL=%d points ", EnumToString(Type), TICKET, int(((Price - ASK) / Point())), FreezeLevel);} } break; //--- SellStop pending order case ORDER_TYPE_SELL_STOP: { //--- Check the distance from the opening price to the activation price Check = ((BID - Price) > FreezeLevel * Point()); if(!Check) {ERRMSG = StringFormat("Order %s #%d cannot be modified: Bid-Open=%d points < SYMBOL_TRADE_FREEZE_LEVEL=%d points ", EnumToString(Type), TICKET, int(((BID - Price) / Point())), FreezeLevel);} } break; } //--- Verification succeeded return(Check); }
You have to verify that your stops and/or pending orders abide by the Stops Level and Freeze Level conditions set by your broker's contract specifications.
Forum on trading, automated trading systems and testing trading strategies
HELP, failed cancel order [Invalid stops] problem!
Fernando Carreiro, 2022.02.08 14:35
Also read the following in detail ... The checks a trading robot must pass before publication in the Market
Especially these points:
Setting the TakeProfit and StopLoss levels within the SYMBOL_TRADE_STOPS_LEVEL minimum level Attempt to modify order or position within the SYMBOL_TRADE_FREEZE_LEVEL freeze level Forum on trading, automated trading systems and testing trading strategies
ERROR: Modification failed due to order or position being close to market
William Roeder, 2020.09.23 13:57
You can't move stops (or pending prices) closer to the market than the minimum: MODE_STOPLEVEL * _Point or SymbolInfoInteger(SYMBOL_TRADE_STOPS_LEVEL).
Requirements and Limitations in Making Trades - Appendixes - MQL4 TutorialOn some ECN type brokers the value might be zero (broker doesn't know.) Use a minimum of two (2) PIPs.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use