-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathStoch.pine
More file actions
48 lines (40 loc) · 2.81 KB
/
Copy pathStoch.pine
File metadata and controls
48 lines (40 loc) · 2.81 KB
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
// ================================================================================================================================================================================
// Stochastic indicator with overbought/oversold zones
// ================================================================================================================================================================================
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Original by TradingView
// Rewritten by Zettt
// This version only differs from the original TradingView version in the way that it displays thicker Stochs lines when over the upTHR or below the downTHR.
//@version=4
study(title="Stochastic", shorttitle="Stoch", format=format.price, precision=2, resolution="")
periodK = input(14, title="K", minval=1, type=input.integer)
periodD = input(3, title="D", minval=1, type=input.integer)
smoothK = input(3, title="Smooth", minval=1, type=input.integer)
upTHR = input(80, title="Up Threshold", minval=0, type=input.integer)
downTHR = input(20, title="Down Threshold", minval=0, type=input.integer)
k = sma(stoch(close, high, low, periodK), smoothK)
d = sma(k, periodD)
overbought = hline(upTHR, "Upper Band", color=#606060)
midline = hline(50)
oversold = hline(downTHR, "Lower Band", color=#606060)
// We need to plot k and d separate because linewidth can't be a mutable variable
plot(k >= upTHR or k <= downTHR ? k : na, title="%K", style=plot.style_linebr, color=#0d47a1, linewidth=2)
plot(k <= upTHR or k >= downTHR ? k : na, title="%K", style=plot.style_linebr, color=#2196f3, linewidth=1)
plot(d >= upTHR or d <= downTHR ? d : na, title="%D", style=plot.style_linebr, color=#e65100, linewidth=2, transp=35)
plot(d <= upTHR or d >= downTHR ? d : na, title="%D", style=plot.style_linebr, color=#ff9800, linewidth=1, transp=35)
// Fill background
fill(overbought, oversold, color=#9915FF, title="Background", transp=100)
// Overbought Stochs crosses
showOnlyFirstSignal = true
upcross = k >= d and k >= downTHR and d <= downTHR
downcross = k <= d and k <= upTHR and d >= upTHR
showLong = showOnlyFirstSignal ? upcross and not upcross[1] : upcross
showShort = showOnlyFirstSignal ? downcross and not downcross[1] : downcross
bgcolor(showLong ? #81c784 : na , title="Long Background", transp=85)
bgcolor(showShort ? #e57373 : na , title="Short Background", transp=85)
// Alerts
// closePrices = security(syminfo.ticker, timeframe.period, close)
// title="STMC Bull " + timeframe.period causes cannot call tostring error, may get fixed in the future
// alertcondition(condition=showLong, title="TF Bull", message="TF Bull")
// alertcondition(condition=showShort, title="TF Bear", message="TF Bear")
alertcondition(condition=showLong or showShort, title="Stochs Crossed", message="Stochs Crossed")