-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudy-rsi-stoch.pine
59 lines (50 loc) · 2.85 KB
/
study-rsi-stoch.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
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Ossy1717
//@version=4
study("(Study) RSI + STOCH Overb/s", overlay = true)
// Inputs //
tradeDirection = input(defval = "All", title = "Long, Short or All Setups?", options = ["All", "Longs", "Shorts"])
stochLen = input(14, minval = 1, title = "Stoch Length")
periodK = input(3, title="K", minval=1)
periodD = input(3, title="D", minval=1)
rsiLen = input(14, minval = 1, title = "RSI Length")
rsiOverbought = input(defval = 70, title = "RSI Overbought", minval = 1, maxval = 100, step = 1)
rsiOversold = input(defval = 30, title = "RSI Oversold", minval = 1, maxval = 100, step = 1)
checkRsiIndicator = input(defval = "Yes", title = "Check RSI Div Indicator", options = ["Yes", "No"])
lenFast = input(5, minval=1, title="Length Fast RSI")
lenSlow = input(14, minval=1, title="Length Slow RSI")
// Inputs //
// RSI
rsi = rsi(close, rsiLen)
// Stoch
stk = sma(stoch(close, high, low, stochLen), 3)
k = sma(stoch(rsi, rsi, rsi, stochLen), periodK) // Blue
d = sma(k, periodD)
// RSI DIV Indicator
upFast = rma(max(change(close), 0), lenFast)
downFast = rma(-min(change(close), 0), lenFast)
rsiFast = downFast == 0 ? 100 : upFast == 0 ? 0 : 100 - (100 / (1 + upFast / downFast))
upSlow = rma(max(change(close), 0), lenSlow)
downSlow = rma(-min(change(close), 0), lenSlow)
rsiSlow = downSlow == 0 ? 100 : upSlow == 0 ? 0 : 100 - (100 / (1 + upSlow / downSlow))
divergence = rsiFast - rsiSlow
isRSIDivBullish = divergence > 0
isRSIDivBearish = divergence < 0
// Conditions
isRSIOverbought = rsi >= rsiOverbought
isRSIOversold = rsi <= rsiOversold
isStochOverbought = k >= rsiOverbought and d >= rsiOverbought
isStochOversold = k <= rsiOversold and d <= rsiOversold
goLong = isRSIOversold and isStochOversold and (checkRsiIndicator == 'No' or (checkRsiIndicator == 'Yes' and isRSIDivBullish)) and (tradeDirection == "All" or tradeDirection == "Longs")
goShort = isRSIOverbought and isStochOverbought and (checkRsiIndicator == 'No' or (checkRsiIndicator == 'Yes' and isRSIDivBearish)) and (tradeDirection == "All" or tradeDirection == "Shorts")
// Visuals
// RSI Debug //
// label.new(bar_index, goLong ? low : high, yloc = goLong ? yloc.belowbar : yloc.abovebar, text=tostring(rsi, '#'), textcolor=color.white, color = color.green, style = goLong ? label.style_label_up : label.style_label_down, size = size.small)
// RSI Debug //
if(goLong)
label.new(x = bar_index, y = low, yloc = yloc.belowbar, text="OS", textcolor=color.white, color = color.green, style = label.style_label_up, size = size.small)
if(goShort)
label.new(x = bar_index, y = high, yloc = yloc.abovebar, text="OB", textcolor=color.white, color = color.red, style = label.style_label_down, size = size.small)
// Alerts
alertcondition(goLong, title='Buy Signal', message='')
alertcondition(goShort, title='Sell Signal', message='')