Invalid stops and modification failed errors in My EA on EURUSD H1 netting — How to fix SL/TP issues?

 

Hello MQL5 community,

I'm developing an Expert Advisor  in a netting account.

the EA work perfectly  but when i want to publish my ea i get these error messages:

  • failed market sell 0.2 EURUSD sl: ... tp: ... [Invalid stops]

  • failed modify #... sell 0.2 EURUSD sl: ... tp: ... [Modification failed due to order or position being close to market]

This happens mainly during order open or modifying SL/TP on open positions. The errors break the EA's trading flow.

What I've tried so far:

  • I retrieve the broker minimum stop level using SYMBOL_TRADE_STOPS_LEVEL .

  • I adjust my stop loss (SL) and take profit (TP) levels according to this stop level before sending orders or modifying positions.

  • I check spreads and trading hours before opening trades.

  • I use CTrade class functions Buy() , Sell() , and PositionModify() to manage trades.

  • // Adjust SL/TP to respect broker stop level
    double AdjustStopLevel(bool isBuy, double price, double stopPrice, int stopLevelPoints)
    {
        double minDistance = stopLevelPoints * _Point;
        if(isBuy)
        {
            if(stopPrice > price - minDistance)
                stopPrice = price - minDistance;
        }
        else
        {
            if(stopPrice < price + minDistance)
                stopPrice = price + minDistance;
        }
        return stopPrice;
    }
    
    // Usage example before order placement:
    int stopLevelPoints = (int)SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL);
    
    sl = AdjustStopLevel(buy, entry, sl, stopLevelPoints);
    tp = AdjustStopLevel(!buy, entry, tp, stopLevelPoints);
    
    bool result;
    if(buy)
        result = trade.Buy(lots, _Symbol, entry, sl, tp, "ZenScalpPro Buy");
    else
        result = trade.Sell(lots, _Symbol, entry, sl, tp, "ZenScalpPro Sell");
    
    if(!result)
        Print("Order failed: ", trade.ResultRetcodeDescription());
    
    // Example of modifying position SL/TP safely:
    if(!trade.PositionModify(ticket, new_sl, new_tp))
        Print("Position modify failed: ", trade.ResultRetcodeDescription());
    
please if you have any idea how to fix reply thank you
 
    if(isBuy)
    {
        if(stopPrice > price - minDistance)
            stopPrice = price - minDistance;
    }
    else
    {
        if(stopPrice < price + minDistance)
            stopPrice = price + minDistance;
    }

One price for different directions.

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
              MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

  3. Prices (open, SL, and TP) must be a multiple of ticksize. Using Point means code breaks on 4 digit brokers (if any still exists), exotics (e.g. USDZAR where spread is over 500 points), and metals. Compute what a logical PIP is and use that, not points.
              How to manage JPY pairs with parameters? - MQL4 programming forum (2017)
              Slippage defined in index points - Expert Advisors and Automated Trading - MQL5 programming forum (2018)

  4. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

    Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
    My GBPJPY shows average spread = 26 points, average maximum spread = 134.
    My EURCHF shows average spread = 18 points, average maximum spread = 106.
    (your broker will be similar).
              Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)