Download MetaTrader 5

help needed with deleting pending order

To add comments, please log in or register
Sell products via the Market and track your sales statistics
Lukasz Podkanski
209
Lukasz Podkanski 2015.02.09 23:01 

 Hi, i'm newbie here, i have developed simple EA wchich place pending orders at specific time. the broker dont allow to have expiration time less than 1 hour so i want EA to delete these orders 5 min after placing.

Here's the code, can u help me? where am i wrong?

//+------------------------------------------------------------------+
//|                                                    Moneytron.mq4 |
//|                                         Copyright © 2015, Donson |
//|                                                                  |
//+------------------------------------------------------------------+

extern double TakeProfit = 70;
extern double StopLoss = 350;
extern double stLot = 0.1;
extern double distance = 3;
extern double n = 1;
extern double m = 28;
extern double h = 14;
extern double m1 = 33;
extern double h1 = 14;

double LotsOptimized()
  {
   double lot;
   
   if(AccountFreeMargin()>=0 && AccountFreeMargin()<2000) lot=1*stLot;
   if(AccountFreeMargin()>=2000 && AccountFreeMargin()<3000) lot=2*stLot;
   if(AccountFreeMargin()>=3000 && AccountFreeMargin()<4000) lot=3*stLot;
   if(AccountFreeMargin()>=4000 && AccountFreeMargin()<5000) lot=4*stLot;
   if(AccountFreeMargin()>=5000 && AccountFreeMargin()<6000) lot=5*stLot;
   if(AccountFreeMargin()>=6000 && AccountFreeMargin()<7000) lot=6*stLot;
   if(AccountFreeMargin()>=7000 && AccountFreeMargin()<8000) lot=7*stLot;
   if(AccountFreeMargin()>=8000 && AccountFreeMargin()<9000) lot=8*stLot;
   if(AccountFreeMargin()>=9000 && AccountFreeMargin()<10000) lot=9*stLot;
    
      
   if(AccountFreeMargin()>=10000) lot=((MathRound(AccountFreeMargin()/1000))/10)*n;        

   if(lot<0.1) lot=0.1;
   if(lot>50)  lot=50;
   return(lot);
  }

int start()
  {
  
   int    total;
   int    ticket, cnt;
   
  
//----
   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);  
     }
  
   
   total=OrdersTotal();
   
  
   { 
      if(Hour()==h && total==0 && Minute()==m)
        {
         ticket=OrderSend(Symbol(),OP_BUYSTOP,LotsOptimized(),Ask+distance*Point,0,Bid+distance*Point-Point*StopLoss,Ask+distance*Point+TakeProfit*Point,"moneytron",16384,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
           }
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
        }
      
      if(Hour()==h && total==1 && Minute()==m )
        {
         ticket=OrderSend(Symbol(),OP_SELLSTOP,LotsOptimized(),Bid-distance*Point,0,Ask-distance*Point+Point*StopLoss,Bid-distance*Point-TakeProfit*Point,"moneytron",16384,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
           }
         else Print("Error opening SELL order : ",GetLastError()); 
         return(0); 
        }
      return(0);
     
   for(cnt=0;cnt<total;cnt++)
     {
     if( OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)==true)
      if(OrderSymbol()==Symbol())  
        {
         if(OrderType()==OP_SELLSTOP && Hour()==h1 && Minute()==m1)
           {
       bool    a= OrderDelete(OrderTicket(),Yellow);
            return(0);
           }
         if(OrderType()==OP_BUYSTOP && Hour()==h1 && Minute()==m1)
           {
        bool   b= OrderDelete(OrderTicket(),Yellow);
            return(0);
           }
       }
     }
   }
 }
 
Files:
Alain Verleyen
Moderator
30746
Alain Verleyen 2015.02.10 20:36  
donson:

 Hi, i'm newbie here, i have developed simple EA wchich place pending orders at specific time. the broker dont allow to have expiration time less than 1 hour so i want EA to delete these orders 5 min after placing.

Here's the code, can u help me? where am i wrong?

//--- you have to count down when closing/deleting  
   for(int cnt=OrdersTotal()-1;cnt>=0;cnt--)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol())
        {
         if((OrderType()==OP_SELLSTOP || OrderType()==OP_BUYSTOP)
            && TimeCurrent()>=OrderOpenTime()+5*60) //--- 5 minutes in seconds            
           {
            //--- delete           
            if(!OrderDelete(OrderTicket(),clrYellow))
              {
               //--- error processing
               //...
              }
           }
        }
     }
