In MQL5, an indicator handle needs to be created only once - and this is done in OnInit.
thank you for your answer.
I did as you suggested and now initialize the handle only in the OnInit method
still, no change. the ATR indicator shows up at the bottom of the chart right after the test is started, as in the screen shot I posted further up.
int atrHandle = INVALID_HANDLE; int atrPeriod = 14; int OnInit() { if (atrHandle == INVALID_HANDLE) { atrHandle = iATR(Symbol(), Period(), atrPeriod); } return INIT_SUCCEEDED; } // modified method double GetATR(int period, int candle=0, string symbol=NULL, ENUM_TIMEFRAMES timeFrame=-1) { symbol = (symbol==NULL) ? Symbol() : symbol; timeFrame = (timeFrame==-1) ? Period() : timeFrame; double valATR[]; ArraySetAsSeries(valATR, true); if (atrHandle==INVALID_HANDLE) { // this never prints so the handle is valid after OnInit() Print("invalid atr handle"); atrHandle = iATR(symbol, timeFrame, period); } CopyBuffer(atrHandle, 0, 0, candle+1, valATR); return valATR[candle]; } // removed any logic besides printing atr value void OnTick() { //--- // prints expected ATR value Print(GetATR(atrPeriod, 1)); }
thank you for your answer.
I did as you suggested and now initialize the handle only in the OnInit method
still, no change. the ATR indicator shows up at the bottom of the chart right after the test is started, as in the screen shot I posted further up.
Everything is correct: during visual testing of Expert Advisors, the tester displays ALL indicators that the Expert Advisor addresses.
oh God :)
ok, thanks a lot for your time and expertise. I guess I will try to minimize the height of the window.
. . .
//--- Hide indicators during testing TesterHideIndicators(true);
thank you for your comment, I wasn't aware of this method.
I don't want to hide any indicator that might possibly sit on the chart, just this one as it's an unwanted side effect.
I use this little hack now, which serves my needs. The user can define via EA's settings whether he wants to see the ATR.
if not, the window is minimized.
// global stuff bool atrWindowMinimized = false; void OnTick() { MinimizeATRWindow(); // stuff ... } void MinimizeATRWindow() { // if user wants to see ATR or we already minimized the window do nothing if (UserWantsToSeeATR || atrWindowMinimized) { return; } int windows = (int)ChartGetInteger(0, CHART_WINDOWS_TOTAL); // get all window indices for(int w=0;w<windows;w++) { // loop over window indices int total = ChartIndicatorsTotal(0,w); // get all indicator indices from current subwindow for(int i=0; i<total; i++) { // loop over indicator indices string name = ChartIndicatorName(0, w, i); // get indicator short name StringToLower(name); if (StringFind(name, "atr")>=0) { // is it the ATR ? ChartSetInteger(0, CHART_HEIGHT_IN_PIXELS, w, 0); // minimize subwindow atrWindowMinimized = true; // set flag return; // quit } } } }
thank you all for your comments.
I always find very high level of support here
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I'm experiencing a strange behaviour in the strategy tester on MT5 and MT4.
I'm reading the ATR value with following code which works fine but
all though there is no ATR indicator on the testing template and I'm reading the ATR value from inside an expert advisor,
that I'm testing, the ATR indicator is added to the chart.
And of course I have nowhere else any code that would in any way load the ATR indicator onto the chart.
This is the only location in all of the script where the ATR is referenced.
I just need the value at candle index: 1
The ATR indicator is added both on MT4 and MT5 strategy testers, just on MT4 it only shows after the test was stopped or is finished normally
while on MT5 it shows all the time during the test already.
Please see as well attached screen.
Thank you.