//+------------------------------------------------------------------+
//|                                           Awesome 4 color v1.mq4 |
//|                              Awesome 4 color modfications by cja |
//+------------------------------------------------------------------+
#property  copyright "Copyright © 2005, MetaQuotes Software Corp."
#property  link      "http://www.metaquotes.net/"
//---- indicator settings
#property  indicator_separate_window
#property description "4 color code by CJA"
#property strict

#property  indicator_buffers 4
#property  indicator_color1  clrLimeGreen
#property  indicator_color2  clrGreen
#property  indicator_color3  clrRed
#property  indicator_color4  clrMaroon
#property  indicator_width1  2
#property  indicator_width2  2
#property  indicator_width3  2
#property  indicator_width4  2
//---- indicator buffers

extern int Number_of_Bars  = 1000;//Number of Bars to Display

int i=0;
double     ind_buffer1[], ind_buffer1s[];
double     ind_buffer2[], ind_buffer2s[];
double     ind_buffer3[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 1 additional buffer used for counting.
   IndicatorBuffers(5);
   //---- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexStyle(3,DRAW_HISTOGRAM);
   
   IndicatorDigits(6);
   SetIndexDrawBegin(0,34);
   SetIndexDrawBegin(1,34);
   SetIndexDrawBegin(3,34);
   SetIndexDrawBegin(4,34);
//---- 3 indicator buffers mapping
 SetIndexBuffer(0,ind_buffer1);
 SetIndexBuffer(1,ind_buffer1s);
 SetIndexBuffer(2,ind_buffer2);
 SetIndexBuffer(3,ind_buffer2s);
 SetIndexBuffer(4,ind_buffer3);
 
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("Awesome 4 color");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Awesome Oscillator                                               |
//+------------------------------------------------------------------+
int start()
  {
   int    limit;
   int    counted_bars=IndicatorCounted();
   double prev,current;
//---- 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;
   if (limit > Number_of_Bars) limit = Number_of_Bars;
      ind_buffer1[Number_of_Bars] =EMPTY_VALUE;
      ind_buffer1s[Number_of_Bars]=EMPTY_VALUE;
      ind_buffer2[Number_of_Bars] =EMPTY_VALUE;
      ind_buffer2s[Number_of_Bars]=EMPTY_VALUE;
      ind_buffer3[Number_of_Bars] =EMPTY_VALUE;
      ind_buffer3[Number_of_Bars] =EMPTY_VALUE;

//---- macd counted in the 1-st additional buffer
   for(i=0; i<limit; i++)
     ind_buffer3[i]=iMA(NULL,0,5,0,MODE_SMA,PRICE_MEDIAN,i)-iMA(NULL,0,34,0,MODE_SMA,PRICE_MEDIAN,i);

//---- dispatch values between 2 buffers
   bool up=true;
   for(i=limit-1; i>=0; i--)
     {
      current=ind_buffer3[i];
      prev=ind_buffer3[i+1];
      if (((current<0)&&(prev>0))||(current<0))   up= false;    
      if (((current>0)&&(prev<0))||(current>0))   up= true;      
      
      if(!up)
        {
         if(current > prev)
           {
            ind_buffer2s[i]=current;
            ind_buffer2[i]=0.0;
            ind_buffer1[i]=0.0;
            ind_buffer1s[i]=0.0;             
           }        
         else
           {
            ind_buffer2[i]=current;
            ind_buffer2s[i]=0.0;
            ind_buffer1[i]=0.0;
            ind_buffer1s[i]=0.0;            
           }             
        }
        else
        {
         if(current < prev)
           {
            ind_buffer1s[i]=current;
            ind_buffer1[i]=0.0;
            ind_buffer2[i]=0.0;
            ind_buffer2s[i]=0.0;
           }
        else
           {
            ind_buffer1[i]=current;
            ind_buffer1s[i]=0.0;
            ind_buffer2[i]=0.0;
            ind_buffer2s[i]=0.0;
            }           
         }       
     }
//---- done
   return(0);
  }

