//============================================================== // Forex Strategy Builder // Copyright (c) Miroslav Popov. All rights reserved. //============================================================== // THIS CODE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, // EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE. //============================================================== using System; using System.Drawing; using ForexStrategyBuilder.Infrastructure.Entities; using ForexStrategyBuilder.Infrastructure.Enums; using ForexStrategyBuilder.Infrastructure.Interfaces; namespace ForexStrategyBuilder.Indicators.Store { public class CCI_Divergence : Indicator { public CCI_Divergence() { IndicatorName = "CCI Divergence"; PossibleSlots = SlotTypes.OpenFilter; //| SlotTypes.CloseFilter; SeparatedChart = true; IndicatorAuthor = "Footon"; IndicatorVersion = "2.0"; IndicatorDescription = "Footon's indi corner: custom indicators for FSB and FST."; } public override void Initialize(SlotTypes slotType) { SlotType = slotType; IndParam.IndicatorType = TypeOfIndicator.IndicatorsMA; // The ComboBox parameters IndParam.ListParam[0].Caption = "Logic"; IndParam.ListParam[0].ItemList = new string[] { "Long if bullish divergence" }; IndParam.ListParam[0].Index = 0; IndParam.ListParam[0].Text = IndParam.ListParam[0].ItemList[IndParam.ListParam[0].Index]; IndParam.ListParam[0].Enabled = true; IndParam.ListParam[0].ToolTip = "Logic of application of the oscillator."; IndParam.ListParam[1].Caption = "Smoothing method"; IndParam.ListParam[1].ItemList = Enum.GetNames(typeof(MAMethod)); IndParam.ListParam[1].Index = (int)MAMethod.Simple; IndParam.ListParam[1].Text = IndParam.ListParam[1].ItemList[IndParam.ListParam[1].Index]; IndParam.ListParam[1].Enabled = true; IndParam.ListParam[1].ToolTip = "The Moving Average method used for smoothing the CCI value."; IndParam.ListParam[3].Caption = "Base price"; IndParam.ListParam[3].ItemList = Enum.GetNames(typeof(BasePrice)); IndParam.ListParam[3].Index = (int)BasePrice.Typical; IndParam.ListParam[3].Text = IndParam.ListParam[3].ItemList[IndParam.ListParam[3].Index]; IndParam.ListParam[3].Enabled = true; IndParam.ListParam[3].ToolTip = "The base price of Commodity Channel Index."; // The NumericUpDown parameters IndParam.NumParam[0].Caption = "CCI period"; IndParam.NumParam[0].Value = 14; IndParam.NumParam[0].Min = 1; IndParam.NumParam[0].Max = 200; IndParam.NumParam[0].Enabled = true; IndParam.NumParam[0].ToolTip = "The period of Commodity Channel Index."; // The CheckBox parameters IndParam.CheckParam[0].Caption = "Use previous bar value"; IndParam.CheckParam[0].Enabled = true; IndParam.CheckParam[0].ToolTip = "Use the indicator value from the previous bar."; return; } public override void Calculate(IDataSet dataSet) { DataSet = dataSet; // Reading the parameters MAMethod maMethod = (MAMethod )IndParam.ListParam[1].Index; BasePrice basePrice = (BasePrice)IndParam.ListParam[3].Index; int iPeriod1 = (int)IndParam.NumParam[0].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; // Calculation int iFirstBar = 51+iPeriod1;//iPeriod1 + iPeriod2 + 2; double[] cci = new double[Bars]; double[] signal = new double[Bars]; double[] bullishDivergence = new double[Bars]; double[] bearishDivergence = new double[Bars]; double[] bearishDivergence1 = new double[Bars]; double[] xxx = new double[Bars]; bool IsIndicatorTrough = false; int lastTrough = 0; bool IsIndicatorPeak = false; int lastPeak = 0; // --------------------------------------------------------- var CCI = new CommodityChannelIndex(); CCI.Initialize(SlotType); CCI.IndParam.ListParam[1].Index = IndParam.ListParam[1].Index; CCI.IndParam.ListParam[2].Index = IndParam.ListParam[3].Index; CCI.IndParam.NumParam[0].Value = IndParam.NumParam[0].Value; CCI.IndParam.CheckParam[0].Checked = IndParam.CheckParam[0].Checked; CCI.Calculate(DataSet); cci = CCI.Component[0].Value; signal = CCI.Component[0].Value; // ---------------------------------------------------------- for (int iBar = iFirstBar; iBar < Bars; iBar++) { //+------------------------------------------------------------------+ //| CatchBullishDivergence | //+------------------------------------------------------------------+ if(cci[iBar-2] <= cci[iBar-3] && cci[iBar-2] < cci[iBar-4] && cci[iBar-2] < cci[iBar-1]) //shiftist 2 vasakule, 1 paremale IsIndicatorTrough = true; else IsIndicatorTrough = false; if(IsIndicatorTrough == true) { int currentTrough = iBar-2; for(int j = iBar - 30; j <= iBar - 5; j++) { //if(signal[i-2] <= signal[i-3] && signal[i-2] <= signal[i-4] && signal[i] <= signal[i-2] && signal[i] <= signal[i-1]) if(cci[j-2] <= cci[j-3] && cci[j-2] < cci[j-4] && cci[j-2] <= cci[j-1] && cci[j-2] < cci[j]) { lastTrough = j-2; } } //xxx[iBar]=Low[lastTrough-1]; if(cci[currentTrough] > cci[lastTrough] && Low[currentTrough] < Low[lastTrough]) { bullishDivergence[iBar] = 1; //---- /*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("Classical bullish divergence on: ", currentTrough); */ } //---- if(cci[currentTrough] < cci[lastTrough] && Low[currentTrough] > Low[lastTrough]) { bullishDivergence[iBar] = 1; //---- /*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("Reverse bullish divergence on: ", currentTrough); */ } } //+------------------------------------------------------------------+ //| CatchBearishDivergence | //+------------------------------------------------------------------+ if(cci[iBar-2] >= cci[iBar-3] && cci[iBar-2] > cci[iBar-4] && cci[iBar-2] > cci[iBar-1]) IsIndicatorPeak = true; else IsIndicatorPeak = false; if(IsIndicatorPeak == true) { int currentPeak = iBar-2; for(int j = iBar - 30; j <= iBar - 5; j++) { if(cci[j-2] >= cci[j-3] && cci[j-2] > cci[j-4] && cci[j-2] >= cci[j-1] && cci[j-2] > cci[j]) lastPeak =j-2; } //---- if(cci[currentPeak] < cci[lastPeak] && High[currentPeak] > High[lastPeak]) { bearishDivergence[iBar] = 1; bearishDivergence1[iBar] = -1; /*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("Classical bearish divergence on: ", currentPeak); */ } if(cci[currentPeak] > cci[lastPeak] && High[currentPeak] < High[lastPeak]) { bearishDivergence[iBar] = 1; bearishDivergence1[iBar] = -1; //---- /*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("Reverse bearish divergence on: ", currentPeak); */ } } } // Saving the components Component = new IndicatorComp[4]; Component[0] = new IndicatorComp(); Component[0].CompName = "Histogram"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Histogram; Component[0].FirstBar = iFirstBar; Component[0].Value = bullishDivergence; Component[3] = new IndicatorComp(); Component[3].CompName = "Histogram"; Component[3].DataType = IndComponentType.IndicatorValue; Component[3].ChartType = IndChartType.Histogram; Component[3].FirstBar = iFirstBar; Component[3].Value = bearishDivergence1; Component[1] = new IndicatorComp(); Component[1].CompName = "Allow entry"; Component[1].DataType = IndComponentType.AllowOpenLong; Component[1].ChartType = IndChartType.NoChart; Component[1].FirstBar = iFirstBar; Component[1].Value = bullishDivergence; Component[2] = new IndicatorComp(); Component[2].CompName = "Allow entry"; Component[2].DataType = IndComponentType.AllowOpenShort; Component[2].ChartType = IndChartType.NoChart; Component[2].FirstBar = iFirstBar; Component[2].Value = bearishDivergence; return; } /// /// Sets the indicator logic description /// public override void SetDescription() { EntryFilterLongDescription = "Long if bullish divergence"; EntryFilterShortDescription = "Short if bearish divergence"; ExitFilterLongDescription = "Close if bearish divergence"; ExitFilterShortDescription = "Close if bullish divergence"; return; } /// /// Indicator to string /// public override string ToString() { string sString = IndicatorName + (IndParam.CheckParam[0].Checked ? "* (" : " (") + IndParam.ListParam[1].Text + ", " + // Method IndParam.NumParam[1].ValueToString + ")"; // Period return sString; } } }