SuperTrend Deviation Histogram free

by andurei in category Trend at 06/11/2020
Description

 

A picture speaks more than a thousand words. 

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 = 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;
                }
            }


        }
    }

}
Comments
5