Category Oscilators  Published on 03/04/2023

Momentum Tendency indicator

Description

This indicator measures the price tendency in a correlated dependency with the logarithmic detrended momentum of price and detrended price.

Use the long zone when the indicator value is +3 and the short zone when the indicator value is -3.

Use stars to identify the opposite overstretch of the price (oversold/overbought) in correlation with the trading zone.


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

namespace cAlgo
{
    [Levels(0)]
    [Indicator(IsOverlay = false, AccessRights = AccessRights.None)]
    public class mMomentumTendency : Indicator
    {
        [Parameter("Periods (14)", DefaultValue = 14)]
        public int inpPeriods { get; set; }
        [Parameter("Periods (3)", DefaultValue = 3)]
        public int inpLookBack { get; set; }
        [Parameter("Smooth Type (vid)", DefaultValue = MovingAverageType.VIDYA)]
        public MovingAverageType inpSmoothType { get; set; }
        [Parameter("Show Arrows ", DefaultValue = true)]
        public bool inpShowArrows { get; set; }

        [Output("MomentumTendency", LineColor = "Black", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outMomentumTendency { get; set; }
        
        private MovingAverage _ma;
        private IndicatorDataSeries _momentum, _log, _tendency;
        private DetrendedPriceOscillator _dpomom, _dpo;
        

        protected override void Initialize()
        {
            _ma = Indicators.MovingAverage(Bars.ClosePrices, inpPeriods, inpSmoothType);
            _log = CreateDataSeries();
            _momentum = CreateDataSeries();
            _dpomom = Indicators.DetrendedPriceOscillator(_momentum, inpPeriods, MovingAverageType.Simple);
            _dpo = Indicators.DetrendedPriceOscillator(Bars.ClosePrices, inpPeriods, MovingAverageType.Simple);
            _tendency = CreateDataSeries();
        }

        public override void Calculate(int i)
        {
            _log[i] = Math.Log(_ma.Result[i]);
            _momentum[i] = _log[i-1] - _log[i-inpLookBack];
            
            _tendency[i] = (_momentum[i] > 0 ? +1 : -1) + (_dpomom.Result[i] > 0 ? +1 : -1) + (_dpo.Result[i] > 0 ? +1 : -1);
            
            if(inpShowArrows)
            {
                if(_tendency[i] == +3 && Bars.OpenPrices[i] > Bars.ClosePrices[i])
                    Chart.DrawIcon("MomentumTendencyUp" + Bars.OpenTimes.Last(0).ToString(), ChartIconType.Star, Bars.OpenTimes.Last(0), Bars.LowPrices.Last(0), "Teal");
                else
                    Chart.RemoveObject("MomentumTendencyUp" + Bars.OpenTimes.Last(0).ToString());
                 
                if(_tendency[i] == -3 && Bars.OpenPrices[i] < Bars.ClosePrices[i])
                    Chart.DrawIcon("MomentumTendencyDown" + Bars.OpenTimes.Last(0).ToString(), ChartIconType.Star, Bars.OpenTimes.Last(0), Bars.HighPrices.Last(0), "Brown");
                else
                    Chart.RemoveObject("MomentumTendencyDown" + Bars.OpenTimes.Last(0).ToString());
            }
            
            outMomentumTendency[i] = _tendency[i];
        }
    }
}

mfejza's avatar
mfejza

Joined on 25.01.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: mMomentumTendency.algo
  • Rating: 5
  • Installs: 602
  • Modified: 03/04/2023 20:46
Comments
Log in to add a comment.
No comments found.