iMAOnArray() - What am I doing wrong?

 
Greetings, I am novice MQL4 user and trying to create custom indicator. I want to use moving average to smooth
noisy result of my function. What am I doing wrong?

#property indicator_label1  "delta"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrSlateGray
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

#property indicator_label2  "smoothed"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

#property indicator_label3  "signal"
#property indicator_type3   DRAW_HISTOGRAM
#property indicator_color3  clrYellow
#property indicator_style3  STYLE_SOLID
#property indicator_width3  2

double deltaBuffer[];
double smoothedBuffer[];
double signalBuffer[];

input int DELTA_Period = 20;
input int DELTA_Shift = 0;
input ENUM_MA_METHOD DELTA_Method = MODE_SMA;
input ENUM_APPLIED_PRICE DELTA_AppliedPrice = PRICE_CLOSE;

string symbol;
int timeFrame;

int shift;

int OnInit()
{
   SetIndexBuffer(0, deltaBuffer);
   SetIndexBuffer(1, smoothedBuffer);
   SetIndexBuffer(2, signalBuffer);
   
   symbol = Symbol();
   timeFrame = PERIOD_CURRENT;
   shift = 0;

   return(INIT_SUCCEEDED);
}

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 uncalculatedBar = rates_total - prev_calculated;
   
   for (int i = 0; i < uncalculatedBar; i++)
   {
      deltaBuffer[i] = MovingAverageDelta(symbol, timeFrame, DELTA_Period, DELTA_Shift + i, DELTA_Method, DELTA_AppliedPrice, shift);
      smoothedBuffer[i] = iMAOnArray(deltaBuffer, 0, 3, 0, MODE_SMA, 0);
   }
   return(rates_total);
}


double MovingAverageDelta(string marketSymbol, int chartTimeFrame, int MAD_period, int MAD_shift, int MAD_method, int MAD_appliedPrice, int chartShift)
{
   double currentMA = iMA(marketSymbol, chartTimeFrame, MAD_period, MAD_shift, MAD_method, MAD_appliedPrice, chartShift);
   double previousMA =iMA(marketSymbol, chartTimeFrame, MAD_period, MAD_shift + 1, MAD_method, MAD_appliedPrice, chartShift);
   
   return NormalizeDouble((currentMA - previousMA) / Point, Digits);
}

The attachment shows my result, red line should by moving average.
Step on New Rails: Custom Indicators in MQL5
Step on New Rails: Custom Indicators in MQL5
  • www.mql5.com
Finally we've got an opportunity to try the new trade terminal - MetaTrader 5. No doubt, it is noteworthy and has many new features as compared to its predecessor. The important advantages of this platform among others are: Essentially modified language allowing now to use the object-oriented programming, still allowing to use the rich...
Files:
Capture.JPG  168 kb
 

Just a suggestion, not checked or tested.

   int uncalculatedBar = rates_total - prev_calculated;
   if(prev_calculated==0)
      uncalculatedBar--;     //You may want to change this so that it doesn't calculate the first MA period bars

   for (int i = uncalculatedBar; i >=0; i--)
      deltaBuffer[i] = MovingAverageDelta(symbol, timeFrame, DELTA_Period, DELTA_Shift + i, DELTA_Method, DELTA_AppliedPrice, shift);
   for (int i = uncalculatedBar; i >=0; i--)
      smoothedBuffer[i] = iMAOnArray(deltaBuffer, 0, 3, 0, MODE_SMA, i);
Edited to correct the error that William noticed.
 
     smoothedBuffer[i] = iMAOnArray(deltaBuffer, 0, 3, 0, MODE_SMA, 0);
You are placing the same value into each element.
 
Ok, thank You all for help. Turned out I misunderstood meaning of shift and MA_shift,
plus some minor mistakes with number of indicator_buffer and indicator_plot.

Two for loops helped. It turned out in one loop  indicator had to fill deltaBuffer
and with full deltaBuffer I was able to calculate moving average.

I have one more question:
Its regarding MovingAverageDelta():
Is there some way to get access to previous calculated values of MA.
It seams waste of computing power and time to calculate MA two times.

PS.: Current result in attachment.
Files:
Reason: