// ALF Indicator (Advance Laguerre Filter) // by Squalou, 08 June 2013 //============================================================== // Forex Strategy Builder // Copyright © Miroslav Popov. All rights reserved. // http://forexsb.com/ //============================================================== // 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 { /// /// ALF Indicator /// public class ALF : Indicator { /// /// Sets the default indicator parameters for the designated slot type /// public ALF() { // General properties IndicatorName = "ALF"; PossibleSlots = SlotTypes.Open | SlotTypes.OpenFilter | SlotTypes.Close | SlotTypes.CloseFilter; IndicatorAuthor = "Squalou, heavy revisions and bug fixes by Footon"; IndicatorVersion = "2.0"; IndicatorDescription = "Advanced Laguerre Filter"; } public override void Initialize(SlotTypes slotType) { SlotType = slotType; // The ComboBox parameters IndParam.ListParam[0].Caption = "Logic"; if (SlotType == SlotTypes.Open) IndParam.ListParam[0].ItemList = new string[] { "Enter at ALF" }; else if (SlotType == SlotTypes.OpenFilter) IndParam.ListParam[0].ItemList = new string[] { "ALF rises", "ALF falls", "The bar opens above ALF", "The bar opens below ALF", "The bar opens above ALF after opening below it", "The bar opens below ALF after opening above it", "The position opens above ALF", "The position opens below ALF", }; else if (SlotType == SlotTypes.Close) IndParam.ListParam[0].ItemList = new string[] { "Exit at ALF" }; else if (SlotType == SlotTypes.CloseFilter) IndParam.ListParam[0].ItemList = new string[] { "ALF rises", "ALF falls", "The bar closes below ALF", "The bar closes above ALF", }; else IndParam.ListParam[0].ItemList = new string[] { "Not Defined" }; 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 ALF."; IndParam.ListParam[1].Caption = "Base price"; IndParam.ListParam[1].ItemList = Enum.GetNames(typeof(BasePrice)); IndParam.ListParam[1].Index = (int)BasePrice.Median; IndParam.ListParam[1].Text = IndParam.ListParam[1].ItemList[IndParam.ListParam[1].Index]; IndParam.ListParam[1].Enabled = true; IndParam.ListParam[1].ToolTip = "The price ALF is based on"; // The NumericUpDown parameters IndParam.NumParam[0].Caption = "Lookback"; IndParam.NumParam[0].Value = 20; IndParam.NumParam[0].Min = 2; IndParam.NumParam[0].Max = 200; IndParam.NumParam[0].Enabled = true; IndParam.NumParam[0].ToolTip = "Lookback bars"; IndParam.NumParam[1].Caption = "Median"; IndParam.NumParam[1].Value = 5; IndParam.NumParam[1].Min = 2; IndParam.NumParam[1].Max = 200; IndParam.NumParam[1].Enabled = true; IndParam.NumParam[1].ToolTip = "bars to calc the Median value of distance-from-ALF"; IndParam.NumParam[2].Caption = "Shift"; IndParam.NumParam[2].Value = 0; IndParam.NumParam[2].Min = 0; IndParam.NumParam[2].Max = 200; IndParam.NumParam[2].Enabled = true; IndParam.NumParam[2].ToolTip = "bars to right-shift ALF line"; // 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; } /// /// Calculates the indicator's components /// public override void Calculate(IDataSet dataSet) { DataSet = dataSet; // Reading the parameters BasePrice basePrice = (BasePrice)IndParam.ListParam[1].Index; int Lookback = (int)IndParam.NumParam[0].Value; int Median = (int)IndParam.NumParam[1].Value; int iShift = (int)IndParam.NumParam[2].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; if (Median > Lookback) Median = Lookback; // Calculation double[] adPrice = Price(basePrice); double[] adALF = new double[Bars]; double[] Diff = new double[Bars]; double[] L0 = new double[Bars]; double[] L1 = new double[Bars]; double[] L2 = new double[Bars]; double[] L3 = new double[Bars]; double HH,LL,alpha=0; int iFirstBar = Lookback + iShift + 2 + iPrvs; for (int iBar = 1; iBar < Bars - iShift; iBar++) { Diff[iBar] = Math.Abs(adPrice[iBar] - adALF[iBar-1]); if (iBar > Lookback) { // get the largest and smallest Distance-from-ALF values HH = double.MinValue; LL = double.MaxValue; for (int j=0;j HH) HH = Diff[iBar-j]; if (Diff[iBar-j] < LL) LL = Diff[iBar-j]; } if (Math.Abs(HH-LL) > Epsilon) { // Use the Median value of the Distance from ALF over the last Median bars to determine alpha double sum=0; for (int j=0;j High[iBar - 1] && dValue < Open[iBar]) || // The Open price jumps above the indicator (dValue1 < Low[iBar - 1] && dValue > Open[iBar]) || // The Open price jumps below the indicator (Close[iBar - 1] < dValue && dValue < Open[iBar]) || // The Open price is in a positive gap (Close[iBar - 1] > dValue && dValue > Open[iBar])) // The Open price is in a negative gap dTempVal = Open[iBar]; Component[1].Value[iBar] = dTempVal; // Entry or exit value } } else { Component = new IndicatorComp[3]; Component[1] = new IndicatorComp(); Component[1].ChartType = IndChartType.NoChart; Component[1].FirstBar = iFirstBar; Component[1].Value = new double[Bars]; Component[2] = new IndicatorComp(); Component[2].ChartType = IndChartType.NoChart; Component[2].FirstBar = iFirstBar; Component[2].Value = new double[Bars]; } Component[0] = new IndicatorComp(); Component[0].CompName = "ALF Value"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Line; Component[0].ChartColor = Color.Purple; Component[0].FirstBar = iFirstBar; Component[0].Value = adALF; if (SlotType == SlotTypes.Open) { Component[1].CompName = "Position opening price"; Component[1].DataType = IndComponentType.OpenPrice; } else if (SlotType == SlotTypes.OpenFilter) { Component[1].DataType = IndComponentType.AllowOpenLong; Component[1].CompName = "Is long entry allowed"; Component[2].DataType = IndComponentType.AllowOpenShort; Component[2].CompName = "Is short entry allowed"; } else if (SlotType == SlotTypes.Close) { Component[1].CompName = "Position closing price"; Component[1].DataType = IndComponentType.ClosePrice; } else if (SlotType == SlotTypes.CloseFilter) { Component[1].DataType = IndComponentType.ForceCloseLong; Component[1].CompName = "Close out long position"; Component[2].DataType = IndComponentType.ForceCloseShort; Component[2].CompName = "Close out short position"; } if (SlotType == SlotTypes.OpenFilter || SlotType == SlotTypes.CloseFilter) { switch (IndParam.ListParam[0].Text) { case "ALF rises": IndicatorRisesLogic(iFirstBar, iPrvs, adALF, ref Component[1], ref Component[2]); break; case "ALF falls": IndicatorFallsLogic(iFirstBar, iPrvs, adALF, ref Component[1], ref Component[2]); break; case "The bar opens above ALF": BarOpensAboveIndicatorLogic(iFirstBar, iPrvs, adALF, ref Component[1], ref Component[2]); break; case "The bar opens below ALF": BarOpensBelowIndicatorLogic(iFirstBar, iPrvs, adALF, ref Component[1], ref Component[2]); break; case "The bar opens above ALF after opening below it": BarOpensAboveIndicatorAfterOpeningBelowLogic(iFirstBar, iPrvs, adALF, ref Component[1], ref Component[2]); break; case "The bar opens below ALF after opening above it": BarOpensBelowIndicatorAfterOpeningAboveLogic(iFirstBar, iPrvs, adALF, ref Component[1], ref Component[2]); break; case "The position opens above ALF": Component[0].PosPriceDependence = PositionPriceDependence.BuyHigherSellLower; Component[0].UsePreviousBar = iPrvs; Component[1].DataType = IndComponentType.Other; Component[1].ShowInDynInfo = false; Component[2].DataType = IndComponentType.Other; Component[2].ShowInDynInfo = false; break; case "The position opens below ALF": Component[0].PosPriceDependence = PositionPriceDependence.BuyLowerSelHigher; Component[0].UsePreviousBar = iPrvs; Component[1].DataType = IndComponentType.Other; Component[1].ShowInDynInfo = false; Component[2].DataType = IndComponentType.Other; Component[2].ShowInDynInfo = false; break; case "The bar closes below ALF": BarClosesBelowIndicatorLogic(iFirstBar, iPrvs, adALF, ref Component[1], ref Component[2]); break; case "The bar closes above ALF": BarClosesAboveIndicatorLogic(iFirstBar, iPrvs, adALF, ref Component[1], ref Component[2]); break; default: break; } } return; } /// /// Sets the indicator logic description /// public override void SetDescription() { EntryPointLongDescription = "at the " + ToString(); EntryPointShortDescription = "at the " + ToString(); ExitPointLongDescription = "at the " + ToString(); ExitPointShortDescription = "at the " + ToString(); switch (IndParam.ListParam[0].Text) { case "ALF rises": EntryFilterLongDescription = "the " + ToString() + " rises"; EntryFilterShortDescription = "the " + ToString() + " falls"; ExitFilterLongDescription = "the " + ToString() + " rises"; ExitFilterShortDescription = "the " + ToString() + " falls"; break; case "ALF falls": EntryFilterLongDescription = "the " + ToString() + " falls"; EntryFilterShortDescription = "the " + ToString() + " rises"; ExitFilterLongDescription = "the " + ToString() + " falls"; ExitFilterShortDescription = "the " + ToString() + " rises"; break; case "The bar opens above ALF": EntryFilterLongDescription = "the bar opens above the " + ToString(); EntryFilterShortDescription = "the bar opens below the " + ToString(); break; case "The bar opens below ALF": EntryFilterLongDescription = "the bar opens below the " + ToString(); EntryFilterShortDescription = "the bar opens above the " + ToString(); break; case "The position opens above ALF": EntryFilterLongDescription = "the position opening price is higher than the " + ToString(); EntryFilterShortDescription = "the position opening price is lower than the " + ToString(); break; case "The position opens below ALF": EntryFilterLongDescription = "the position opening price is lower than the " + ToString(); EntryFilterShortDescription = "the position opening price is higher than the " + ToString(); break; case "The bar opens above ALF after opening below it": EntryFilterLongDescription = "the bar opens above the " + ToString() + " after opening below it"; EntryFilterShortDescription = "the bar opens below the " + ToString() + " after opening above it"; break; case "The bar opens below ALF after opening above it": EntryFilterLongDescription = "the bar opens below the " + ToString() + " after opening above it"; EntryFilterShortDescription = "the bar opens above the " + ToString() + " after opening below it"; break; case "The bar closes above ALF": ExitFilterLongDescription = "the bar closes above the " + ToString(); ExitFilterShortDescription = "the bar closes below the " + ToString(); break; case "The bar closes below ALF": ExitFilterLongDescription = "the bar closes below the " + ToString(); ExitFilterShortDescription = "the bar closes above the " + ToString(); break; default: break; } return; } /// /// Indicator to string /// public override string ToString() { string sString = IndicatorName + (IndParam.CheckParam[0].Checked ? "* (" : " (") + IndParam.ListParam[1].Text + ", " + // Method IndParam.ListParam[2].Text + ", " + // Price IndParam.NumParam[0].ValueToString + ", " + // MA period IndParam.NumParam[2].ValueToString + ")"; // MA shift return sString; } } }