CCI Divergence by footon
49825 downloads / 3961 views / Created: 21.10.2015 Average Rating: 0
Indicator Description
CCI Divergence
Comments
//==============================================================
// 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;
}
}
}
//+--------------------------------------------------------------------+ //| 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 <Forexsb.com/Indicator.mqh> #include <Forexsb.com/Enumerations.mqh> #include <Forexsb.com/Indicators/CommodityChannelIndex.mqh> //## 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<Data.Bars; iBar++) { //+------------------------------------------------------------------+ //| CatchBullishDivergence | //+------------------------------------------------------------------+ if(adIndicator1[iBar-2] <= adIndicator1[iBar-3] && adIndicator1[iBar-2] < adIndicator1[iBar-4] && adIndicator1[iBar-2] < adIndicator1[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(adIndicator1[j-2] <= adIndicator1[j-3] && adIndicator1[j-2] < adIndicator1[j-4] && adIndicator1[j-2] <= adIndicator1[j-1] && adIndicator1[j-2] < adIndicator1[j]) { lastTrough = j-2; } } if(adIndicator1[currentTrough] > 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); } //+------------------------------------------------------------------+
Risk warning: Forex, spread bets and CFD are leveraged products. They may not be suitable for you as they carry a high degree of risk to your capital and you can lose more than your initial investment. You should ensure you understand all of the risks.
Copyright © 2006 - 2024, Forex Software Ltd.;
Copyright © 2006 - 2024, Forex Software Ltd.;