//+------------------------------------------------------------------+
//|                                                        Copyright |
//|                                                           Palmer |
//|                                              www.fxtradeblog.com |
//+------------------------------------------------------------------+

#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1 C'255,255,0'
#property indicator_color2 C' 90,185, 90' 
#property indicator_color3 C' 23,139, 23' 
#property indicator_color4 C'255,255,255' 
#property indicator_color5 C'255,185,185' 
#property indicator_color6 C'255,115,115' 
#property indicator_color7 C'255, 76, 76' 
#property indicator_color8 C'255,32,32' 


extern int    fast=8;
extern int    slow=21;
extern double threshold=     0.0024;

//---- buffers
   double     ExtBuffer0[];   
   double     ExtBuffer1[];
   double     ExtBuffer2[];
   double     ExtBuffer3[]; 
   double     ExtBuffer4[];   
   double     ExtBuffer5[];
   double     ExtBuffer6[]; 
   double     ExtBuffer7[];
   double     ExtBuffer8[]; 
    

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int init()
{
   SetIndexBuffer(0,ExtBuffer0);
   SetIndexBuffer(1,ExtBuffer1);
   SetIndexBuffer(2,ExtBuffer2);
   SetIndexBuffer(3,ExtBuffer3);
   SetIndexBuffer(4,ExtBuffer4);
   SetIndexBuffer(5,ExtBuffer5);
   SetIndexBuffer(6,ExtBuffer6);
   SetIndexBuffer(7,ExtBuffer7);
   
   SetIndexStyle(0,DRAW_HISTOGRAM, EMPTY, 4, C'255,255,0');//yellow
   SetIndexStyle(1,DRAW_HISTOGRAM, EMPTY, 4, C'90,185,90');
   SetIndexStyle(2,DRAW_HISTOGRAM, EMPTY, 4, C'57,128,22'); //dark green
   SetIndexStyle(3,DRAW_HISTOGRAM, EMPTY, 4, C'255,255,255'); //white
   SetIndexStyle(4,DRAW_HISTOGRAM, EMPTY, 4, C'255,185,185');
   SetIndexStyle(5,DRAW_HISTOGRAM, EMPTY, 4, C'255,115,115');
   SetIndexStyle(6,DRAW_HISTOGRAM, EMPTY, 4, C'255,76,76');
   SetIndexStyle(7,DRAW_HISTOGRAM, EMPTY, 4, C'255,32,32'); //dark red
   
   for (int i=0;i<indicator_buffers;i++) SetIndexLabel(i,NULL);   


   IndicatorShortName("821Gap");
   return(1);
}

int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int start()
  {
  
  
  
   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--;
   
   int    pos=Bars-counted_bars;
 
 
   double em1diffbull;
   double em1diffbear;
   double em8;
   double em21;
   double em1h;
   double em1l;

   
  
//---- main calculation loop
   while(pos>=0)
     {          
         em1h = iMA(NULL,0,1,0,MODE_EMA,PRICE_HIGH,pos);
         em1l = iMA(NULL,0,1,0,MODE_EMA,PRICE_LOW,pos);
         em8 = iMA(NULL,0,fast,0,MODE_EMA,PRICE_CLOSE,pos);    
         em21 = iMA(NULL,0,slow,0,MODE_EMA,PRICE_CLOSE,pos);
         if(em8!=0) em1diffbull = (em1l-em8)/em8;
         if(em1h!=0)  em1diffbear = (em8-em1h)/em1h;
   
         
         // ** Draw Blocks ** //      
        
        
        
                if (em8 < em21)
                   {drawblock(0,0,0,0,0,0,0,5,pos);}//RED
                
               if (em8 < em21 && em1h < em8 && em1diffbear > 0.0024)
                  {drawblock(5,0,0,0,0,0,0,0,pos);}//YELLOW
                          
              if (em8 > em21)
                 {drawblock(0,0,5,0,0,0,0,0,pos);}//DARK GREEN
           
              if(em8 > em21 && em1l > em8 && em1diffbull > 0.0024)
                {drawblock(5,0,0,0,0,0,0,0,pos);}//YELLOW
           
            
       
           
         pos--;
     }
//----
   return(0);
  }
      
void drawblock(int a,int b,int c,int d,int e,int f,int g,int h,int pos)

{
         
               ExtBuffer0[pos]=a;    
               ExtBuffer1[pos]=b;
               ExtBuffer2[pos]=c;
               ExtBuffer3[pos]=d;
               ExtBuffer4[pos]=e;
               ExtBuffer5[pos]=f;
               ExtBuffer6[pos]=g;
               ExtBuffer7[pos]=h;
} 