//============================================================== // Forex Strategy Builder // Copyright © 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 BandpassFilter : Indicator { public BandpassFilter() { // General properties IndicatorName = "Bandpass Filter"; PossibleSlots = SlotTypes.OpenFilter | SlotTypes.CloseFilter; SeparatedChart = true; IndicatorAuthor = "Denny Imanuel"; IndicatorVersion = "2.0"; IndicatorDescription = "A custom indicator for FSB and FST."; } public override void Initialize(SlotTypes slotType) { SlotType = slotType; // The ComboBox parameters IndParam.ListParam[0].Caption = "Logic"; IndParam.ListParam[0].ItemList = new string[] { "The Bandpass Filter line rises", "The Bandpass Filter line falls", "The Bandpass Filter line is higher than zero", "The Bandpass Filter line is lower than zero", "The Bandpass Filter line crosses the zero line upward", "The Bandpass Filter line crosses the zero line downward", "The Bandpass Filter line changes its direction upward", "The Bandpass Filter line changes its direction downward", "The Bandpass Filter line crosses the Average Peak upward", "The Bandpass Filter line crosses the Average Valley downward", "The Bandpass Filter line is higher than the Average Peak", "The Bandpass Filter line is lower than the Average Valley" }; 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 indicator."; 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 smoothing method of Moving Average."; IndParam.ListParam[2].Caption = "Base price"; IndParam.ListParam[2].ItemList = Enum.GetNames(typeof(BasePrice)); IndParam.ListParam[2].Index = (int)BasePrice.Median; IndParam.ListParam[2].Text = IndParam.ListParam[2].ItemList[IndParam.ListParam[2].Index]; IndParam.ListParam[2].Enabled = true; IndParam.ListParam[2].ToolTip = "The price the Moving Average is based on."; // The NumericUpDown parameters IndParam.NumParam[0].Caption = "Bandpass Period"; IndParam.NumParam[0].Value = 10; IndParam.NumParam[0].Min = 1; IndParam.NumParam[0].Max = 200; IndParam.NumParam[0].Enabled = true; IndParam.NumParam[0].ToolTip = "The period of bandpass filter calculation."; IndParam.NumParam[1].Caption = "Smoothing Period"; IndParam.NumParam[1].Value = 20; IndParam.NumParam[1].Min = 1; IndParam.NumParam[1].Max = 200; IndParam.NumParam[1].Enabled = true; IndParam.NumParam[1].ToolTip = "The period of bandpass filter calculation."; IndParam.NumParam[2].Caption = "Bandpass Delta"; IndParam.NumParam[2].Value = 0.5; IndParam.NumParam[2].Min = 0; IndParam.NumParam[2].Max = 10; IndParam.NumParam[2].Point = 2; IndParam.NumParam[2].Enabled = true; IndParam.NumParam[2].ToolTip = "The delta value for bandpass filter."; IndParam.NumParam[3].Caption = "Bandwidth Multiplier"; IndParam.NumParam[3].Value = 0.1; IndParam.NumParam[3].Min = 0; IndParam.NumParam[3].Max = 10; IndParam.NumParam[3].Point = 2; IndParam.NumParam[3].Enabled = true; IndParam.NumParam[3].ToolTip = "The divisor to determine the upper and lower bandwidth."; // 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."; IndParam.CheckParam[1].Caption = "Use smoothed bandpass filter"; IndParam.CheckParam[1].Checked = true; IndParam.CheckParam[1].Enabled = true; IndParam.CheckParam[1].ToolTip = "Use the smoothed value of bandpass filter."; } public override void Calculate(IDataSet dataSet) { DataSet = dataSet; // Reading the parameters MAMethod maMethod = (MAMethod )IndParam.ListParam[1].Index; BasePrice basePrice = (BasePrice)IndParam.ListParam[2].Index; int iPeriod = (int)IndParam.NumParam[0].Value; int iSmooth = (int)IndParam.NumParam[1].Value; double Delta = IndParam.NumParam[2].Value; double dMpl = IndParam.NumParam[3].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; double[] adPrice = Price(basePrice); double[] adBPFilter = new double[Bars]; double[] adBPSignal = new double[Bars]; double[] adBPPeak = new double[Bars]; double[] adBPValley = new double[Bars]; double[] adAvgPeak = new double[Bars]; double[] adAvgValley = new double[Bars]; // Calculation int iFirstBar = iPeriod + 2; double Beta = Math.Cos(2*Math.PI/iPeriod); double Gamma = 1/Math.Cos(4*Math.PI*Delta/iPeriod); double Alpha = Gamma - Math.Sqrt(Math.Pow(Gamma,2)-1); double Theta = 0.5*(1-Alpha); double Omega = Beta*(1+Alpha); for (int iBar = 2; iBar < Bars; iBar++) { adBPFilter[iBar] = Theta*(adPrice[iBar] - adPrice[iBar-2]) + Omega*adBPFilter[iBar-1] - Alpha*adBPFilter[iBar-2]; if (adBPFilter[iBar]adBPFilter[iBar-2]) adBPPeak[iBar] = adBPFilter[iBar-1]; else adBPPeak[iBar] = adBPPeak[iBar-1]; if (adBPFilter[iBar]>adBPFilter[iBar-1] && adBPFilter[iBar-1]