// Stochastic RSI indicator
//
// Input configuration
// - rsiLengthInputInput: 14; number of close entries to use when calculating the RSI
// - stochLengthInput: 3; lookback period for the high and low of the RSI
// - kLengthInput: 3; smoothing length of the stoch entries
// - dLengthInput: 3; smoothing length of the k entries
//
// Output:
// - %K: solid line
// - %D: solid line
// - StochasticRSI: solid line
// - Overbought: dotted line
// - Oversold: dotted line
//@version=5
indicator("Stochastic RSI", shorttitle="StochRSI", overlay=false, timeframe="D", timeframe_gaps=true)
// Input parameters
int rsiLengthInput = input.int(14, title="RSI Length")
int stochLengthInput = input.int(14, title="Stochastic Length")
int kLengthInput = input.int(3, title="%K Length")
int dLengthInput = input.int(3, title="%D Length")
float rsi = ta.rsi(close, rsiLengthInput)
stochRSI(float rsi) =>
float rsiLowest = ta.lowest(rsi, stochLengthInput)
float rsiHighest = ta.highest(rsi, stochLengthInput)
float stochRsi = (rsi - rsiLowest) / (rsiHighest - rsiLowest) * 100
float k = ta.sma(stochRsi, kLengthInput)
float d = ta.sma(k, dLengthInput)
[stochRsi, k, d]
[stochRsi, k, d] = stochRSI(rsi)
// Plot %K, %D and StochRSI
plot(k, title="%K", color=color.blue)
plot(d, title="%D", color=color.red)
plot(stochRsi, title="stochRSI", color=color.fuchsia)
// Plot horizontal lines
hline(80, "Overbought", color=color.red, linestyle=hline.style_dotted)
hline(20, "Oversold", color=color.green, linestyle=hline.style_dotted)
hline(50, "Middle", color=color.gray, linestyle=hline.style_dotted)