Category Oscilators  Published on 09/01/2023

Directional Volatility indicator

Description

Directional Volatility indicator. The indicator displays two volatility lines; bullish (green component) and bearish (red component).


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

namespace cAlgo
{
    [Levels(0)]
    [Indicator(IsOverlay = false, AutoRescale = false, AccessRights = AccessRights.None)]
    public class mDirectionalVolatility : Indicator
    {
        [Parameter("Periods (14)", DefaultValue = 14)]
        public int inpPeriods { get; set; }
        [Parameter("Deviation (3.0)", DefaultValue = 3.0)]
        public double inpDeviation { get; set; }

        [Output("DirectionalVolatility Bullish", LineColor = "Green", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outDirectionalVolatilityBullish { get; set; }
        [Output("DirectionalVolatility Bearish", LineColor = "Red", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outDirectionalVolatilityBearish { get; set; }
        
        private IndicatorDataSeries _pricelong, _priceshort;
        private MovingAverage _malong, _mashort;
        private StandardDeviation _stddevmalong, _stddevmashort;
        

        protected override void Initialize()
        {
            _pricelong = CreateDataSeries();
            _priceshort = CreateDataSeries();
            _malong = Indicators.MovingAverage(_pricelong, inpPeriods, MovingAverageType.Exponential);
            _mashort = Indicators.MovingAverage(_priceshort, inpPeriods, MovingAverageType.Exponential);
            _stddevmalong = Indicators.StandardDeviation(_malong.Result, inpPeriods, MovingAverageType.Simple);
            _stddevmashort = Indicators.StandardDeviation(_mashort.Result, inpPeriods, MovingAverageType.Simple);
        }

        public override void Calculate(int i)
        {
            _priceshort[i] = Bars.ClosePrices[i-1] - Bars.LowPrices[i];
            _pricelong[i] = Bars.HighPrices[i] - Bars.ClosePrices[i-1];
            
            outDirectionalVolatilityBullish[i] = (_malong.Result[i] + inpDeviation * _stddevmalong.Result[i]) / Symbol.TickSize;
            outDirectionalVolatilityBearish[i] = (_mashort.Result[i] + inpDeviation * _stddevmashort.Result[i]) / Symbol.TickSize;        
        }
    }
}

mfejza's avatar
mfejza

Joined on 25.01.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: mDirectionalVolatility.algo
  • Rating: 5
  • Installs: 700
  • Modified: 09/01/2023 00:33
Comments
Log in to add a comment.
No comments found.