"The Stocashi" - Stochastic RSI + Heikin-Ashi
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//This is just a fun script to give a different representation to the ever popular Stochastic RSI
//Even for me over the years the stochastic has been a difficult one to use in trading mearly beause of its choppy look.
//Since Heikin-Ashi Candles do such a powerful job in smoothing out the look of choppy markets,
//I decided to test it out on the look of the Stochastic RSI.
//From an initial visual standpoint it worked out WAY better than I thought but it seemed to need something more.
//I decided to use the PineScript "Color.From_Gradient" feature to give the Stochastic a more 3 dimentional look, which really brought the "old-school" indicator to life.

// © CoffeeshopCrypto

//@version=5

indicator("Stochastic RSI + Heikin-Ashi", shorttitle="Stocashi", overlay=false)

// Calculate the Stochastic RSI
src = input(close, title="Source")
smoothK = input.int(8, minval=1, title="Stochastic %K Smoothing", group = "Stocashi Inputs")
smoothD = input.int(3, minval=1, title="Stochastic %D Smoothing", group = "Stocashi Inputs")
lengthRSI = input.int(14, minval=1, title="RSI Length", group = "Stocashi Inputs")
rsi1 = ta.rsi(src, lengthRSI)
lengthStoch = input.int(14, minval=1, title="Stochastic Length", group = "Stocashi Inputs")
k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK) - 50
d = ta.sma(k, smoothD)


// Calculate the Stochastic Heikin-Ashi candles
ha_open = (k[1] + d[1]) / 2
ha_close = (k + d + d + k) / 4
ha_high = math.max(k, math.max(ha_open, ha_close))
ha_low = math.min(k, math.min(ha_close, ha_open))

// Plot the Heikin-Ashi Stochastic RSI
down_colorbottom = input.color(#086e05, title="Bullish Move Start", inline = "Bullish / Bearish Momentum", group = "Bullish / Bearish Movement")
down_colortop = input.color(#8dc474, title="Bullish Move End", inline = "Bullish / Bearish Momentum", group = "Bullish / Bearish Movement")
up_color_top = input.color(#af0505, title="Bearish Move Start", inline = "Bullish / Bearish Momentum", group = "Bullish / Bearish Movement")
up_color_bottom = input.color(#e08e8e, title="Bearish Move End", inline = "Bullish / Bearish Momentum", group = "Bullish / Bearish Movement")
up_color_ob = input.color(color.rgb(255, 82, 82, 95), title="Overbough Level", inline = "OB/OS Levels", group = "Overbough / Oversould levels")
up_color_os = input.color(color.rgb(122, 255, 82, 95), title="Oversold Level", inline = "OB/OS Levels", group = "Overbough / Oversould levels")

gradientColourup = color.from_gradient(k, -50, 50, up_color_bottom, up_color_top)
//gradientColouruptop = color.from_gradient(k, -50, 30, up_color, down_color)
gradientColourdn = color.from_gradient(k, -50, 50, down_colorbottom, down_colortop)
//gradientColourdntop = color.from_gradient(k, -30, 50, up_color, down_color)
bar_color = ha_close >= ha_open ? gradientColourdn : gradientColourup

// Use the up and down colors in your script
//////////////////////////////////////////////
plotcandle(ha_open, ha_high, ha_low, ha_close,title= "Stochatic Heiken Ashi Gradient", color=bar_color, wickcolor = bar_color)
mindline = hline(0, color=#d47305a1)
line80 = hline(40, color=color.rgb(247, 143, 143, 66), linestyle = hline.style_dotted, editable = false)
overbought = hline(50, color=color.rgb(247, 143, 143, 66), linestyle = hline.style_dotted, editable = false)
linem10 = hline(-40, color=color.rgb(122, 255, 82, 66), linestyle = hline.style_dotted, editable = false)
oversold = hline(-50, color=color.rgb(122, 255, 82, 66), linestyle = hline.style_dotted, editable = false)
fill(line80, overbought, color = color.rgb(255, 82, 82, 95), title = "OB")
fill(linem10, oversold, color = color.rgb(122, 255, 82, 95), title = "OS")