Indicator wrong loop

 
#include <MovingAverages.mqh>

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   1
#property indicator_type1   DRAW_LINE
#property indicator_color1  Blue
#property indicator_label1  "Diference"

//--- global variables
int         InpMAPeriod=20;                  
int         ExtPlotBegin=0;
//---- indicator buffer
double      ExtdiferenceBuffer[];
double      ExtMABuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtdiferenceBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ExtMABuffer,INDICATOR_CALCULATIONS);
//--- set accuracy
   IndicatorSetInteger(INDICATOR_DIGITS,2);
//--- sets first bar from what index will be drawn
   ExtPlotBegin=InpMAPeriod-1;
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpMAPeriod);
//---- line shifts when drawing
   PlotIndexSetInteger(0,PLOT_SHIFT,0);
//--- name for DataWindow
   IndicatorSetString(INDICATOR_SHORTNAME,"Diference: ");
//--- set index labels
   PlotIndexSetString(0,PLOT_LABEL,"Diference");
//---- sets drawing line empty value--
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

//---- initialization done
   return(INIT_SUCCEEDED);
}
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
{
//--- variables
   int pos;
//--- indexes draw begin settings, when we've recieved previous begin
   if(ExtPlotBegin!=InpMAPeriod+begin)
     {
      ExtPlotBegin=InpMAPeriod+begin;
      PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtPlotBegin);
     }
//--- check for bars count
   if(rates_total<ExtPlotBegin)     return(0);
//--- starting calculation
   if(prev_calculated>1) pos=prev_calculated-1;
   else pos=0;
   
//--- main cycle
   for(int i=pos;i<rates_total && !IsStopped();i++)
   {
      //---SMA
      ExtMABuffer[i]=SimpleMA(i,InpMAPeriod,price);
      
      //---Diference
      double diference = fabs(ExtMABuffer[i-6]-ExtMABuffer[i-1]);
      ExtdiferenceBuffer[i] = diference; 
   }   
      
//--- return value of prev_calculated for next call
   return(rates_total);
}


Hello guys, 

I'm learning about indicators and wanna do a simple one, but i'm having problems in the loop logic. I know it is wrong but can't fix it.


Here is the indicator's ideia: He calculates a simple moving average of close price then returns the value of SMA[6]-SMA[1]. The plot will be the result of SMA[6]-SMA[1] only.

I've already tried to do 2 diferent plots with a shift of 6 and 1, but the shift is only visual the return value is the same for both, resulting in SMA[6]-SMA[1] = 0 


Any ideias how to do the correct loop?

Sorry for the bad english :)