Topics
28 Sep 2015, 09:33
 3060
 3
Replies

lunarus
28 Sep 2015, 13:34

I made a correction & it looks identical to the results in the other platform but I wasn't sure if the codes was exactly correct

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, ScalePrecision = 5)]
    public class UniversalOscillator : Indicator
    {
        #region Variables

        private IndicatorDataSeries _filt;
        private IndicatorDataSeries _wn;
        private IndicatorDataSeries _pk;
        private double a1;
        private double b1;
        private double c1;
        private double c2;
        private double c3;



        #endregion

        #region Parameters

        [Output("Universal", Color = Colors.Red, IsHistogram = true)]
        public IndicatorDataSeries Universal { get; set; }


        [Parameter("BandEdge", DefaultValue = 20)]
        public int Period { get; set; }

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

        #endregion

        protected override void Initialize()
        {
            _filt = CreateDataSeries();
            _wn = CreateDataSeries();
            _pk = CreateDataSeries();

            a1 = Math.Exp(-Math.Sqrt(2) * Math.PI / Period);
            b1 = 2 * a1 * Math.Cos(Math.Sqrt(2) * Math.PI / Period);
            c2 = b1;
            c3 = -a1 * a1;
            c1 = 1 - c2 - c3;

            _wn[0] = 0;
            _wn[1] = 0;

            _filt[0] = 0;
            _filt[1] = 0;

            _pk[0] = 1E-07;
            _pk[1] = 1E-07;

        }

        public override void Calculate(int index)
        {

            if (index < 2)
                return;

            _wn[index] = (Source[index] - Source[index - 2]) / 2;

            _filt[index] = c1 * (_wn[index] + _wn[index - 1]) / 2 + c2 * _filt[index - 1] + c3 * _filt[index - 2];

            _pk[index] = 0.991 * _pk[index - 1];
            if (Math.Abs(_filt[index]) > _pk[index])
                _pk[index] = Math.Abs(_filt[index]);
            if (_pk[index] != 0)
                Universal[index] = _filt[index] / _pk[index];

        }
    }
}

 


@lunarus