//+------------------------------------------------------------------+ 
//| zLSMA                                                            | 
//| Copyright © 2004, MetaQuotes Software Corp.                      | 
//| http://www.metaquotes.net                                        | 
//| LSMA cleaned up                                                  | 
//| 2006.09.06 16:00                                                 | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright © 2005, FX Sniper " 
#property link "http://www.metaquotes.net/" 

//---- indicator settings 
#property indicator_chart_window 
#property indicator_buffers 5
#property indicator_color1 Yellow  //colors are numbered base 1 - why I don't know
#property indicator_color2 Lime
#property indicator_color3 Red 
#property indicator_color4 Black
#property indicator_color5 Black

extern int Rperiod = 34; 

//---- buffers 
double ExtMapBuffer0[];   //notice - buffers are numbered starting from 0 to match set index 
double ExtMapBuffer1[];   //and other zero based arrays
double ExtMapBuffer2[]; 
double sum[]; 
double wt[]; 

int width; 
int Draw4HowLong; 
int shift; 
int i; 
int loopbegin; 
int length; 
double lengthvar; 
double tmp ; 

//+------------------------------------------------------------------+ 
//| Custom indicator initialization function | 
//+------------------------------------------------------------------+ 
int init() { 
   //---- 2 additional buffers are used for counting. 
   IndicatorBuffers(5); 

   //---- drawing settings 
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2); 
   SetIndexBuffer(0,ExtMapBuffer0); 
   SetIndexLabel(0,"buf0");

   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2); 
   SetIndexBuffer(1,ExtMapBuffer1); 
   SetIndexLabel(1,"buf1");

   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2); 
   SetIndexBuffer(2,ExtMapBuffer2); 
   SetIndexLabel(2,"buf2");

   SetIndexStyle(3,DRAW_NONE,0,1); 
   SetIndexBuffer(3,sum); 
   SetIndexLabel(3,"sum");

   SetIndexStyle(4,DRAW_NONE,0,1); 
   SetIndexBuffer(4,wt); 
   SetIndexLabel(4,"wt");

   //---- initialization done 
   return(0); 
} 

int start() {
   Draw4HowLong = Bars - Rperiod - 5; 
   length = Rperiod; 
   loopbegin = Draw4HowLong - length - 1; 

   for(shift = loopbegin; shift >= 0; shift--) { 
      sum[1] = 0; 
      for(i = length; i >= 1 ; i--) { 
         lengthvar = length + 1; 
         lengthvar /= 3; 
         tmp = 0; 
         tmp = ( i - lengthvar)*Close[length-i+shift]; 
         sum[1]+=tmp; 
      } 
      wt[shift] = sum[1]*6/(length*(length+1)); 

      //========== COLOR CODING =========================================== 
      //yellow always paints
      //the higher numbered buffer paints on top of the lower (way to go MT!)
      ExtMapBuffer0[shift] = wt[shift]; //yellow
      ExtMapBuffer1[shift] = wt[shift]; //green 
      ExtMapBuffer2[shift] = wt[shift]; //red

      if (wt[shift+1] > wt[shift]) {            //current bar is lower, so it can not be green 
         ExtMapBuffer1[shift+1] = EMPTY_VALUE; 
      } else if (wt[shift+1] < wt[shift]) {     //current bar is higher, so it can not be red 
            ExtMapBuffer2[shift+1] = EMPTY_VALUE;
         } else {                               //current bar equals prev bar, so let it be yellow    
            ExtMapBuffer1[shift+1]=EMPTY_VALUE; //was this written by some left-handed lisdexic?
            ExtMapBuffer2[shift+1]=EMPTY_VALUE;
      } 
   } 
   return(0); 
} 
//+------------------------------------------------------------------+ 