Category Trend  Published on 06/11/2020

SuperTrend Deviation Histogram

Description

 

A picture speaks more than a thousand words. 



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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AutoRescale = true, AccessRights = AccessRights.None)]

    public class SuperTrendDeviation : Indicator
    {

        // ---------------------------------------------------------------------------------------------------------------

        [Parameter("MA Period", DefaultValue = 25)]

        public int MAPeriod { get; set; }

        [Parameter("Deviation", DefaultValue = 5)]
        public int Deviation { get; set; }

        [Parameter("Data")]
        public DataSeries Data { get; set; }


        // ---------------------------------------------------------------------------------------------------------------

        [Output("Up Histogram", LineColor = "Aqua", PlotType = PlotType.Histogram)]

        public IndicatorDataSeries Results { get; set; }

        [Output("Down Histogram", PlotType = PlotType.Histogram, LineColor = "Red")]

        public IndicatorDataSeries Results2 { get; set; }


        /*
        [Output("Flat Line", PlotType = PlotType.Histogram,  LineColor = "White")]

        public IndicatorDataSeries Results3 { get; set; }
        */


        // ---------------------------------------------------------------------------------------------------------------

        private StandardDeviation stdev;

        private MovingAverage maPrice;

        private MovingAverage maSuper;

        private IndicatorDataSeries st;


        protected override void Initialize()
        {
            // Initialize and create nested indicators

            st = CreateDataSeries();

            stdev = Indicators.StandardDeviation(Data, Deviation, MovingAverageType.Simple);
            maPrice = Indicators.MovingAverage(Data, 1, MovingAverageType.Simple);

            maSuper = Indicators.MovingAverage(Data, MAPeriod, MovingAverageType.Simple);

        }

        public override void Calculate(int index)
        {
            // Calculate value at specified index


            var lastST = st[index - 1];

            var lastST2 = st[index - 2];


            if (maPrice.Result[index] > maSuper.Result[index])
            {

                if (double.IsNaN(lastST))
                {
                    st[index] = Math.Max((Bars.LowPrices[index] - stdev.Result[index]), Bars.ClosePrices[index - 1]);
                }

                else
                {
                    st[index] = Math.Max((Bars.LowPrices[index] - stdev.Result[index]), st[index - 1]);
                }

            }

            if (maPrice.Result[index] < maSuper.Result[index])
            {

                if (double.IsNaN(lastST))
                {
                    st[index] = Math.Min((Bars.HighPrices[index] + stdev.Result[index]), Bars.ClosePrices[index]);

                }
                else
                {

                    st[index] = Math.Min((Bars.HighPrices[index] + stdev.Result[index]), st[index - 1]);

                }
            }

            if (maPrice.Result[index] == maSuper.Result[index])
            {
                st[index] = st[index - 1];
            }


            if (st[index] > st[index - 1])
            {
                Results[index] = 1;
                Results2[index] = 0;
            }

            else if (st[index] < st[index - 1])
            {
                Results2[index] = 1;
                Results[index] = 0;
            }
            else if (st[index] == st[index - 1])
            {

                if (Results[index - 1] == 1)
                {
                    Results[index] = 1;
                    Results2[index] = 0;
                }

                if (Results2[index - 1] == 1)
                {
                    Results[index] = 0;
                    Results2[index] = 1;
                }
            }


        }
    }

}


AN
andurei

Joined on 30.09.2020

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Super Trend Histogram.algo
  • Rating: 5
  • Installs: 2284
  • Modified: 13/10/2021 09:55
Comments
Log in to add a comment.
No comments found.