//--------------------------FX5 edited by FOREXflash------------------------------ #property indicator_separate_window #property indicator_buffers 4 #property indicator_color1 Green #property indicator_color2 Red #property indicator_color3 Magenta #property indicator_color4 Blue //---- #define arrowsDisplacement 0.0001 //---- input parameters extern string separator1 = "*** CCI Settings ***"; extern int fastEMA = 12; extern int slowEMA = 26; extern int signalSMA = 9; extern string separator2 = "*** Indicator Settings ***"; extern bool drawIndicatorTrendLines = true; extern bool drawPriceTrendLines = true; extern bool displayAlert = true; //---- buffers double bullishDivergence[]; double bearishDivergence[]; double CCI[]; double signal[]; //---- static datetime lastAlertTimePk; // last alert for a Peak static datetime lastAlertTimeTr; // last alert for a Trough static string indicatorName; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators SetIndexStyle(0, DRAW_ARROW); SetIndexStyle(1, DRAW_ARROW); SetIndexStyle(2, DRAW_LINE); SetIndexStyle(3, DRAW_LINE); //---- SetIndexBuffer(0, bullishDivergence); SetIndexBuffer(1, bearishDivergence); SetIndexBuffer(2, CCI); SetIndexBuffer(3, signal); //---- SetIndexArrow(0, 233); SetIndexArrow(1, 234); //---- indicatorName = "CCI_Divergence(" + fastEMA + ")"; SetIndexDrawBegin(3, signalSMA); IndicatorDigits(Digits + 2); IndicatorShortName(indicatorName); return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { for(int i = ObjectsTotal() - 1; i >= 0; i--) { string label = ObjectName(i); if(StringSubstr(label, 0, 18) != "CCI_DivergenceLine") continue; ObjectDelete(label); } return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { int countedBars = IndicatorCounted(); if(countedBars < 0) countedBars = 0; CalculateIndicator(countedBars); //---- return(0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CalculateIndicator(int countedBars) { for(int i = Bars - countedBars; i >= 0; i--) { CalculateCCI(i); CatchBullishDivergence(i + 1); //IHF used to be i + 2 CatchBearishDivergence(i + 1); //IHF used to be i + 2 } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CalculateCCI(int i) { CCI[i] = iCCI(Symbol(),0,fastEMA,PRICE_TYPICAL,i); signal[i] = iCCI(Symbol(),0,fastEMA,PRICE_TYPICAL,i); //IHF a real signal isn't used in CCI (except for Woodies where use signal period =6 with standerd period = 14) } //IHF this index just seems to be used for detecting last peak and last trough //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CatchBullishDivergence(int shift) { if(IsIndicatorTrough(shift) == false) return; int currentTrough = shift; int lastTrough = GetIndicatorLastTrough(shift); //---- if(CCI[currentTrough] > CCI[lastTrough] && Low[currentTrough] < Low[lastTrough]) { bullishDivergence[currentTrough] = CCI[currentTrough] - arrowsDisplacement; //---- if(drawPriceTrendLines == true) DrawPriceTrendLine(Time[currentTrough], Time[lastTrough], Low[currentTrough], Low[lastTrough], Green, STYLE_SOLID); //---- if(drawIndicatorTrendLines == true) DrawIndicatorTrendLine(Time[currentTrough], Time[lastTrough], CCI[currentTrough], CCI[lastTrough], Green, STYLE_SOLID); //---- if(displayAlert == true) DisplayAlert("CCI Classical bullish divergence on: ", currentTrough, False); // was not a peak } //---- if(CCI[currentTrough] < CCI[lastTrough] && Low[currentTrough] > Low[lastTrough]) { bullishDivergence[currentTrough] = CCI[currentTrough] - arrowsDisplacement; //---- if(drawPriceTrendLines == true) DrawPriceTrendLine(Time[currentTrough], Time[lastTrough], Low[currentTrough], Low[lastTrough], Green, STYLE_DOT); //---- if(drawIndicatorTrendLines == true) DrawIndicatorTrendLine(Time[currentTrough], Time[lastTrough], CCI[currentTrough], CCI[lastTrough], Green, STYLE_DOT); //---- if(displayAlert == true) DisplayAlert("CCI Hidden bullish divergence on: ", currentTrough, False); // was not a peak } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CatchBearishDivergence(int shift) { if(IsIndicatorPeak(shift) == false) return; int currentPeak = shift; int lastPeak = GetIndicatorLastPeak(shift); //---- if(CCI[currentPeak] < CCI[lastPeak] && High[currentPeak] > High[lastPeak]) { bearishDivergence[currentPeak] = CCI[currentPeak] + arrowsDisplacement; if(drawPriceTrendLines == true) DrawPriceTrendLine(Time[currentPeak], Time[lastPeak], High[currentPeak], High[lastPeak], Red, STYLE_SOLID); if(drawIndicatorTrendLines == true) DrawIndicatorTrendLine(Time[currentPeak], Time[lastPeak], CCI[currentPeak], CCI[lastPeak], Red, STYLE_SOLID); if(displayAlert == true) DisplayAlert("CCI Classical bearish divergence on: ", currentPeak, True); // was a peak } if(CCI[currentPeak] > CCI[lastPeak] && High[currentPeak] < High[lastPeak]) { bearishDivergence[currentPeak] = CCI[currentPeak] + arrowsDisplacement; //---- if(drawPriceTrendLines == true) DrawPriceTrendLine(Time[currentPeak], Time[lastPeak], High[currentPeak], High[lastPeak], Red, STYLE_DOT); //---- if(drawIndicatorTrendLines == true) DrawIndicatorTrendLine(Time[currentPeak], Time[lastPeak], CCI[currentPeak], CCI[lastPeak], Red, STYLE_DOT); //---- if(displayAlert == true) DisplayAlert("CCI Hidden bearish divergence on: ", currentPeak, True); // was a peak } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool IsIndicatorPeak(int shift) { if(CCI[shift] >= CCI[shift+1] && CCI[shift] > CCI[shift+2] && CCI[shift] > CCI[shift-1]) return(true); else return(false); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool IsIndicatorTrough(int shift) { if(CCI[shift] <= CCI[shift+1] && CCI[shift] < CCI[shift+2] && CCI[shift] < CCI[shift-1]) return(true); else return(false); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int GetIndicatorLastPeak(int shift) { for(int i = shift + 5; i < Bars; i++) { if(signal[i] >= signal[i+1] && signal[i] >= signal[i+2] && signal[i] >= signal[i-1] && signal[i] >= signal[i-2]) { for(int j = i; j < Bars; j++) { if(CCI[j] >= CCI[j+1] && CCI[j] > CCI[j+2] && CCI[j] >= CCI[j-1] && CCI[j] > CCI[j-2]) return(j); } } } return(-1); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int GetIndicatorLastTrough(int shift) { for(int i = shift + 5; i < Bars; i++) { if(signal[i] <= signal[i+1] && signal[i] <= signal[i+2] && signal[i] <= signal[i-1] && signal[i] <= signal[i-2]) { for (int j = i; j < Bars; j++) { if(CCI[j] <= CCI[j+1] && CCI[j] < CCI[j+2] && CCI[j] <= CCI[j-1] && CCI[j] < CCI[j-2]) return(j); } } } return(-1); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void DisplayAlert(string message, int shift, bool peak) {if (peak){ if(shift <= 2 && Time[shift] != lastAlertTimePk) { lastAlertTimePk = Time[shift]; Alert(message, Symbol(), " , ", Period(), " minutes chart"); } } // was a Peak if (!peak){ if(shift <= 2 && Time[shift] != lastAlertTimeTr) { lastAlertTimeTr = Time[shift]; Alert(message, Symbol(), " , ", Period(), " minutes chart"); } } // not a Peak } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void DrawPriceTrendLine(datetime x1, datetime x2, double y1, double y2, color lineColor, double style) { string label = "CCI_DivergenceLine" + DoubleToStr(x1, 0); ObjectDelete(label); ObjectCreate(label, OBJ_TREND, 0, x1, y1, x2, y2, 0, 0); ObjectSet(label, OBJPROP_RAY, 0); ObjectSet(label, OBJPROP_COLOR, lineColor); ObjectSet(label, OBJPROP_STYLE, style); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void DrawIndicatorTrendLine(datetime x1, datetime x2, double y1, double y2, color lineColor, double style) { int indicatorWindow = WindowFind(indicatorName); if(indicatorWindow < 0) return; string label = "CCI_DivergenceLine_" + DoubleToStr(x1, 0); ObjectDelete(label); ObjectCreate(label, OBJ_TREND, indicatorWindow, x1, y1, x2, y2, 0, 0); ObjectSet(label, OBJPROP_RAY, 0); ObjectSet(label, OBJPROP_COLOR, lineColor); ObjectSet(label, OBJPROP_STYLE, style); } //+------------------------------------------------------------------+