TimeCurrent()== HOURS:MINUTES IN MT5 SCRIPT

 

Hi

I would like to make EA trade from 00:01 till 23:00. Also if there's any open position, to be closed on 23:15.

How can I write the code, so the hours and minutes (above specified) be written by the code?

if( TimeCurrent()==
 
Martin:

Hi

I would like to make EA trade from 00:01 till 23:00. Also if there's any open position, to be closed on 23:15.

How can I write the code, so the hours and minutes (above specified) be written by the code?


TimeToString(TimeGMT(),TIME_MINUTES));
replace TimeGMT() with any Time function you wish e.g. TimeTradeServer or TimeCurrent
 
R4tna C #:

This does not look right:

TimeCurrent() is of type datetime and you are comparing with a string.

Also TimeGMT() is based on terminal time with daylight saving - TimeCurrent() is server time - it is possible they will never match


static datetime a = TimeGMT()+PeriodSeconds(PERIOD_M1);

if(TimeGMT()==a){
Print("Time matched");
a=0; // reset
}
today market is close so see this example with TimeGMT
 
***
 
Arpit T #:


today market is close so see this example with TimeGMT
static datetime a = TimeGMT()+PeriodSeconds(PERIOD_M1);

if(TimeGMT()==a){
Print("Time matched");
a=0; // reset
}

You are setting it to 60 secs in the future - will it ever match? You can test/debug it with a script even when the market is closed

 
Martin:

How can I write the code, so the hours and minutes (above specified) be written by the code?

if ((TimeLocal() >= D'00:01') && ((TimeLocal() <= D'23:00'))) // Between 00:01 and 23:00.
 
  1. fxsaber #:
    if ((TimeLocal() >= D'00:01') && ((TimeLocal() <= D'23:00'))) // Between 00:01 and 23:00.

    Wrong. You are comparing to a fixed date, not today date.

  2. TimeLocal and TimeGMT are probably wrong. All chart times are server times.

  3. István Reich #: if (time.hour == 23 && time. min == 15) {

    This assumes every bar every exists — they don't. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart.

  4. Martin: I would like to make EA trade from 00:01 till 23:00. Also if there's any open position, to be closed on 23:15.
    #define HR0001 60
    #define HR2300 82800 // (23*3600)
    #define HR2315 83700 // (23*3600+15*60)
    
    int now = time();
    if(now >= HR2315)                 close_all();
    if(now >= HR0001 && now < HR2300) trade_now();
              date/time (2017)
              Find bar of the same time one day ago - MQL4 programming forum (2017)

    When dealing with time, a lot of people use strings; they can not be optimized. Using (int) seconds or (double) hours and minutes can be.