-
Notifications
You must be signed in to change notification settings - Fork 0
/
sto & rsi.pine
69 lines (52 loc) · 3.16 KB
/
sto & rsi.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
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// RSI & Stochastic. Same as the built-in scripts with changing color when overbought or oversold.
//@version=5
indicator(title="RSI & Stochastic", shorttitle="RSI & Sto", format=format.price, precision=2, timeframe="", timeframe_gaps=true)
///////////////
// RSI
gRSI = "RSI Settings"
gRSIMA = "RSI MA Settings"
showRSI = input(true, title="Show RSI", group=gRSI)
ma(source, length, type) =>
switch type
"SMA" => ta.sma(source, length)
"Bollinger Bands" => ta.sma(source, length)
"EMA" => ta.ema(source, length)
"SMMA (RMA)" => ta.rma(source, length)
"WMA" => ta.wma(source, length)
"VWMA" => ta.vwma(source, length)
rsiLengthInput = input.int(14, minval=1, title="RSI Length", group=gRSI)
rsiSourceInput = input.source(close, "Source", group=gRSI)
maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group=gRSIMA)
maLengthInput = input.int(14, title="MA Length", group=gRSIMA)
bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group=gRSIMA)
up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maTypeInput)
isBB = maTypeInput == "Bollinger Bands" and showRSI
rsiColor = rsi > 70 or rsi < 30 ? color.yellow : color.purple
plot(showRSI ? rsi : na, "RSI", color=rsiColor, linewidth=2)
plot(showRSI ? rsiMA : na, "RSI-based MA", color=color.gray)
rsiUpperBand = hline(showRSI ? 70 : na, "RSI Upper Band", color=#787B86)
hline(50, "RSI Middle Band", color=color.new(#787B86, 50))
rsiLowerBand = hline(showRSI ? 30 : na, "RSI Lower Band", color=#787B86)
fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 95), title="RSI Background")
bbUpperBand = plot(isBB ? rsiMA + ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Upper Bollinger Band", color=color.green)
bbLowerBand = plot(isBB ? rsiMA - ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Lower Bollinger Band", color=color.green)
fill(bbUpperBand, bbLowerBand, color= isBB ? color.new(color.green, 95) : na, title="Bollinger Bands Background")
///////////////
// Stochastic
gSto = "Stochastic Settings"
showSto = input(true, title="Show Stochastic", group=gSto)
periodK = input.int(14, title="%K Length", minval=1, group=gSto)
smoothK = input.int(1, title="%K Smoothing", minval=1, group=gSto)
periodD = input.int(3, title="%D Smoothing", minval=1, group=gSto)
k = ta.sma(ta.stoch(close, high, low, periodK), smoothK)
d = ta.sma(k, periodD)
stoColor = k > 80 or k < 20 ? color.orange : color.blue
plot(showSto ? k : na, title="%K", color=stoColor)
plot(showSto ? d : na, title="%D", color=color.gray)
h0 = hline(showSto ? 80 : na, "Stochastic Upper Band", color=#787B86)
h1 = hline(showSto ? 20 : na, "Stochastic Lower Band", color=#787B86)
fill(h0, h1, color=color.rgb(33, 150, 243, 95), title="Stochastic Background")