//+------------------------------------------------------------------+
//|                                           GH-CCI BAR CHART       |      
//|                             Copyright c 2009, Godfreyh@gmail.com |
//|                                  REFRESH ISSUES FIXED BY Obaidah |
//|                                        http://www.metaquotes.net |
//|  26/03/12 mod poruchik                 http://www.onix-trade.net |
//+------------------------------------------------------------------+
//| take care that this indicator doesnt resize the trendline in the 
//|indicator window, for that reason,if you run that in real time 
//|(before the bar close), it will give you different high/low than 
//|when you run it after the bar close
//+-------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 4

extern 	color 	OneColor = Blue,
				      UpColor = DodgerBlue,
			      	DnColor = Red;



extern 	bool UseOneColor = false;
extern int CCI_Period =14;

//----

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|
int init()
  {

IndicatorBuffers(4); 

//   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0, ExtMapBuffer1);//high/low buffer
//   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(1, ExtMapBuffer2); //high/low buffer
//   SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexBuffer(2, ExtMapBuffer3); //open buffer
//   SetIndexStyle(3,DRAW_HISTOGRAM);
   SetIndexBuffer(3, ExtMapBuffer4);// close buffer
//----
//---- indicator buffers mapping
	IndicatorShortName("GH CCI BAR CHARTv2"); 

   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
  ObjectsDeleteAll(); 

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   // take care that this indicator doesnt resize the trendline in the indicator window, for that reason, 
   //if you run that in real time (before the bar close), it will give you different high/low than when you run it 
   //after the bar close
   double cciOpen, cciHigh, cciLow, cciClose;
   if(Bars<=10) return(0);
   ExtCountedBars=IndicatorCounted();
//---- check for isible errors
   if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
   if (ExtCountedBars>0) ExtCountedBars--;
   int i=Bars-ExtCountedBars-1; // when Bars increases by 1 i will be 1, in the next time extcountedbars will 
   //increase by one and i will be equal to 0, when a new bar comes the value of Bars will increase by one and so,
   //the value of i will be 1 again
   while(i>=0)
     {
      cciOpen= iCCI(NULL,0,CCI_Period,PRICE_OPEN,i);
      cciClose=iCCI(NULL,0,CCI_Period,PRICE_CLOSE,i);
      cciHigh= iCCI(NULL,0,CCI_Period,PRICE_HIGH,i);
      cciLow=  iCCI(NULL,0,CCI_Period,PRICE_LOW,i);
    
      if (cciOpen<cciClose) 
        {
         ExtMapBuffer1[i]=cciLow;
         ExtMapBuffer2[i]=cciHigh;
        } 
      else
        {
         ExtMapBuffer1[i]=cciHigh;
         ExtMapBuffer2[i]=cciLow;
        } 
      ExtMapBuffer3[i]=cciOpen;
      ExtMapBuffer4[i]=cciClose;
 	   
 	   
 	   		if(UseOneColor)//   double cciOpen, cciHigh, cciLow, cciClose;
		{
			drawLine("BB"+Time[i],Time[i],cciOpen,cciClose,3,OneColor); // Time[i] instead of i, for, during the real time 
			//calculating, the value of i will range between 1 and 0, so you'll have the same name for each new bar 
			//.... in the draw function it will skipe drawing new one because of the first if statement
			//So we had to assine something uniqe to the name of the line, which is the time of that bar.
			drawLine("SB"+Time[i],Time[i],cciHigh,cciLow,1,OneColor);
		}

 		else
		{	
			if(cciClose<cciOpen)
			{
				drawLine("BB"+Time[i],Time[i],cciOpen,cciClose,3,DnColor);
				drawLine("SB"+Time[i],Time[i],cciHigh,cciLow,1,DnColor);
			}
			else
			{
				drawLine("BB"+Time[i],Time[i],cciOpen,cciClose,3,UpColor);
				drawLine("SB"+Time[i],Time[i],cciHigh,cciLow,1,UpColor);
			}			
		}			
	   
 	   
 	   
 	   
 	   i--;
     }
     
     
//----
   return(0);
  }
  
  
//----  
  
  
void drawLine(string name,datetime time, double pfrom, double pto, int width,color Col)
{
         if(ObjectFind(name) != 0)
         {
            ObjectCreate(name, OBJ_TREND, WindowFind("GH CCI BAR CHARTv2"), time, pfrom,time,pto);
            ObjectSet(name, OBJPROP_STYLE, STYLE_SOLID);
            ObjectSet(name, OBJPROP_COLOR, Col);
            ObjectSet(name,OBJPROP_WIDTH,width);
            ObjectSet(name,OBJPROP_RAY,0);
         }
         else
         {
            ObjectDelete(name);
            ObjectCreate(name, OBJ_TREND, WindowFind("GH CCI BAR CHARTv2"), time, pfrom,time,pto);
            ObjectSet(name, OBJPROP_STYLE, STYLE_SOLID);
            ObjectSet(name, OBJPROP_COLOR, Col);        
            ObjectSet(name,OBJPROP_WIDTH,width);
            ObjectSet(name,OBJPROP_RAY,0);
          
         }
}  
  
  
  
//+------------------------------------------------------------------+