Is it possible to work with live ticks in MQL5 indicators? - page 4

 
Dominik Egert #: EDIT2:

After testing the indicator on live markets (BTCUSD), I noticed some strange behaviours, and had to fix quite some issues. - Currently I dont know if this is working properly, as resutls I am getting are weird, to say the least. - But for sure, OnCalculate is called much more often than tick_volume is counted up, and CopyTicksRange is somewhat unreliable for copying current tick data. At least I thought. So I switched to CopyTicks and used that, now things get interesting.

OnCalculate seems to be called for every price change, and it seems not all ticks are reflected in CopyTicks result array. But I would guess, best is to see the results for yourself, and try to understand... - I am not even able to express exactly what is going on.

Yes, even for your first posted code, I noticed that it was being called more times than the number reported in the tick volume.

I reserved my commenting on it at the time, because at the moment I am only able to test on cryptos.

Once the markets open and we are able to further test on other more active symbols will we be able to know what is going on.

 
Dominik Egert #: So I switched to CopyTicks and used that, now things get interesting. OnCalculate seems to be called for every price change, and it seems not all ticks are reflected in CopyTicks result array. But I would guess, best is to see the results for yourself, and try to understand... - I am not even able to express exactly what is going on.

Irrespective of the strange and unexpected behaviour, the better solution is still to process the tick data directly, as I described in my previous post #10.

The code I supplied, properly handles sequencing the ticks, even when the millisecond-timestamp are the same (which happens when the ticks come in faster than with millisecond resolution).

 
Fernando Carreiro #:

Irrespective of the strange and unexpected behaviour, the better solution is still to collect and process the tick data directly, as I described in my previous post #10.

The code I supplied, properly handles sequencing the ticks, even when the millisecond-timestamp are the same (which happens when the ticks come in faster than with millisecond resolution).

This millisecond stuff is interesting, because I noticed OnCalculate being called with no updates to ticks at all...
 
Fernando Carreiro #:

Yes, even for your first posted code, I noticed that it was being called more times than the number reported in the tick volume.

I reserved my commenting on it at the time, because at the moment I am only able to test on cryptos.

Once the markets open and we are able to further test on other more active symbols will we be able to know what is going on.

Tick volume is only considering "bid ticks" (for chart based on bid price), while OnCalculate() is called for any tick (bid, ask...).

About the main point of the topic, of course OnCalculate() can miss ticks. It has always been and it will always be.

 
Alain Verleyen #: Tick volume is only considering "bid ticks" (for chart based on bid price), while OnCalculate() is called for any tick (bid, ask...). About the main point of the topic, of course OnCalculate() can miss ticks. It has always been and it will always be.

That is what I have always thought before and what tests I carried a long time ago also proved, but I am now reconsidering it under the recent tests carried out.

Even if my recent tests were on only cryptos, the code which was purposefully delayed, still had the events being queued and not skipped.

So, currently I am not so sure about it, and I will reserve my judgment until later.

 
Alain Verleyen #:
About the main point of the topic, of course OnCalculate() can miss ticks. It has always been and it will always be.

In 2021, as I programmed a very heavy indicator, I noticed the chart thread stalls, but as soon as the indicator was ready and up to date, the calculation loop was somewhat around 10ms, all ticks got processed. - Well, I am not sure if actually ALL ticks got processed, but for sure more than tick volume was. Meaning, OnCalculate was called more often than tick volume would let me suspect.

Additionally, as soon as the indicator was ready, the chart began to draw all missed candles, and I could watch the (queued up) ticks being sent to the chart one by one. - SO I was able to watch within a few seconds all the ticks and candles being drawn on chart.

Please try my testing indicator and see for yourself. - You will notice it gets much more often called than OnTick() does, and this is the case since (at least) May 2021.

 
Fernando Carreiro #:

Irrespective of the strange and unexpected behaviour, the better solution is still to process the tick data directly, as I described in my previous post #10.

The code I supplied, properly handles sequencing the ticks, even when the millisecond-timestamp are the same (which happens when the ticks come in faster than with millisecond resolution).

This is an interesting point, which i have handled in my own ticks processing solution.

 
Dominik Egert #:

In 2021, as I programmed a very heavy indicator, I noticed the chart thread stalls, but as soon as the indicator was ready and up to date, the calculation loop was somewhat around 10ms, all ticks got processed. - Well, I am not sure if actually ALL ticks got processed, but for sure more than tick volume was. Meaning, OnCalculate was called more often than tick volume would let me suspect.

Additionally, as soon as the indicator was ready, the chart began to draw all missed candles, and I could watch the (queued up) ticks being sent to the chart one by one. - SO I was able to watch within a few seconds all the ticks and candles being drawn on chart.

Please try my testing indicator and see for yourself. - You will notice it gets much more often called than OnTick() does, and this is the case since (at least) May 2021.

Yes, I am beginning to see that this may indeed be the case, and it also explains why the indicators stall/freeze so much during high impact news.

 
Dominik Egert # :

In 2021, as I programmed a very heavy indicator, I noticed the chart thread stalls, but as soon as the indicator was ready and up to date, the calculation loop was somewhat around 10ms, all ticks got processed. - Well, I am not sure if actually ALL ticks got processed, but for sure more than tick volume was. Meaning, OnCalculate was called more often than tick volume would let me suspect.

Additionally, as soon as the indicator was ready, the chart began to draw all missed candles, and I could watch the (queued up) ticks being sent to the chart one by one. - SO I was able to watch within a few seconds all the ticks and candles being drawn on chart.

Please try my testing indicator and see for yourself. - You will notice it gets much more often called than OnTick() does, and this is the case since (at least) May 2021.

You are right.

Forum on trading, automated trading systems and testing trading strategies

OnCalculate and OnTick function

Renat Fatkhullin , 2013.10.26 23:37

One of the reasons is the simultaneous arrival of a pack of quotes. This happens with some brokers and feeds.

In this case, the indicator works every tick , and the expert is called once on the last tick. If you call the expert using previous quotes in turn, then the expert will clearly try to trade at outdated prices and will receive a direct discrepancy with the market environment, even within its own terminal.

However OnCalculate() can still miss ticks.
 
Alain Verleyen #:

You are right.

However OnCalculate() can still miss ticks.
For verification of this statement, and I am not sure what is required to actually miss some calls, I integrated a backlog counter, and it seems OnCalculate catches back up again, depending on the random delay I integrated.

I will test some different delays and see how this works. But as far as I could see over a couple if hours the backlog counter was not really very high. It went up to above 140, but came back down again to below 30....

I am not sure how to interpret or make a conclusion beyond the answer to the OP question:

Yes, it is very possible and most reliant to use OnCalculate to process live ticks and presume, most ticks will generate a call to OnCalculate.

Additionally, it is (very probably) the case, you will get more calls to OnCalculate than you have tick volume. - Even if the indicator is heavy in calculations and introduces lag.

But this also means, having lots of indicators in the same chart thread, you will experience lag by times.

And another conclusion that is for sure, an indicator inside an EA might not jet have received the new tick data, while your EAs OnTick is already executing. Thus bringing you into a situation, where you will experience lag in the data of the indicator.