Code execution independent of incoming ticks

While analysing the keywords from search engines I found topics for a few another entries. Today, I will describe a timer mechanism, which I also call “asynchronous mode”. How does it work? I will explain it right now.

Every EA has three functions:

  • init – called for initializing the EA
  • deinit – called when removing the EA
  • start – called for every tick

Sometimes the solution with start function is not enough, for example in EAs, which trade on the base of correlations of a few pairs or in any kind of news traders. In such cases you can’t just wait for the next coming tick, because it may come too late.

The solution is an asynchronous call (which means independent from coming ticks) of the desired code.

Implementing this mechanism is quite easy:

extern int TimerInterval=500;

int init() {
   timer();
}

void timer() {

   while(true) {
    Sleep(TimerInterval);

    if(IsStopped() || !IsExpertEnabled()) {
     return;
    }

    RefreshRates();
    start();
   }
}

int start() {
   // main code
}

Comments on the above code:

  • Just after the EA initialization the timer function runs and the endless loop executes.
  • TimerInterval sets the time in miliseconds between consecutive start function calls.
  • RefreshRates() is necessary, because in case of the above loop, the EA can’t work according to coming ticks, which means it doesn’t refresh Ask and Bid variables automatically. RefreshRates solves this problem.
  • The IsStopped() condition is here to be able to end the work of EA correctly in case of removing it from the chart (it will be removed faster than in forced mode, when the terminal forces removal after 2,5 seconds).
  • During this loop it is not possible to edit EAs parameters.
  • A partial solution to this problem is placing an IsExpertEnabled() condition – thanks to that after the global trade permission is turned off, it will exit the loop and it will be possible to change its parameters.

Share with your friends:

Send us your comments

Name (required)

Email (required)

Message

  • Facebook