//+------------------------------------------------------------------+
//|                                                MA_In_Color.mq4   |
//|                      Copyright � 2004, MetaQuotes Software Corp. |
//| Modified from LSMA_In_Color to use any MA by Robert Hill         |     
//+------------------------------------------------------------------+
#property  copyright "Copyright � 2006, FX Sniper and Robert Hill"
#property  link      "http://www.metaquotes.net/"

//---- indicator settings

#property  indicator_chart_window
#property  indicator_buffers 3
#property indicator_color1 Yellow      
#property indicator_color2 Aqua
#property indicator_color3 Magenta

extern int       MAPeriod=5;
extern string note1 = "Type: SMA=0, EMA=1, SMMA=2, LWMA=3";
extern int       MAType      = 1;
extern int       myLineWidth = 3;
extern int       myBoxWidth  = 3;
//---- buffers

double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
/*

PARM6 FOR MA = 

PRICE_CLOSE	0	Close price.
PRICE_OPEN	1	Open price.
PRICE_HIGH	2	High price.
PRICE_LOW	3	Low price.
PRICE_MEDIAN	4	Median price, (high+low)/2.
PRICE_TYPICAL	5	Typical price, (high+low+close)/3.
PRICE_WEIGHTED	6	Weighted close price, (high+low+close+close)/4.


PARM3 FOR MA = 
MODE_SMA	0	Simple moving average,
MODE_EMA	1	Exponential moving average,
MODE_SMMA	2	Smoothed moving average,
MODE_LWMA	3	Linear weighted moving average.

*/
//---- variables

int    MAMode;
string strMAType;

string tShortName ;

color ColorMA ;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(3);
   
//---- drawing settings
   SetIndexBuffer(2,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(0,ExtMapBuffer3);
   
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,myLineWidth);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,myLineWidth);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,myLineWidth);

switch (MAType)
   {
      case 1: strMAType="EMA"; MAMode=MODE_EMA; break;
      case 2: strMAType="SMMA"; MAMode=MODE_SMMA; break;
      case 3: strMAType="LWMA"; MAMode=MODE_LWMA; break;
      case 4: strMAType="LSMA"; break;
      default: strMAType="SMA"; MAMode=MODE_SMA; break;
   }
   tShortName = "maic"+strMAType+ " (" +MAPeriod + ") " ;
   IndicatorShortName(tShortName);
//---- initialization done
   return(0);
  }

double LSMA(int Rperiod, int shift)
{
   int i;
   double sum;
   int length;
   double lengthvar;
   double tmp;
   double wt;

   length = Rperiod;
 
   sum = 0;
   for(i = length; i >= 1  ; i--)
   {
     lengthvar = length + 1;
     lengthvar /= 3;
     tmp = 0;
     tmp = ( i - lengthvar)*Close[length-i+shift];
     sum+=tmp;
    }
    wt = sum*6/(length*(length+1));
    
    return(wt);
}

int deinit()
{ 
ObjectDelete(tShortName);  
                          
return(0);
}

int start()

  {
  
   double MA_Cur, MA_Prev;
   int limit;
   int counted_bars = IndicatorCounted();
   //---- check for possible errors
   if (counted_bars<0) return(-1);
   //---- last counted bar will be recounted
   if (counted_bars>0) counted_bars--;
   limit = Bars - counted_bars;

   for(int i=limit; i>=0; i--)
   {
      if (MAType == 4)
      {
        MA_Cur = LSMA(MAPeriod,i);
        MA_Prev = LSMA(MAPeriod,i+1);
      }
      else
      {
        MA_Cur = iMA(NULL,0,MAPeriod,0,MAMode,PRICE_CLOSE,i);
        MA_Prev = iMA(NULL,0,MAPeriod,0,MAMode,PRICE_CLOSE,i+1);
      }
 
         
//========== COLOR CODING ===========================================               
        
       ExtMapBuffer3[i] = MA_Cur; //red 
       ExtMapBuffer2[i] = MA_Cur; //green
       ExtMapBuffer1[i] = MA_Cur; //yellow
       
        if (MA_Prev > MA_Cur)
        {
        ExtMapBuffer2[i] = EMPTY_VALUE;
        ColorMA = indicator_color3 ;
        }
       else if (MA_Prev < MA_Cur) 
        {
        ExtMapBuffer1[i] = EMPTY_VALUE; //-1 red/greem tight
        ColorMA = indicator_color2 ;
        }
         else 
         {
         ColorMA = indicator_color1 ;
         ExtMapBuffer1[i]=EMPTY_VALUE;//EMPTY_VALUE;
         ExtMapBuffer2[i]=EMPTY_VALUE;//EMPTY_VALUE;
         }
        
      }
    MessageBox(DoubleToStr(ExtMapBuffer1[0], Digits));
    MessageBox(DoubleToStr(ExtMapBuffer2[0], Digits));
    
    if (ObjectFind(tShortName) != 0)
      {
          ObjectCreate(tShortName,OBJ_ARROW,0,Time[0],MA_Cur);
          ObjectSet(tShortName,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tShortName,OBJPROP_COLOR,ColorMA);  
          ObjectSet(tShortName,OBJPROP_WIDTH,myBoxWidth);  
      } 
      else
      {
         ObjectMove(tShortName,0,Time[0],MA_Cur);
         ObjectSet(tShortName,OBJPROP_COLOR,ColorMA);  
      }
     
    
      return(0);
  }
//+------------------------------------------------------------------+