Fill Moving Average buffer from "rates_total"

 

Hello,

I am trying to compare 2 different EMA from H1 timeframe with close price. But I get "Array out of range" error. I run this indicator on M2 timeframe.


How Can I fill the MA buffer from rates_total?


//+------------------------------------------------------------------+
//|                                                         MACS.mq5 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   1
//--- plot H1
#property indicator_label1  "H1"
#property indicator_type1   DRAW_COLOR_ARROW
#property indicator_color1  Red,Green
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2


//--- input parameters
input int      ShortEMA=8;
input int      LongEMA=25;
//--- indicator buffers
double         H1Buffer[],H1Color[];





double MASH1[],MALH1[];


int MASH1Handler,MALH1Handler;


   
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,H1Buffer,INDICATOR_DATA);
   SetIndexBuffer(1,H1Color,INDICATOR_COLOR_INDEX);
   

   
   PlotIndexSetInteger(0,PLOT_COLOR_INDEXES,2);


   PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,Red);
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,Green);

   
   MASH1Handler= iMA(_Symbol,PERIOD_H1,8,0,MODE_EMA,PRICE_CLOSE);
   MALH1Handler= iMA(_Symbol,PERIOD_H1,25,0,MODE_EMA,PRICE_CLOSE);

    
//---
   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[])
  {
  
//---
   ArraySetAsSeries(MASH1,true);
   ArraySetAsSeries(MALH1,true);



    CopyBuffer(MASH1Handler, 0, 0, rates_total, MASH1);
    CopyBuffer(MALH1Handler, 0, 0, rates_total, MALH1);
     


  
   int start = 0;



      for(int i=start;i<rates_total;i++)
      
     {

     if((MASH1[i] > MALH1[i]) && (low[i] > MASH1[i])){
     H1Color[i] = 1;
    
     }
     
     if((MASH1[i] < MALH1[i]) && (high[i] < MASH1[i])){
           H1Color[i] = 0;
     }
      

      H1Buffer[i] = 0.02;

     }
   
  
   return(rates_total);
  }
//+------------------------------------------------------------------+


Reason: