//+------------------------------------------------------------------+
//|                           Moving Average (Directional Color).mq4 |
//|                                  Copyright © 2006, BundyRaider |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, BundyRaider"
#property link      ""

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Yellow
#property indicator_color2 DodgerBlue
#property indicator_color3 Salmon

#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2

//---- input parameters

  /*
   *****  Moving Average Types   *****
   
      0  =  MODE_SMA          Simple moving average, 
      1  =  MODE_EMA          Exponential moving average, 
      2  =  MODE_SMMA         Smoothed moving average, 
      3  =  MODE_LWMA         Linear weighted moving average. 
 */
 /*
   *****     Price Constant Types    *****
      
      0  =  PRICE_CLOSE       Close price. 
      1  =  PRICE_OPEN        Open price. 
      2  =  PRICE_HIGH        High price. 
      3  =  PRICE_LOW         Low price. 
      4  =  PRICE_MEDIAN      Median price, (high+low)/2. 
      5  =  PRICE_TYPICAL     Typical price, (high+low+close)/3. 
      6  =  PRICE_WEIGHTED    Weighted close price, (high+low+close+close)/4. 
 */
extern int        MAPeriod=5;
extern int        MAType=2;
extern int        MAPriceConstant=0;
extern int        MAShift=0;
//---- buffers

double IndicatorBase[];
double IndicatorUp[];
double IndicatorDown[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,IndicatorBase);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,IndicatorUp);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,IndicatorDown);
   
   SetIndexEmptyValue(0,NULL);
   SetIndexEmptyValue(1,NULL);
   SetIndexEmptyValue(2,NULL);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   
   if(counted_bars<0) return(-1);
   //---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   //********************************************************************
   //          Main Loop
   //********************************************************************
   
   
   for(int i=0; i<limit; i++)
     {
      double MovingAverage          =  iMA(NULL,0,MAPeriod,MAShift,MAType,MAPriceConstant,i);
      double PreviousMovingAverage  =  iMA(NULL,0,MAPeriod,MAShift,MAType,MAPriceConstant,i+1);
      
      IndicatorBase[i]=MovingAverage;
      IndicatorUp[i]  =MovingAverage;
      IndicatorDown[i]=MovingAverage;

      if(MovingAverage>PreviousMovingAverage)
        {
         IndicatorDown[i]=NULL;
        }
      if(MovingAverage<PreviousMovingAverage)
        {
         IndicatorUp[i]=NULL;
        }

     }
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+ 