Super Trend Plus free

by kkostaki in category Trend at 29/08/2012
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. The exponential moving average equals the value of the Down Trend Indicator Dots in an uptrend  and the value of the Up Trend Dots in a downtrend.

 

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
//#reference: ..\Indicators\AverageTrueRange.algo

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true)]
    public class SuperTrendPlus : Indicator
    {
        private AverageTrueRange _averageTrueRange;
        private ExponentialMovingAverage _ema;
        private bool[] _trend;

        [Parameter()]
        public DataSeries Source { get; set; }

        [Parameter(DefaultValue = 1, MaxValue = 2, MinValue = 1)]
        public int Period { get; set; }

        [Parameter(DefaultValue = 1.618, MaxValue = 2.0, MinValue = 1.0)]
        public double Multiplier { get; set; }


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

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

        [Output("EMA")]
        public IndicatorDataSeries EMA { get; set; }


        protected override void Initialize()
        {
            _trend = new bool[1];
            _ema = Indicators.ExponentialMovingAverage(Source, Period);
            _averageTrueRange = Indicators.GetIndicator<AverageTrueRange>(Period);
        }

        public override void Calculate(int index)
        {
            double close = MarketSeries.Close[index];

            if (index < 1)
            {
                _trend[index] = true;
                UpTrend[index] = close;
                DownTrend[index] = close;

                return;
            }

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

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

            if (close > DownTrend[index - 1])
            {
                _trend[index] = true;

            }
            else if (close < UpTrend[index - 1])
            {
                _trend[index] = false;
            }
            else
            {
                _trend[index] = _trend[index - 1];
            }

            double lowerValue = median - _averageTrueRange.Result[index] * Multiplier;
            double upperValue = median + _averageTrueRange.Result[index] * Multiplier;

            if (_trend[index] && !_trend[index - 1])
            {
                UpTrend[index] = lowerValue;
            }
            else if (!_trend[index] && _trend[index - 1])
            {
                DownTrend[index] = upperValue;
            }
            else if (_trend[index])
            {
                UpTrend[index] = lowerValue > UpTrend[index - 1] ? lowerValue : UpTrend[index - 1];
            }
            else
            {
                DownTrend[index] = upperValue < DownTrend[index - 1] ? upperValue : DownTrend[index - 1];
            }

            EMA[index] = _ema.Result[index];
        }
    }
}
Comments

alualdi - September 20, 2012 @ 07:53

hi looks like the code has an error - please check (Build failed : error 1)

kkostaki - October 12, 2012 @ 11:28

Hi the indicator references another indicator /algos/show/139 Maybe this is the issue. Let me know.

ghazisameer - August 30, 2018 @ 13:35

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

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.Liness, Thickness = 3)]

        public IndicatorDataSeries UpTrend { get; set; }

 

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

                }

            }

        }

5