Modification failed due to order or position being close to market

 
So my EA can't pass the verification check, I getting the error

test on EURUSD,H1 (netting)
 2021.02.19 11:17:00   failed instant buy 0.2 NZDJPY at 76.504, close #10 sell 0.2 NZDJPY 75.919 [Modification failed due to order or position being close to market]

I have known this error for years and I always handling it by checking freeze/stop zones then add a few pips as a breathing room then send the order. Here are my codes:
// Adjust above the stop level
double AdjustAboveStopLevel(string pSymbol, double pPrice, int pPoints = 10)
{
        double currPrice = SymbolInfoDouble(pSymbol,SYMBOL_ASK);
        double point = SymbolInfoDouble(pSymbol,SYMBOL_POINT);
        double stopLevel = SymbolInfoInteger(pSymbol,SYMBOL_TRADE_STOPS_LEVEL) * point;
        double stopPrice = currPrice + stopLevel;
        double addPoints = pPoints * point;
        
        if(pPrice > (stopPrice + addPoints)) 
                return(pPrice);
        else
        {
                double newPrice = stopPrice + addPoints;
                Print("Price adjusted above stop level to "+DoubleToString(newPrice));
                return(newPrice);
        }
}

// Adjust Below the Stop Levels
double AdjustBelowStopLevel(string pSymbol, double pPrice, int pPoints = 10)
{
        double bidPrice = SymbolInfoDouble(pSymbol,SYMBOL_BID);
        double point = SymbolInfoDouble(pSymbol,SYMBOL_POINT);
        double stopLevel = SymbolInfoInteger(pSymbol,SYMBOL_TRADE_STOPS_LEVEL) * point;
        double stopPrice = bidPrice - stopLevel;
        double addPoints = pPoints * point;
        
        if(pPrice < (stopPrice - addPoints)) 
                return(pPrice);
        else
        {
                double newPrice = stopPrice - addPoints;
                Print("Price adjusted below stop level to "+DoubleToString(newPrice));
                return(newPrice);
        }
}
That should solve the problem and that is what I have been used on other EAs that didn't trigger the error

Confusion:
What I'm confused is that the error when sending the buy order to the sell position which means, it is closing since they have the same size.
But I thought this error should not occur when closing a position, as the order is using the market price and with some allowed deviation, it should good.

Here are the closing codes:
long pOrderType = OrderGetInteger(ORDER_TYPE);
        string symbol = OrderGetString(ORDER_SYMBOL);
        
        do 
        {
                bool sent;
                sent = CTrade::PositionClose(pTicket);
                
                checkCode = CheckReturnCode(m_result.retcode);
                
                if(checkCode == CHECK_RETCODE_OK) break;
                else if(checkCode == CHECK_RETCODE_ERROR)
                {
                        string errDesc = TradeServerReturnCodeDescription(m_result.retcode);
                        Alert("Closing market order for ",pTicket," | type: ", pOrderType," : Error ",m_result.retcode," - ",errDesc);
                        LogTradeRequest();
                        break;
                }
                else
                {
                        Print("Server error detected, retrying...");
                        Sleep(RETRY_DELAY);
                        retryCount++;
                }
        }
        while(retryCount < MAX_RETRIES);
        
        if(retryCount >= MAX_RETRIES)
        {
                string errDesc = TradeServerReturnCodeDescription(m_result.retcode);
                Alert("Max retries exceeded: Error ",m_result.retcode," - ",errDesc);
        }
I'm just calling the MQL5 trade.mqh library function CTrade::PositionClose(ticket)

I'm I missing something? Any help?
 
Michael Reuben Msidada:
I have known this error for years and I always handling it by checking freeze/stop zones then add a few pips as a breathing room then send the order.

I'm I missing something? Any help?

On ECN brokers, the broker may not know, thus the functions return zero.

Your code then only uses one PIP.

 
William Roeder #:

On ECN brokers, the broker may not know, thus the functions return zero.

Your code then only uses one PIP.

So you think it okay to just ignore the

SymbolInfoInteger(pSymbol,SYMBOL_TRADE_STOPS_LEVEL)

and hardcode the stoplevel breathing room to may be 3 pips/30 points?

How you guys doing it?

 
I have handled the error by doing these modification for anyone wondering
// Adjust stop level
double AdjustAboveStopLevel(string pSymbol, double pPrice, int pPoints = 10, int pStopLevel=50)
{
        double currPrice = SymbolInfoDouble(pSymbol,SYMBOL_ASK);
        double point = SymbolInfoDouble(pSymbol,SYMBOL_POINT);
        double stopLevel = SymbolInfoInteger(pSymbol,SYMBOL_TRADE_STOPS_LEVEL) * point;
        double stopPrice;
        if(stopLevel>0){ // Incase broker stoplevel return 0
                stopPrice = currPrice + stopLevel;
        }else{
                stopPrice = currPrice + pStopLevel * point;
                Print("SymbolInfoInteger(pSymbol,SYMBOL_TRADE_STOPS_LEVEL) is returning 0, artificial StopLevel:", pStopLevel, " will be used instead.");
        }
        double addPoints = pPoints * point;
        
        if(pPrice > (stopPrice + addPoints)) 
                return(pPrice);
        else
        {
                double newPrice = stopPrice + addPoints;
                Print("Price adjusted above stop level to "+DoubleToString(newPrice));
                return(newPrice);
        }
}


double AdjustBelowStopLevel(string pSymbol, double pPrice, int pPoints = 10, int pStopLevel=50)
{
        double bidPrice = SymbolInfoDouble(pSymbol,SYMBOL_BID);
        double point = SymbolInfoDouble(pSymbol,SYMBOL_POINT);
        double stopLevel = SymbolInfoInteger(pSymbol,SYMBOL_TRADE_STOPS_LEVEL) * point;
        double stopPrice;
        if(stopLevel>0){ // Incase broker stoplevel return 0
                stopPrice = bidPrice - stopLevel;
        }else{
                stopPrice = bidPrice - pStopLevel * point;
                Print("SymbolInfoInteger(pSymbol,SYMBOL_TRADE_STOPS_LEVEL) is returning 0, artificial StopLevel:", pStopLevel, " will be used instead.");
        }

        double addPoints = pPoints * point;
        
        if(pPrice < (stopPrice - addPoints)) 
                return(pPrice);
        else
        {
                double newPrice = stopPrice - addPoints;
                Print("Price adjusted below stop level to "+DoubleToString(newPrice));
                return(newPrice);
        }
}