Coding Questions

 

Hi,

I just coded a very simple indicator that give the range of candlesticks.  I want to create a moving average of that in the second buffer.  The moving average works but only on a forward basis.  I'll post the code below; if anyone can help so that the moving average works throughout the whole chart, that would be a huge help.  Here's the code:


#property indicator_separate_window

#property indicator_buffers 2

#property indicator_color1 Red

#property indicator_color2 Green

//---- input parameters

extern int       Smoothing=5;

//---- buffers

double ExtMapBuffer1[];

double ExtMapBuffer2[];

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int init()

  {

//---- indicators

   SetIndexStyle(0,DRAW_LINE);

   SetIndexBuffer(0,ExtMapBuffer1);

   SetIndexBuffer(1,ExtMapBuffer2);

//----

   return(0);

  }

//+------------------------------------------------------------------+

//| Custom indicator deinitialization function                       |

//+------------------------------------------------------------------+

int deinit()

  {

//----

   

//----

   return(0);

  }

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

int start()

  {

  

   int    Counted_bars=IndicatorCounted();

   int i, n;                        //Counting variables

   double m, x, y;                  //Calculation variables for second while loop

   double z = Smoothing;

   i=Bars-Counted_bars-1;           // Index of the first uncounted

   while(i>=0)                      // Loop for uncounted bars

//----

   {

      ExtMapBuffer1[i]= MathAbs(High[i]-Low[i]);      //First Buffer - Period Range

      

      

      while(n<=Smoothing)                             //Loop to Calculate smoothed values

         {

           m = MathAbs(High[n]-Low[n]);               //m is Period range

           x = (m+x);                                 //Running total of m

           

           n++;                                       //Counter

         }

      y = (x/z);                                      //Buffer value - Running total divided by period of calculation

   

     

      ExtMapBuffer2[i]= y;                            //Second buffer - Moving average of Period range for n periods

      i--;                                            //Counter

   }

    

    

//----

   return;

  }



Is this the best method, or is it easier to take the value from the first buffer and plug it into a moving average calculation?  Any help is greatly appreciated!


Best,


Evan