Download MetaTrader 5

problem with Delete pending order based on Time

To add comments, please log in or register
mohsenvixa
12
mohsenvixa  

 Hi to all

my EA working at daily time and I want Delete Pending order after 2 Day But , my Code don't work true .

I don't know where is problem , I hope that you answer me.

thanks

bool BuyMode;
bool SellMode;
double slB;
double slS;
extern double Price_Distance=10; 
extern double LotSize=0.2;
int ticket ;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {

// BUY AND SELL MODES------------------------------------------------+
if (Volume[0]>1) 
   return(0);
   BuyMode  = ((iRSI(NULL,0,2,PRICE_CLOSE,1)<10)&& DayOfWeek()==3);
   //SellMode = ((iRSI(NULL,PERIOD_D1,2,PRICE_CLOSE,1)>90));

// StopLoss & TakeProfit---------------------------------------------+

//   slB = iLow (Symbol(), 0 , iLowest (Symbol(),0,MODE_LOW , 2 , 0) );
//   slS = iHigh(Symbol(), 0 , iHighest(Symbol(),0,MODE_HIGH, 2 , 0) );

//Orders Counting & Close Orders------------------------------------+

   int orderscnt=0;
for(int i=OrdersTotal()-1;i>=0;i--)
     {
      //--------------------------------------
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if((OrderSymbol()==Symbol()) && ((OrderType()==OP_SELL) || (OrderType()==OP_BUY)))
        {
         orderscnt++;
        }
  //-Close Positione Buy if
      if((OrderSymbol()==Symbol()) && (OrderType()==OP_BUY) && (iRSI(NULL,0,3,PRICE_CLOSE,1)>75))
        {
          OrderClose(OrderTicket(),OrderLots(),Bid,1,Black);
     
     }

      //-Close Positione Sell if 
      if((OrderSymbol()==Symbol()) && (OrderType()==OP_SELL) && (iRSI(NULL,PERIOD_D1,2,PRICE_CLOSE,1)<30))
        {
         OrderClose(OrderTicket(),OrderLots(),Ask,1,Black);
        }
     }
// Buy AND Sell Orders ----------------------------------------------+
  
 if((BuyMode) && (orderscnt<5))
     {
      orderdelete();
      ticket=OrderSend(Symbol(),OP_BUYSTOP,LotSize,High[1],2,slB,0,"Nothing",123456789,0,Blue);
     }

   if((SellMode) && (orderscnt<5))
     {
      orderdelete();
      ticket=OrderSend(Symbol(),OP_SELLSTOP,LotSize,Low[1],2,slS,0,"Nothing",123456789,0,Red);
     }

   return(0);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
void orderdelete()
{
int maxDuration = 60 * 60*24*2; 
for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
    OrderSelect(pos, SELECT_BY_POS)           
&&  OrderSymbol()== Symbol()){              
    int duration = TimeCurrent() - OrderOpenTime();
    if (duration >= maxDuration)
         OrderDelete( OrderTicket());
}      return(0);
      }
     
Alain Verleyen
Moderator
31467
Alain Verleyen  

my EA working at daily time and I want Delete Pending order after 2 Day But , my Code don't work true .

Hi,

What do you mean ? The pendings are not deleted I suppose ?

OrderDelete() is a function which returns a bool, if false that means you have an error, print the error code to know what happened. Please read the documentation.

Keith Watford
Moderator
9980
Keith Watford  

Your orderdelete() function is only called if BuyMode is true and BuyMode will only be true on a Wednesday.

Code is only executed on the first tick of the bar. If you miss that incoming tick, no execution.

PS. it is not a good idea to name functions "orderdelete", especially when posting your code as it is easily misread and confused with the standard function "OrderDelete". Better to use something like "DeletePending"

mohsenvixa
12
mohsenvixa  
GumRai:

Your orderdelete() function is only called if BuyMode is true and BuyMode will only be true on a Wednesday.

Code is only executed on the first tick of the bar. If you miss that incoming tick, no execution.

