Category Trend  Published on 08/09/2022

FIR (Finite Impulse Response Filter)

Description

Indicator FIR is a symmetrically weighted filter (Finite Impulse Response Filter)


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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class mFIR_htf : Indicator
    {
        [Parameter("Base Timeframe", DefaultValue = "Minute")]
        public TimeFrame prmTF1 { get; set; }
        [Parameter("Data Source (close)", DefaultValue = PriceTypes.Close)]
        public PriceTypes PriceType { get; set; }


        [Output("FIR - Finite Impulse Response Filter", LineColor = "Black", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries outFIR { get; set; }


        private Bars _barsTF1;
        private IndicatorDataSeries _price;
        private IndicatorDataSeries _fir;


        protected override void Initialize()
        {
            _barsTF1 = prmTF1 > Bars.TimeFrame ? MarketData.GetBars(prmTF1) : Bars;
            while (_barsTF1.OpenTimes[0] > Bars.OpenTimes[0])
                _barsTF1.LoadMoreHistory();

            _price = CreateDataSeries();
            _fir = CreateDataSeries();
        }

        public override void Calculate(int i)
        {
            if (i < 4)
            {
                _fir[i] = Bars.ClosePrices[i];
                return;
            }

            var idx = _barsTF1.OpenTimes.GetIndexByTime(Bars.OpenTimes[i]);
            if (idx != -1)
            {
                switch (PriceType)
                {
                    case PriceTypes.Open:
                        _price[i] = _barsTF1.OpenPrices[idx];
                        break;
                    case PriceTypes.Close:
                        _price[i] = _barsTF1.ClosePrices[idx];
                        break;
                    case PriceTypes.High:
                        _price[i] = _barsTF1.HighPrices[idx];
                        break;
                    case PriceTypes.Low:
                        _price[i] = _barsTF1.LowPrices[idx];
                        break;
                    case PriceTypes.Median:
                        _price[i] = _barsTF1.MedianPrices[idx];
                        break;
                    case PriceTypes.Typical:
                        _price[i] = _barsTF1.TypicalPrices[idx];
                        break;
                    case PriceTypes.Weighted:
                        _price[i] = _barsTF1.WeightedPrices[idx];
                        break;
                    default:
                        _price[i] = _barsTF1.ClosePrices[idx];
                        break;
                }

                _fir[i] = (_price[i] + 2.0 * _price[i - 1] + 2.0 * _price[i - 2] + _price[i - 3]) / 6.0;
            }

            outFIR[i] = _fir[i];
        }
    }

    public enum PriceTypes
    {
        Open,
        Close,
        High,
        Low,
        Median,
        Typical,
        Weighted
    }
}




mfejza's avatar
mfejza

Joined on 25.01.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: mFIR.algo
  • Rating: 5
  • Installs: 724
  • Modified: 08/09/2022 19:21
Comments
Log in to add a comment.
No comments found.