Issues With For Loop & Daily MA on PERIOD_M15

 

Hi all, 

I was having an issue with a HTF moving average calculating the entire rates_total on every loop due to a badly designed for loop. I have created an indicator to help debug this and redesign the loop so that I am only calculating the most recent bar (after the intial loop through the whole chart). That part is resolved, I am now only calcualting the most bar.

However now, my HTF MA is only calculating the most recent bar, as opposed to the whole history, and I can't work out why.

I have added text labels of 'i' in blue, and 'DailyBar' (the daily bar shift) in red to help debug but I still cannot work it out. (See attached screenshot)

If anyone can set me right here it would be greatly appreciated. 

Thanks in advance. 



//+------------------------------------------------------------------+
//|                                                          CB-.mq5 |
//|                                                            Chris |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Chris"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#include <CustomFunctions.mqh>
//+------------------------------------------------------------------+
//| indicator properties                                             |
//+------------------------------------------------------------------+
#property indicator_buffers 10
#property indicator_plots 6

#property indicator_color1 clrRed
#property indicator_label1 "Fast Daily"
#property indicator_style1 STYLE_SOLID
#property indicator_type1 DRAW_LINE
#property indicator_width1 2
//+------------------------------------------------------------------+
//| variables                                                        |
//+------------------------------------------------------------------+
bool     buyCandle, sellCandle;

//+------------------------------------------------------------------+
//|buffers and handles                                               |
//+------------------------------------------------------------------+
double   fastDailyBuffer[];
double   fastDailyValues[];
int      fastDailyHandle;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {

//--- indicator buffers mapping
   SetIndexBuffer(0, fastDailyBuffer, INDICATOR_DATA);
   ArraySetAsSeries(fastDailyBuffer, true);
   ArraySetAsSeries(fastDailyValues, true);

//+------------------------------------------------------------------+
//| handle creation                                                  |
//+------------------------------------------------------------------+
   fastDailyHandle    =  iMA(Symbol(), PERIOD_D1, 8, 0, MODE_EMA, PRICE_CLOSE);

   if(fastDailyHandle   == INVALID_HANDLE)
     {
      printf("Failed to create handles");
      return(INIT_FAILED);
     }
   printf("handles created successfully");


   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[])
  {

//--- array series setting
   ArraySetAsSeries(time, true);
   ArraySetAsSeries(open, true);
   ArraySetAsSeries(high, true);
   ArraySetAsSeries(low, true);
   ArraySetAsSeries(close, true);
   ArraySetAsSeries(tick_volume, true);
   ArraySetAsSeries(volume, true);
   ArraySetAsSeries(spread, true);


   int barsToCalculate;

   if(prev_calculated == 0)
      barsToCalculate = rates_total - 1;
   else
      barsToCalculate = rates_total - prev_calculated;


//+------------------------------------------------------------------+
//| for loop                                                         |
//+------------------------------------------------------------------+
   for(int i = barsToCalculate; i >= 0; i--)
     {
         Print("i = " + i);
       
      int DailyBar = iBarShift(Symbol(), PERIOD_D1, time[i], false);
      Print("DailyBar is: " + DailyBar);
      
      datetime DailyTime = iTime(Symbol(), PERIOD_D1, DailyBar);
      Print("DailyTime is: " + DailyTime);

      CopyBuffer(fastDailyHandle, 0, DailyTime, 1, fastDailyValues);
      fastDailyBuffer[i] = fastDailyValues[0];

//+------------------------------------------------------------------+
//| print labels for visual debug                                    |
//+------------------------------------------------------------------+
 TextCreate(0, (string)Symbol() + i,
                 0,
                 iTime(Symbol(), PERIOD_CURRENT, i),
                 High(i) + 10 * Point(),
                 (string)i,
                 "Arial",
                 8,
                 clrBlue,
                 0,
                 ANCHOR_CENTER,
                 false,
                 false,
                 false);
                 
TextCreate(0, (string)Symbol() + DailyBar,
                 0,
                 iTime(Symbol(), PERIOD_CURRENT, i),
                 High(i) + 30 * Point(),
                 (string)DailyBar,
                 "Arial",
                 8,
                 clrRed,
                 0,
                 ANCHOR_CENTER,
                 false,
                 false,
                 false);

     }//for loop
   return(rates_total);
  }//on calculate
 
TextCreate(0, (string)Symbol() + i,

TextCreate(0, (string)Symbol() + DailyBar,

Do not use series index in object names, as they are not unique. As soon as a new bar starts, you will be trying to create a new name (e.g., “name0”), same, existing, previous, name (e.g., “name0” now on bar one.)

Use time (as int) or a non-series index:

#define  SERIES(I)   (Bars - 1 - I) // As-series to non-series or back.
 
William Roeder #:

Do not use series index in object names, as they are not unique. As soon as a new bar starts, you will be trying to create a new name (e.g., “name0”), same, existing, previous, name (e.g., “name0” now on bar one.)

Use time (as int) or a non-series index:

Hi William, 

I see what you mean, Thank you for that.

So in fact it isn't failing to count the entirety of bars at all. It is just trying to create the same object again and again on the most recent candle.

Appreciate that, Cheers. 


Reason: