Stochastic Candles
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © peacefulLizard50262

//@version=5
indicator("Stoch", overlay = false)
OHLC(src, int len = 4) =>
    Open  = ta.sma(src[1], len)
    High  = ta.highest(src, 1)
    Low   = ta.lowest(src, 1)
    Close = ta.wma(src, len)
    [Open, High, Low, Close]

can    = input.int(4, "Candle Transform Length")
length = input.int(14, "Length")
d      = input.int(9, "%D Smoothing")

sel   = input.bool(true, "Candle Colour", "Chance how the candles are colored. If this is enabled it will color the candles bassed on the transformed open/close. Otherwise this indicator will use source/source[1]")
red   = input.color(color.new(#ef5350, 0), "")
green = input.color(color.new(#26a69a, 0), "")

src = ta.stoch(close, high, low, length)
D   = ta.sma(ta.sma(src, d), 2)


[Open, High, Low, Close] = OHLC(src, can)

colour = sel ? Open < Close ? green : red : src > src[1] ? green : red

plot(D, "%D", color.orange)
plotcandle(Open, High, Low, Close, "%K", colour, colour, true, bordercolor = colour)

h0 = hline(80, "Upper Band", color=#787B86)
hline(50, "Middle Band", color=color.new(#787B86, 50))
h1 = hline(20, "Lower Band", color=#787B86)
fill(h0, h1, color=color.rgb(33, 150, 243, 90), title="Background")