-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrsi-indicator.pine
250 lines (198 loc) · 8.36 KB
/
rsi-indicator.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
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © systemalphatrader
//@version=4
study(title='RSI [SystemAlpha]', shorttitle='RSI [SA]', format=format.price)
len = input(title="RSI Period", minval=1, defval=14)
src = input(title="RSI Source", defval=close)
lbR = input(title="Pivot Lookback Right", defval=5)
lbL = input(title="Pivot Lookback Left", defval=5)
ob = input(70, minval=0, maxval = 100, title="Overbought")
os = input(30, minval=0, maxval = 100, title="Oversold")
showsignals = input(true, title="Show Buy/Sell Signals")
//Divergence Input
divtype = input(title="Divergence Type", defval='Regular', options=['Regular', 'Hidden', 'Both', 'None'])
plotBull = divtype == 'Regular' or divtype == 'Both'
plotHiddenBull = divtype == 'Hidden' or divtype == 'Both'
plotBear = divtype == 'Regular' or divtype == 'Both'
plotHiddenBear = divtype == 'Hidden' or divtype == 'Both'
showlabel = input(true, title="Show Divergence Label")
showpivot = input(false, title="Show Pivot Points")
rangeUpper = input(title="Max of Lookback Range", defval=60)
rangeLower = input(title="Min of Lookback Range", defval=5)
chcut = true //input(true, title = "Check Cut-Through!")
bearColor = color.red
bullColor = color.green
hiddenBullColor = color.new(color.green, 50)
hiddenBearColor = color.new(color.red, 50)
textColor = color.white
noneColor = color.new(color.white, 100)
osc = rsi(src, len)
plot(osc, title="RSI", linewidth=1, color=color.blue)
hline(50, title="Centerline", linestyle=hline.style_dashed,color=color.gray)
lineBuyBottom = plot(os, color=color.gray, title="Oversold Level")
lineSellTop = plot(ob, color=color.gray, title="Overbought Level")
lineBuy = plot(series=(osc < os ? osc : os), transp=100, title="n/a", editable=false)
lineSell = plot(series=(osc > ob ? osc : ob), transp=100, title="n/a", editable=false)
fill(lineSellTop, lineBuyBottom, title="Background", color=color.gray, transp=80)
fill(plot1=lineBuy, plot2=lineBuyBottom, color=color.red, transp=50, title="Oversold Area")
fill(plot1=lineSellTop, plot2=lineSell, color=color.green, transp=50, title="Overbought Area")
float top = na
float bot = na
top := pivothigh(osc, lbL, lbR)
bot := pivotlow(osc, lbL, lbR)
phFound = na(top) ? false : true
plFound = na(bot) ? false : true
_inRange(cond) =>
bars = barssince(cond == true)
rangeLower <= bars and bars <= rangeUpper
alertcondition(osc[1] > ob and osc[2] < ob, title="RSI value crosses over overbought level", message="Check charts for a RSI cross over overbought level")
alertcondition(osc[1] < ob and osc[2] > ob, title="RSI value crosses under overbought level", message="Check charts for a RSI cross under overbought level")
alertcondition(osc[1] > os and osc[2] < os, title="RSI value crosses over oversold level", message="Check charts for a RSI cross over oversold level")
alertcondition(osc[1] < os and osc[2] > os, title="RSI value crosses under oversold level", message="Check charts for a RSI cross under oversold level")
//No Cut cut-through
topc = 0, botc = 0
topc := top ? lbL : nz(topc[1]) + 1
botc := bot ? lbL : nz(botc[1]) + 1
// check cut-through in indicators
nocut1(indi, len)=>
_ret = true
diff = (indi - nz(indi[len])) / len
ln = indi - diff
for x = 1 to len -1
if nz(indi[x]) > ln
_ret := false
break
ln := ln - diff
_ret
topok = nocut1(osc, topc)
// check cut-through in indicators
nocut2(indi, len)=>
_ret = true
diff = (indi - nz(indi[len])) / len
ln = indi - diff
for x = 1 to len -1
if nz(indi[x]) < ln
_ret := false
break
ln := ln - diff
_ret
botok = nocut2(osc, botc)
//Show Pivot
plotshape(showpivot and phFound and (not chcut or topok) ? osc[lbR] : na,title="Pivot High",text="[PH]",style=shape.labeldown,color=color.white,textcolor=color.black,location=location.absolute,transp=0,offset = -lbR)
plotshape(showpivot and plFound and (not chcut or botok) ? osc[lbR] : na,title="Pivot Low",text="[PL]",style=shape.labeldown,color=color.white,textcolor=color.black,location=location.absolute,transp=0,offset = -lbR)
//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL = osc[lbR] > valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
// Price: Lower Low
priceLL = low[lbR] < valuewhen(plFound, low[lbR], 1)
bullCond = plotBull and priceLL and oscHL and plFound
plot(
plFound ? osc[lbR] : na,
offset=-lbR,
title="Regular Bullish",
linewidth=1,
color=(bullCond ? bullColor : noneColor),
transp=0
)
plotshape(
bullCond and showlabel ? osc[lbR] : na,
offset=-lbR,
title="Regular Bullish Label",
text="R",
style=shape.labelup,
location=location.absolute,
color=bullColor,
textcolor=textColor,
transp=0
)
alertcondition(bullCond, title="Regular bullish divergence in RSI found", message="Check charts for a regular bullish divergence found with RSI")
//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL = osc[lbR] < valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
// Price: Higher Low
priceHL = low[lbR] > valuewhen(plFound, low[lbR], 1)
hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound
plot(
plFound ? osc[lbR] : na,
offset=-lbR,
title="Hidden Bullish",
linewidth=1,
color=(hiddenBullCond ? hiddenBullColor : noneColor),
transp=50
)
plotshape(
hiddenBullCond and showlabel ? osc[lbR] : na,
offset=-lbR,
title="Hidden Bullish Label",
text="H",
style=shape.labelup,
location=location.absolute,
color=bullColor,
textcolor=textColor,
transp=50
)
alertcondition(hiddenBullCond, title="Hidden bullish divergence in RSI found", message="Check charts for a hidden bullish divergence found with RSI")
//------------------------------------------------------------------------------
// Regular Bearish
// Osc: Lower High
oscLH = osc[lbR] < valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])
// Price: Higher High
priceHH = high[lbR] > valuewhen(phFound, high[lbR], 1)
bearCond = plotBear and priceHH and oscLH and phFound
plot(
phFound ? osc[lbR] : na,
offset=-lbR,
title="Regular Bearish",
linewidth=1,
color=(bearCond ? bearColor : noneColor),
transp=0
)
plotshape(
bearCond and showlabel ? osc[lbR] : na,
offset=-lbR,
title="Regular Bearish Label",
text="R",
style=shape.labeldown,
location=location.absolute,
color=bearColor,
textcolor=textColor,
transp=0
)
alertcondition(bearCond, title="Regular bearish divergence in RSI found", message="Check charts for a regular bearish divergence found with RSI")
//------------------------------------------------------------------------------
// Hidden Bearish
// Osc: Higher High
oscHH = osc[lbR] > valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])
// Price: Lower High
priceLH = high[lbR] < valuewhen(phFound, high[lbR], 1)
hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound
plot(
phFound ? osc[lbR] : na,
offset=-lbR,
title="Hidden Bearish",
linewidth=1,
color=(hiddenBearCond ? hiddenBearColor : noneColor),
transp=50
)
plotshape(
hiddenBearCond and showlabel ? osc[lbR] : na,
offset=-lbR,
title="Hidden Bearish Label",
text="H",
style=shape.labeldown,
location=location.absolute,
color=bearColor,
textcolor=textColor,
transp=50
)
alertcondition(hiddenBearCond, title="Hidden bearish divergence in RSI found", message="Check charts for a hidden bearish divergence found with RSI")
// Buy/Sell Signals
// use curvature information to filter out some false positives
sellsignals = showsignals and phFound and osc[lbR] and osc[lbR] > ob
buysignals = showsignals and plFound and osc[lbR] and osc[lbR] < os
plot(buysignals ? osc[lbR]: na, color = color.white , style = plot.style_circles, linewidth = 3, title="Buy Signal Outline", offset=-lbR)
plot(buysignals ? osc[lbR]: na, style=plot.style_circles, color= color.green, linewidth=2, title="Buy Signal", transp=0, offset=-lbR)
plot(sellsignals ? osc[lbR]: na, color = color.white , style = plot.style_circles, linewidth = 3, title="Sell Signal Outline", offset=-lbR)
plot(sellsignals ? osc[lbR]: na, style=plot.style_circles, color= color.red, linewidth=2, title="Sell Signal", transp=0, offset=-lbR)