//+--------------------------------------------------------------------+ //| Copyright: (C) 2014 Forex Software Ltd. | //| Website: http://forexsb.com/ | //| Support: http://forexsb.com/forum/ | //| License: Proprietary under the following circumstances: | //| | //| This code is a part of Forex Strategy Builder. It is free for | //| use as an integral part of Forex Strategy Builder. | //| One can modify it in order to improve the code or to fit it for | //| personal use. This code or any part of it cannot be used in | //| other applications without a permission. | //| The contact information cannot be changed. | //| | //| NO LIABILITY FOR CONSEQUENTIAL DAMAGES | //| | //| In no event shall the author be liable for any damages whatsoever | //| (including, without limitation, incidental, direct, indirect and | //| consequential damages, damages for loss of business profits, | //| business interruption, loss of business information, or other | //| pecuniary loss) arising out of the use or inability to use this | //| product, even if advised of the possibility of such damages. | //+--------------------------------------------------------------------+ #property copyright "Copyright (C) 2014 Forex Software Ltd." #property link "http://forexsb.com" #property version "2.00" #property strict #include #include #include //## Requires CommodityChannelIndex.mqh //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class CCIDivergence : public Indicator { public: CCIDivergence(SlotTypes slotType) { SlotType=slotType; IndicatorName="CCI Divergence"; WarningMessage = ""; IsAllowLTF = true; ExecTime = ExecutionTime_DuringTheBar; IsSeparateChart = false; IsDiscreteValues = false; IsDeafultGroupAll = false; } virtual void Calculate(DataSet &dataSet); }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CCIDivergence::Calculate(DataSet &dataSet) { Data=GetPointer(dataSet); MAMethod maMethod = (MAMethod ) ListParam[1].Index; BasePrice basePrice = (BasePrice) ListParam[3].Index; int iPeriod1 = (int) NumParam[0].Value; int iPrvs = CheckParam[0].Checked ? 1 : 0; // Calculation const int firstBar=51+iPeriod1; double bullishDivergence[]; ArrayResize(bullishDivergence,Data.Bars); ArrayInitialize(bullishDivergence,0); double bearishDivergence[]; ArrayResize(bearishDivergence,Data.Bars); ArrayInitialize(bearishDivergence,0); double bearishDivergence1[]; ArrayResize(bearishDivergence1,Data.Bars); ArrayInitialize(bearishDivergence1,0); bool IsIndicatorTrough = false; int lastTrough = 0; bool IsIndicatorPeak = false; int lastPeak = 0; // ---------------------------------------------------- CommodityChannelIndex *cci=new CommodityChannelIndex(SlotType); cci.ListParam[1].Index = ListParam[1].Index; cci.ListParam[2].Index = ListParam[3].Index; cci.NumParam[0].Value=NumParam[0].Value; cci.NumParam[1].Value=100; cci.NumParam[2].Value=0.015; cci.CheckParam[0].Checked=CheckParam[0].Checked; cci.Calculate(dataSet); double adIndicator1[]; ArrayResize(adIndicator1,Data.Bars); ArrayCopy(adIndicator1,cci.Component[0].Value); delete cci; // ----------------------------------------------------- for(int iBar=firstBar; iBar adIndicator1[lastTrough] && Data.Low[currentTrough] < Data.Low[lastTrough]) { bullishDivergence[iBar] = 1; } //---- if(adIndicator1[currentTrough] < adIndicator1[lastTrough] && Data.Low[currentTrough] > Data.Low[lastTrough]) { bullishDivergence[iBar] = 1; } } //+------------------------------------------------------------------+ //| CatchBearishDivergence | //+------------------------------------------------------------------+ if(adIndicator1[iBar-2] >= adIndicator1[iBar-3] && adIndicator1[iBar-2] > adIndicator1[iBar-4] && adIndicator1[iBar-2] > adIndicator1[iBar-1]) IsIndicatorPeak = true; else IsIndicatorPeak = false; if(IsIndicatorPeak == true) { int currentPeak = iBar-2; for(int j = iBar - 30; j <= iBar - 5; j++) { if(adIndicator1[j-2] >= adIndicator1[j-3] && adIndicator1[j-2] > adIndicator1[j-4] && adIndicator1[j-2] >= adIndicator1[j-1] && adIndicator1[j-2] > adIndicator1[j]) lastPeak =j-2; } //---- if(adIndicator1[currentPeak] < adIndicator1[lastPeak] && Data.High[currentPeak] > Data.High[lastPeak]) { bearishDivergence[iBar] = 1; bearishDivergence1[iBar] = -1; } if(adIndicator1[currentPeak] > adIndicator1[lastPeak] && Data.High[currentPeak] < Data.High[lastPeak]) { bearishDivergence[iBar] = 1; bearishDivergence1[iBar] = -1; //---- } } } // Saving the components ArrayResize(Component[0].Value,Data.Bars); Component[0].CompName = "Allow long entry"; Component[0].DataType = IndComponentType_AllowOpenLong; Component[0].FirstBar = firstBar; ArrayCopy(Component[0].Value,bullishDivergence); ArrayResize(Component[1].Value,Data.Bars); Component[1].CompName = "Allow short entry"; Component[1].DataType = IndComponentType_AllowOpenShort; Component[1].FirstBar = firstBar; ArrayCopy(Component[1].Value,bearishDivergence); ArrayResize(Component[2].Value,Data.Bars); Component[2].CompName = "Histogram"; Component[2].DataType = IndComponentType_IndicatorValue; Component[2].FirstBar = firstBar; ArrayCopy(Component[2].Value,bullishDivergence); ArrayResize(Component[3].Value,Data.Bars); Component[3].CompName = "Histogram"; Component[3].DataType = IndComponentType_IndicatorValue; Component[3].FirstBar = firstBar; ArrayCopy(Component[3].Value,bearishDivergence1); } //+------------------------------------------------------------------+