Download MetaTrader 5

EA won't place pending orders at specified time

To add comments, please log in or register
trader3000
227
trader3000  

Hi Everyone

I would like my EA to open two pending orders (straddle) at a specified time e.g. before a news release On demo, nothing happens at the specified StartTime (and no errors), but on the Strategy tester I'm getting error 130, but I can't see how stoplevel could be a problem.   Will someone please have a look and let me know what I'm doing wrong.  Or is there a simpler/ better way to do this?  Thank you

//Global variables
extern datetime StartTime = D'2016.03.08 11:37';

int start(){
   int buy_ticket=0;
   int sell_ticket=0;
   total=0;
   for(i = OrdersTotal()-1; i >= 0 ; i--)
   if(OrderSelect(i, SELECT_BY_POS) &&  OrderMagicNumber()==magicNumber &&  OrderSymbol()== Symbol()){             
      total++;
      if(OrderType()==OP_BUYSTOP) buy_ticket=OrderTicket();
      if(OrderType()==OP_SELLSTOP) sell_ticket=OrderTicket();   
    }

   if(total==0 && TimeCurrent()==StartTime)
     {
      ticket=OrderSend(Symbol(), OP_BUYSTOP, Lots, Ask, 3, 0, 0, "Original", magicNumber, 0, Blue);
      return(0);  
    
      ticket=OrderSend(Symbol(), OP_SELLSTOP, Lots, Bid, 3, 0, 0, "Original", magicNumber, 0, Red);
      return(0);  
     }
Keith Watford
Moderator
10637
Keith Watford  
if(total==0 && TimeCurrent()==StartTime)

You may not get a tick at exactly that time, if you don't, the code will not be executed

      ticket=OrderSend(Symbol(), OP_BUYSTOP, Lots, Ask, 3, 0, 0, "Original", magicNumber, 0, Blue);
      return(0);  
    
      ticket=OrderSend(Symbol(), OP_SELLSTOP, Lots, Bid, 3, 0, 0, "Original", magicNumber, 0, Red);
      return(0);  

You cannot place stop orders at the current price

Why the returns in the block of code. The 2nd order will never be placed

trader3000
227
trader3000  
GumRai:

You may not get a tick at exactly that time, if you don't, the code will not be executed

You cannot place stop orders at the current price

Why the returns in the block of code. The 2nd order will never be placed

Thank you very much for your reply.  I have now learned that the parameters for a BUYSTOP and SELLSTOP is not the same as for a BUY or SELL order, so I'm currently working on fixing that first.  And will report back

Vasyl Nosal
19054
Vasyl Nosal  
Trader3000:

Thank you very much for your reply.  I have now learned that the parameters for a BUYSTOP and SELLSTOP is not the same as for a BUY or SELL order, so I'm currently working on fixing that first.  And will report back

 

Moderator tells you about 3 problems.

1) Price

2) return(0)

3) time 

trader3000
227
trader3000  

Thank you very much everyone for your help.  I have now modified my code as follows, but only the SELLSTOP order gets send.  Why is the BUYSTOP not send? 

There are no more errors. Thanks

double Price=Pipmove*Point*10;

if(total==0 && Time[0]==StartTime){
  ticket=OrderSend(Symbol(), OP_BUYSTOP, Lots, Ask+Price, 3, OrderOpenPrice() - StopLoss*Point*10, 0, "Original", magicNumber, 0, Blue);
  ticket=OrderSend(Symbol(), OP_SELLSTOP, Lots, Bid-Price, 3, OrderOpenPrice() + StopLoss*Point*10, 0, "Original", magicNumber, 0, Red);
  return(0);  
 }
Keith Watford
Moderator
10637
Keith Watford  
double Price=Pipmove*Point*10;

if(total==0 && Time[0]==StartTime){
  ticket=OrderSend(Symbol(), OP_BUYSTOP, Lots, Ask+Price, 3, OrderOpenPrice() - StopLoss*Point*10, 0, "Original", magicNumber, 0, Blue);
  ticket=OrderSend(Symbol(), OP_SELLSTOP, Lots, Bid-Price, 3, OrderOpenPrice() + StopLoss*Point*10, 0, "Original", magicNumber, 0, Red);
  return(0);  
 }

Why are you using OrderOpenPrice? There is no order selected and if there is, it would be illogical to use it for an order that has not opened yet.

double Price=Pipmove*Point*10;

if(total==0 && Time[0]==StartTime){
  ticket=OrderSend(Symbol(), OP_BUYSTOP, Lots, Ask+Price, 3, Ask+Price - StopLoss*Point*10, 0, "Original", magicNumber, 0, Blue);
  ticket=OrderSend(Symbol(), OP_SELLSTOP, Lots, Bid-Price, 3, Bid-Price + StopLoss*Point*10, 0, "Original", magicNumber, 0, Red);
  return(0);  
 }

makes more sense.

trader3000
227
trader3000  

GumRai
:

Why are you using OrderOpenPrice? There is no order selected and if there is, it would be illogical to use it for an order that has not opened yet.

makes more sense.

Thank you.  Yes I actually realized OrderOpenPrice wasn't working after I posted above and changed it to what you suggested, but the EA only works in the Tester - not on demo.  In the tester it opens both pending orders at the exact time that I've set.  But on demo it does not send any orders and there are also no errors.  At first it did send the SELLSTOP, but now nothing happens. I think it may have something to do with total==0 or with the StartTime not coinciding with a tick.

