Weird issue with iMAOnArray

 

Hi all,

I am trying to create a simple indicator, basically a MACD on the RSI.

Calculating the RSI and the two moving averages is no problem, works fine.

Subtracting the two moving averages also works fine. (When I change the script to plot the resulting array of the subtraction, it plots ok.)

So far, so good, but then I need to apply another smoothing to the resulting value, and that's where iMAOnArray only returns 0.

The weird part is, it works when you do iMAOnArray on the first or the second moving average individually, but it doesn't work on (FirstMovingAverage - SecondMovingAverage) ??!! WTH?

What's going on?

Here is the code:


#property indicator_separate_window
#property indicator_buffers    4
#property indicator_color1     White
#property indicator_color2     Yellow
#property indicator_color3     Green
#property indicator_color4     Aqua
#property indicator_level1     25.0
#property indicator_level2     40.0
#property indicator_level3     50.0
#property indicator_level4     60.0
#property indicator_level5     75.0
#property indicator_levelcolor clrSilver
#property indicator_levelstyle STYLE_DOT

extern ENUM_APPLIED_PRICE rsi_src = PRICE_MEDIAN; //Source
extern int RSI_Period = 5; // RSI Smoothing Period
extern int MA1_Period = 5; // RSI MA 1 Period
extern int MA2_Period = 200; // RSI MA 2 Period
extern int MACD_MA_Period = 5; // RSI MACD MA Period
extern ENUM_MA_METHOD rsi_ma_method = MODE_EMA; // RSI/MACD Smoothing Method
//extern int upperlimit_1 = 60;
//extern int lowerlimit_1 = 40;
//extern int upperlimit_2 = 75;
//extern int lowerlimit_2 = 25;

double rsi_buffer[];
double ma1_buffer[];
double ma2_buffer[];
double macd_buffer[];
double macd_ma_buffer[];

int init()
{
    IndicatorShortName("QM3-RSI-MACD");

    IndicatorBuffers(4);

    SetIndexBuffer(0, rsi_buffer, INDICATOR_CALCULATIONS);
    SetIndexBuffer(1, ma1_buffer, INDICATOR_CALCULATIONS);
    SetIndexBuffer(2, ma2_buffer, INDICATOR_CALCULATIONS);
    SetIndexBuffer(3, macd_ma_buffer, INDICATOR_CALCULATIONS);

    SetIndexStyle(0, DRAW_LINE);
    SetIndexStyle(1, DRAW_LINE);
    SetIndexStyle(2, DRAW_LINE);
    SetIndexStyle(3, DRAW_LINE);

    SetIndexLabel(0, "RSI");
    SetIndexLabel(1, "MA 1");
    SetIndexLabel(2, "MA 2");
    SetIndexLabel(3, "MACD MA");

    return(0);
}

int start()
{
   int limit, counted_bars = IndicatorCounted();
   int max_lookback = 2500;
   int i;

   if (counted_bars < 0) return(-1);
   
   if (counted_bars > 0) counted_bars--;
   
   limit = Bars - counted_bars;
   
   if ( limit > max_lookback )
      limit = max_lookback;
   
   for (i = limit; i >= 0; i--)
   {
     rsi_buffer[i] = iRSI(NULL, 0, RSI_Period, rsi_src, i);
     ma1_buffer[i] = iMAOnArray(rsi_buffer, Bars, MA1_Period, 0, rsi_ma_method, i);
     ma2_buffer[i] = iMAOnArray(rsi_buffer, Bars, MA2_Period, 0, rsi_ma_method, i);
     macd_buffer[i] = ma1_buffer[i] - ma2_buffer[i];

     // THE FOLLOWING RETURNS ONLY 0
     macd_ma_buffer[i] = iMAOnArray(macd_buffer, Bars, MACD_MA_Period, 0, rsi_ma_method, i);

     // BUT THE FOLLOWING WORKS
     // macd_ma_buffer[i] = iMAOnArray(ma1_buffer, Bars, MACD_MA_Period, 0, rsi_ma_method, i);
     // OR
     // macd_ma_buffer[i] = iMAOnArray(ma2_buffer, Bars, MACD_MA_Period, 0, rsi_ma_method, i);
   }
   
   return(0);
}


Thank you very much in advance.

Cheers

Moving Average - Trend Indicators - Technical Indicators - Price Charts, Technical and Fundamental Analysis - MetaTrader 5 Help
  • www.metatrader5.com
The Moving Average Technical Indicator shows the mean instrument price value for a certain period of time. When one calculates the moving average...
 

You have to add a buffer : macd_buffer. 

Right now It is like you have an array( macd_buffer )which is not initialized(allocated)

 

Thank you very much!
It works now!

All I had to do is change the number of buffers to 5 and add the line

    SetIndexBuffer(4, macd_buffer, INDICATOR_CALCULATIONS);


Cheers