forked from Electrified-Trading/Libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSupportResistanceAndTrend.pine
277 lines (241 loc) · 8.98 KB
/
SupportResistanceAndTrend.pine
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Electrified (electrifiedtrading)
// @version=5
// @description Contains utilities for finding key levels of support, resistance and direction of trend.
library('SupportResitanceAndTrend', true)
import Electrified/MovingAverages/10 as MA
import Electrified/DataCleaner/5 as Data
///////////////////////////////////////////////////
// @function A more flexible version of SuperTrend that allows for supplying the series used and sensitivity adjustment by confirming close bars.
// @param multiple The multiple to apply to the average true range.
// @param h The high values.
// @param l The low values.
// @param atr The average true range values.
// @param closeBars The number of bars to confirm a change in trend.
// @returns [trend, up, dn, unconfirmed, warn, reversal]
export superTrendPlus(
simple float multiple,
series float h,
series float l,
series float atr,
simple int closeBars = 0) =>
up = l-(multiple*atr)
up1 = nz(up[1], up)
up := l[1] > up1 ? math.max(up, up1) : up
dn = h+(multiple*atr)
dn1 = nz(dn[1], dn)
dn := h[1] < dn1 ? math.min(dn, dn1) : dn
var lastU = up1
var lastD = dn1
var trend = 0
var confirmation = 0
var unconfirmed = 0
// cover gap cases
if(trend != +1 and l > lastD)
trend := +1
else if(trend != -1 and h < lastU)
trend := -1
// confirmed cases
else if(trend != +1 and h > lastD)
unconfirmed += 1
if(confirmation<closeBars and close[1]>lastD)
confirmation += 1
if(confirmation>=closeBars)
trend := +1
else if(trend != -1 and l < lastU)
unconfirmed += 1
if(confirmation<closeBars and close[1]<lastU)
confirmation += 1
if(confirmation>=closeBars)
trend := -1
if(trend[1]!=trend)
lastU := up1
lastD := dn1
confirmation := 0
unconfirmed := 0
else
lastU := trend==+1 ? math.max(lastU, up1) : up1
lastD := trend==-1 ? math.min(lastD, dn1) : dn1
// Trend is clearly continuing? Reset confirmation.
if(trend==+1 and dn1>dn1[1] or trend==-1 and up1<up1[1])
confirmation := 0
unconfirmed := 0
[trend, lastU, lastD, unconfirmed, unconfirmed[1]==0 and unconfirmed > 0, trend != trend[1]]
///////////////////////////////////////////////////
getATR(simple string mode, simple int period, simple float maxDeviation) =>
trCleaned = maxDeviation==0 ? ta.tr : Data.naOutliers(ta.tr, period, maxDeviation)
MA.get(mode, period, trCleaned)
////
///////////////////////////////////////////////////
// @function superTrendPlus with simplified parameters.
// @param multiple The multiple to apply to the average true range.
// @param period The number of bars to measure.
// @param mode The type of moving average to use with the true range.
// @param closeBars The number of bars to confirm a change in trend.
// @returns [trend, up, dn, unconfirmed, warn, reversal]
export superTrend(
simple float multiple,
simple int period,
simple string mode = 'WMA',
simple int closeBars = 0,
simple float maxDeviation = 0) =>
atr = getATR(mode, period, maxDeviation)
superTrendPlus(multiple, high, low, atr, closeBars)
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// @function superTrendPlus with default compensation for extreme volatility.
// @param multiple The multiple to apply to the average true range.
// @param period The number of bars to measure.
// @param mode The type of moving average to use with the true range.
// @param closeBars The number of bars to confirm a change in trend.
// @param maxDeviation The optional standard deviation level to use when cleaning the series. The default is the value of the provided level.
// @returns [trend, up, dn, unconfirmed, warn, reversal]
export superTrendCleaned(
simple float multiple,
simple int period,
simple string mode = 'WMA',
simple int closeBars = 0,
simple float maxDeviation = 4) =>
atr = getATR(mode, period, maxDeviation)
superTrendPlus(multiple, high, low, atr, closeBars)
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// @function Identifies support and resistance levels by when a stochastic RSI reverses.
// @returns [trend, up, dn, unconfirmed, warn, reversal]
export stochSR(
simple int smoothK,
simple int smoothD,
simple int lengthRSI,
simple int lengthStoch,
series float src = hlc3,
simple float lowerBand = 20,
simple float upperBand = 80,
simple float lowerReversal = 20,
simple float upperReversal = 80) =>
rsi1 = ta.rsi(src, lengthRSI)
stoch = ta.stoch(rsi1, rsi1, rsi1, lengthStoch)
k = ta.wma(stoch, smoothK)
d = ta.sma(k, smoothD)
k_c = ta.change(k)
d_c = ta.change(d)
kd = k - d
var hi = high
var lo = low
var phi = high
var plo = low
var state = 0
if(d<lowerBand)
phi := high
if(d>upperBand)
plo := low
if(high>phi)
phi := high
if(low<plo)
plo := low
if(state!=-1 and d<lowerBand)
state := -1
else if(state!=+1 and d>upperBand)
state := +1
if(hi>phi and state==+1 and k<d and k<lowerReversal)
hi := phi
if(lo<plo and state==-1 and k>d and k>upperReversal)
lo := plo
if(high>hi)
hi := high
if(low<lo)
lo := low
[lo, hi]
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// @function Identifies anchored VWAP levels by when a stochastic RSI reverses.
// @returns [loAVWAP, hiAVWAP, hiAVWAP_next, loAVWAP_next]
export stochAVWAP(
simple int smoothK,
simple int smoothD,
simple int lengthRSI,
simple int lengthStoch,
series float src = hlc3,
simple float lowerBand = 20,
simple float upperBand = 80,
simple float lowerReversal = 20,
simple float upperReversal = 80,
simple bool useHiLow = false) =>
rsi1 = ta.rsi(src, lengthRSI)
stoch = ta.stoch(rsi1, rsi1, rsi1, lengthStoch)
k = ta.wma(stoch, smoothK)
d = ta.sma(k, smoothD)
k_c = ta.change(k)
d_c = ta.change(d)
kd = k - d
var hi = high
var lo = low
var phi = high
var plo = low
var state = 0
var float hiAVWAP_s = 0
var float loAVWAP_s = 0
var float hiAVWAP_v = 0
var float loAVWAP_v = 0
var float hiAVWAP_s_next = 0
var float loAVWAP_s_next = 0
var float hiAVWAP_v_next = 0
var float loAVWAP_v_next = 0
if(d<lowerBand or high>phi)
phi := high
hiAVWAP_s_next := 0
hiAVWAP_v_next := 0
if(d>upperBand or low<plo)
plo := low
loAVWAP_s_next := 0
loAVWAP_v_next := 0
if(high>hi)
hi := high
hiAVWAP_s := 0
hiAVWAP_v := 0
if(low<lo)
lo := low
loAVWAP_s := 0
loAVWAP_v := 0
vwapHi = useHiLow ? high : hlc3
vwapLo = useHiLow ? low : hlc3
hiAVWAP_s += vwapHi * volume
loAVWAP_s += vwapLo * volume
hiAVWAP_v += volume
loAVWAP_v += volume
hiAVWAP_s_next += vwapHi * volume
loAVWAP_s_next += vwapLo * volume
hiAVWAP_v_next += volume
loAVWAP_v_next += volume
if(state!=-1 and d<lowerBand)
state := -1
else if(state!=+1 and d>upperBand)
state := +1
if(hi>phi and state==+1 and k<d and k<lowerReversal)
hi := phi
hiAVWAP_s := hiAVWAP_s_next
hiAVWAP_v := hiAVWAP_v_next
if(lo<plo and state==-1 and k>d and k>upperReversal)
lo := plo
loAVWAP_s := loAVWAP_s_next
loAVWAP_v := loAVWAP_v_next
hiAVWAP = hiAVWAP_s / hiAVWAP_v
loAVWAP = loAVWAP_s / loAVWAP_v
hiAVWAP_next = hiAVWAP_s_next / hiAVWAP_v_next
loAVWAP_next = loAVWAP_s_next / loAVWAP_v_next
[loAVWAP, hiAVWAP, hiAVWAP_next, loAVWAP_next]
///////////////////////////////////////////////////
// Demo
ATR = "Average True Range", CONFIRM = "Confirmation", DISPLAY = "Display"
WMA = "WMA", EMA = "EMA", SMA = "SMA", VWMA = "VWMA", VAWMA = "VAWMA"
mode = input.string(VAWMA, "Mode", options=[SMA,EMA,WMA,VWMA,VAWMA], group=ATR, tooltip="Selects which averaging function will be used to determine the ATR value.")
atrPeriod = input.int(120, "Period", group=ATR, tooltip="The number of bars to use in calculating the ATR value.")
atrM = input.float(3.0, title="Multiplier", step=0.5, group=ATR, tooltip="The multiplier used when defining the super trend limits.")
closeBars = input.int(2, "Closed Bars", minval = 0, group=CONFIRM, tooltip="The number of closed bars that have to exceed the super-trend value before the trend reversal is confirmed.")
[trend1, up, dn, unconfirmed1, warn1, reversal1] =
superTrend(atrM, atrPeriod, mode=mode, closeBars=closeBars)
plot(up)
plot(dn)
[trend2, upCleaned, dnCleaned, unconfirmed2, warn2, reversal2] =
superTrendCleaned(atrM, atrPeriod, mode=mode, closeBars=closeBars)
plot(upCleaned, color=color.green)
plot(dnCleaned, color=color.green)