Help with opening times - My first Expert Advisor

 

Hi I am an experienced developer but this is my first attempt at an MQL4 expert advisor.

I am looking open two pending orders on the Germany 30 indicies as soon as it opens (7:00 BST). As my broker is alpari and from what I have read their servers work on GMT+3 this means I should try and get the orders to open at 9:00am

If I take this code out the ea tries to place an order whenever start is called (the send order is unsuccessful returning a Error code 130, but that's a different issue, one thing at a time Rich :-) )

Any help telling me what's wrong with the code would be very much apprcieated.

#define  EETHR859 32340           // seconds for 859am

#define  EETHR900 32400            // seconds for 900am

#define  EETHR905 32700          // seconds for 905am

//---------------------------------------------------------------------------------

     

//Check in the window to open order on Germany 30

bool isTimeToOpen;

if ((tod > EETHR900) && (tod < EETHR905))

   {

      isTimeToOpen=true;

   }



if ((isTimeToOpen) && (spread < 4))

    {    

      Opn_B=true;

      Opn_S=true; 

      Print("Orders can open ");  

   }

   else

   {

      Opn_B=false;

      Opn_S=false;

   } 
 
rwatts50:

Hi I am an experienced developer but this is my first attempt at an MQL4 expert advisor.

I am looking open two pending orders on the Germany 30 indicies as soon as it opens (7:00 BST). As my broker is alpari and from what I have read their servers work on GMT+3 this means I should try and get the orders to open at 9:00am

If I take this code out the ea tries to place an order whenever start is called (the send order is unsuccessful returning a Error code 130, but that's a different issue, one thing at a time Rich :-) )

Any help telling me what's wrong with the code would be very much apprcieated.

Hard to say, not enough information . . . what is tod ? timeofday I assume, why not use that as a variable name ? but how is it calculated/stated ? for it to work it would have to be something like TimeCurrent() - Midnight where midnight is something like this: Midnight
 
RaptorUK:
Hard to say, not enough information . . . what is tod ? timeofday I assume, why not use that as a variable name ? but how is it calculated/stated ? for it to work it would have to be something like TimeCurrent() - Midnight where midnight is something like this: Midnight

Many thanks for your help RaptorUK, I have been having difficulty getting my head around MQL4, but Im sure the Eureka moment will happen soon ;)


tod was originally calculated as:

datetime now = Time[0]

int      tod = now % 86400; 

Given I had read elsewhere that the time was given in seconds I declared this as an int, although from your post it seems that was wrong :-(. I also take your point on the variable name

I have now changed this to the following.

datetime Midnight = TimeCurrent() - (TimeCurrent()%(PERIOD_M1 * 60));
datetime TimeOfDay = TimeCurrent() - Midnight;

The code now runs as originally posted with the 'tod' variable changed to 'TimeOfDay'

It looks like I will have to wait until Monday to test as I'm not sure the strategy tester is working accurately. The journal is showing print statements at very strange times, usually between 22:00 and 23:00 for the dates I have chosen to test. I'm not yet sure if it's a code problem, a strategy tester problem, or a Historical data problem... Arrrrrrrrrrrr lol Would you trust the strategy tester, if so does it still sound like there's a problem with the code?

 

Should

datetime Midnight = TimeCurrent() - (TimeCurrent()%(PERIOD_M1 * 60));

be

datetime Midnight = TimeCurrent() - (TimeCurrent()%(PERIOD_D1 * 60));

??

 
GumRai:

Should

be

??

Yes it should . . .
 
Thanks guys, will let you know how it goes on Monday :)
 
Worked great. Thanks RaptorUK and GunRai. Much appreciated.