Respecter SYMBOL_TRADE_STOPS_LEVEL
Ajuste un SL/TP trop proche du prix courant à la distance minimale imposée par le broker — sans ce contrôle, OrderSend échoue avec « Invalid stops » (retcode 10016).
Cas d'usage
Robustesse multi-brokers : le stops level varie de 0 à 50+ points selon les serveurs.
Prérequis
MetaTrader 5, MQL5
MQL5
double EnforceStopsLevel(string symbol, ENUM_ORDER_TYPE type,
double price, double sl)
{
long stopsLevel = SymbolInfoInteger(symbol, SYMBOL_TRADE_STOPS_LEVEL);
if(stopsLevel <= 0)
return sl; // pas de contrainte sur ce serveur
double point = SymbolInfoDouble(symbol, SYMBOL_POINT);
double minDist = stopsLevel * point;
int digits = (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);
if(type == ORDER_TYPE_BUY && price - sl < minDist)
sl = NormalizeDouble(price - minDist, digits);
if(type == ORDER_TYPE_SELL && sl - price < minDist)
sl = NormalizeDouble(price + minDist, digits);
return sl;
}Résultat
2026.06.10 09:47:55.221 StopsGuard (GBPUSD,M5) stops_level serveur = 20 pts (distance minimale SL/TP) 2026.06.10 09:47:55.222 StopsGuard (GBPUSD,M5) BUY @ 1.27543 : SL demandé 1.27535 trop proche (8 pts) 2026.06.10 09:47:55.222 StopsGuard (GBPUSD,M5) SL ajusté -> 1.27523 (20 pts) : « Invalid stops » 10016 évité 2026.06.10 09:48:10.118 StopsGuard (GBPUSD,M5) SELL : SL 1.27590 déjà à 47 pts -> inchangé
Stops LevelInvalid StopsBrokerValidation