Category Other  Published on 23/04/2020

Infinite Impulse Response Filter

Description

For further investigation

This is a simple infinite impulse response filter as used in digital signal processing. You may enhance its order, change type and experiment with parameter sets.

Note that while scrolling on time axis cTrader needs to recalculate and therefore result may look weird until calculation is done.

Further information withing source code:

// -------------------------------------------------------------------------------------------------
//
//      This is a IIR-Filter Playground
//      Notes: IIR-Filter output needs to stabilize. Discard a certain amount of output values
//      before taking result in consideration
//    
// -------------------------------------------------------------------------------------------------

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class IIR_Playground : Indicator
    {
        const int NZEROS = 4;
        const int NPOLES = 4;

        double[] xv = new double[NZEROS + 1];
        double[] yv = new double[NPOLES + 1];

        double GAIN = 0;
        double a1 = 0;
        double a2 = 0;
        double a3 = 0;
        double a4 = 0;

        [Parameter("Filter Set", DefaultValue = 1)]
        public int filter { get; set; }

        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Output("Result", LineColor = "Orange")]
        public IndicatorDataSeries Result { get; set; }

        /// <summary>
        /// Called from cTrader
        /// </summary>
        /// <param name="index"></param>
        public override void Calculate(int index)
        {
            Result[index] = ShiftNextDoubleToIIRFilter(Source[index]);
        }

        /// <summary>
        /// Called from cTrader
        /// </summary>
        protected override void Initialize()
        {
            switch (filter)
            {
                case 0:
                    GAIN = 180.4096236;
                    a1 = -0.8008026467;
                    a2 = 3.1325885702;
                    a3 = -4.8527941875;
                    a4 = 3.5017954271;
                    break;
                case 1:
                    GAIN = 49.79245061;
                    // 2.5 - 3
                    a1 = -0.6413515381;
                    a2 = -0.4504011831;
                    a3 = -1.6413515381;
                    a4 = -0.5640098549;
                    break;
                case 2:
                    GAIN = 49.79238683;
                    // 3 - 3.5
                    a1 = -0.6413515381;
                    a2 = -1.3071151433;
                    a3 = -2.237607386;
                    a4 = -1.6368203505;
                    break;
                case 3:
                    GAIN = 49.79149609;
                    // 3.5 -4
                    a1 = -0.6413515381;
                    a2 = -2.0358795661;
                    a3 = -3.2023696139;
                    a4 = -2.5494074657;
                    break;
                case 4:
                    GAIN = 49.77828606;
                    // 4 - 4.5
                    a1 = -0.6413515381;
                    a2 = -2.5653579122;
                    a3 = -4.1671318418;
                    a4 = -3.2124408155;
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// shifts in next value to IIR-filter set
        /// </summary>
        /// <param name="input">Input value</param>
        /// <returns>IIR filter set output</returns>
        double ShiftNextDoubleToIIRFilter(double input)
        {
            xv[0] = xv[1];
            xv[1] = xv[2];
            xv[2] = xv[3];
            xv[3] = xv[4];
            xv[4] = input / GAIN;
            yv[0] = yv[1];
            yv[1] = yv[2];
            yv[2] = yv[3];
            yv[3] = yv[4];
            yv[4] = (xv[0] + xv[4]) - 2 * xv[2] + (a1 * yv[0]) + (a2 * yv[1]) + (a3 * yv[2]) + (a4 * yv[3]);
            return yv[4];
        }
    }
}


 


// -------------------------------------------------------------------------------------------------
//
//      This is a IIR-Filter Playground
//      Notes: IIR-Filter output needs to stabilize. Discard a certain amount of output values
//      before taking result in consideration
//    
// -------------------------------------------------------------------------------------------------

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class IIR_Playground : Indicator
    {
        const int NZEROS = 4;
        const int NPOLES = 4;

        double[] xv = new double[NZEROS + 1];
        double[] yv = new double[NPOLES + 1];

        double GAIN = 0;
        double a1 = 0;
        double a2 = 0;
        double a3 = 0;
        double a4 = 0;

        [Parameter("Filter Set", DefaultValue = 1)]
        public int filter { get; set; }

        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Output("Result", LineColor = "Orange")]
        public IndicatorDataSeries Result { get; set; }

        /// <summary>
        /// Called from cTrader
        /// </summary>
        /// <param name="index"></param>
        public override void Calculate(int index)
        {
            Result[index] = ShiftNextDoubleToIIRFilter(Source[index]);
        }

        /// <summary>
        /// Called from cTrader
        /// </summary>
        protected override void Initialize()
        {
            switch (filter)
            {
                case 0:
                    GAIN = 180.4096236;
                    a1 = -0.8008026467;
                    a2 = 3.1325885702;
                    a3 = -4.8527941875;
                    a4 = 3.5017954271;
                    break;
                case 1:
                    GAIN = 49.79245061;
                    // 2.5 - 3
                    a1 = -0.6413515381;
                    a2 = -0.4504011831;
                    a3 = -1.6413515381;
                    a4 = -0.5640098549;
                    break;
                case 2:
                    GAIN = 49.79238683;
                    // 3 - 3.5
                    a1 = -0.6413515381;
                    a2 = -1.3071151433;
                    a3 = -2.237607386;
                    a4 = -1.6368203505;
                    break;
                case 3:
                    GAIN = 49.79149609;
                    // 3.5 -4
                    a1 = -0.6413515381;
                    a2 = -2.0358795661;
                    a3 = -3.2023696139;
                    a4 = -2.5494074657;
                    break;
                case 4:
                    GAIN = 49.77828606;
                    // 4 - 4.5
                    a1 = -0.6413515381;
                    a2 = -2.5653579122;
                    a3 = -4.1671318418;
                    a4 = -3.2124408155;
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// shifts in next value to IIR-filter set
        /// </summary>
        /// <param name="input">Input value</param>
        /// <returns>IIR filter set output</returns>
        double ShiftNextDoubleToIIRFilter(double input)
        {
            xv[0] = xv[1];
            xv[1] = xv[2];
            xv[2] = xv[3];
            xv[3] = xv[4];
            xv[4] = input / GAIN;
            yv[0] = yv[1];
            yv[1] = yv[2];
            yv[2] = yv[3];
            yv[3] = yv[4];
            yv[4] = (xv[0] + xv[4]) - 2 * xv[2] + (a1 * yv[0]) + (a2 * yv[1]) + (a3 * yv[2]) + (a4 * yv[3]);
            return yv[4];
        }
    }
}


WE
westend.trading

Joined on 12.08.2018

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: IIR_Playground.algo
  • Rating: 0
  • Installs: 1387
  • Modified: 13/10/2021 09:55
Comments
Log in to add a comment.
SO
solfamido_jb · 4 years ago

Sorry but, what is this?