Multicurrency EA. What to use: OnTimer, OnChartEvent, OnTick, OnFakeindicator? - page 3

 
Csquared #:
yeah and its definitely causing me issues lol. If I solve it in the next day or so I'll post it here
And in case you don't already know... TimeGMTOffset() is the missing piece of the puzzle.
 
Ryan L Johnson #:
And in case you don't already know... TimeGMTOffset() is the missing piece of the puzzle.

The problem with iSpy, even when live, is that it has a context window of certain size...basically it can store up to certain number of events, i think is like about 1,000 which should be good for most cases... but when you get tens of ticks per second in many pairs, it overflows the window and all those commands will not be processed... that is the explanation I encountered some time back for a disparity of results I was getting with the same EA. Additionally, Ispy is heavy on the use of resources, I use it for just getting bid and ask when working with more than 15 pairs... with that many, the use of indicators,  ruins the functionality/performance of the ea. My iSpy was simplified to send the event chart only once a minute.

#property indicator_chart_window
#property indicator_plots 0

input long            chart_id=0;        // chart id
input ushort          custom_event_id=0; // event id

//+------------------------------------------------------------------+
int OnCalculate (const int rates_total,        // size of price[] array
                 const int prev_calculated,  // bars, calculated at the previous call
                 const int begin,            // starting index of data
                 const double& price[]       // array for the calculation
   )
  {
   static datetime StartTime;
   double IsNewBar=0;

      datetime ThisBarTime = iTime(_Symbol, PERIOD_M1,0);
     
      if(StartTime != ThisBarTime)
      {
      StartTime = ThisBarTime;
      IsNewBar=1;
      }  else
      {
      IsNewBar=0;
      }
        
   // When the new tick, let's generate the "New tick" custom event  
      if(IsNewBar==1) { EventChartCustom(chart_id,custom_event_id,0,IsNewBar,_Symbol); }

   //--- return value of prev_calculated for next call
   return(rates_total);
  }
 
Camilo Mora:

A solution was OnTimer, another deceiving tool when using strategy tester, it turns out that on strategy tester on Timer is set by the tick data you set in the main chart. Results on OnTimer were very unreliable when running the EA optimization on agents from a farm. I was not able to figure out why agents set different OnTimers, but the same multicurrency ea produces exactly the same result in a local machine but varying results when run in agents.

Could you please investigate this matter further and clarify. Alain already asked you about this, but you did not respond.

The problem with syncronization is (as always) a trade off between stability (of event sequence) and accuracy (how fast events are obtained and processed). The solution with timer should work for the first option. I'm afraid that a native and reliable solution for the 2-nd option is not provided in MT5 at the moment.

If you're trading by symbols with market books, the easiest method to receive multi-symbols ticks is to subscibe to OnBookEvent for all required symbols, though it can also demonstrate a sort of degradation as number of symbols increases, say, up to 28.

As for off-topic subject related to news trading in sync, everyone interested in it can find some insights in the blogpost "Backtesting news trading based on economic calendar history with adjusted timings".

Documentation on MQL5: OnBookEvent / Event Handling
Documentation on MQL5: OnBookEvent / Event Handling
  • www.mql5.com
The function is called in indicators and EAs when the BookEvent event occurs. It is meant for handling Depth of Market changes. Parameters symbol...
 
Camilo Mora #:

The problem with iSpy, even when live, is that it has a context window of certain size...basically it can store up to certain number of events, i think is like about 1,000 which should be good for most cases... but when you get tens of ticks per second in many pairs, it overflows the window and all those commands will not be processed... that is the explanation I encountered some time back for a disparity of results I was getting with the same EA. Additionally, Ispy is heavy on the use of resources, I use it for just getting bid and ask when working with more than 15 pairs... with that many, the use of indicators,  ruins the functionality/performance of the ea. My iSpy was simplified to send the event chart only once a minute.

Is there a way to actually measure how resource intensive iSpy or the iSpyM1 code is? I would think that its pretty resource efficient for the M1 version because even basic custom indicators definitely have more calculations inside them that will get called every tick inside OnCalculate and probably more reliable for actual trading EAs than a iSpy that uses every tick (because of missed ticks, latency, etc). Perhaps your computer/VPS isn't good enough or you were doing this on older builds that weren't more efficient (I see you made this same thread a couple years ago)? And I thought Mt5 was supposed to be very good at handling lots of calculations, maybe it doesn't cleanup custom chart events very well or something and this was later fixed because I haven't had any issues?
Camilo Mora - 2022cmora - Trader's profile
Camilo Mora - 2022cmora - Trader's profile
  • 2026.01.03
  • www.mql5.com
Trader's profile
 
Stanislav Korotky #:

Could you please investigate this matter further and clarify. Alain already asked you about this, but you did not respond.

The problem with syncronization is (as always) a trade off between stability (of event sequence) and accuracy (how fast events are obtained and processed). The solution with timer should work for the first option. I'm afraid that a native and reliable solution for the 2-nd option is not provided in MT5 at the moment.

If you're trading by symbols with market books, the easiest method to receive multi-symbols ticks is to subscibe to OnBookEvent for all required symbols, though it can also demonstrate a sort of degradation as number of symbols increases, say, up to 28.

As for off-topic subject related to news trading in sync, everyone interested in it can find some insights in the blogpost "Backtesting news trading based on economic calendar history with adjusted timings".

Hi... in my case I use 28 symbols..... however, i did an incremental test from 2 to 28 pairs, and even when using 2 pairs the results in the farm were not consistent. the only thing that generated consistent results with small number of pairs was the ispy. but when reaching some 15 pairs results started to get dissimilar, which let me to think that it is at about that point when the events start to be bypasses. so far, the only thing that appears reliable for optimization in farm agents with multicurrency eas with large number of pairs is a using a pair as a heartbeat.
 
Camilo Mora #:
Hi... in my case I use 28 symbols..... however, i did an incremental test from 2 to 28 pairs, and even when using 2 pairs the results in the farm were not consistent. the only thing that generated consistent results with small number of pairs was the ispy. but when reaching some 15 pairs results started to get dissimilar, which let me to think that it is at about that point when the events start to be bypasses. so far, the only thing that appears reliable for optimization in farm agents with multicurrency eas with large number of pairs is a using a pair as a heartbeat.

My question was specifically about your problems with timer, because the timer is the best heartbeat possible.

Probably you can prepare a test case, because many nuances in the source code may have side effects.

 
Stanislav Korotky #:

My question was specifically about your problems with timer, because the timer is the best heartbeat possible.

Probably you can prepare a test case, because many nuances in the source code may have side effects.

No, The OnTimer is actually a bad guide for strategy tester.

First, you need to know that ontimer works differently on live trading vs on strategy tester. On live, the OnTimer is based on real time, (your computers clock). In strategy tester, the timer is simulated. It is tied to the arrival of ticks on the main chart symbol you are testing. The "seconds" that pass in the timer are calculated based on the timestamps of the historical data. 

In the Strategy Tester, if there are no ticks for 10 minutes, the timer effectively pauses for those 10 minutes because no new "simulated time" is being fed into the system via ticks. This can cause logic that depends on time-elapsed (like trailing stops or news-avoidance) to fail completely.

... this is super concerning as some pairs have data gaps  (real and at times bad data), that create problems for the onTimer. In strategy tester, when using agents of a farm, this error scales up, as each agent can set the OnTimer differently depending on the settings. 


This is particularly concerning for multicurrency ea due to Synchronization: In a multi-currency EA, the OnTimer triggers based on the primary symbol's ticks. If the primary symbol has a gap but a secondary symbol (that you are also trading) has a tick, the OnTimer might still not trigger, causing you to miss trades on the secondary pair.


The solution I am reviewing suggest using a simulated pair, that has ticks every minute, which serves as a heartbeat to test strategies in actual secondary pairs.

 
Camilo Mora # :

But, The OnTimer is actually a bad guide for strategy testers.

First, you need to know that ontimer works differently on live trading vs on strategy tester. On live, the OnTimer is based on real time, (your computers clock). In strategy tester, the timer is simulated . It is tied to the arrival of ticks on the main chart symbol you are testing. The "seconds" that pass in the timer are calculated based on the timestamps of the historical data.

In the Strategy Tester , if there are no ticks for 10 minutes, the timer effectively pauses for those 10 minutes because no new "simulated time" is being fed into the system via ticks. This can cause logic that depends on time-elapsed (like trailing stops or news-avoidance) to fail completely.

... this is super concerning as some pairs have data gaps (real and at times bad data), that create problems for the onTimer. In strategy tester, when using agents of a farm, this error scales up, as each agent can set the OnTimer differently depending on the settings.


Your information contradicts to what MQ developers said many years ago (unfortunately I didn't find an English automatically translated "mirror" for this topic).

Forum for trading, automatic trading systems and testing trading strategies

Testing in the OnTimer() mode

Slava , 2010.11.08 15:53

Instead of speculating, it would be better to just check it out for yourself.

Timer events are generated regardless of the presence or absence of ticks . Try testing on Saturday and Sunday – there are no ticks, but the timer is running.

Also, read the documentation. The "on bar open" mode generates M1 OHLC ticks. However, the OnTick expert advisor only launches at the start of a bar on the tested timeframe.

That would be a great flaw, if the tester would generate a timer event as you described, but I can't exclude a possibility of a bug in MT5.

 
Stanislav Korotky #:

Your information contradicts to what MQ developers said many years ago (unfortunately I didn't find an English automatically translated "mirror" for this topic).

That would be a great flaw, if the tester would generate a timer event as you described, but I can't exclude a possibility of a bug in MT5.

There is no bug. What this user said is just incorrect.

Timer set on 30 seconds, backtest started 2026.01.01 (no ticks that day).


 
Alain Verleyen #:

There is no bug. What this user said is just incorrect.

Timer set on 30 seconds, backtest started 2026.01.01 (no ticks that day).


Please try an ea calling data on BTCUSD, in which the main symbol is a currency pair like EURUSD, run the simulation with the on timer...  Compare those results to a case in which the ea's main symbol is BTCUSD. In my case the results do not match, despite the ea always calling  BTCUSD.

I am not sure for the reason, but According to the MQL5 Reference (Testing Features), the OnTimer event is only processed when the simulator handles a tick. If there are no ticks (even if the "virtual" seconds have passed), the timer will not trigger.  While in real-time mode, timer events are generated based on the system clock, in the strategy tester, the timer is dependent on the simulated time progress, which is driven by tick arrival.

Again we are talking about multicurrency use of the OnTimer in strategy tester. An the extent to which one can use a single chart to operate multiple symbols.