//+------------------------------------------------------------------+
//|                                             Brooky_CCI_Shade.mq4 |
//|                      Copyright © 2010, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 SteelBlue
#property indicator_color2 OrangeRed
#property indicator_color3 Gold


#property indicator_width1 5
#property indicator_width2 5
#property indicator_width3 5
#property indicator_width4 2

#property indicator_level1 0

#import "user32.dll"
   int GetDC(int hwnd);
   int ReleaseDC(int hwnd,int hdc);
#import "gdi32.dll"
   color GetPixel(int hdc,int x,int y);

//---- input parameters
extern string NOTE  = "+--DLL Must Be Allowed--+";
extern string NOTE2 = "+--For Auto Clear Backgound--+";
extern string Coder = "+Brooky-Indicators.com+";
extern string settings = "+--Select Period & Levels--+";
extern int       CCI_Period=14;
extern int       OBought=100;
extern int       OSold=-100;
extern string colors = "+--Select Colors for Levels--+";
extern color oblevel = Blue;
extern color oslevel = Red;
//---- buffers
double CCIbuff[];

double obbuff[];

double osbuff[];

double blankbuff[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   
   
   SetIndexBuffer(0,obbuff);SetIndexStyle(0,DRAW_HISTOGRAM);
   
   SetIndexBuffer(1,osbuff); SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_DOT);
  
   SetIndexBuffer(2,blankbuff); 
  
   SetIndexStyle(3,DRAW_LINE);SetIndexBuffer(3,CCIbuff);
   
   IndicatorShortName("Brooky_CCI_Shade ("+CCI_Period+")");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
    ObjectDelete("cob");
        ObjectDelete("cos");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
  int start()
    {
     int limit;
     int counted_bars=IndicatorCounted();
  //---- check for possible errors
     if(counted_bars<0) return(-1);
  //---- the last counted bar will be recounted
     if(counted_bars>0) counted_bars--;
     limit=Bars-counted_bars;
     double CCInow;
     
   int hwnd=WindowHandle(Symbol(),Period());
   int hdc=GetDC(hwnd);
   color back_ground=GetPixel(hdc,1,2);
   ReleaseDC(hwnd,hdc);
   SetIndexStyle(2,DRAW_HISTOGRAM,0,EMPTY,back_ground);
  //---- main loop
     for(int i=0; i<limit; i++)
       {
       obbuff[i]   =EMPTY_VALUE;
       osbuff[i]   =EMPTY_VALUE;
       blankbuff[i]=EMPTY_VALUE;
       

       CCInow= iCCI(NULL,0,CCI_Period,PRICE_CLOSE,i);
       CCIbuff[i]=CCInow;
       
       if(CCInow<=OSold)
              {
                obbuff[i]   =EMPTY_VALUE;
                osbuff[i]   =CCInow;
                blankbuff[i]=OSold;
              }
       if(CCInow>=OBought)
              {
                obbuff[i]   =CCInow;
                osbuff[i]   =EMPTY_VALUE;
                blankbuff[i]=OBought;
              }
       string obline = "cob";
              int windowIndex=WindowFind("Brooky_CCI_Shade ("+CCI_Period+")");
              ObjectDelete("cob"+i);
              ObjectCreate(obline, OBJ_HLINE, windowIndex, Time[i],OBought);
              ObjectSet(obline, OBJPROP_STYLE, 1);
              ObjectSet(obline, OBJPROP_COLOR,oblevel);       
       string osline = "cos";
              int windowIndex2=WindowFind("Brooky_CCI_Shade ("+CCI_Period+")");
              ObjectDelete("cos"+i);
              ObjectCreate(osline, OBJ_HLINE, windowIndex2, Time[i],OSold);
              ObjectSet(osline, OBJPROP_STYLE, 1);
              ObjectSet(osline, OBJPROP_COLOR,oslevel);        
       }
  //---- done
     return(0);
    }
//+------------------------------------------------------------------+