Array out of range in strategy tester - page 2

 

Thanks people,

checking the Bars value solved the problem.

 
Fernando Carreiro #:

Always check the "Bars" variable to make sure the index into Time[], High[], Low[], Close[], etc. will be valid or not.

This is especially important when running under the Strategy Tester with no "Use Date" limits, and during the first bar.

There will be no "mismatch" reported, because there is no mismatch. It simply just has the current bar data available.

Please can you explain what you mean by checking? I have the same issue. It is a pattern recognition EA that look for previous candles so use ratesH1[1], ratesH1[2], etc values and in the strategy tester it crash directly at the start because array out of range but in live markets work fine

 
Bryan Djoufack Nguessong #:

Please can you explain what you mean by checking? I have the same issue. It is a pattern recognition EA that look for previous candles so use ratesH1[1], ratesH1[2], etc values and in the strategy tester it crash directly at the start because array out of range but in live markets work fine

If I am not mistaken, this could help

https://www.mql5.com/en/docs/series/bars

Documentation on MQL5: Timeseries and Indicators Access / Bars
Documentation on MQL5: Timeseries and Indicators Access / Bars
  • www.mql5.com
Bars - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
just check the oninit time in oninit function and save in global value

OnInitTime = iTime(_Symbol, _Period, 0);

after this you must check if count of candles from starting strategy tester is enough then do every thing you want

bool checkTime = iTime(_Symbol, _Period, 1)> OnInitTime+( PeriodSeconds( _Period)*(  Count Of candle you want to scape from starting point )) ;

check checkTime variable before access to your array
 
@Mohammad Inanloo #: just check the oninit time in oninit function and save in global value

You should not access time series data in the OnInit() event handler, as the data may not be available yet (e.g. after opening MetaTrader and it is still not connected to the trade server or the chart data has not loaded yet).

Instead, do it in OnTick() when receiving the first tick.

 
Mohammad Inanloo #: bool checkTime = iTime(_Symbol, _Period, 1)> OnInitTime+( PeriodSeconds( _Period)*(  Count Of candle you want to scape from starting point )) ;

This assumes every bar every exists — they don't. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
          "Free-of-Holes" Charts - MQL4 Articles (2006)
          No candle if open = close ? - MQL4 programming forum (2010)

Just compare the bar index to Bars.

Reason: