Category Oscilators  Published on 11/08/2023

Momentum Average Levels indicator

An update for this algorithm is currently pending moderation. Please revisit this page shortly to access the algorithm's latest version.
Description

The Momentum indicator is calculated as a ratio of current price to the price from several n periods ago.

As a market peaks, the Momentum indicator climbs sharply and then falls off diverging from the continued upward or sideways movement of the price. Similarly, at a market bottom, Momentum drops sharply and then begins to climb well ahead of prices. Both of these situations result in divergences between the indicator and prices.

In this version, since the indicator components have two levels, it shows the advancing sentiment of momentum within a number of Levels periods.




mfejza's avatar
mfejza

Joined on 25.01.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: mMomentumAverage.algo
  • Rating: 5
  • Installs: 572
  • Modified: 11/08/2023 08:36
Comments
Log in to add a comment.
mfejza's avatar
mfejza · 1 year ago
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo
{
    [Cloud("Shade", "Shade0", FirstColor = "Green", SecondColor = "Red", Opacity = 0.1)]
    [Indicator(AccessRights = AccessRights.None)]
    public class mMomentumAverage : Indicator
    {
        [Parameter("Momentum Period (21)", DefaultValue = 21)]
        public int inpPeriodMomentum { get; set; }
        [Parameter("Average Period (10)", DefaultValue = 10)]
        public int inpPeriodAvgerage { get; set; }
        [Parameter("Average Smooth Type (sma)", DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType inpAverageSmoothType { get; set; }
        [Parameter("Level Period (10)", DefaultValue = 10)]
        public int inpPeriodLevel { get; set; }

        [Output("Momentum", LineColor = "Black", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outMom { get; set; }
        [Output("LevelUp", LineColor = "Green", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outMomLevelUp { get; set; }
        [Output("LevelDown", LineColor = "Red", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outMomLevelDown { get; set; }
        [Output("Shade", LineColor = "Transparent", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outShade { get; set; }
        [Output("Shade0", LineColor = "Transparent", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outShade0 { get; set; }
        
        private double _alpha;
        private MovingAverage _average;
        private MomentumOscillator _momentum;
        private IndicatorDataSeries _mom, _levelup, _leveldn;
        
        
        protected override void Initialize()
        {
            _alpha = 2.0 / (1.0 + inpPeriodLevel);
            _average = Indicators.MovingAverage(Bars.ClosePrices, inpPeriodAvgerage, inpAverageSmoothType);
            _momentum = Indicators.MomentumOscillator(_average.Result, inpPeriodMomentum);
            _mom = CreateDataSeries();
            _levelup = CreateDataSeries();
            _leveldn = CreateDataSeries();
        }

        public override void Calculate(int i)
        {
            _mom[i] = i>inpPeriodMomentum ? _momentum.Result[i] : 100;
            _levelup[i]  = i>inpPeriodMomentum ? (_mom[i] < _leveldn[i-1]) ? _levelup[i-1] : _levelup[i-1] + _alpha * (_mom[i] - _levelup[i-1]) : _mom[i];
            _leveldn[i]  = i>inpPeriodMomentum ? (_mom[i] > _levelup[i-1]) ? _leveldn[i-1] : _leveldn[i-1] + _alpha * (_mom[i] - _leveldn[i-1]) : _mom[i];
        
            outMom[i] = _mom[i];
            outMomLevelUp[i] = _levelup[i];
            outMomLevelDown[i] = _leveldn[i];
            outShade[i] = _mom[i] > _levelup[i] || _mom[i] < _leveldn[i] ? _mom[i] : 100.0;
            outShade0[i] = _mom[i] > _levelup[i] ? _levelup[i] : _mom[i] < _leveldn[i] ? _leveldn[i] : 100.0;
        }
    }
}