OPEN-SOURCE SCRIPT

Stochastic Oscillator with Signals

132
//version=5
indicator("Stochastic Oscillator with Signals", overlay=true)

// Input parameters
k = input.int(14, title="%K Length")
d = input.int(3, title="%D Length")
smoothK = input.int(3, title="Smoothing")

// Calculating Stochastic Oscillator
kValue = ta.sma(ta.stoch(close, high, low, k), smoothK)
dValue = ta.sma(kValue, d)

// Overbought and Oversold levels
obLevel = 80
osLevel = 20

// Buy and Sell Conditions
buySignal = ta.crossover(kValue, dValue) and kValue < osLevel
sellSignal = ta.crossunder(kValue, dValue) and kValue > obLevel

// Plotting the Stochastic Oscillator
plot(kValue, title="%K", color=color.blue)
plot(dValue, title="%D", color=color.orange)
hline(obLevel, "Overbought", color=color.red)
hline(osLevel, "Oversold", color=color.green)

// Plot Buy and Sell Signals on Candlestick Chart
plotshape(buySignal, location=location.belowbar, color=color.green, style=shape.labelup, title="Buy", text="Buy")
plotshape(sellSignal, location=location.abovebar, color=color.red, style=shape.labeldown, title="Sell", text="Sell")

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.