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.
/
KK
kkostaki
Joined on 24.08.2012
- Distribution: Free
- Language: C#
- Trading platform: cTrader Automate
- File name: SuperTrendPlus.algo
- Rating: 5
- Installs: 7970
- Modified: 13/10/2021 09:54
Note that publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section, please use the Copyright Infringement Notification form to submit a claim.
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;
}
}
}