Running code once per month

 

Hi,

I want to run a code once a month on OnTimer() function and here's how I do it.

void CStrategy::OnTimerEvent(string& SymbolArray[], int numberOfTradeableSymbols, string FileName, TRADE_DIRECTION TradeDirection) {

   MqlDateTime dt;
   datetime now=TimeLocal(dt);

   //if not saturday and at specified time
   if(dt.day_of_week == 0                                                                                            // run on sunday
         && (dt.day == 1 || dt.day == 2 || dt.day == 3 || dt.day == 4 || dt.day == 5 || dt.day == 6 || dt.day == 7)  // first sunday of the month
         && dt.hour == InpReOptimizingStartHour && dt.min == InpReOptimizingStartMinute) {                           // at specified hour
      for(int SymbolLoop=0; SymbolLoop < numberOfTradeableSymbols; SymbolLoop++) {
         string CurrentSymbol = SymbolArray[SymbolLoop];

         CStrategy::InitLoadFile(SymbolArray, SymbolLoop, numberOfTradeableSymbols, FileName, TradeDirection);
      }
   } else {
      return;

   }
}

Does this correct way to do that?

 

I am not aware of the event handler function you are using. 

But there is something strange to me. Could the function be called several times(or not at all) during the hh:mm of first Sunday of month?

 

should be one time on first sunday of the month.

(dt.day == 1 || dt.day == 2 || dt.day == 3 || dt.day == 4 || dt.day == 5 || dt.day == 6 || dt.day == 7)  // first sunday of the month

the code above used since we don't know the date of the first sunday but we know that it would fall between date 1st to 7th.

// input variable
input uchar InpReOptimizingStartHour = 05;
input uchar InpReOptimizingStartMinute = 00;

dt.hour == InpReOptimizingStartHour && dt.min == InpReOptimizingStartMinute) {                           // at specified hour

and on the hour and minute it will only runs once depending on the hour and minute that specified

 
already tested and it works like I want, even though at the end I change it to more simpler one.
Reason: