"Cannot set timer" ???? in ontime function - page 2

 
Fernando Carreiro #:

No, you are not supposed to run EventSetTimer() repeatedly. You only set it once, for example in the OnInit(), or on the first OnTick(), and never again for the duration of the EA, disabling it in the OnDeinit() cycle.

The only other time you would need to call it again, is if you need to change the timer frequency.

EDIT: If you are using MT4, then also, please read the following thread as it might be related:

Hello Fernando,

Great to read your comment.

Could you please explain how to change the timer frequency. I looked several threads, not finding an answer. I think my code ignores the new frequency of 60 seconds and just uses the 5 seconds the entire time. :(

Thank you in advance.


#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

int t=5;


int OnInit()
  {

   EventSetTimer(t);
   
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   
  }

void OnTick()
  {
   
  }

void OnTimer()
  {
   double   close    = iClose(Symbol(),    PERIOD_M1    ,1);
   Comment (close);
   t=60;
  }

 
@tylerdd #: Could you please explain how to change the timer frequency. I looked several threads, not finding an answer. I think my code ignores the new frequency of 60 seconds and just uses the 5 seconds the entire time. :(
By killing the timer with EventKillTimer and setting a new one with EventSetTimer or EventSetMillisecondTimer.
 
Fernando Carreiro #:
By killing the timer with EventKillTimer and setting a new one with EventSetTimer or EventSetMillisecondTimer.

Good evening Fernando,


Thank you for your answer. At the same time where should I do this in the code above.

I think I got it now, using the integer s to check if the comment shown on the chart changes every 5 seconds or every 60 seconds after the 1st run.

int t=5;
int s=1;

int OnInit()
  {
   EventSetTimer(t);
   

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   
  }

void OnTick()
  {

   
  }

void OnTimer()
  {

   double   close    = iClose(Symbol(),    PERIOD_M1   ,1);
                                                     
     Comment (close, s);
     EventKillTimer();
     t=60;
     s=s+1;
     EventSetTimer(t);

     }

 
@tylerdd #: Good evening Fernando, Thank you for your answer. At the same time where should I do this in the code above. Within "void OnTick() " or "void OnTimer""? When the code above start, the time interval for the EventSetTimer equals 5 sec.  After the first run of the code, the interval should be equal to 60 sec (running every 60 sec).

If you want to become a good programmer — Don't Ask, experiment!

8 Signs You Aren't Meant to Be a Programmer
8 Signs You Aren't Meant to Be a Programmer
  • 2022.03.14
  • www.makeuseof.com
Tech is the industry to be in right now. So many young people make careers in tech their ultimate goal. Many of them make it, only to realize that their hearts lie elsewhere. Is programming right for me? Will I like coding? Asking yourself these questions and answering them honestly may have you thinking twice. The reality: some people just...
 
Fernando Carreiro #:

If you want to become a good programmer — Don't Ask, experiment!

Perfect. I think I got it (see the changed code) :) . Have a nice evening and thank you.

Reason: