OP_BUY/SELL setting the exact time?

 

Hi all,

It's my first post here.

I'm starting to learn about EA recebtly, and I've a doubt about time functions. I've been looking for a code that allow to buy/sell at a specific time, but I only found examples that work with a time value of HH:MM. What about seconds? Is it possible to define a buy/sell order, at an specific HH:MM:SS?

I made some trials, but what I only obtain are buy/sell operations executed at the begining of market minute. For example, how can i set a buy order at 14:55:55?

I tried samples with this kind of terms, but with no results (based on the Timentry.mq4 file):

extern int    Hour_For_Long_Entry    = 14;    
extern int    Minute_For_Long_Entry  = 55;
extern int    Seconds_For_Long_Entry = 55;

[...]

 if(Long_Entry==true && Hour()==Hour_For_Long_Entry && Minute()==Minute_For_Short_Entry && Seconds()==Seconds_For_Short_Entry && IsTradeAllowed()==true)

=>OP_BUY/SELL....


Sorry for such elementary question (novice).


Thanks in advance.

Dek.
 

dekam: For example, how can i set a buy order at 14:55:55?

extern int    Hour_For_Long_Entry    = 14;    
extern int    Minute_For_Long_Entry  = 55;
extern int    Seconds_For_Long_Entry = 55;
:
 if(Long_Entry==true && Hour()==Hour_For_Long_Entry && Minute()==Minute_For_Short_Entry 
 && Seconds()==Seconds_For_Short_Entry && IsTradeAllowed()==true) 
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. if(Long_Entry==true && ... && IsTradeAllowed()==true)
    You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.

  3. If there was no tick at your exact time, then your if won't open. If there is multiple ticks in the same second, then your if might try to open multiple orders (depends on how long it takes to open an order.)
    extern int    Hour_For_Long_Entry    = 14;                                       
    extern int    Minute_For_Long_Entry  = 55;                                       
    extern int    Seconds_For_Long_Entry = 55;                                       
    
    #define HR2400 (PERIOD_D1 * 60)  // 86400 = 24 * 3600
    int      TimeOfDay(datetime when=0){      if(when == 0)  when = TimeCurrent();
                                              return( when % HR2400 );            }
    //datetime DateOfDay(datetime when=0){      if(when == 0)  when = TimeCurrent();
    //                                          return( when - TimeOfDay(when) );   }
    //datetime Tomorrow( datetime when=0){      if(when == 0)  when = TimeCurrent();
    //                                          return(DateOfDay(when) + HR2400);   }
    //datetime Yesterday(datetime when=0){      if(when == 0)  when = TimeCurrent();
    //   int iD1 = iBarShift(NULL, PERIOD_D1, DateOfDay(when) - 1);
    //                                       return( iTime(NULL, PERIOD_D1, iD1) ); }
    :
     if(isLongEnabled){
       int entryTime = Hour_For_Long_Entry * 3600 + Minute_For_Long_Entry * 60 + Seconds_For_Long_Entry;
       if(TimeOfDay() >= entryTime && IsTradeAllowed()){
          Long_entry=False; // One open only
          :
    }  }

  4. You could also do
    extern int    entryTime = 53755; // 14:55:55
    
     if(isLongEnabled){
       if(TimeOfDay() >= entryTime && IsTradeAllowed()){ ...
    
    or
    extern string entryTime = "14:55:55"
    
     if(isLongEnabled){
       if(TimeOfDay() >= StringToTime(entryTime) && IsTradeAllowed()){ ...
 
WHRoeder:

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.

  3. If there was no tick at your exact time, then your if won't open. If there is multiple ticks in the same second, then your if might try to open multiple orders (depends on how long it takes to open an order.)

  4. You could also do or

Thanks a lot WHRoeder. I'll try some of those senteces. As I said, I'm a total novice on this; It's years since I've learned and practice  C, and C++, so the structure it's familiar to me, but not the exact functions. MQL4-5 seems to be very interesting to code.

Really, your help is much apreciated.

Regards from Spain. :)

Dek.


Reason: