Error handling - freeze level

 

 I get this error: 

failed cancel order #10 sell stop 0.35 XAUUSD at 1310.86 sl: 1313.16 tp: 1308.56 [Invalid stops]

I assume it is because the price is too close to the order before the EA sends a request to delete the order. I use this function to delete orders, and I do check for freeze level, but I must be doing it wrong.

void PendingOrderDelete()
{
   ulong ticket = 0;
   double Ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   double Bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
  

   for(int i=0; i<OrdersTotal(); i++) {
      ticket = orderInfo.Ticket();

      if(!orderInfo.SelectByIndex(i))
         continue;
      if(orderInfo.Magic()!=inpMagicEA)
         continue;
      if(orderInfo.Symbol()!=_Symbol)
         continue;
         
      // Checking freeze level
      if(orderInfo.OrderType()==ORDER_TYPE_BUY_STOP)
        {
        double orderPrice = orderInfo.PriceOpen();
          if(orderPrice-Ask >= SymbolInfoInteger(_Symbol,SYMBOL_TRADE_FREEZE_LEVEL))
          {
            return;
           }
        }  
         if(orderInfo.OrderType()==ORDER_TYPE_SELL_STOP)
        {
        double orderPrice = orderInfo.PriceOpen();
          if(Bid-orderPrice >= SymbolInfoInteger(_Symbol,SYMBOL_TRADE_FREEZE_LEVEL)){
            return;
           }
        }  
                
      if(ticket!=0) {
         trade.OrderDelete(ticket);
      }
   }
}

Also, does anyone know if order expiration can cause this error? If by the time the order expires, the market is too close to the order to expire it, would that cause the same issue? In that case, I cant think of a way to check for that.. 

Hope someone can help me

 

Your topic has been moved to the section: Expert Advisors and Automated Trading

In the future, please consider which section is most appropriate for your query.

MQL5 forum: Expert Advisors and Automated Trading
MQL5 forum: Expert Advisors and Automated Trading
  • www.mql5.com
How to create an Expert Advisor (a trading robot) for Forex trading
 
Fernando Carreiro #:

Your topic has been moved to the section: Expert Advisors and Automated Trading

In the future, please consider which section is most appropriate for your query.

ok, thanks. Can you spot what could be causing the error? 
 
altmql #: ok, thanks. Can you spot what could be causing the error? 

No sorry, I did not analyse your code, but here is some information that might help...

Articles

The checks a trading robot must pass before publication in the Market

MetaQuotes, 2016.08.01 09:30

Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.

Forum on trading, automated trading systems and testing trading strategies

Tick size vs Point(), can be a little tricky in Multicurrency EA

Fernando Carreiro, 2022.03.09 12:11

Tick Size and Point Size can be very different especially on stocks and other symbols besides forex.

Always use Tick Size to adjust and align your prices, not the point size. In essence, make sure that your price quotes, are properly aligned to the Tick size (see following examples).

...
double tickSize = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE );
...
double normalised_price = round( price / tick_size ) * tick_size;
...
// Or use a function
double Round2Ticksize( double price )
{
   double tick_size = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE );
   return( round( price / tick_size ) * tick_size );
};
 
Fernando Carreiro #:
double tickSize = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE );
I changed to ticksize instead of point. But I still get the same error. Is there a way to get a more detailed explanation of why the errors happen during the validation process on mql5 market? 
 

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 Tutorial

On some ECN type brokers, the value might be zero (the broker doesn't know). Use a minimum of two (2) PIPs.

The checks a trading robot must pass before publication in the Market - MQL5 Articles (2016)