Strange timer behavior

 

Hi,

I came across a strange behavior of timer which I cannot comprehend.

When I run the following simple EA:

int period = 1800;      // 0.5 hour

int OnInit()
{
        bool r = EventSetTimer(period);
        Print("Set Timer to ", period, " seconds. Result: ", r);
        return(0);
}

void OnDeinit(const int reason)
{
        EventKillTimer();
}

void OnTimer()
{
        Print("Timer");
        EventKillTimer();
        period += 1800;
        if(period <= 3*3600)    // 3 hours
        {
                bool r = EventSetTimer(period);
                Print("Set Timer to ", period, " seconds. Result: ", r);
        }
}

I receive the expected behavior, i.e. OnTimer() function is called with expected increasing periods from 0.5 h to 3 h:

2012.10.29 18:05:08     2012.10.15 10:30:00   Timer
2012.10.29 18:04:56     2012.10.15 07:30:00   Set Timer to 10800 seconds. Result: true
2012.10.29 18:04:56     2012.10.15 07:30:00   Timer
2012.10.29 18:04:47     2012.10.15 05:00:00   Set Timer to 9000 seconds. Result: true
2012.10.29 18:04:47     2012.10.15 05:00:00   Timer
2012.10.29 18:04:39     2012.10.15 03:00:00   Set Timer to 7200 seconds. Result: true
2012.10.29 18:04:39     2012.10.15 03:00:00   Timer
2012.10.29 18:04:33     2012.10.15 01:30:00   Set Timer to 5400 seconds. Result: true
2012.10.29 18:04:33     2012.10.15 01:30:00   Timer
2012.10.29 18:04:29     2012.10.15 00:30:00   Set Timer to 3600 seconds. Result: true
2012.10.29 18:04:29     2012.10.15 00:30:00   Timer
2012.10.29 18:04:27     2012.10.15 00:00:00   Set Timer to 1800 seconds. Result: true

However, when I reverse the logic to decreasing periods:

int period = 3*3600;

int OnInit()
{
        bool r = EventSetTimer(period);
        Print("Set Timer to ", period, " seconds. Result: ", r);
        return(0);
}

void OnDeinit(const int reason)
{
        EventKillTimer();
}

void OnTimer()
{
        Print("Timer");
        EventKillTimer();
        period -= 1800;
        if(period >= 1800)      // 0.5 hour
        {
                bool r = EventSetTimer(period);
                Print("Set Timer to ", period, " seconds. Result: ", r);
        }
}

I get the following output:

2012.10.29 18:11:10     2012.10.15 11:30:00   Timer
2012.10.29 18:11:08     2012.10.15 11:00:00   Timer
2012.10.29 18:11:08     2012.10.15 10:30:00   Timer
2012.10.29 18:11:04     2012.10.15 10:00:00   Set Timer to 1800 seconds. Result: true
2012.10.29 18:11:04     2012.10.15 10:00:00   Timer
2012.10.29 18:11:00     2012.10.15 09:00:00   Set Timer to 3600 seconds. Result: true
2012.10.29 18:11:00     2012.10.15 09:00:00   Timer
2012.10.29 18:10:54     2012.10.15 07:30:00   Set Timer to 5400 seconds. Result: true
2012.10.29 18:10:54     2012.10.15 07:30:00   Timer
2012.10.29 18:10:46     2012.10.15 05:30:00   Set Timer to 7200 seconds. Result: true
2012.10.29 18:10:46     2012.10.15 05:30:00   Timer
2012.10.29 18:10:37     2012.10.15 03:00:00   Set Timer to 9000 seconds. Result: true
2012.10.29 18:10:37     2012.10.15 03:00:00   Timer
2012.10.29 18:10:25     2012.10.15 00:00:00   Set Timer to 10800 seconds. Result: true

Where do these extra calls to OnTimer come from? It seems that the first calls to function EventKillTimer() are not effective.

Is it a bug or am I missing something here?

Tested on build 712.

Get in touch with developers using Service Desk!
Get in touch with developers using Service Desk!
  • www.mql5.com
We therefore attach great importance to all user reports about issues in our programs and try to answer each one of them.
 
Looks like a bug.
Get in touch with developers using Service Desk!
Get in touch with developers using Service Desk!
  • www.mql5.com
We therefore attach great importance to all user reports about issues in our programs and try to answer each one of them.
 
graziani:
Looks like a bug.

Did you read documentation?

Normally, this function must be called from the OnInit() function or from a class constructor.

Please do not change timer "on fly"
 
stringo:

Did you read documentation?

Yes i did, but to quick. :)

 i didn't say it is a bug, i said that it looks like a bug.
 IMO, there are no reasons to limit changing of timer to OnInit(), from programmers point of view.
 So from my point of view, and obviously  Pr1mer's point, this looks like a bug.
 However, in your implementation, you have done so, and documented it.

So Pr1mer, why didn't you read the documentation? :)
 

 

graziani, we have couple servicedesk requests from you look like you don't read documentation. he-he

Pr1mer exposed Tester logs (not real work logs). And Strategy Tester has some limitations. For example test events - ticks and timers - are collected for some time (1 hour) and cannot be removed from the collection.

There is very strange to use timer with such way. Just use it with minimum step and dont change this step

 
stringo:

graziani, we have couple servicedesk requests from you look like you don't read documentation. he-he 

Show me one and you have a lunch on me. (Except the one I closed, because thats obviously brokers fault).


stringo:

For example test events - ticks and timers - are collected for some time (1 hour) and cannot be removed from the collection.

I guess this is also written in documentation somewhere? :P
 
I guess this behavior will not be documented, because Strategy Tester developing is not stopped yet. And "strange timer behavior" will be fixed in the future
 

graziani - thanks for your input, stringo - thank you for explanation

I need such a functionality to simulate my past trades based on my trade history. I wanted my EA to wait in periods between trades - these are variable so changing timer would be necessary.

Reason: