LowPass Filter MA by Squalou

44213 downloads / 4524 views / Created: 18.06.2013
 Average Rating: 5

Indicator Description

A 3rd-order LowPass Filter moving average;
The smoothest MA ever!

Its lag is Period/3, similar to a Linear Weighted MA, twice shorter than an EMA, but so much smoother and stable!

Its smoothness is similar to a double-SMA (ie SMA-of-SMA);

For reference, here is some information on various MAs lag :

lag(LowPassFilter(MAperiod)~~ MAperiod/3; => lag(LowPass(21)) = 7;

lag(EMA(MAperiod)) = (MAperiod+1)/2 - 1; => lag( EMA(21)) = 10;
lag(LWMA(MAperiod)) ~~ MAperiod/3; => lag( LWMA(21)) = 7;
lag(Double-LWMA(MAperiod)) ~~ 2*MAperiod/3; => lag(DLWMA(21)) = 14;
lag(Hull-MA(MAperiod)) ~~ MAperiod/6; => lag( HMA(21)) = 3.5;

The Hull-MA has the shortest lag of all MAs, and is also very smooth/stable, which makes it a good choice, but it overshoots a lot.

LowPass does not overshoot, and still has the lowest lag compared to classic MAs.

This LowPass indicator handles a Shift so you can delay it if ever needed.
You can also change its color, orange being the default.

Period can be set to up 200*24=4800, which allows to plot/use the LowPass MA on an H1 strategy with periods designed for Daily charts (24*Daily period = H1 period);
Example: Period = 4800 = 24*200 on an H1 chart/strategy will produce a LowPass MA equivalent to a 200-days LowPass MA.

Comments

// LowPassFilter Indicator // by Squalou, 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 { /// /// LowPass Indicator /// public class LowPass : Indicator { /// /// Sets the default indicator parameters for the designated slot type /// public LowPass() { // General properties IndicatorName = "LowPass Filter"; PossibleSlots = SlotTypes.Open | SlotTypes.OpenFilter | SlotTypes.Close | SlotTypes.CloseFilter; IndicatorAuthor = "Squalou"; IndicatorVersion = "1.0"; IndicatorDescription = "LowPass Filter line on chart"; } 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 LP" }; else if (SlotType == SlotTypes.OpenFilter) IndParam.ListParam[0].ItemList = new string[] { "LowPass rises", "LowPass falls", "The bar opens above LowPass", "The bar opens below LowPass", "The bar opens above LowPass after opening below it", "The bar opens below LowPass after opening above it", "The position opens above LowPass", "The position opens below LowPass", }; else if (SlotType == SlotTypes.Close) IndParam.ListParam[0].ItemList = new string[] { "Exit at LowPass" }; else if (SlotType == SlotTypes.CloseFilter) IndParam.ListParam[0].ItemList = new string[] { "LowPass rises", "LowPass falls", "The bar closes below LowPass", "The bar closes above LowPass", }; 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 LowPass."; IndParam.ListParam[1].Caption = "Base price"; IndParam.ListParam[1].ItemList = Enum.GetNames(typeof(BasePrice)); IndParam.ListParam[1].Index = (int)BasePrice.Close; IndParam.ListParam[1].Text = IndParam.ListParam[1].ItemList[IndParam.ListParam[1].Index]; IndParam.ListParam[1].Enabled = true; IndParam.ListParam[1].ToolTip = "The price LowPass is based on."; IndParam.ListParam[3].Caption = "Line Color"; IndParam.ListParam[3].ItemList = new string [] { "Orange", "Blue", "Red", "Green", "Purple", }; IndParam.ListParam[3].Index = 0; IndParam.ListParam[3].Text = IndParam.ListParam[3].ItemList[IndParam.ListParam[3].Index]; IndParam.ListParam[3].Enabled = true; IndParam.ListParam[3].ToolTip = "Color of line on chart for this MA WTF value."; // The NumericUpDown parameters IndParam.NumParam[0].Caption = "Period"; IndParam.NumParam[0].Value = 100; IndParam.NumParam[0].Min = 1; IndParam.NumParam[0].Max = 200; IndParam.NumParam[0].Enabled = true; IndParam.NumParam[0].ToolTip = "Period bars"; IndParam.NumParam[1].Caption = "Shift"; IndParam.NumParam[1].Value = 0; IndParam.NumParam[1].Min = 0; IndParam.NumParam[1].Max = 200; IndParam.NumParam[1].Enabled = true; IndParam.NumParam[1].ToolTip = "How many bars to shift (delay)"; // 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 iPeriod = (int)IndParam.NumParam[0].Value; int iShift = (int)IndParam.NumParam[1].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; // TimeExecution if (basePrice == BasePrice.Open && iPeriod == 1 && iShift == 0) IndParam.ExecutionTime = ExecutionTime.AtBarOpening; // Calculation double[] adPrice = Price(basePrice); double[] adLowPass = new double[Bars]; double[] lp = new double[Bars]; double[] Diff = new double[Bars]; double HH,LL; double a = Math.Exp(-Math.PI/iPeriod); double b = 2*a*Math.Cos(Math.Sqrt(3)*Math.PI/iPeriod); double c = Math.Exp(-2*Math.PI/iPeriod); //a * a; int iFirstBar = 24 + iShift + 2 + iPrvs; for (int iBar = 4; iBar < Bars - iShift; iBar++) { lp[iBar] = (b + c) * lp[iBar - 1] - (c + b * c) * lp[iBar - 2] + c * c * lp[iBar - 3] + (1 - b + c) * (1 - c) * adPrice[iBar]; adLowPass[iBar+iShift] = lp[iBar]; } // Saving the components if (SlotType == SlotTypes.Open || SlotType == SlotTypes.Close) { Component = new IndicatorComp[2]; Component[1] = new IndicatorComp(); Component[1].Value = new double[Bars]; for (int iBar = iFirstBar; iBar < Bars; iBar++) { // Covers the cases when the price can pass through the MA without a signal double dValue = adLowPass[iBar - iPrvs]; // Current value double dValue1 = adLowPass[iBar - iPrvs - 1]; // Previous value double dTempVal = dValue; if ((dValue1 > 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 = "MA Value"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Line; Component[0].ChartColor = Color.FromName(IndParam.ListParam[3].ItemList[IndParam.ListParam[3].Index]); Component[0].FirstBar = iFirstBar; Component[0].Value = adLowPass; 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 "LowPass rises": IndicatorRisesLogic(iFirstBar, iPrvs, adLowPass, ref Component[1], ref Component[2]); break; case "LowPass falls": IndicatorFallsLogic(iFirstBar, iPrvs, adLowPass, ref Component[1], ref Component[2]); break; case "The bar opens above LowPass": BarOpensAboveIndicatorLogic(iFirstBar, iPrvs, adLowPass, ref Component[1], ref Component[2]); break; case "The bar opens below LowPass": BarOpensBelowIndicatorLogic(iFirstBar, iPrvs, adLowPass, ref Component[1], ref Component[2]); break; case "The bar opens above LowPass after opening below it": BarOpensAboveIndicatorAfterOpeningBelowLogic(iFirstBar, iPrvs, adLowPass, ref Component[1], ref Component[2]); break; case "The bar opens below LowPass after opening above it": BarOpensBelowIndicatorAfterOpeningAboveLogic(iFirstBar, iPrvs, adLowPass, ref Component[1], ref Component[2]); break; case "The position opens above LowPass": 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 LowPass": 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 LowPass": BarClosesBelowIndicatorLogic(iFirstBar, iPrvs, adLowPass, ref Component[1], ref Component[2]); break; case "The bar closes above LowPass": BarClosesAboveIndicatorLogic(iFirstBar, iPrvs, adLowPass, 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 "LowPass rises": EntryFilterLongDescription = "the " + ToString() + " rises"; EntryFilterShortDescription = "the " + ToString() + " falls"; ExitFilterLongDescription = "the " + ToString() + " rises"; ExitFilterShortDescription = "the " + ToString() + " falls"; break; case "LowPass falls": EntryFilterLongDescription = "the " + ToString() + " falls"; EntryFilterShortDescription = "the " + ToString() + " rises"; ExitFilterLongDescription = "the " + ToString() + " falls"; ExitFilterShortDescription = "the " + ToString() + " rises"; break; case "The bar opens above LowPass": EntryFilterLongDescription = "the bar opens above the " + ToString(); EntryFilterShortDescription = "the bar opens below the " + ToString(); break; case "The bar opens below LowPass": EntryFilterLongDescription = "the bar opens below the " + ToString(); EntryFilterShortDescription = "the bar opens above the " + ToString(); break; case "The position opens above LowPass": 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 LowPass": 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 LowPass 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 LowPass 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 LowPass": ExitFilterLongDescription = "the bar closes above the " + ToString(); ExitFilterShortDescription = "the bar closes below the " + ToString(); break; case "The bar closes below LowPass": 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[1].ValueToString + ")"; // MA shift return sString; } } }
//+--------------------------------------------------------------------+ //| Copyright: (C) 2014, Miroslav Popov - All rights reserved! | //| 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 | //| another applications without a permission. 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 2014, Miroslav Popov" #property link "http://forexsb.com" #property version "1.00" #property strict #include <Forexsb.com/Indicator.mqh> #include <Forexsb.com/Enumerations.mqh> //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class LowPassFilter : public Indicator { public: LowPassFilter(SlotTypes slotType) { SlotType=slotType; IndicatorName="LowPass Filter"; WarningMessage = ""; IsAllowLTF = true; ExecTime = ExecutionTime_DuringTheBar; IsSeparateChart = false; IsDiscreteValues = false; IsDeafultGroupAll = false; } virtual void Calculate(DataSet &dataSet); }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void LowPassFilter::Calculate(DataSet &dataSet) { Data=GetPointer(dataSet); // Reading the parameters BasePrice basePrice = (BasePrice)ListParam[1].Index; int iPeriod = (int)NumParam[0].Value; int iShift = (int)NumParam[1].Value; int iPrvs = CheckParam[0].Checked ? 1 : 0; // TimeExecution if(basePrice == BasePrice_Open && iPeriod == 1 && iShift == 0) ExecTime = ExecutionTime_AtBarOpening; // Calculation double adPrice[]; Price(basePrice,adPrice); double adLowPass[]; ArrayResize(adLowPass,Data.Bars);ArrayInitialize(adLowPass, 0); double lp[]; ArrayResize(lp,Data.Bars);ArrayInitialize(lp, 0); double MathPI=3.14159265359; double a = MathExp(-MathPI/iPeriod); double b = 2*a*MathCos(MathSqrt(3)*MathPI/iPeriod); double c=MathExp(-2*MathPI/iPeriod); //a * a; int iFirstBar = 24 + iShift + 2 + iPrvs; for (int iBar = 4; iBar < Data.Bars - iShift; iBar++) { lp[iBar] = (b + c) * lp[iBar - 1] - (c + b * c) * lp[iBar - 2] + c * c * lp[iBar - 3] + (1 - b + c) * (1 - c) * adPrice[iBar]; adLowPass[iBar+iShift] = lp[iBar]; } // Saving the components if(SlotType==SlotTypes_Open || SlotType==SlotTypes_Close) { ArrayResize(Component[1].Value,Data.Bars); for(int iBar=iFirstBar; iBar<Data.Bars; iBar++) { // Covers the cases when the price can pass through the MA without a signal double dValue = adLowPass[iBar - iPrvs]; // Current value double dValue1 = adLowPass[iBar - iPrvs - 1]; // Previous value double dTempVal= dValue; if((dValue1>Data.High[iBar-1] && dValue<Data.Open[iBar]) || // The Data.Open price jumps above the indicator (dValue1 < Data.Low[iBar - 1] && dValue > Data.Open[iBar]) || // The Data.Open price jumps below the indicator (Data.Close[iBar - 1] < dValue && dValue < Data.Open[iBar]) || // The Data.Open price is in a positive gap (Data.Close[iBar - 1] > dValue && dValue > Data.Open[iBar])) // The Data.Open price is in a negative gap dTempVal=Data.Open[iBar]; Component[1].Value[iBar]=dTempVal; // Entry or exit value } } else { ArrayResize(Component[1].Value,Data.Bars); Component[1].FirstBar=iFirstBar; ArrayResize(Component[2].Value,Data.Bars); Component[2].FirstBar=iFirstBar; } ArrayResize(Component[0].Value,Data.Bars); Component[0].CompName = "MA Value"; Component[0].DataType = IndComponentType_IndicatorValue; Component[0].FirstBar = iFirstBar; ArrayCopy(Component[0].Value,adLowPass); 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) { if(ListParam[0].Text=="LowPass rises") { IndicatorRisesLogic(iFirstBar,iPrvs,adLowPass,Component[1],Component[2]); } else if(ListParam[0].Text=="LowPass falls") { IndicatorFallsLogic(iFirstBar,iPrvs,adLowPass,Component[1],Component[2]); } else if(ListParam[0].Text=="The bar opens above LowPass") { BarOpensAboveIndicatorLogic(iFirstBar,iPrvs,adLowPass,Component[1],Component[2]); } else if(ListParam[0].Text=="The bar opens below LowPass") { BarOpensBelowIndicatorLogic(iFirstBar,iPrvs,adLowPass,Component[1],Component[2]); } else if(ListParam[0].Text=="The bar opens above LowPass after opening below it") { BarOpensAboveIndicatorAfterOpeningBelowLogic(iFirstBar,iPrvs,adLowPass,Component[1], Component[2]); } else if(ListParam[0].Text=="The bar opens below LowPass after opening above it") { BarOpensBelowIndicatorAfterOpeningAboveLogic(iFirstBar,iPrvs,adLowPass,Component[1], Component[2]); } else if(ListParam[0].Text=="The position opens above LowPass") { 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; } else if(ListParam[0].Text=="The position opens below LowPass") { 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; } else if(ListParam[0].Text=="The bar closes below LowPass") { BarClosesBelowIndicatorLogic(iFirstBar,iPrvs,adLowPass,Component[1],Component[2]); } else if(ListParam[0].Text=="The bar closes above LowPass") { BarClosesAboveIndicatorLogic(iFirstBar,iPrvs,adLowPass,Component[1],Component[2]); } } } //+------------------------------------------------------------------+
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.;