SuperTrend Standard Deviation Edition free

Description
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgocTrader

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] = ...



        }
    }

}
Comments
0