orderdelete error

 

I am getting a lot of these errors in my code:

Alert: Order 153 failed to close. Error:4108

Any ideas why?


//+------------------------------------------------------------------+
//|                                    Manage open trades by ATR.mq4 |
//|                      Copyright © 2009, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#include <WinUser32.mqh>
#include <stderror.mqh>
#include <stdlib.mqh>

void logError(string functionName, string msg, int errorCode = -1)
  {
    Print("ERROR: in " + functionName + "()");
    Print("ERROR: " + msg );
    
    int err = GetLastError();
    if(errorCode != -1) 
        err = errorCode;
        
    if(err != ERR_NO_ERROR) 
      {
        Print("ERROR: code=" + err + " - " + ErrorDescription( err ));
      }    
  }

int start()
{
//+------------------------------------------------------------------+
//  You open a buy on Actual Ask, you close it on Actual Bid.
//  You open a sell on Actual Bid, you close it on Actual Ask.
//+------------------------------------------------------------------+

   int    nDigits;
   int    nMultiply;
   if(Symbol()=="GBPJPY" || Symbol()=="EURJPY" || Symbol()=="USDJPY" || Symbol()=="GOLD")  {nDigits = 3;nMultiply=100;}
   else {nDigits = 5;nMultiply=10000;}
   
   double currATR = iATR(NULL,PERIOD_D1,14,0) * nMultiply;
   double TradeTargetPrice = 26;
   double TradeStopLoss = 26;
   double BufferShort = 0.0006;
   double BufferLong = 0.0006; //takes into account the spread should be more than 6
   double StartHour = 22;
   double EndHour = 7;
   double diff = (EndHour-0)+(24-StartHour);

//////////////////////////////////////////////////////////
//IT IS NOW 2100GMT, delete any existing pending orders
//////////////////////////////////////////////////////////
bool is_timetodelete=false;
if(Hour()==22 && Minute()==0 && Seconds()==0) {is_timetodelete = true; }

if (is_timetodelete == true)
{      
      int total = OrdersTotal();
      for(int i=total-1;i>=0;i--) //if no orders then it won't run through this again
      {
         OrderSelect(i, SELECT_BY_POS);
         int type   = OrderType();

          bool result = false;
    
         switch(type)
         {
            //Close pending orders
            case OP_BUYLIMIT  :
            case OP_BUYSTOP   : result = OrderDelete( OrderTicket() );
            case OP_SELLLIMIT :
            case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
         }
    
         if(result == false)
         {
            Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
            Sleep(3000);
         }  
      }
}

//////////////////////////////////////////////////////////
//IT IS NOW 0700GMT, set up the pending orders
//////////////////////////////////////////////////////////
bool is_timetocreatependingorders = false;
int ticket; 

if(Hour()==7 && Minute()==0 && Seconds()==0) 
{
   is_timetocreatependingorders = true;
   double OvernightHigh = High[iHighest(NULL,PERIOD_H1,MODE_HIGH,diff,0)];
   double OvernightLow = Low[iLowest(NULL,PERIOD_H1,MODE_HIGH,diff,0)];  
   Print("High="+OvernightHigh + " Ask="+Ask);
   Print("Low="+OvernightLow + " Bid="+Bid);

   //Extra buffer for if price is too close to ask/bid, give it an extra 5
   if (MathAbs(OvernightHigh-Ask) <=0.0005) {BufferLong=BufferLong+0.005;}
   if (MathAbs(OvernightLow-Bid) <=0.0005) {BufferShort=BufferShort+0.005;}

   if (OrdersTotal() == 0 && is_timetocreatependingorders == true)
      {
         //LONG
         RefreshRates();
         ticket=OrderSend(Symbol(),OP_BUYSTOP,1,OvernightHigh+BufferLong,NULL,(OvernightHigh+BufferLong)-(TradeStopLoss*Point),(OvernightHigh+BufferLong)+(TradeTargetPrice*Point),"LONG",0,0,Green);
      
         if(!ticket)
            {
               int errorCode = GetLastError();
               if(errorCode != ERR_NO_RESULT ) 
                  logError("OrderSend", "Error", errorCode);
            }
   
         //SHORT
         RefreshRates();
         ticket=OrderSend(Symbol(),OP_SELLSTOP,1,OvernightLow-BufferShort,NULL,(OvernightLow-BufferShort)+(TradeStopLoss*Point),(OvernightLow-BufferShort)-(TradeTargetPrice*Point),"SHORT",0,0,Red);
   
         if(!ticket)
            {
               errorCode = GetLastError();
               if(errorCode != ERR_NO_RESULT ) 
                  logError("OrderSend", "Error", errorCode);
            }
      }
   }

}      
 

Replace

switch(type)
         {
            //Close pending orders
            case OP_BUYLIMIT  :
            case OP_BUYSTOP   : result = OrderDelete( OrderTicket() );
            case OP_SELLLIMIT :
            case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
         }

to

if(type>1)result = OrderDelete( OrderTicket() );
 
Roger:

Replace [...] to [...]

Fleshing out Roger's answer with a reason, for SanMiguel's benefit, in the code Roger is replacing OrderDelete() gets called twice if the order type is a buy-stop or a buy-limit. Processing "falls through" from one part of a switch statement to another unless there's a "break". For an example of a similar problem involving switch statements without breaks, see my comment timestamped 2009.03.26 21:56 in 'Evaluating Account Balance'

 
jjc:

Fleshing out Roger's answer with a reason, for SanMiguel's benefit, in the code Roger is replacing OrderDelete() gets called twice if the order type is a buy-stop or a buy-limit. Processing "falls through" from one part of a switch statement to another unless there's a "break". For an example of a similar problem involving switch statements without breaks, see my comment timestamped 2009.03.26 21:56 in 'Evaluating Account Balance'


what about the sell-stop and sell-limit ? is that 1 as well?


If 1 of these pending order trades is triggered and hits its take profit, I want to cancel the other order.

If it instead, hits its stop loss, I want to keep the other order.


Would I use a flag for this in this code?

Does a pending order change from op_buylimit to op-Buy when triggered?

//////////////////////////////////////////////////////////
//IT IS NOW after 0700GMT but before 2100GMT, monitor order count
//////////////////////////////////////////////////////////
if (Hour()>=7 && Hour() <21) 
{
      int totalOrdrs = OrdersTotal();
      if (totalOrdrs > 0) 
      {
         for(i=totalOrdrs-1;i>=0;i--) //if no orders then it won't run through this again
         {
            OrderSelect(i, SELECT_BY_POS);
            type   = OrderType();
            result = false;
            if(type=OP_BUY || type==OP_SELL) // ie one of the orders has been triggered, now we delete the other one.
            {
                  for(i=totalOrdrs-1;i>=0;i--) //if no orders then it won't run through this again
                  {
                     OrderSelect(i, SELECT_BY_POS);
                     type   = OrderType();
                     result = false;
                     if(type>1)result = OrderDelete( OrderTicket() );
                     if(result == false)
                     {
                        Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
                        Sleep(3000);
                     }
                  }
              
            }
          }
      }
}

}
Reason: