Category Oscilators  at 11/08/2023

Momentum Average Levels indicator

Notice. There is an update for this algorithm which is awaiting moderation. Please check back soon to view the latest version of this algorithm.
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 25.01.2022

  • Type: Free
  • Language: C#
  • Trading Platform: cTrader Automate
  • Filename: mMomentumAverage.algo
  • Rating: 5
  • Installs: 192
Comments
Only logged in users can post a comment
mfejza's avatar
mfejza · 3 months 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;
        }
    }
}