Help with changing an indicator

 

Hello, I have the indicator in the code below which colours a moving average depending on whether it is going up or down. I would like to change it so that it looks back 30 bars so it only is green if the ma is higher than it was 30 bars ago or red if it is lower than 30 bars ago. I did this on the mt4 version with no problems but I am new to mt5 and just can't figure it out. I don't get the iCustomMA syntax and what r means? Anyway, if anyone could point me in the right direction I would appreciate it. Thanks


 {
      int i=(int)MathMax(prev_calculated-1,0);for (; i<rates_total && !IsStopped(); i++)
      {
         double price = getPrice(Price,open,close,high,low,i,rates_total,0);
         MaBuffer[i] = iCustomMa(MaMethod,price,MaPeriod,rates_total,i);
         
          if (i>0)
         {
            ColorBuffer[i] = ColorBuffer[i-1];
               if (MaBuffer[i]>MaBuffer[i-1]) {ColorBuffer[i]=0;}
               if (MaBuffer[i]<MaBuffer[i-1]) {ColorBuffer[i]=1; }
         } else ColorBuffer[i]=0;
      }
   
   return(rates_total);
  }
//+------------------------------------------------------------------+
string shortName(int mode)
{
      switch(mode)
     {
      case _sma   : return "Simple MA ("+(string)MaPeriod+")";
      case _ema   : return "Exponential MA ("+(string)MaPeriod+")";
      case _smma  : return "Smoothed MA ("+(string)MaPeriod+")";
      case _lwma  : return "Linear weighted  MA ("+(string)MaPeriod+")";
      default       : return "Moving Average ("+(string)MaPeriod+")";
     }
}
double iCustomMa(int mode,double price,int period,int bars,int r)
  {
   switch(mode)
     {
      case _sma   : return(iSMA(price,period,bars,r));
      case _ema   : return(iEMA(price,period,bars,r));
      case _smma  : return(iSMMA(price,period,bars,r));
      case _lwma  : return(iLWMA(price,period,bars,r));
      default       : return(price);
     }
  }
  
  
double  maArray[];
double iSMA(double price, int period, int bars, int r)
{
   if (ArraySize(maArray)!=bars) ArrayResize(maArray,bars); 
   
   maArray[r] = price;

   double avg = price; 
    int k=1;
   for(; k<period && (r-k)>=0; k++) 
      avg += maArray[r-k];
   
   return(avg/(double)k);
}

double iEMA(double price,double period,int bars, int r)
  {
   if (ArraySize(maArray)!=bars) ArrayResize(maArray,bars); 
   maArray[r]=price;
   if(r>0 && period>1)
      maArray[r]=maArray[r-1]+(2.0/(1.0+period))*(price-maArray[r-1]);
      
   return(maArray[r]);
  }
  
  double iSMMA(double price,double period,int bars, int r)
  {
   if (ArraySize(maArray)!=bars) ArrayResize(maArray,bars); 

   maArray[r]=price;
   if(r>1 && period>1)
      maArray[r]=maArray[r-1]+(price-maArray[r-1])/period;
      
   return(maArray[r]);
  }
  
double iLWMA(double price,double period,int bars, int r)
  {
   if (ArraySize(maArray)!=bars) ArrayResize(maArray,bars); 


   maArray[r] = price; if(period<1) return(price);
   double sumw = period;
   double sum  = period*price;

   for(int k=1; k<period && (r-k)>=0; k++)
     {
      double weight=period-k;
      sumw  += weight;
      sum   += weight*maArray[r-k];
     }
   return(sum/sumw);
  }  

//
double getPrice(ENUM_APPLIED_PRICE  tprice,const double &open[],const double &close[],const double &high[],const double &low[],int i,int _bars,int instanceNo=0)
  {
   switch(tprice)
   {
         case PRICE_CLOSE:     return(close[i]);
         case PRICE_OPEN:      return(open[i]);
         case PRICE_HIGH:      return(high[i]);
         case PRICE_LOW:       return(low[i]);
         case PRICE_MEDIAN:    return((high[i]+low[i])/2.0);
         case PRICE_TYPICAL:   return((high[i]+low[i]+close[i])/3.0);
         case PRICE_WEIGHTED:  return((high[i]+low[i]+close[i]+close[i])/4.0);
   }
   return(0);
  }
 
Here is an article about your question: https://www.mql5.com/en/articles/135
Creating Multi-Colored Indicators in MQL5
Creating Multi-Colored Indicators in MQL5
  • www.mql5.com
In this article, we will consider how to create multi-colored indicators or convert the existing ones to multi-color. MQL5 allows to represent the information in the convenient form. Now it isn't necessary to look at a dozen of charts with indicators and perform analyses of the RSI or Stochastic levels, it's better just to paint the candles with different colors depending on the values of the indicators.
 
Carl Schreiber:
Here is an article about your question: https://www.mql5.com/en/articles/135
Thank you, I will have a look and see if I can code my own from scratch. Might be easier than trying to alter someone else's code!
 
if (MaBuffer[i]>MaBuffer[i-lookback]) {ColorBuffer[i]=0;}
if (MaBuffer[i]<MaBuffer[i-lookback]) {ColorBuffer[i]=1; }

I think this is what's needed but it doesn't work. I'm guessing because at the start of the moving average to the very left of the chart it's trying to compare the two ma's 30 bars apart but there is nothing there to compare it with. I'm not sure if I am right about that or what to do about it. Anyone got any ideas?

On a side note, after looking at some of the mql5 documentation, is it not possible in mt5 to just get a moving average by using iMA, it seems you have to fill it into a buffer before you can do anything with it?! So in mt4 you would just use iMA(Symbol(),1,malength,0,matype,0,1); and it would give you the ma value for the previous close and you can't just do that in mt5?

 
Anybody got any ideas about this?