//+------------------------------------------------------------------+ //| CCIAverage.mq4 | //| Copyright © 2009, Maxim Kovalevskiy | //| email: caspermax@gmail.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2009, Maxim Kovalevskiy" #property link "caspermax@gmail.com" //---- #property indicator_separate_window #property indicator_buffers 2 #property indicator_color1 LightSeaGreen #property indicator_color2 Red //---- input parameters extern int CCIPeriod = 14; extern int AveragePeriod = 14; //---- buffers double CCIBuffer[]; double AverageBuffer[]; double RelBuffer[]; double DevBuffer[]; double MovBuffer[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { string short_name; //---- 3 additional buffers are used for counting. IndicatorBuffers(5); SetIndexBuffer(2, RelBuffer); SetIndexBuffer(3, DevBuffer); SetIndexBuffer(4, MovBuffer); //---- indicator lines SetIndexStyle(0, DRAW_LINE); SetIndexBuffer(0, CCIBuffer); SetIndexStyle(1, DRAW_LINE); SetIndexBuffer(1, AverageBuffer); //---- if(CCIPeriod <= 0) CCIPeriod = 14; if(AveragePeriod <= 0) AveragePeriod = 14; //---- SetIndexDrawBegin(0, CCIPeriod); SetIndexDrawBegin(0, AveragePeriod); //---- name for DataWindow and indicator subwindow label short_name="CCI(" + CCIPeriod + ") Average("+ AveragePeriod +")"; IndicatorShortName(short_name); SetIndexLabel(0, short_name); //---- return(0); } //+------------------------------------------------------------------+ //| Commodity Channel Index | //+------------------------------------------------------------------+ int start() { int i, k, counted_bars = IndicatorCounted(); double price, sum, mul; if(CCIPeriod <= 1) return(0); if(Bars <= CCIPeriod) return(0); if(AveragePeriod <= 1) return(0); if(Bars <= AveragePeriod) return(0); //---- initial zero if(counted_bars < 1) { for(i = 1; i <= CCIPeriod; i++) CCIBuffer[Bars-i] = 0.0; for(i = 1; i <= CCIPeriod; i++) DevBuffer[Bars-i] = 0.0; for(i = 1; i <= CCIPeriod; i++) MovBuffer[Bars-i] =0.0; } //---- last counted bar will be recounted counted_bars = IndicatorCounted(); if(counted_bars < 0) return(-1); if(counted_bars > 0) counted_bars--; int limit = Bars - counted_bars; if(counted_bars==0) limit-=1+MathMax(CCIPeriod,AveragePeriod); //---- moving average for(i = 0; i < limit; i++) MovBuffer[i] = iMA(NULL, 0, CCIPeriod, 0, MODE_SMA, PRICE_TYPICAL, i); //---- standard deviations i=limit; mul = 0.015 / CCIPeriod; while(i >= 0) { sum = 0.0; k = i + CCIPeriod - 1; while(k >= i) { price =(High[k] + Low[k] + Close[k]) / 3; sum += MathAbs(price - MovBuffer[i]); k--; } DevBuffer[i] = sum*mul; i--; } i=limit; while(i >= 0) { price = (High[i] + Low[i] + Close[i]) / 3; RelBuffer[i] = price - MovBuffer[i]; i--; } //---- cci counting i=limit; while(i >= 0) { if(DevBuffer[i] == 0.0) CCIBuffer[i] = 0.0; else CCIBuffer[i] = RelBuffer[i] / DevBuffer[i]; i--; } //---- i=limit; while(i >= 0) { int averagesum = 0; for (k=0; k < AveragePeriod; k++) { averagesum = averagesum + CCIBuffer[i+k]; } AverageBuffer[i] = averagesum / AveragePeriod; i--; } //---- average counting //---- return(0); } //+------------------------------------------------------------------+