Problems getting a plot buffer coming from the adding of two previous calculated buffers

 

Hi,

I'd really apreciate some help with the fixing of this code, that pretends to plot 3 curves: b,c and one named ku derived from some extra calculations with other arrays. Facts are that no problem getting the values of b and c and be shown on the chart, but I'm not able to see whats the problem wit the calculation of ku that is always represented as a line with value 0. 

Thanks in advance.

       
//--------------------------------------------------------------------
// userindicator.mq4
// The code should be used for educational purpose only.
//--------------------------------------------------------------------
#property indicator_separate_window    // Indicator is drawn in the main window
#property indicator_buffers 3       // Number of buffers
#property indicator_color1 Blue     // Color of the 1st line
#property indicator_color2 Red      // Color of the 2nd line
#property indicator_color3 Yellow      // Color of the 2nd line

// External variables
extern int length = 35;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double b[],c[],ku[];             // Declaring arrays (for indicator buffers)
double TR[],k[],r[];

//double MathMax(double value1, double value2)
//double MathMin(double value1, double value2)

double alpha = 1 / length;                    // Declaring values for calculate ku

//--------------------------------------------------------------------
int init()                          // Special function init()
  {
   SetIndexBuffer(0,b);             // Assigning an array to a buffer
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2); // Line style
   SetIndexLabel(0,"Slow Speed Line");
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   SetIndexBuffer(1,c);             // Assigning an array to a buffer
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1); // Line style
   SetIndexLabel(1,"Fast Primary Trend Line");
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   SetIndexBuffer(2,ku);            // Assigning an array to a buffer
   SetIndexStyle(2,DRAW_LINE,STYLE_DOT,2); // Line style
   SetIndexLabel(2,"No Trend Zone - Upperline");
   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   return;                          // Exit the special funct. init()
  }
//--------------------------------------------------------------------
int start()                         // Special function start()
  {
   int i,                           // Bar index
       Counted_bars;                // Number of counted bars
//--------------------------------------------------------------------
   Counted_bars=IndicatorCounted(); // Number of counted bars
   i=Bars-Counted_bars-1;           // Index of the first uncounted

   while(i>=0)                      // Loop for uncounted bars
     {
      b[i]= iMA(NULL,0,3,0,MODE_SMA,PRICE_CLOSE,i);  // Value of 0 buffer on i bar
      c[i]= iMA(NULL,0,18,0,MODE_EMA,PRICE_CLOSE,i); // Value of 1st buffer on i bar
      k[i]= iMA(NULL,0,length,0,MODE_EMA,PRICE_CLOSE,i);
      TR[i] = MathMax(High[i], Close[i+1]) - MathMin(Low[i], Close[i+1]);

      if(r[i] == EMPTY_VALUE)
        {
         r[i] = iMA(NULL,0,length,0,MODE_SMA,TR[i],i);
        }
      else
        {
         r[i] = alpha * TR[i] + (1 - alpha) * r[i+1];
        }

        ku[i] =  k[i] + r[i] * 0.5;


      i--;                          // Calculating index of the next bar
     }

//ObjectCreate("x",OBJ_LABEL,0,0,0);
//ObjectSet("x",OBJPROP_CORNER,CORNER_LEFT_UPPER);
//ObjectSet("x",OBJPROP_XDISTANCE,10);
//ObjectSet("x",OBJPROP_YDISTANCE,120);
//ObjectSetText("x","Sellindicator:"+DoubleToStr(k[i],Digits),20,"Arial",Red);

//--------------------------------------------------------------------
   return;                          // Exit the special funct. start()
  }
//--------------------------------------------------------------------
//+------------------------------------------------------------------+
 

Your problem is that arrays k[], r[], and TR[] have no size. You would know that if you had used strict.

Always use strict. Fixing the warnings will save you hours of debugging, but you must understand the differences.

You should stop using the old event handlers and IndicatorCounted() and start using new event handlers.
          Event Handling Functions - MQL4 Reference
          How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016)