PS. it is not a good idea to name functions "orderdelete", especially when posting your code as it is easily misread and confused with the standard function "OrderDelete". Better to use something like "DeletePending"

 Thanks for your reply (GumRai and angevoyageur)
I show my problem in the picture below

 

bool BuyMode;
bool SellMode;
double slB;
double slS;
extern double Price_Distance=10; 
extern double LotSize=0.2;
int ticket ;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {

// BUY AND SELL MODES------------------------------------------------+
if (Volume[0]>1) 
   return(0);
   BuyMode  = ((iRSI(NULL,0,2,PRICE_CLOSE,1)<10));
   SellMode = ((iRSI(NULL,0,2,PRICE_CLOSE,1)>90));

// StopLoss & TakeProfit---------------------------------------------+

//   slB = iLow (Symbol(), 0 , iLowest (Symbol(),0,MODE_LOW , 2 , 0) );
//   slS = iHigh(Symbol(), 0 , iHighest(Symbol(),0,MODE_HIGH, 2 , 0) );

//Orders Counting & Close Orders------------------------------------+

   int orderscnt=0;
for(int i=OrdersTotal()-1;i>=0;i--)
     {
      //--------------------------------------
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if((OrderSymbol()==Symbol()) && ((OrderType()==OP_SELL) || (OrderType()==OP_BUY)))
        {
         orderscnt++;
        }
  //-Close Positione Buy if
      if((OrderSymbol()==Symbol()) && (OrderType()==OP_BUY) && (iRSI(NULL,0,3,PRICE_CLOSE,1)>75))
        {
          OrderClose(OrderTicket(),OrderLots(),Bid,1,Black);
     
     }

      //-Close Positione Sell if 
      if((OrderSymbol()==Symbol()) && (OrderType()==OP_SELL) && (iRSI(NULL,PERIOD_D1,2,PRICE_CLOSE,1)<30))
        {
         OrderClose(OrderTicket(),OrderLots(),Ask,1,Black);
        }
     }
// Buy AND Sell Orders ----------------------------------------------+
  
 if((BuyMode) && (orderscnt<5))
     {
      orderdelete();
      ticket=OrderSend(Symbol(),OP_BUYSTOP,LotSize,High[1],2,slB,0,"Nothing",123456789,0,Blue);
     }

   if((SellMode) && (orderscnt<5))
     {
      orderdelete();
      ticket=OrderSend(Symbol(),OP_SELLSTOP,LotSize,Low[1],2,slS,0,"Nothing",123456789,0,Red);
     }

   return(0);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
void orderdelete()
{
int maxDuration = 60 * 60*24*2; 
for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
    OrderSelect(pos, SELECT_BY_POS)           
&&  OrderSymbol()== Symbol()){              
    int duration = TimeCurrent() - OrderOpenTime();
    if (duration >= maxDuration)
         OrderDelete( OrderTicket());
}      return(0);
      }

 


Alain Verleyen
Moderator
31467
Alain Verleyen  
mohsenvixa:

 Thanks for your reply (GumRai and angevoyageur)
I show my problem in the picture below

It's kind to thank us, but it seems you didn't take any of our advices into account ?
fxtrader_
247
fxtrader_  

I'm new to mql4 but i think this will work for you.

in the ordersend use TimeCurrent()+ExpirTime

Keith Watford
Moderator
9980
Keith Watford  
Maybe the RSI was not above 90 and not less than 10
whroeder1
15017
whroeder1  
  1. if (Volume[0]>1) 
    Bars is unreliable (a refresh/reconnect can change number of bars on chart) volume is unreliable (miss ticks) Always use time. New candle - MQL4 forum
  2.          OrderDelete( OrderTicket());
    Check your return codes and find out why. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. Your orderdelete (bad name) does not check if the currently selected order is still pending. It will attempt to delete open orders more than two days old.

  4. FxTrader_: I'm new to mql4 but i think this will work for you. in the ordersend use TimeCurrent()+ExpirTime
    Not all brokers allow expiration time

    147

    ERR_TRADE_EXPIRATION_DENIED

    Expirations are denied by broker

To add comments, please log in or register