//+------------------------------------------------------------------+
//|                                                 MA color nrp,mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property  copyright "mladen"
#property  link      ""

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 Yellow
#property indicator_color2 Green
#property indicator_color3 Green
#property indicator_color4 Orange
#property indicator_color5 Orange
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 2
#property indicator_width5 2

//
//
//
//
//

double ma[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];

//
//
//
//
//

extern int  MAPeriod      = 34;
extern int  MAMethod      = MODE_EMA;
extern int  MAShift       = 0;
extern int  MAPriceType   = 0;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int init()
{
   SetIndexBuffer(0,ma);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexBuffer(3,ExtMapBuffer4);
   SetIndexBuffer(4,ExtMapBuffer5);
   return(0);
}

//
//
//
//
//

int start()
{ 
   int      counted_bars=IndicatorCounted();
   int      limit,i;


   if(counted_bars < 0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = Bars-counted_bars;

   //
   //
   //
   //
   //
   
   if (ma[limit] > ma[limit+1]) CleanPoint(limit,ExtMapBuffer2,ExtMapBuffer3);
   if (ma[limit] < ma[limit+1]) CleanPoint(limit,ExtMapBuffer4,ExtMapBuffer5);

   //
   //
   //
   //
   //
      
   for(i = limit; i >= 0; i--) ma[i]=iMA(NULL,0,MAPeriod,MAShift,MAMethod,MAPriceType,i);
   for(i = limit; i >= 0; i--)
   {  
      ExtMapBuffer4[i] = EMPTY_VALUE;
      ExtMapBuffer5[i] = EMPTY_VALUE;
      ExtMapBuffer2[i] = EMPTY_VALUE;
      ExtMapBuffer3[i] = EMPTY_VALUE;
      
         //
         //
         //
         //
         //
            
         if (ma[i] > ma[i+1]) PlotPoint(i,ExtMapBuffer2,ExtMapBuffer3,ma);
         if (ma[i] < ma[i+1]) PlotPoint(i,ExtMapBuffer4,ExtMapBuffer5,ma);
   }
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void CleanPoint(int i,double& first[],double& second[])
{
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

//
//
//
//
//

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (first[i+1] == EMPTY_VALUE)
      {
         if (first[i+2] == EMPTY_VALUE) {
                first[i]   = from[i];
                first[i+1] = from[i+1];
                second[i]  = EMPTY_VALUE;
            }
         else {
                second[i]   =  from[i];
                second[i+1] =  from[i+1];
                first[i]    = EMPTY_VALUE;
            }
      }
   else
      {
         first[i]   = from[i];
         second[i]  = EMPTY_VALUE;
      }
}