running code once a day and broker time

 

Hi,

I want to ask about how run a code once a day and here's the code that I used. Is this a correct way to do it?

void OnTimer()
  {
   int hour = 00;		
   int minute = 05;
   MqlDateTime ActivationTime;
   TimeToStruct(TimeCurrent(), ActivationTime);

   if(ActivationTime.hour == hour && ActivationTime.min == minute)
     {
      Print("This code should be printed once per day");
     }
  }

And the second question is about broker time, here's a screenshot that I got from one of the quotes from my broker,


There's 2 column one is quotes column and the other is trade column. If I'm not wrong, the quotes column is the time where the quotes is available and trade column is where I can open a position when the time is allowed. So does EA still running OnTimer() event handling at 00:00 (quotes time allowed but trade time not allowed)?

 
Luandre Ezra:

Hi,

I want to ask about how run a code once a day and here's the code that I used. Is this a correct way to do it?

And the second question is about broker time, here's a screenshot that I got from one of the quotes from my broker,


There's 2 column one is quotes column and the other is trade column. If I'm not wrong, the quotes column is the time where the quotes is available and trade column is where I can open a position when the time is allowed. So does EA still running OnTimer() event handling at 00:00 (quotes time allowed but trade time not allowed)?

  1. Wrong because you will print for one minute the same message again and again,
    and if the is no tick during this minute nothing appears.
    use:
    static datetime tLimit = D'00:05:00'; 
    ...
    if (time > tLimit) { print(); tLimit += 86400; }
    (untested)
  2. OnTimer() acts beyond sent ticks,
  3. If you send an order too early (quotes=yes,trade=no) you probably(!) get this error 10018, TRADE_RETCODE_MARKET_CLOSED, Market is closed
 
  1. Luandre Ezra: I want to ask about how run a code once a day and here's the code that I used. Is this a correct way to do it?
       if(ActivationTime.hour == hour && ActivationTime.min == minute)

    What if there is no tick that specific minute? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday.

  2. Carl Schreiber #:
    1. … use:
    2. static datetime tLimit = D'00:05:00'; 
                  ⋮
                  if (time > tLimit) { print(); tLimit += 86400; }

    That is five (5) AM when it was compiled. Not today. It will run n ticks, where n is the duration between today and compile date.

    And over the weekend, it will run on the first tick, not five AM.

  3. #define  MAX_DATETIME   D'3000.12.31 23:59:59'
    #define HR0500 (5*3600)
    static datetime oncePerDay;
    static datetime dayCur=0; datetime dayPre=dayCur; dayCur=date(); bool isNewDay = dayPre != dayCur;
    if( isNewDay ) oncePerDay = dayCur + HR0500;
    
    if( TimeCurrent() >= oncePerDay){ oncePerDay=MAX_DATETIME; … }
              Find bar of the same time one day ago - MQL4 programming forum #1 & #6 (2017)

 

Carl Schreiber: If you send an order too early (quotes=yes,trade=no) you probably(!) get this error 10018, TRADE_RETCODE_MARKET_CLOSED, Market is closed
I used OnTimer() event handling to calculate std dev and correlation of quotes, it calculates daily close of last day and n period of days prior, it has nothing to do with opening a trade. So the problem about market closed would never be happen. 

William Roeder: What if there is no tick that specific minute? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday.

what I'm understand about OnTimer() is that it works based on time and not tick so, I don't think there's a problem with no tick in that specific minutes right?


I find this code on how to run code once a day and it works when I test it.

string lastRunDate = NULL;
int hour = 00;
int minute = 05;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
//Hour = timeTemp.hour;
//Minute = timeTemp.min;

   EventSetTimer(60);
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTimer()
  {

   MqlDateTime ActivationTime;
   TimeToStruct(TimeLocal(), ActivationTime);

   if(lastRunDate!=TimeToString(TimeCurrent(), TIME_DATE) && ActivationTime.hour == hour && ActivationTime.min == minute)
     {
      Print("This code should be printed once per day");
      //after your codes, `remember today` !
      lastRunDate = TimeToString(TimeCurrent(), TIME_DATE);
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   EventKillTimer();
  }
//+------------------------------------------------------------------+


Finally I choose not to use OnTimer() because I need to call the code on .mqh files. I create a new function called NewDay() and run it on code that I want to run once a day. The only problem is I don't know how it will works on production environment, but so far when I tested it works like I intended.

Carl Schreiber
Carl Schreiber
  • 2023.04.17
  • www.mql5.com
Trader's profile
 
Luandre Ezra #: what I'm understand about OnTimer() is that it works based on time and not tick so, I don't think there's a problem with no tick in that specific minutes right?

There has been no tick, so nothing has changed, including server time. If the previous tick wasn't valid, it is still not valid.

Reason: