datetime and iOpen

 
Hi

I am a newbee in this and have a simple question...

I'm trying to write a simple EA which should open position based on the open price of certain bar time

for example :

if open price at 4 o'clock is bigger than open price 5 o'clock, buy

and vice versa.

I have been able to make the code for the condition above using the code below. But the problem is i need to compile everyday so that the datetime variable can grab the recent date, if not, it would still take the yesterday that which would result in the open price of 4am of yesterday price. How should i change the code so that it can work by itself (without i have to compile everyday)

   datetime buka04 = D'04:00';
   datetime buka05 = D'05:00';

   int baropen04=iBarShift(NULL,PERIOD_H1,buka04);
   int baropen05=iBarShift(NULL,PERIOD_H1,buka05);
   int ticket;
   int total=OrdersTotal();
   
      if (iOpen(NULL,PERIOD_H1,baropen04) > iOpen(NULL,PERIOD_H1,baropen05))
      {
        ticket=OrderSend(Symbol(),OP_BUY,0.1,Ask,3,0,0,"My order #2",16384,0,Green);
        if(ticket<0)
        {
        Alert("OrderSend failed with error #",GetLastError());
        return(0);
        }
      }
     else if (iOpen(NULL,PERIOD_H1,baropen04) < iOpen(NULL,PERIOD_H1,baropen05))
     {
        ticket=OrderSend(Symbol(),OP_SELL,0.1,Bid,3,0,0,"My order #2",16384,0,Green);
        if(ticket<0)
        {
        Alert("OrderSend failed with error #",GetLastError());
        return(0);
        }
     }
   }
 

Use day(), month(), year() function to get current day value and then concatenate the hour(which you can input)

Then use strtotime function to convert it into corresponding time and then continue as usual.

 
datetime buka04 = D'04:00';
datetime buka05 = D'05:00';

These are 4 and 5 O'clock of the day you compiled the EA, not 4 and 5 O'clock of today.

First you need to check if the current time is before 5. If it is then what do you do? Use yesterday's 4/5? If you're on a broker using GMT, then Sunday's bars have no 4/5? Market open 21 or 2200z

Simple
Efficient
datetime buka04 = StrToTime("04:00");
datetime buka05 = StrToTime("05:00");
#define HR2400      86400           // 24 * 3600
#define HR0400      14400
#define HR0500      18000
datetime TimeOfDay(datetime when){ return(when%HR2400         ); }
datetime DateOfDay(datetime when){ return(when-TimeOfDay(when)); }
:
datetime now = TimeCurrent(),
         bod = DateOfDay(now),
         buka04 = bod + HR0400,
         buka05 = bod + HR0500;
if (now < buk05) ...
Reason: