Category Oscilators  Published on 10/07/2023

Disparity Index indicator

Description

The Disparity Index indicator is used to define price reversal levels.

The histogram bars (disparity index) when rising above the upper band, the overbought level is reached.
The histogram bars when decreasing below the lower band, the oversold level is reached.



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

namespace cAlgo
{
    [Levels(0)]
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class mDisparityIndex : Indicator
    {
        [Parameter("Periods (10)", DefaultValue = 10, Group = "DisparityIndex")]
        public int inpPeriods { get; set; }
        [Parameter("Levels Coefficients (2.618)", DefaultValue = 2.618, Group = "DisparityIndex")]
        public double inpLevelsCoefficients { get; set; }
        [Parameter("Smooth Type (sma)", DefaultValue = MovingAverageType.Simple, Group = "DisparityIndex")]
        public MovingAverageType inpSmoothType { get; set; }

        [Output("Disparity Index", LineColor = "Transparent", LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outDisparityIndex { get; set; }
        [Output("Band Up", LineColor = "Black", LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outDisparityIndexUp { get; set; }
        [Output("Band Down", LineColor = "Black", LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outDisparityIndexDown { get; set; }
        [Output("Histogram Up", PlotType = PlotType.Histogram, LineColor = "Green", Thickness = 3)]
        public IndicatorDataSeries outDisparityIndexUpC { get; set; }
        [Output("Histogram Down", PlotType = PlotType.Histogram, LineColor = "Red", Thickness = 3)]
        public IndicatorDataSeries outDisparityIndexDnC { get; set; }


        private IndicatorDataSeries _absroc, _di;
        private MovingAverage _maprice, _maabsroc;


        protected override void Initialize()
        {
            _absroc = CreateDataSeries();
            _di = CreateDataSeries();
            _maprice = Indicators.MovingAverage(Bars.ClosePrices, inpPeriods, inpSmoothType);
            _maabsroc = Indicators.MovingAverage(_absroc, inpPeriods, inpSmoothType);
        }

        public override void Calculate(int i)
        {
            _absroc[i] = Math.Abs(Bars.ClosePrices[i] - Bars.ClosePrices[i - 1]) / Bars.ClosePrices[i] * 100;

            _di[i] = 100 * (Bars.ClosePrices[i] - _maprice.Result[i]) / (_maprice.Result[i]);

            outDisparityIndex[i] = _di[i];
            outDisparityIndexUp[i] = _maabsroc.Result[i] * inpLevelsCoefficients;
            outDisparityIndexDown[i] = -_maabsroc.Result[i] * inpLevelsCoefficients;

            if (!double.IsNaN(_di[i]))
            {
                if (_di[i] > 0)
                {
                    outDisparityIndexUpC[i] = _di[i];
                    outDisparityIndexDnC[i] = double.NaN;
                }
                else
                {
                    outDisparityIndexUpC[i] = double.NaN;
                    outDisparityIndexDnC[i] = _di[i];
                }
            }
            else
            {
                outDisparityIndexUpC[i] = double.NaN;
                outDisparityIndexDnC[i] = double.NaN;
            }
        }
    }
}


mfejza's avatar
mfejza

Joined on 25.01.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: mDisparityIndex.algo
  • Rating: 5
  • Installs: 1310
  • Modified: 13/09/2022 19:18
Comments
Log in to add a comment.
mfejza's avatar
mfejza · 10 months ago

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

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class DemoDisparityIndex : Robot
    {
        [Parameter("Periods (10)", DefaultValue = 10, Group = "DisparityIndex")]
        public int inpPeriods { get; set; }
        [Parameter("Levels Coefficients (2.618)", DefaultValue = 2.618, Group = "DisparityIndex")]
        public double inpLevelsCoefficients { get; set; }
        [Parameter("Smooth Type (sma)", DefaultValue = MovingAverageType.Simple, Group = "DisparityIndex")]
        public MovingAverageType inpSmoothType { get; set; }
        
        private mDisparityIndex _di;
        

        protected override void OnStart()
        {
            _di = Indicators.GetIndicator<mDisparityIndex>(inpPeriods, inpLevelsCoefficients, inpSmoothType);
        }

        protected override void OnTick()
        {
            if(_di.outDisparityIndex.LastValue > _di.outDisparityIndexUp.LastValue)
            {
                //Long zone
            }
            
            if(_di.outDisparityIndex.LastValue < _di.outDisparityIndexDown.LastValue)
            {
                //Short zone
            }
        }

        protected override void OnStop()
        {
            _di = null;
        }
    }
}