Category Trend  Published on 21/02/2020

Squeeze Momentum Indicator

Description

Conversion from LazyBear's script: 

 


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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SqueezeMomentumIndicator : Indicator
    {
        [Parameter("BB Period", DefaultValue = 20)]
        public int length { get; set; }
        [Parameter("BB Deviation", DefaultValue = 2)]
        public double mult { get; set; }
        [Parameter("KC Period", DefaultValue = 20)]
        public int lengthKC { get; set; }
        [Parameter("KC Deviation", DefaultValue = 1.5)]
        public double multKC { get; set; }

        [Output("Main", PlotType = PlotType.Points, Thickness = 0, LineColor = "Black")]
        public IndicatorDataSeries Result { get; set; }
        [Output("Bullish Exp", PlotType = PlotType.Histogram, Thickness = 3, LineColor = "Lime")]
        public IndicatorDataSeries BullExp { get; set; }
        [Output("Bullish Con", PlotType = PlotType.Histogram, Thickness = 3, LineColor = "Green")]
        public IndicatorDataSeries BullCon { get; set; }
        [Output("Bearish Exp", PlotType = PlotType.Histogram, Thickness = 3, LineColor = "Red")]
        public IndicatorDataSeries BearExp { get; set; }
        [Output("Bearish Con", PlotType = PlotType.Histogram, Thickness = 3, LineColor = "DarkRed")]
        public IndicatorDataSeries BearCon { get; set; }

        [Output("Squeeze On", PlotType = PlotType.Points, Thickness = 3, LineColor = "Yellow")]
        public IndicatorDataSeries SqueezeOn { get; set; }
        [Output("Squeeze Off", PlotType = PlotType.Points, Thickness = 3, LineColor = "Blue")]
        public IndicatorDataSeries SqueezeOff { get; set; }

        BollingerBands BB;
        KeltnerChannels KC;
        IndicatorDataSeries Difference;
        TimeSeriesMovingAverage TSMA;

        protected override void Initialize()
        {
            BB = Indicators.BollingerBands(Bars.ClosePrices, length, mult, MovingAverageType.Simple);
            KC = Indicators.KeltnerChannels(lengthKC, MovingAverageType.Simple, lengthKC, MovingAverageType.Simple, multKC);
            Difference = CreateDataSeries();
            TSMA = Indicators.TimeSeriesMovingAverage(Difference, lengthKC);
        }

        public override void Calculate(int index)
        {
            bool sqzOn = BB.Bottom[index] > KC.Bottom[index] && BB.Top[index] < KC.Top[index];
            bool sqzOff = BB.Bottom[index] < KC.Bottom[index] && BB.Top[index] > KC.Top[index];
            bool noSqz = !sqzOn && !sqzOff;

            double HLAverage = (Bars.HighPrices.Maximum(lengthKC) + Bars.LowPrices.Minimum(lengthKC)) / 2;
            double SecondAverage = (HLAverage + KC.Main[index]) / 2;

            Difference[index] = Bars.ClosePrices[index] - SecondAverage;

            Result[index] = TSMA.Result[index];

            if (Result[index] > 0)
                if (Result[index] > Result[index - 1])
                {
                    BullExp[index] = Result[index];
                    BullCon[index] = 0;
                }
                else
                {
                    BullExp[index] = 0;
                    BullCon[index] = Result[index];
                }

            if (Result[index] <= 0)
                if (Result[index] > Result[index - 1])
                {
                    BearCon[index] = Result[index];
                    BearExp[index] = 0;
                }
                else
                {
                    BearExp[index] = Result[index];
                    BearCon[index] = 0;
                }

            if (sqzOn)
            {
                SqueezeOn[index] = 0;
                SqueezeOff[index] = double.NaN;
            }
            if (sqzOff)
            {
                SqueezeOff[index] = 0;
                SqueezeOn[index] = double.NaN;
            }
        }
    }
}


CY
cysecsbin.01

Joined on 10.11.2018 Blocked

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: SqueezeMomentumIndicator.algo
  • Rating: 0
  • Installs: 4352
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
No comments found.