Category Oscilators  Published on 08/09/2022

Dynamic Range CCI

Description

This CCI (Commodity Index Channel) custom indicator show/correct the CCI indicator levels -100 and +100, based on standard deviation on indicator value



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

namespace cAlgo
{
    [Levels(-100, +100, 0)]
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class mDynamicRangeCCI : Indicator
    {
        [Parameter("Main Period CCI", DefaultValue = 20)]
        public int PeriodCCI { get; set; }
        [Parameter("Smooth Period CCI", DefaultValue = 1)]
        public int SmoothCCI { get; set; }


        [Output("Main", LineColor = "Black", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outMain { get; set; }
        [Output("Up", LineColor = "Red", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outUp { get; set; }
        [Output("Down", LineColor = "Red", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outDown { get; set; }


        private IndicatorDataSeries _price;
        private MovingAverage _sma;
        private StandardDeviation _std;
        private IndicatorDataSeries _cci;
        private MovingAverage _ema1;
        private MovingAverage _ema2;
        private StandardDeviation _stdposneg;
        private IndicatorDataSeries _max;
        private IndicatorDataSeries _min;
        private IndicatorDataSeries _h;
        private IndicatorDataSeries _l;


        protected override void Initialize()
        {
            _price = CreateDataSeries();
            _cci = CreateDataSeries();
            _sma = Indicators.MovingAverage(_price, PeriodCCI, MovingAverageType.Simple);
            _std = Indicators.StandardDeviation(_price, PeriodCCI, MovingAverageType.Simple);
            _ema1 = Indicators.MovingAverage(_cci, SmoothCCI, MovingAverageType.Exponential);
            _ema2 = Indicators.MovingAverage(_ema1.Result, SmoothCCI, MovingAverageType.Exponential);
            _stdposneg = Indicators.StandardDeviation(_ema2.Result, PeriodCCI, MovingAverageType.Simple);
            _max = CreateDataSeries();
            _min = CreateDataSeries();
            _h = CreateDataSeries();
            _l = CreateDataSeries();
        }

        public override void Calculate(int i)
        {
            if (i < PeriodCCI + 2)
            {
                _price[i] = Symbol.PipSize * 100;
                _cci[i] = Symbol.PipSize;
                outMain[i] = 0;
                return;
            }

            _price[i] = Bars.ClosePrices[i] - Bars.ClosePrices[i - 1];
            _cci[i] = (_price[i] - _sma.Result[i]) / (0.015 * _std.Result[i]);

            _max[i] = Math.Max(_stdposneg.Result[i], -_stdposneg.Result[i]);
            _min[i] = Math.Min(_stdposneg.Result[i], -_stdposneg.Result[i]);
            _h[i] = _max.Maximum(PeriodCCI);
            _l[i] = _min.Minimum(PeriodCCI);

            outMain[i] = _ema2.Result[i];
            outUp[i] = _h[i];
            outDown[i] = _l[i];
        }
    }
}


mfejza's avatar
mfejza

Joined on 25.01.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: mDynamicRangeCCI.algo
  • Rating: 5
  • Installs: 692
Comments
Log in to add a comment.
No comments found.