double Price=Pipmove*Point*10;
double SL=(StopLoss-Pipmove)*Point*10;

if(total==0 && Time[0]==StartTime){
      ticket=OrderSend(Symbol(), OP_BUYSTOP, Lots, Ask+Price, 3, Ask-SL, 0, "Original", magicNumber, 0, Blue);
      Print("Error setting Buy stop: ",GetLastError());
      ticket=OrderSend(Symbol(), OP_SELLSTOP, Lots, Bid-Price, 3, Bid+SL, 0, "Original", magicNumber, 0, Red);
      Print("Error setting Sell stop: ",GetLastError());
     }  

 I also tried this, but the orders are not send

ticket=OrderSend(Symbol(), OP_BUYSTOP, Lots, Ask+Price, 3, Ask+Price - StopLoss*Point*10, 0, "Original", magicNumber, 0, Blue);
ticket=OrderSend(Symbol(), OP_SELLSTOP, Lots, Bid-Price, 3, Bid-Price + StopLoss*Point*10, 0, "Original", magicNumber, 0, Red);

 





 

 


 

trader3000
227
trader3000  
Thank you I have resolved the issue by changing total==0 to total<2
Keith Watford
Moderator
10637
Keith Watford  
Trader3000:

Thank you.  Yes I actually realized OrderOpenPrice wasn't working after I posted above and changed it to what you suggested, but the EA only works in the Tester - not on demo.  In the tester it opens both pending orders at the exact time that I've set.  But on demo it does not send any orders and there are also no errors.  At first it did send the SELLSTOP, but now nothing happens. I think it may have something to do with total==0 or with the StartTime not coinciding with a tick.

 I also tried this, but the orders are not send

I refer you to my earlier reply

GumRai:

You may not get a tick at exactly that time, if you don't, the code will not be executed







 

 


 

trader3000
227
trader3000  
GumRai:

Thank you for your help.  The exact time of the tick does not seem to be an issue, because the orders are both being send, but I have now noticed that the two pending orders get send about 30 seconds after my Time[0] variable.  I.e. 30 seconds into the formation of candle [0].  Can someone please explain why there is this ~30 second delay?  Is that the time the EA takes to do all the calculations and run through the loops?  There are plenty of ticks within the first couple seconds after the time setting.  This does not really bother me, or affects the operation of the EA, but I'm just curious why this is.  Also, I am not using seconds in my Time variable.  Only date, hour and minutes, so I presume the operation will be in affect for the whole minute, rather than the exact start time?  Thanks

int buy_ticket=0;
  int sell_ticket=0;
  int buystop_ticket=0;
  int sellstop_ticket=0;
  total=0;
  for(i = OrdersTotal()-1; i >= 0 ; i--)
  if(OrderSelect(i, SELECT_BY_POS) && OrderMagicNumber()==magicNumber && OrderSymbol()==Symbol()){             
      total++;
      if(OrderType()==OP_BUYSTOP) buystop_ticket=OrderTicket();
      if(OrderType()==OP_SELLSTOP) sellstop_ticket=OrderTicket();
      if(OrderType()==OP_BUY) buy_ticket=OrderTicket();
      if(OrderType()==OP_SELL) sell_ticket=OrderTicket();
    }
    
    if(total<2 && Time[0]==StartTime){
      ticket=OrderSend(Symbol(), OP_BUYSTOP, Lots, Ask+Price, 3, Ask-SL, 0, "Original", magicNumber, 0, Blue);
      ticket=OrderSend(Symbol(), OP_SELLSTOP, Lots, Bid-Price, 3, Bid+SL, 0, "Original", magicNumber, 0, Red);  
    }
trader3000
227
trader3000  
Trader3000:

Thank you for your help.  The exact time of the tick does not seem to be an issue, because the orders are both being send, but I have now noticed that the two pending orders get send about 30 seconds after my Time[0] variable.  I.e. 30 seconds into the formation of candle [0].  Can someone please explain why there is this ~30 second delay?  Is that the time the EA takes to do all the calculations and run through the loops?  There are plenty of ticks within the first couple seconds after the time setting.  This does not really bother me, or affects the operation of the EA, but I'm just curious why this is.  Also, I am not using seconds in my Time variable.  Only date, hour and minutes, so I presume the operation will be in affect for the whole minute, rather than the exact start time?  Thanks

 

Hi Everyone

 

I now have this part working smoothly, but would like to incorporate something in my EA.  I want the EA to determine the offset between local time (PC time) and Broker (server) time and then either add or subtract that time.  For example, my local time now is one hour behind my Broker time.  Therefore I need to first add 3600 seconds to the time so that Broker time=PC time and then subtract the Minutes_before_news variable(300 seconds) for the trade to open at the correct time.  At the moment I add this manually, but obviously this will change during the year due to DST and from broker to broker.

 I would like the EA to determine whether the time needs to be added or subtracted depending on whether the server timezone is ahead or behind my PC time.  I have come this far, but it won't compile.  Any suggestions, please?  Thank you 

extern datetime StartTime1 = D'2016.04.08 16:24';
extern int Minutes_before_news=5; //Open the two pending orders

int start(){
if(Time[0]>TimeLocal()) datetime time=Time[0]-TimeLocal(); //Will not compile (time - undeclared identifier)
if(Time[0]<TimeLocal()) datetime time=Time[0]+TimeLocal(); //Will not compile (time - undeclared identifier)
int Min=Minutes_before_news*60; //Convert to seconds
datetime time1=StartTime1+time-Min;
12
To add comments, please log in or register