Download MetaTrader 5
To add comments, please log in or register
Do you need private account monitoring? The Signals will provide you one!
Lukasz Podkanski
198
Lukasz Podkanski 2015.02.09 22:30 

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++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol()==Symbol())  
        {
         if(OrderType()==OP_SELLSTOP && Hour()==h1 && Minute()==m1)
           {
            OrderDelete(OrderTicket(),Yellow);
            return(0);
           }
         if(OrderType()==OP_BUYSTOP && Hour()==h1 && Minute()==m1)
           {
            OrderDelete(OrderTicket(),Yellow);
            return(0);
           }
       }
     }
   }
 }
Carl Schreiber
5937
Carl Schreiber 2015.02.10 07:00  

well just save

1) the ticket_number = oderSend() and

2) lot_size and

3) the tmeDel = sending time + 300 sec of the pending order and

4) if (TimeCurrent()>tmeDel) OrderDelete(ticket_number,...)

gadget
100
gadget 2015.02.11 10:22  

Donson,

the following should work better than your code.

for(cnt=0;cnt<total;cnt++) {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol()==Symbol()) {
         if(OrderType() ==OP_SELLSTOP || OrderType()==OP_BUYSTOP) {
            if (OrderCloseTime() < TimeCurrent() - 5 * 60) {
                                OrderDelete(OrderTicket(),Yellow);
                        }                       
         }
       }
}

Rgds.

gadget
100
gadget 2015.02.11 10:22  
Sorry, OrderOpenTime() instead of OrderCloseTime().
whroeder1
13600
whroeder1 2015.02.11 14:04  
donson: Here's the code, can u help me? where am i wrong?
   for(cnt=0;cnt<total;cnt++)
  1. You must count down when closing/deleting in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
  2.          if(OrderType()==OP_SELLSTOP && Hour()==h1 && Minute()==m1)
    
    What if there is no tick in that minute?
  3.          if(OrderType()==OP_BUYSTOP && Hour()==h1 && Minute()==m1)
    
    What if there is not a second tick in that minute?
  4. Just remember when you opened the orders. When 5 minutes have passed, then close everything.
    static datetime pendingsCreated;
    :
    if(ticket>0){ pendingsCreated = TimeCurrent();
    :
    if(TimeCurrent() > pendingsCreated + 5*60){ // Begin closing

hji
26
hji 2015.02.13 07:11  
gadget:

Donson,

the following should work better than your code.

Rgds.


I don't see how this is better than his code. Could you explain how?
/
To add comments, please log in or register