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 (20)", DefaultValue = 20)]
        public int inpPeriodCCI { get; set; }
        [Parameter("Smooth Period CCI (1)", 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; }
        [Output("OpenLong", LineColor = "Black", PlotType = PlotType.Points, Thickness = 3)]
        public IndicatorDataSeries outOpenLong { get; set; }
        [Output("OpenShort", LineColor = "Black", PlotType = PlotType.Points, Thickness = 3)]
        public IndicatorDataSeries outOpenShort { get; set; }

        private IndicatorDataSeries _price, _cci, _max, _min, _hh, _ll, _vwapmul, _vwapclose, _mul, _lead, _lag, _vwapleadlag;
        private MovingAverage _sma, _ema1, _ema2;
        private StandardDeviation _std, _stdposneg;


        protected override void Initialize()
        {
            _price = CreateDataSeries();
            _cci = CreateDataSeries();
            _max = CreateDataSeries();
            _min = CreateDataSeries();
            _hh = CreateDataSeries();
            _ll = CreateDataSeries();
            _sma = Indicators.MovingAverage(_price, inpPeriodCCI, MovingAverageType.Simple);
            _std = Indicators.StandardDeviation(_price, inpPeriodCCI, MovingAverageType.Simple);
            _ema1 = Indicators.MovingAverage(_cci, SmoothCCI, MovingAverageType.Exponential);
            _ema2 = Indicators.MovingAverage(_ema1.Result, SmoothCCI, MovingAverageType.Exponential);
            _stdposneg = Indicators.StandardDeviation(_ema2.Result, inpPeriodCCI, MovingAverageType.Simple);
            _vwapmul = CreateDataSeries();
            _vwapclose = CreateDataSeries();
            _mul = CreateDataSeries();
            _lead = CreateDataSeries();
            _lag = CreateDataSeries();
            _vwapleadlag = CreateDataSeries();
        }

        public override void Calculate(int i)
        {
            _price[i] = i>1 ? Bars.ClosePrices[i] - Bars.ClosePrices[i-1] : Bars.ClosePrices[i] - Bars.TypicalPrices[i];
            _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]);
            _hh[i] = i>inpPeriodCCI ? _max.Maximum(inpPeriodCCI) : _max[i];
            _ll[i] = i>inpPeriodCCI ? _min.Minimum(inpPeriodCCI) : _min[i];

            outMain[i] = _ema2.Result[i];
            outUp[i] = _hh[i];
            outDown[i] = _ll[i];
            
            /////////////////////////////////////////////////////////////////
            _mul[i] = Bars.ClosePrices[i] * Bars.ClosePrices[i];
            _vwapmul[i] = ((_mul.Sum(inpPeriodCCI) * Bars.TickVolumes.Sum(inpPeriodCCI)) / Bars.TickVolumes.Sum(inpPeriodCCI)) / inpPeriodCCI;
            _vwapclose[i] = ((Bars.ClosePrices.Sum(inpPeriodCCI) * Bars.TickVolumes.Sum(inpPeriodCCI)) / Bars.TickVolumes.Sum(inpPeriodCCI)) / inpPeriodCCI;
            _lead[i] = _vwapmul[i] / _vwapclose[i];
            _lag[i] = ((Bars.TypicalPrices.Sum(inpPeriodCCI) * Bars.TickVolumes.Sum(inpPeriodCCI)) / Bars.TickVolumes.Sum(inpPeriodCCI)) / inpPeriodCCI;
            _vwapleadlag[i] = _lead[i] - _lag[i];
            
            outOpenLong[i] = i>5 && _ema2.Result[i-1] < _ll[i-1] && _ema2.Result[i] > _hh[i] && _vwapleadlag[i] > 0 ? +110 : double.NaN;
            outOpenShort[i] = i>5 && _ema2.Result[i-1] > _hh[i-1] && _ema2.Result[i] < _ll[i] && _vwapleadlag[i] < 0 ? -110 : double.NaN;
        }
    }
}


mfejza's avatar
mfejza

Joined on 25.01.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: mDynamicRangeCCI.algo
  • Rating: 5
  • Installs: 23
  • Modified: 03/12/2024 20:28
Comments
Log in to add a comment.
No comments found.