Running a code every 10 seconds

 

How to run a code every 10 seconds in MQL5

this can be done in a programming language like Javascript


setInterval(() => {
   console.log("This will tick every 10 seconds");
} , 10000);

where 10000 milli-seconds equals to 10 seconds

how to achieve this in MQL5 ?

 

That can be achieved with "OnTimer".

Try this one.

#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots   0
//--- input parameters
input int    Interval = 10;   //seconds
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   //--- indicator buffers mapping
   EventSetTimer(Interval);
   //---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   EventKillTimer();
   Comment("");
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double& price[])
{
   //---
   //--- return value of prev_calculated for next call
   return(rates_total);
}
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
{
   //---
   Comment("\r\n\r\n" + TimeToString(TimeCurrent(), TIME_MINUTES | TIME_SECONDS) + " This will tick every 10 seconds");
   //---
}
//+------------------------------------------------------------------+
 
Naguisa Unada:

That can be achieved with "OnTimer".

Try this one.

It worked with me , many thanks

 

Sleep (10);

https://docs.mql4.com/common/sleep

Sleep - Common Functions - MQL4 Reference
Sleep - Common Functions - MQL4 Reference
  • docs.mql4.com
Sleep - Common Functions - MQL4 Reference
 
Sabil Yudifera:

Sleep (10);

https://docs.mql4.com/common/sleep

I see that's a good idea.

But it's "Sleep (10000)" instead of "Sleep (10)" because it's milliseconds.

 

Hi gurus, what if i want my EA to start only at 11am for example. 

I tried doing this, but it doesnt seem to work. it is not responding. 

Appreciate if you can point out what am i missing? Thanks!


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

int OnInit()
  {
  
  
  datetime StartTime = D'11:15';
  
//--- create timer
   if(TimeLocal()==StartTime)
     {
      EventSetTimer(15);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();

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

  {
   Comment("\r\n\r\n" + TimeToString(TimeLocal(), TIME_MINUTES | TIME_SECONDS) + " This will tick every 10 seconds");



Nagisa Unada:

That can be achieved with "OnTimer".

Try this one.

 
Nagisa Unada # :

Das kann mit "OnTimer" erreicht werden.

Probieren Sie diese.

thanks 

but i dont know wehre schud i put my Code.

And when I run the program, it breaks quickly
Reason: