Discrepancy values in EA OnTick() and CopyTicksRange()

 

Hello!

I'm creating an EA primarily using OnTick() and to confirm it's working properly I was using CopyTicksRange() to double check the results.

The issue was that they were diverging and upon further investigation I noticed that the Ticks from one are completely different from the other, even from the same period.


I took a 1 sec timeframe from a random day and printed all the Ticks from both functions.

EA:

void OnTick()
{
  MqlTick latest_price;
  SymbolInfoTick(_Symbol, latest_price)

  if (latest_price.time_msc > 1737059790000 && latest_price.time_msc < 1737059799000)
  {
      printf("EA | Ticks found! | Time: " + latest_price.time + " | time_msc: " + latest_price.time_msc + " | Bid: " + latest_price.bid + " | Ask: " + latest_price.ask);
  }
}


I tried the following code on the OnInit() from the EA but it came empty, so I used an Indicator:

int OnInit()
{

   MqlTick ticks_array[];
   int n = CopyTicksRange(_Symbol, ticks_array, COPY_TICKS_ALL, 1737059795000, 1737059796000);
   
   printf("Ticks found: " + n);
   
   for (int i = 0; i < n; i++)
   {
      printf("Indicator | Ticks found! | Time: " + ticks_array[i].time + " | time_msc: " + ticks_array[i].time_msc + " | Bid: " + ticks_array[i].bid + " | Ask: " + ticks_array[i].ask);
   }

   return(INIT_SUCCEEDED);
}


The results from the EA:

2025.01.20 22:46:17.582 2025.01.16 20:36:35   EA | Ticks found! | Time: 2025.01.16 20:36:35 | time_msc: 1737059795000 | Bid: 1.03013 | Ask: 1.03019

The results from the Indicator:

2025.01.20 22:39:27.883 Test_ind (EURUSD,M1)    Ticks found: 4
2025.01.20 22:39:27.883 Test_ind (EURUSD,M1)    Indicator | Ticks found! | Time: 2025.01.16 20:36:35 | time_msc: 1737059795034 | Bid: 1.03018 | Ask: 1.03028
2025.01.20 22:39:27.883 Test_ind (EURUSD,M1)    Indicator | Ticks found! | Time: 2025.01.16 20:36:35 | time_msc: 1737059795187 | Bid: 1.03018 | Ask: 1.03027
2025.01.20 22:39:27.883 Test_ind (EURUSD,M1)    Indicator | Ticks found! | Time: 2025.01.16 20:36:35 | time_msc: 1737059795261 | Bid: 1.03018 | Ask: 1.03028
2025.01.20 22:39:27.883 Test_ind (EURUSD,M1)    Indicator | Ticks found! | Time: 2025.01.16 20:36:35 | time_msc: 1737059795312 | Bid: 1.03018 | Ask: 1.03027


Why are they so different? In both amount and values?

I would understand if the OnTick() was an average of all Ticks inside that second, but there's no direct correlation! Bid is 5 points different and Ask 9~10 points.

Am I fetching the values wrong? Am I missing something?


So why are they so different and which one is more precise? If I have to pick one to get the best results, which one should I use?


Thank you in advance!

 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
  1. Never user any trading or symbol related functions in OnInit(). That handler is strictly for initialisation and there are no guarantee that connection as well established by then. Wait for the first new tick event before processing anything related to the data feed, contract specifications or trading. So, please correct your example code and try it again.
  2. The OnTick() event handler will not be called for every new tick event. Until it returns, no new tick events are queued for process. In other words it can easily skip ticks.
  3. Time millisecond timestamp on tick data is not unique. You can easily have multiple ticks during the same millisecond timestamp.
  4. Please inform us of which modelling method you are using—"Every tick" or "Every tick based on real ticks"?
 

Hi Fernando!


Thank you a lot, you are 100% correct!

The issue was on the EA Strategy Tester, I thought I was using "Every tick based on real ticks", I set to that but it got back to "Every tick" and I'm not sure how.

Changing back to "Every tick based on real ticks" fixed the issue, now the Ticks from the EA are exactly like the ones returned from CopyTicksRange().


Thank you!