OnTimer Run just once a day, at 10 every morning

To add comments, please log in or register
Jose Luis Lominchar
276
Jose Luis Lominchar  

I want to develop an EA in mql4 that open just one operation at a day (at 10:15 pm).

For this kind of systems, perhaps OnTick event handler is unnecessary.

Does the next code do what I want?

//Run just once a day, at 10 every morning

int OnInit() {
        //Create timer   
        MqlDateTime clock;
        TimeGMT(clock);
        EventSetTimer((6 - clock.hour) * 3600 - clock.min * 60 - clock.sec);
   
        return(INIT_SUCCEEDED);
}

void OnTimer() {
        EventSetTimer(3600 * 24);
        MqlDateTime clock;
        TimeGMT(clock);
        Print(clock.day);
}

void OnDeinit(const int reason) {
        //Destroy timer
        EventKillTimer();   
}
whroeder1
17949
whroeder1  
JLLMNCHR: just one operation at a day (at 10:15 pm). Does the next code do what I want?

No it does not.

  1. You are trying to get the first event at the proper time and then every day after that. The first event will not be exact. The after that will slowly drift, later and later.

  2. You will also get events, when the market is closed (week ends.)

  3. Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

  4. #define HR2215 80100                                               // (22*60+15)*60
    void OnTick(){
       const SECONDS when = HR2215;
       datetime now = TimeCurrent();
       SECONDS  tod = time(now);
    
       static datetime nextEvent = 0;
       if(nextEvent == 0 && tod > when) nextEvent = tomorrow();        // Start up after when.
    
       if(tod < when                                                   // Waiting for the time.
       || nextEvent > now) return;                                     // Previously handled.
    
       nextEvent = tomorrow();                                         // No more today.
       // Do it
    }
              Find bar of the same time one day ago - Simple Trading Strategies - MQL4 and MetaTrader 4 - MQL4 programming forum
Jose Luis Lominchar
276
Jose Luis Lominchar  

Thats is awesome! I'll try as soon as posible.

Thanks!

To add comments, please log in or register