Supertrend Mql5 MTF Problems?

 
Hi,

This is my first time dabbling with Mql5 code. I wanted to modify a commonly used Mql5 Supertrend Indicator and make it MTF with separate the bars when changing direction.

I did some research and this is what I can come up. From what I've read, iBarShift is required but I'm not sure about rates_total.

Everything looks okay, as I do not receive any errors when adding 2 of this indicator with different timeframes to a chart (eg. indicator set to M1 on a M1 Chart, with the same indicator also added but set to M5) but I can't be sure? Are there any obvious errors? I've commented out some of the original code.

Thanks.
#property indicator_label1  "Supertrend Down"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_width1  2

#property indicator_label2  "Supertrend Up"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrGreen
#property indicator_width2  2

enum enTimeFrames
{
   tf_cu  = PERIOD_CURRENT, // Current time frame
   tf_m1  = PERIOD_M1,      // 1 minute
   tf_m2  = PERIOD_M2,      // 2 minutes
   tf_m3  = PERIOD_M3,      // 3 minutes
   tf_m4  = PERIOD_M4,      // 4 minutes
   tf_m5  = PERIOD_M5,      // 5 minutes
   tf_m6  = PERIOD_M6,      // 6 minutes
   tf_m10 = PERIOD_M10,     // 10 minutes
   tf_m12 = PERIOD_M12,     // 12 minutes
   tf_m15 = PERIOD_M15,     // 15 minutes
   tf_m20 = PERIOD_M20,     // 20 minutes
   tf_m30 = PERIOD_M30,     // 30 minutes
   tf_h1  = PERIOD_H1,      // 1 hour
   tf_h2  = PERIOD_H2,      // 2 hours
   tf_h3  = PERIOD_H3,      // 3 hours
   tf_h4  = PERIOD_H4,      // 4 hours
   tf_h6  = PERIOD_H6,      // 6 hours
   tf_h8  = PERIOD_H8,      // 8 hours
   tf_h12 = PERIOD_H12,     // 12 hours
   tf_d1  = PERIOD_D1,      // daily
   tf_w1  = PERIOD_W1,      // weekly
   tf_mn  = PERIOD_MN1,     // monthly
   tf_cp1 = -1,             // Next higher time frame
   tf_cp2 = -2,             // Second higher time frame
   tf_cp3 = -3              // Third higher time frame
};

input enTimeFrames       inpTimeFrame = tf_cu;      // Time frame
input int    Periods=5;
input double Multiplier=2;

double SuperTrendUp[];
double SuperTrendDown[];
double SuperTrend[];
double Atr[];
double Up[];
double Down[];
double Middle[];
double trend[];

int atrHandle;
int shift;
int changeOfTrend;
int flag;
int flagh;

ENUM_TIMEFRAMES _indicatorTimeFrame;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
  
  _indicatorTimeFrame  = MathMax(_Period,timeFrameGet(inpTimeFrame));
  
//--- indicator buffers mapping
   SetIndexBuffer(0,SuperTrendUp,INDICATOR_DATA);
   SetIndexBuffer(1,SuperTrendDown,INDICATOR_DATA);
   SetIndexBuffer(2,SuperTrend,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3,Atr,INDICATOR_CALCULATIONS);
   SetIndexBuffer(4,Up,INDICATOR_CALCULATIONS);
   SetIndexBuffer(5,Down,INDICATOR_CALCULATIONS);
   SetIndexBuffer(6,Middle,INDICATOR_CALCULATIONS);
   SetIndexBuffer(7,trend,INDICATOR_CALCULATIONS);

   //atrHandle=iATR(_Symbol,_Period,Period);
   atrHandle=iATR(_Symbol,_indicatorTimeFrame,Periods);
   //CopyBuffer(atrHandle,0,0,to_copy,Atr);
   
//---
    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[])
  {
//---
 
   
   /*int to_copy;
   if(prev_calculated>rates_total || prev_calculated<0) to_copy=rates_total;
   else
     {
      to_copy=rates_total-prev_calculated;
      if(prev_calculated>0) to_copy++;
     }*/

   //if(IsStopped()) return(0); //Checking for stop flag
   /*if(CopyBuffer(atrHandle,0,0,to_copy,Atr)<=0)
     {
      return(0);
     }*/

   int first;
   if(prev_calculated>rates_total || prev_calculated<=0) // checking for the first start of calculation of an indicator
     {
      first=Periods; // starting index for calculation of all bars
     }
   else
     {
      first=prev_calculated-1;
      shift=iBarShift(_Symbol,_indicatorTimeFrame,time[rates_total-1]);// starting number for calculation of new bars
     }
     
   if(BarsCalculated(atrHandle)<shift)
      return(0);
      
   for(int i=first; i<rates_total && !_StopFlag; i++)
     {
      shift=iBarShift(_Symbol,_indicatorTimeFrame,time[i]);
      if(CopyBuffer(atrHandle,0,shift,1,Atr)!=-1);
      {
         Middle[i]=(high[i]+low[i])/2;
         Up[i]  = Middle[i] +(Multiplier*Atr[i]);
         Down[i]= Middle[i] -(Multiplier*Atr[i]);
   
         if(close[i]>Up[i-1]) 
           {
            trend[i]=1;
            if(trend[i-1]==-1) changeOfTrend=1;
   
           }
         else if(close[i]<Down[i-1]) 
           {
            trend[i]=-1;
            if(trend[i-1]==1) changeOfTrend=1;
           }
         else if(trend[i-1]==1) 
           {
            trend[i]=1;
            changeOfTrend=0;
           }
         else if(trend[i-1]==-1) 
           {
            trend[i]=-1;
            changeOfTrend=0;
           }
   
         if(trend[i]<0 && trend[i-1]>0) 
           {
            flag=1;
           }
         else 
           {
            flag=0;
           }
   
         if(trend[i]>0 && trend[i-1]<0) 
           {
            flagh=1;
           }
         else 
           {
            flagh=0;
           }
   
         if(trend[i]>0 && Down[i]<Down[i-1])
            Down[i]=Down[i-1];
   
         if(trend[i]<0 && Up[i]>Up[i-1])
            Up[i]=Up[i-1];
   
         if(flag==1)
            Up[i]=Middle[i]+(Multiplier*Atr[i]);
   
         if(flagh==1)
            Down[i]=Middle[i]-(Multiplier*Atr[i]);
   
         //-- Draw the indicator
         if(trend[i]==1) 
           {
            SuperTrend[i]=Down[i];
            SuperTrendDown[i]=SuperTrend[i];
            SuperTrendUp[i]=EMPTY_VALUE;
            if(changeOfTrend==1) 
              {
               SuperTrend[i-1]=SuperTrend[i-2];
               changeOfTrend=0;
              }
            //ColorBuffer[i]=0.0;
           }
         else if(trend[i]==-1) 
           {
            SuperTrend[i]=Up[i];
            SuperTrendUp[i]=SuperTrend[i];
            SuperTrendDown[i]= EMPTY_VALUE;
            if(changeOfTrend==1) 
              {
               SuperTrend[i-1]= SuperTrend[i-2];
               changeOfTrend = 0;
              }
            //ColorBuffer[i]=1.0;
           }
         }
     }

//--- return value of prev_calculated for next call
   return(rates_total);
  }