Candilator RSI [AstrideUnicorn]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AstrideUnicorn

//@version=5
indicator("Candilator RSI", overlay=false)

// indicator inputs
length = input(defval=14, title="Period", group="Main Settings")
overbought = input(defval=70.0, title="Overbought Level", group="Main Settings")
oversold = input(defval=30.0, title="Oversold Level", group="Main Settings")

ShowOutside = input(defval=true, title="Outside Bar Pattern", group="Pattern Detection")
ShowInside = input(defval=true, title="Inside Bar Pattern", group="Pattern Detection")

// calculate RSI with open, high, low and close time series
candilator_open = ta.rsi(open, length)
candilator_high = math.max(ta.rsi(high, length),ta.rsi(low, length))
candilator_low = math.min(ta.rsi(high, length),ta.rsi(low, length))
candilator_close = ta.rsi(close, length)


//Outside bar
outside_bar_bull = barstate.isconfirmed and ShowOutside and candilator_high[0]>candilator_high[1] and candilator_low[0]<candilator_low[1] and candilator_open[0]<candilator_open[1] and candilator_close[0]>candilator_close[1] and candilator_close[0]>candilator_open[0] and candilator_close[1]<candilator_close[2] and candilator_close[0]>candilator_open[1] 
outside_bar_bear = barstate.isconfirmed and ShowOutside and candilator_high[0]>candilator_high[1] and candilator_low[0]<candilator_low[1] and candilator_open[0]>candilator_open[1] and candilator_close[0]<candilator_close[1] and candilator_close[0]<candilator_open[0] and candilator_close[1]>candilator_close[2] and candilator_close[0]<candilator_open[1]

//Inside bar
inside_bar_bull =  barstate.isconfirmed and ShowInside and candilator_open[1]>candilator_close[1] and candilator_close[1]<candilator_close[2] and candilator_open[0]>candilator_close[1] and candilator_open[0]<candilator_open[1] and candilator_close[0]>candilator_close[1] and candilator_close[0]<candilator_open[1] and candilator_high[0]<candilator_high[1] and candilator_low[0]>candilator_low[1] and candilator_close[0]>=candilator_open[0]
inside_bar_bear =  barstate.isconfirmed and ShowInside and candilator_open[1]<candilator_close[1] and candilator_close[1]>candilator_close[2] and candilator_open[0]<candilator_close[1] and candilator_open[0]>candilator_open[1] and candilator_close[0]<candilator_close[1] and candilator_close[0]>candilator_open[1] and candilator_high[0]<candilator_high[1] and candilator_low[0]>candilator_low[1] and candilator_close[0]<=candilator_open[0]

// Define overbought and oversold conditions
oversold_condition = (candilator_open[1] < oversold) or (candilator_open < oversold)
overbought_condition = (candilator_open[1] > overbought) or (candilator_open > overbought)

// Define a conditional color for the candilator bars
candilator_color = candilator_close > candilator_open ? color.green : color.red

// Plot candilator candles and levels
plotcandle(candilator_open,candilator_high,candilator_low,candilator_close,color=candilator_color)
plot(50, color=color.new(color.white,60))
plot(overbought, color=color.new(color.blue,60))
plot(oversold, color=color.new(color.blue,60))

// plot lablels for detected patterns
plotshape(outside_bar_bull and oversold_condition ? 0.6*candilator_low : na, text='Outside Bar', style=shape.labelup, color=color.green, textcolor=color.white, location=location.absolute, size=size.normal, editable=false)
plotshape(outside_bar_bear and overbought_condition ? 1.1*candilator_high : na,text='Outside Bar', style=shape.labeldown, color=color.red, textcolor=color.white, location=location.absolute, size=size.normal, editable=false)
plotshape(inside_bar_bull and oversold_condition ? 0.6*candilator_low : na,text='Inside Bar', style=shape.labelup, color=color.green, textcolor=color.white, location=location.absolute, size=size.normal, editable=false)
plotshape(inside_bar_bear and overbought_condition ? 1.1*candilator_high : na ,text='Inside Bar', style=shape.labeldown, color=color.red, textcolor=color.white, location=location.absolute, size=size.normal, editable=false)