// NOT TESTED
Lukasz Podkanski
209
Lukasz Podkanski 2015.02.11 23:33  
angevoyageur:
thank you
Lukasz Podkanski
209
Lukasz Podkanski 2015.02.12 00:00  
donson:
thank you
angevoyageur:
i have just tested it, and its still not deleting 
Alain Verleyen
Moderator
30746
Alain Verleyen 2015.02.12 00:50  
donson:
i have just tested it, and its still not deleting 
Please post your updated code, and the Experts log (or Journal log from Strategy Tester if it's a backtest).
Lukasz Podkanski
209
Lukasz Podkanski 2015.02.12 01:39  
angevoyageur:
Please post your updated code, and the Experts log (or Journal log from Strategy Tester if it's a backtest).

 

//+------------------------------------------------------------------+
//|                                                    Moneytron.mq4 |
//|                                         Copyright © 2015, Donson |
//|                                                                  |
//+------------------------------------------------------------------+

extern double TakeProfit = 70;
extern double StopLoss = 350;
extern double stLot = 0.1;
extern double distance = 3;
extern double n = 1;
extern double m = 27;
extern double h = 14;
extern double m1 = 5;

double LotsOptimized()
  {
   double lot;
   
   if(AccountFreeMargin()>=0 && AccountFreeMargin()<2000) lot=1*stLot;
   if(AccountFreeMargin()>=2000 && AccountFreeMargin()<3000) lot=2*stLot;
   if(AccountFreeMargin()>=3000 && AccountFreeMargin()<4000) lot=3*stLot;
   if(AccountFreeMargin()>=4000 && AccountFreeMargin()<5000) lot=4*stLot;
   if(AccountFreeMargin()>=5000 && AccountFreeMargin()<6000) lot=5*stLot;
   if(AccountFreeMargin()>=6000 && AccountFreeMargin()<7000) lot=6*stLot;
   if(AccountFreeMargin()>=7000 && AccountFreeMargin()<8000) lot=7*stLot;
   if(AccountFreeMargin()>=8000 && AccountFreeMargin()<9000) lot=8*stLot;
   if(AccountFreeMargin()>=9000 && AccountFreeMargin()<10000) lot=9*stLot;
    
      
   if(AccountFreeMargin()>=10000) lot=((MathRound(AccountFreeMargin()/1000))/10)*n;        

   if(lot<0.1) lot=0.1;
   if(lot>50)  lot=50;
   return(lot);
  }

int start()
  {
  
   int    total;
   int    ticket;
   
  
//----
   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);  
     }
  
   
   total=OrdersTotal();
   
  
   { 
      if(Hour()==h && total==0 && Minute()==m)
        {
         ticket=OrderSend(Symbol(),OP_BUYSTOP,LotsOptimized(),Ask+distance*Point,0,Bid+distance*Point-Point*StopLoss,Ask+distance*Point+TakeProfit*Point,"moneytron",16384,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
           }
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
        }
      
      if(Hour()==h && total==1 && Minute()==m )
        {
         ticket=OrderSend(Symbol(),OP_SELLSTOP,LotsOptimized(),Bid-distance*Point,0,Ask-distance*Point+Point*StopLoss,Bid-distance*Point-TakeProfit*Point,"moneytron",16384,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
           }
         else Print("Error opening SELL order : ",GetLastError()); 
         return(0); 
        }
      return(0);
     
   for(int cnt=OrdersTotal()-1;cnt>=0;cnt--)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol())
        {
         if((OrderType()==OP_SELLSTOP || OrderType()==OP_BUYSTOP)
            && TimeCurrent()>=OrderOpenTime()+m1*60) //--- 5 minutes in seconds            
           {
            //--- delete           
            if(!OrderDelete(OrderTicket(),clrYellow))
              {
               //--- error processing
               //...
              }
           }
        }
     }
   }
 }
Alain Verleyen
Moderator
30746
Alain Verleyen 2015.02.12 08:29  
      return(0);
     
   for(int cnt=OrdersTotal()-1;cnt>=0;cnt--)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol())
        {
         if((OrderType()==OP_SELLSTOP || OrderType()==OP_BUYSTOP)
            && TimeCurrent()>=OrderOpenTime()+m1*60) //--- 5 minutes in seconds            
           {
            //--- delete           
            if(!OrderDelete(OrderTicket(),clrYellow))
              {
               //--- error processing
               //...
              }
           }
        }
     }

The code is never executed. Remove this return statement.

Lukasz Podkanski
209
Lukasz Podkanski 2015.02.12 13:32  
angevoyageur:

The code is never executed. Remove this return statement.

thank you, you're legend
To add comments, please log in or register