Bug in MQL5 (Backtest Only)
cre8iveq:
Why can we not access indicators in OnInit()
https://www.mql5.com/en/forum/227456#comment_6542317
cre8iveq:
wouldn't that be kind of useful?
Maybe.
cre8iveq:
(it took me hours to figure out why this wasn't working)
Sorry that happened. At least you won't do it again.
A backtest, by definition is supposed to get the same results as a live test, or as close to it as possible
Who told you that ?
Who told you that ?
The fact that one thing happens on a live chart, and a different thing happens in the backtest makes it a bug. A backtest, by definition is supposed to get the same results as a live test, or as close to it as possible
This is not a bug, it's how MT5 is working.
Don't use this code in OnInit(), the tester doesn't have time to launch the indicator.
Put this code in the OnInit() function...
nATRHandle = iATR(_Symbol, PERIOD_CURRENT, nATRPeriod);
Comment(CopyBuffer(nATRHandle, 0, 0, 2, arrATR));
If you run this in a live chart, you'll see exactly what you expect... CopyBuffer returns 2. If you run it on a backtest, you'll see CopyBuffer returns -1. An error code. You'll see CopyBuffer returns -1, which is an error code. Why can we not access indicators in OnInit()... wouldn't that be kind of useful? (it took me hours to figure out why this wasn't working)