CopyRates of higher timeframe in OnCalculate function of a custom indicator

 

Hello. I want always have rates of daily timeframe in an array to use it in my custom indicator.

I write it in this way to copy all daily rates in first run, and update only last day's rate in every next OnCalculate() call. But it seems this is not the correct way. Can anyone help me?

//+------------------------------------------------------------------+
//| 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[])
  {
//---
   MqlRates Rates[];
   ArraySetAsSeries(Rates, true);
   int Limit;
   if(prev_calculated == 0)
     {
      Limit = rates_total - 1;
      CopyRates(Symbol(), PERIOD_D1, time[Limit], time[0], Rates);
     }
   else
     {
      Limit = rates_total - prev_calculated;
      CopyRates(Symbol(), PERIOD_D1, 0, 1, Rates);
     }

//--- Main Loop
   for(int i = Limit; i >= 0; i--)
        {

        }
 
Farzad Sadeghishahrestanak:

Hello. I want always have rates of daily timeframe in an array to use it in my custom indicator.

I write it in this way to copy all daily rates in first run, and update only last day's rate in every next OnCalculate() call. But it seems this is not the correct way. Can anyone help me?

Rates is local to OnCalculate . It shouldn't be.
 
Yashar Seyyedin #:
Rates is local to OnCalculate . It shouldn't be.

Oh, you are right. Why I had put it there!

 
thank you @Yashar Seyyedin
 
      Limit = rates_total - 1;
      CopyRates(Symbol(), PERIOD_D1, time[Limit], time[0], Rates);

  1. 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)


  2. In MT4, buffers and MT4 predefined arrays are all ordered AsSeries. There is a difference between the arrays passed to OnCalculate (e.g. low[]) and the MT4 predefined variables (e.g. Low[].) The passed arrays have no default direction, just like MT5.

    To determine the indexing direction of time[], open[], high[], low[], close[], tick_volume[], volume[] and spread[], call ArrayGetAsSeries(). In order not to depend on default values, you should unconditionally call the ArraySetAsSeries() function for those arrays, which are expected to work with.
              Event Handling Functions - Functions - Language Basics - MQL4 Reference

 
William Roeder #:
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)
Thank you. When I have CopyRates() in my code, and I use Strategy Tester on higher timeframes (e.g. copy rates of M5 timeframe when strategy tester is running on daily timeframe) I see it copies 1000 bars maximum (return CopyRates copied elements = 1000). Is there any limitation for CopyRates() when we use Strategy Tester?
Reason: