Problem Deleting pending order with time (MQL5)

 

Hi All

I have a problem with deleting a pending order/s after time. I don't want to delete all orders I just want to delete pending order/s after time. If you know the problem please help.

for(int Loop=OrdersTotal()-1;Loop>=0;Loop--)
  {
   MqlRates Candle[];
   ArraySetAsSeries(Candle,true);
   int rates=CopyRates(Symbol(),0,0,1,Candle);
   
   if(m_order.SelectByIndex(Loop)) 
   if(m_order.Symbol()==Symbol()&&m_order.Magic()==MagicNumber)
   if(m_order.Type()==ORDER_TYPE_BUY_STOP||m_order.Type()==ORDER_TYPE_SELL_STOP)
   if(m_order.TypeTime()<Candle[2].time)
   {
    bool Delete=trade.OrderDelete(m_order.Ticket());
    Print("ErrorCode:",GetLastError());
   }
  }
 

You are copying ONE candle (current candle -> bar #0)

int rates=CopyRates(Symbol(),0,0,1,Candle);

but trying to refer to the THIRD candle:

if(m_order.TypeTime()<Candle[2].time)
 
Vladimir Karputov #:

You are copying ONE candle (current candle -> bar #0)

but trying to refer to the THIRD candle:

I changed it as you mentioned but still not deleting as i want it to.

for(int Loop=OrdersTotal()-1;Loop>=0;Loop--)
  {
   MqlRates Candle[];
   ArraySetAsSeries(Candle,true);
   int rates=CopyRates(Symbol(),0,0,1,Candle);
   
   if(m_order.SelectByIndex(Loop)) 
   if(m_order.Symbol()==Symbol()&&m_order.Magic()==MagicNumber)
   if(m_order.Type()==ORDER_TYPE_BUY_STOP||m_order.Type()==ORDER_TYPE_SELL_STOP)
   if(m_order.TypeTime()<Candle[1].time)
   {
    bool Delete=trade.OrderDelete(m_order.Ticket());
    Print("ErrorCode:",GetLastError());
   }
  }
 
Jack Buda #:

I changed it as you mentioned but still not deleting as i want it to.


You are copying ONE candle (current candle -> bar #0)

int rates=CopyRates(Symbol(),0,0,1,Candle);

but trying to refer to the FIRST candle:

if(m_order.TypeTime()<Candle[1].time)


Another mistake.

 
Better do this: draw a pictures and in the picture show WHAT EXACTLY you want.
 
Vladimir Karputov #:
Better do this: draw a pictures and in the picture show WHAT EXACTLY you want.
Can i post my MT4 code (I know this section is for MT5 though), when using this function? Maybe you might get an idea of what I'm trying to do.
 
Jack Buda # :
Can i post my MT4 code (I know this section is for MT5 though), when using this function? Maybe you might get an idea of what I'm trying to do.

Draw your idea better as a picture.

 

If the buy stop is not activated delete after 2 candles. I hope it makes sense

 
Jack Buda #: If the buy stop is not activated delete after 2 candles. I hope it makes sense

Your image shows a buy stop below the market; you want a buy limit.

 
Jack Buda # :

If the buy stop is not activated delete after 2 candles. I hope it makes sense

Thanks. Here is the function:

//+------------------------------------------------------------------+
//| Delete After N Bars                                              |
//+------------------------------------------------------------------+
void DeleteAfterNBars(const int bars)
  {
   if(bars<1)
      return;
   MqlRates rates[];
   ArraySetAsSeries(rates,true);
   int start_pos=0,count=bars+1;
   if(CopyRates(m_symbol.Name(),InpWorkingPeriod,start_pos,count,rates)!=count)
      return;
//---
   for(int i=OrdersTotal()-1; i>=0; i--) // returns the number of current orders
      if(m_order.SelectByIndex(i))     // selects the pending order by index for further access to its properties
         if(m_order.Symbol()==m_symbol.Name() && m_order.Magic()==InpMagic)
            if(m_order.TimeSetup()<=rates[count-1].time)
               if(!m_trade.OrderDelete(m_order.Ticket()))
                  if(InpPrintLog)
                     Print(__FILE__," ",__FUNCTION__,", ERROR: ","CTrade.OrderDelete ",m_order.Ticket());
  }
 
Jack Buda:

Hi All

I have a problem with deleting a pending order/s after time. I don't want to delete all orders I just want to delete pending order/s after time. If you know the problem please help.

This is simple way.

Try add expiry time to Open pending order command

eatrade.SellStop(Volume, SellPrice, _Symbol, SL, TP, ORDER_TIME_SPECIFIED, expdate, "S" + _Symbol + EnumToString(PERIOD_CURRENT) + "S")
Reason: