In Strategy tester, template's indicator doesn't load iMA() correctly

 

Hello. I attached my custom indicator to a naked chart and save the template as "tester.tpl". Then I run strategy tester and select the indicator again. So in visual mode I see 2 of this indicator.

These 2 indicators works different! After many checks, I found that the one is attached by template doesn't update pricesArray[]. Chart goes forward but iMA() still returns same values. How can I solve this problem?

This problem not solved even by replacing CopyClose() instead of iMA()

if(prev_calculated == 0)
        limit = rates_total - 1-10;
else
        limit = rates_total - prev_calculated;

for(i = limit+10; i >= 0; i--)
      pricesArray[i] = iMA(NULL, HigherTimeframe(), 1, 0, MODE_SMA, PRICE_CLOSE, i)

Edit: When I press start button I see both indicators draw fine at the past, but after the starting time, the one that is in template not update correctly

The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
What are the differences between the three modes of testing in MetaTrader 5, and what should be particularly looked for? How does the testing of an EA, trading simultaneously on multiple instruments, take place? When and how are the indicator values calculated during testing, and how are the events handled? How to synchronize the bars from different instruments during testing in an "open prices only" mode? This article aims to provide answers to these and many other questions.
 
  1. if(prev_calculated == 0)
            limit = rates_total - 1-10;
    else
            limit = rates_total - prev_calculated;

    Do your lookbacks correctly #9#14 & #19. (2016)

  2.       pricesArray[i] = iMA(NULL, HigherTimeframe(), 1, 0, MODE_SMA, PRICE_CLOSE, i)

    You are mixing apples and oranges.

  3. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

  4. Is pricesArray an array and did you resize it properly, or a buffer?

    Always post all relevant code (using Code button) or attach the source file.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

 
William Roeder #:
  1. Do your lookbacks correctly #9#14 & #19. (2016)

  2. You are mixing apples and oranges.

Thank you. I'm aware about the mistakes you mentioned. But forget those. I'm curious about another problem. Why iMA() return different values on same indicator (one is attached to chart (tester template) and the other one select by strategy tester)?

I attached a screenshot from strategy tester

In strategy tester one, iMA() returns new data with forwarding the chart, but in attached one, iMA() return same old data. Both are 1 function, why act different?


 
William Roeder #:
Is pricesArray an array and did you resize it properly, or a buffer?
buffer
 
William Roeder #:
Always post all relevant code
//+------------------------------------------------------------------+
//|                                                         test.mq4 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   Print("Window: ",WindowOnDropped(),"     MA: ",iMA(Symbol(),PERIOD_M15,1,0,MODE_SMA,PRICE_CLOSE,1));
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

I have deleted all distraction codes. attach this to a chart, save template as tester.tpl. Then open strategy tester and select this indicator and M5 timeframe. After start you see 2 same indicator but they print different values. First one prints same price all time,but second one updates correctly