How OnTimer work with Oninit

 

Hello, How OnTimer work with Oninit. Every 60 seconds all four Print will be executed?


bool assignVal=True;

int OnInit()
  {

Print("Checking Print 1 with Timer");

if(MarketInfo(Symbol(), MODE_STOPLEVEL) > 0)
     {
      
        Print("Checking Print 2 with Timer");
      //--- set timer to update old xml file every x hours

      EventSetTimer(60);
     }
assignVal=true;
Print("Checking Print 3 with Timer");

}


void OnTimer()
  {
//---
   assignVal=true;
   Print("Checking Print 4 with Timer");
//---
  }
 
anuj71:
Every 60 seconds all four Print will be executed?

Have you tried running your code to find out?

 
Vladislav Boyko #:

Have you tried running your code to find out?

Yes, it does not called Oninit function again after 60 seconds. This is example code, In my original code, it calling the function every 60 seconds. here it is.


bool assignVal=true;

int OnInit()
  {
//+------------------------------------------------------------------+
//| News Filter Start                                                |
//+------------------------------------------------------------------+
//--- get today time
   TimeOfDay=(int)TimeLocal()%86400;
   Midnight=TimeLocal()-TimeOfDay;
//--- set xml file name ffcal_week_this (fixed name)
   xmlFileName=INAME+"-ffcal_week_this.xml";
//--- checks the existence of the file.
   if(!FileIsExist(xmlFileName))
     {
      xmlDownload();
      xmlRead();
      Print("XML File downloaded");
     }
//--- else just read it
   else
     {
      xmlRead();
      Print("Ordered to Reading XML News");
     }
//--- get last modification time
   xmlModifed=(datetime)FileGetInteger(xmlFileName,FILE_MODIFY_DATE,false);
//--- check for updates
   if(FileIsExist(xmlFileName))
     {
      if(xmlModifed<TimeLocal()-(News_ReCheckMinutes * 60))
        {
         Print(INAME+": xml file is out of date - Updating");
         xmlUpdate();
         ObjectsDeleteAll(0,"#",-1,-1);
        }
      //--- set timer to update old xml file every x hours

      EventSetTimer(1 * 60);
     }

   assignVal=true;

//+------------------------------------------------------------------+
//| News Filter End                                                  |
//+------------------------------------------------------------------+

//Money Management
   if(Money_Management)
     {
      if(MoneyManagement_Type == Balance_Percentage)
         gTargeted_Revenue = (1 + Target_Revenue/100) * AccountBalance();
      else
         gTargeted_Revenue = Target_Revenue + AccountBalance();
         Print("Money Management => gTarget_Revenue Updated : " + gTargeted_Revenue);
     }  

return(INIT_SUCCEEDED);
  }

void OnTimer()
  {
//---
   assignVal=true;
   Print(INAME+": xml file is out of date");
   xmlUpdate();
//---
  }


My question is OnInit called ones but with this timer how it calling every 60 seconds and if it calling every 60 seconds how it calling only news filter function not the below new filter function?. i am trying to understand how Ontimer works.

 
In the code you provided, if MarketInfo(Symbol(), MODE_STOPLEVEL) > 0, then the OnTimer event function will be called every 60 seconds, which means that only the 4th Print call will be triggered every 60 seconds and only if the condition you provided is true. If OnTimer is not triggering every X seconds, debug your code and check if the condition evaluates to true or false
 
anuj71:

Hello, How OnTimer work with Oninit. Every 60 seconds all four Print will be executed?

bool assignVal=True;

int OnInit()
  {

Print("Checking Print 1 with Timer");

if(MarketInfo(Symbol(), MODE_STOPLEVEL) > 0)
     {
      
        Print("Checking Print 2 with Timer");
      //--- set timer to update old xml file every x hours

      EventSetTimer(60);
     }
assignVal=true;
Print("Checking Print 3 with Timer");

}


void OnTimer()
  {
//---
   assignVal=true;
   Print("Checking Print 4 with Timer");
//---
  }


Hello
Maybe this will help


bool assignVal=True;

int OnInit()
  {

Print("Checking Print 1 with Timer");

// if(MarketInfo(Symbol(), MODE_STOPLEVEL) > 0)
//     {
//      
//        Print("Checking Print 2 with Timer");
//      //--- set timer to update old xml file every x hours
//
//      EventSetTimer(60);
//     }

EventSetTimer(60);

assignVal=true;
Print("Checking Print 3 with Timer");

}


void OnTimer()
  {
//---
   assignVal=true;
   Print("Checking Print 4 with Timer");
//---


if(MarketInfo(Symbol(), MODE_STOPLEVEL) > 0)
     {
      
        Print("Checking Print 2 with Timer");
      //--- set timer to update old xml file every x hours

//    EventSetTimer(60);
     }

  }

Prints 1 and 3 are done once

Print 4 runs every 60 seconds

Green condition is inspected every 60 seconds

Whether Print 2 will be printed or not depends on the specific symbol and its settings on the trading server

 
anuj71 #:

Yes, it does not called Oninit function again after 60 seconds. This is example code, In my original code, it calling the function every 60 seconds. here it is.



My question is OnInit called ones but with this timer how it calling every 60 seconds and if it calling every 60 seconds how it calling only news filter function not the below new filter function?. i am trying to understand how Ontimer works.

It calls the OnTimer function not Oninit
 
anuj71:

Hello, How OnTimer work with Oninit. Every 60 seconds all four Print will be executed?



You are not checking if EventSetTimer was successful.

Search the forum for EventSetTimer in MQL4. It isn't always successful.
 
anuj71 #:

Yes, it does not called Oninit function again after 60 seconds. This is example code, In my original code, it calling the function every 60 seconds. here it is.

My question is OnInit called ones but with this timer how it calling every 60 seconds and if it calling every 60 seconds how it calling only news filter function not the below new filter function?. i am trying to understand how Ontimer works.

It doesn't work the way you think. Roughly speaking, the terminal will call the OnTimer() function with the frequency that you specified in EventSetTimer(). The Timer event does not cause OnInit() to be called. OnInit() is called when the Init event occurs.

https://www.mql5.com/en/docs/runtime/event_fire#timer

The Timer event is periodically generated by the client terminal for the Expert Advisor that has activated the timer by the EventSetTimer function. Usually, this function is called by OnInit. Timer event processing is performed by the OnTimer function. After the operation of the Expert Advisor is completed, it is necessary to destroy the timer using the EventKillTimer function, which is usually called in the OnDeinit function.

https://www.mql5.com/en/docs/event_handlers/ontimer

OnTimer

The function is called in EAs during the Timer event generated by the terminal at fixed time intervals.

The Timer event is periodically generated by the client terminal for an EA, which activated the timer using the EventSetTimer() function. Usually, this function is called in the OnInit() function. When the EA stops working, the timer should be eliminated using EventKillTimer(), which is usually called in the OnDeinit() function.

The information in the links above (regarding the generation of the Timer event and its handling in OnTimer) is also true for MQL4

Reason: