Category Trend  Published on 06/06/2014

Supertrend

Description

Trend Indicator showing an Up Trend when the current close price is higher than the main trend line in uptrend and a Down Trend when the current close price is lower than main trend line in downtrend. The main trend line in uptrend equals to the median price plus a multiple of the average true range and the main line in downtrend equals the median price minus the multiple of the average true range.


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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class Supertrend : Indicator
    {
        [Parameter(DefaultValue = 10)]
        public int Period { get; set; }

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

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

        [Output("DownTrend", Color = Colors.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;

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

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

            double median = (MarketSeries.High[index] + MarketSeries.Low[index]) / 2;
            double atr = _averageTrueRange.Result[index];

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


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

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

            // Main Logic
            if (MarketSeries.Close[index] > _upBuffer[index - 1])
            {
                _trend[index] = 1;
                if (_trend[index - 1] == -1)
                    _changeofTrend = true;
            }
            else if (MarketSeries.Close[index] < _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];

            // Draw Indicator
            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;
                }
            }
        }
    }
}


SP
spka111

Joined on 16.08.2012

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Supertrend.algo
  • Rating: 5
  • Installs: 18130
  • Modified: 13/10/2021 09:55
Comments
Log in to add a comment.
EM
emil.kupco · 3 years ago

Is not there a mistake at line 67?

IA
IandelMar · 4 years ago

For Lines only change line 16,19 into PlotType.DiscontinuousLine

GN
gnsmaciej · 5 years ago

ale someone ablle to add an alert sound by changing the direction of super trend ? 

20
2020children · 6 years ago

there is stop loss and take profit (at 5 and 20) for example). what is this number mean is it 5 bars or 5 minuetes or 5 Pips or what exactly

GH
ghazisameer · 6 years ago

I also prefer it to lines rather than dots. Please help in this regard

 

NI
nicolalazzarin80 · 6 years ago

HI at all, i'm new here and i need help..

Some one can tell me in wich way i can change Supertrend indicator from Dots ti Lines?

Thanks

 

VI
Vince · 6 years ago

Hi spka111,

I modified a little of your Indicator and referenced to you. If you do not want it referenced or displayed in the indicator section please say so and I will remove it.

https://ctrader.com/algos/indicators/show/1775

Kind regards,

Vince

cAlgo_Fanatic's avatar
cAlgo_Fanatic · 11 years ago
Please use the button "Add Reference" to locate and add the AverageTrueRange.algo file to the Supertrend. Make sure the AverageTrueRange is located in the correct folder and that it has been build first.
LI
Lipi · 11 years ago
Hello, do you have any idea why I get this error message when I try to build the supertrend indicator? "Error: Could not find file: "C\Users....etc...\indicators\AvarageTrueRange.algo" I added the Avarage True Range Indicator, but I get this error message :( Thanks!
SP
spka111 · 11 years ago
Please download again. Fixed a small issue with the output.