Category Trend  Published on 19/09/2020

SuperTrend Standard Deviation Edition

Description


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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, 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 Line", LineColor = "Green", PlotType = PlotType.DiscontinuousLine)]

        public IndicatorDataSeries Results { get; set; }

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

        public IndicatorDataSeries Results2 { get; set; }


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

        public IndicatorDataSeries Results3 { get; set; }


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

        private StdTest stdev;

        private MovingAverage maPrice;

        private MovingAverage maSuper;

        private IndicatorDataSeries st;


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

            st = CreateDataSeries();

            stdev = Indicators.GetIndicator<StdTest>(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] = st[index];
            }

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



            // Result[index] = ...



        }
    }

}


AN
andrey.thales18

Joined on 31.08.2020

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