Category Trend  Published on 30/08/2020

Supertrend MTF

Description

It was bit disappointing that Out of the box Supertrend indicator is not multi time frame. Hence, I created one. Thanks to FireMyst for helping me figure out issues with my logic :)


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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class SupertrendMTF : Indicator
    {
        [Parameter(DefaultValue = "Daily")]
        public TimeFrame timeFrame { get; set; }

        [Parameter(DefaultValue = 10)]
        public int Period { get; set; }

        [Parameter(DefaultValue = 3.0)]
        public double Multiplier { get; set; }

        [Output("UpTrend", LineColor = "Green", PlotType = PlotType.Points, Thickness = 3)]
        public IndicatorDataSeries UpTrend { get; set; }

        [Output("DownTrend", LineColor = "Red", PlotType = PlotType.Points, Thickness = 3)]
        public IndicatorDataSeries DownTrend { get; set; }

        private IndicatorDataSeries _upBuffer;
        private IndicatorDataSeries _downBuffer;
        private AverageTrueRange _averageTrueRange;
        private int[] _trend;
        private bool _changeofTrend;
        private Bars customBars;
        private double median, atr;

        protected override void Initialize()
        {
            customBars = MarketData.GetBars(timeFrame);
            _trend = new int[1];
            _upBuffer = CreateDataSeries();
            _downBuffer = CreateDataSeries();
            _averageTrueRange = Indicators.AverageTrueRange(customBars, Period, MovingAverageType.Simple);
        }


        public override void Calculate(int index)
        {
            UpTrend[index] = double.NaN;
            DownTrend[index] = double.NaN;

            int customIndex = customBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
            median = (customBars.HighPrices[customIndex] + customBars.LowPrices[customIndex]) / 2;
            atr = _averageTrueRange.Result[customIndex];

            _upBuffer[index] = median + Multiplier * atr;
            _downBuffer[index] = median - Multiplier * atr;


            if (index < 1)
            {
                _trend[index] = 1;
                return;
            }

            Array.Resize(ref _trend, _trend.Length + 1);

            if (customBars.ClosePrices[customIndex] > _upBuffer[index - 1])
            {
                _trend[index] = 1;
                if (_trend[index - 1] == -1)
                    _changeofTrend = true;
            }
            else if (customBars.ClosePrices[customIndex] < _downBuffer[index - 1])
            {
                _trend[index] = -1;
                if (_trend[index - 1] == -1)
                    _changeofTrend = true;
            }
            else if (_trend[index - 1] == 1)
            {
                _trend[index] = 1;
                _changeofTrend = false;
            }
            else if (_trend[index - 1] == -1)
            {
                _trend[index] = -1;
                _changeofTrend = false;
            }

            if (_trend[index] < 0 && _trend[index - 1] > 0)
                _upBuffer[index] = median + (Multiplier * atr);
            else if (_trend[index] < 0 && _upBuffer[index] > _upBuffer[index - 1])
                _upBuffer[index] = _upBuffer[index - 1];

            if (_trend[index] > 0 && _trend[index - 1] < 0)
                _downBuffer[index] = median - (Multiplier * atr);
            else if (_trend[index] > 0 && _downBuffer[index] < _downBuffer[index - 1])
                _downBuffer[index] = _downBuffer[index - 1];

            if (_trend[index] == 1)
            {
                UpTrend[index] = _downBuffer[index];
                if (_changeofTrend)
                {
                    UpTrend[index - 1] = DownTrend[index - 1];
                    _changeofTrend = false;
                }
            }
            else if (_trend[index] == -1)
            {
                DownTrend[index] = _upBuffer[index];
                if (_changeofTrend)
                {
                    DownTrend[index - 1] = UpTrend[index - 1];
                    _changeofTrend = false;
                }
            }
        }
    }
}


VO
voldemort

Joined on 10.07.2020

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: SupertrendMTF.algo
  • Rating: 0
  • Installs: 2668
Comments
Log in to add a comment.
DA
dave.anderson.consulting · 3 years ago

First of all I would like to thank the developer of this indicator and for making the code available.

But .... a very important but ....

There is a big issue in this indicator. The higher timeframe dots do not repaint on the lower timeframe!
If you put an hourly timeframe of this indicator on the M15 chart, you will see that the dots of hourly are up and down on the previous M15 bars.


No matter what timeframe, during the formation of the last bar, every indicator should repaint on that last bar. For the same reason on the MTF version of every indicator, the indicator should repaint on the last bar of the higher timeframe on lower charts.


So in case of above example, all of the dots of the hourly supertrend should repaint on the M15 chart for that hour. Until the hourly bar is closed.

In short, do not use this indicator until the repaint issue is resolved.

nguyenluat.6996's avatar
nguyenluat.6996 · 3 years ago

Many thanks, that's what I'm looking for.

If I could not find it, I would build it by my self!