Koalafied RSI Decision Points
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TJ_667

// Momentum conditions as detailed in RSI : The Complete Guide by John Hayden 
// Decision points are conditions based on changes of these rsi values. Pauses in an uptrend, exiting high momentum values, breakouts and failures. 

// 2:1 momentum is associated with RSI values of 66.67 and 33.33 respectfully. In an Uptrend an RSI value of 40 should not be broken and in a downtrend 
// a RSI value of 60 should not be exceeded. If so, then there is buying/selling pressure in the opposite direction (but not necessarily enough for a trend reversal).
// Alternatively it may show the presence of HTF traders. 
// 4:1 momentum (RSI values of 80/20) can be associated with extreme market conditions, typically thought of as being Overbought or Oversold. 



//@version=5
indicator('Koalafied RSI Decision Points', overlay=true)

// ---------- INPUTS ---------- //

var GRP1 = "INPUTS"

len = input.int(14, minval=1, title='RSI Length', group = GRP1)


var GRP2 = "PLOTS"
showBG = input(true, 'Show RSI Background State', group = GRP2)
showlines = input(true, "Show Decision Point Levels", group = GRP2)
plot_sel = input.string(title='Plot Selection', defval='Both', options=['Both', 'Shapes', 'Candles'], group = GRP2)
transp = input.int(92, "Background Transparency", minval = 0, maxval = 100, group = GRP2)

// ---------- CALCS ---------- //

rsi = ta.rsi(close, len)

// Trend State
var state = 0
if ta.crossover(rsi, 66.67)
    state := 1
    state
if ta.crossunder(rsi, 33.33)
    state := 2
    state
if state == 1 and ta.crossunder(rsi, 40)
    state := 3
    state
if state == 2 and ta.crossover(rsi, 60)
    state := 3
    state
state := state


// Decision Point Conditions

pause1 = state == 3 and state[1] == 1
pause2 = state == 3 and state[1] == 2

mom_bull = state == 1 and state[1] == 3
mom_bear = state == 2 and state[1] == 3

exit_bull = state == 1 and ta.crossunder(rsi, 66.67)
exit_bear = state == 2 and ta.crossover(rsi, 33.33)

failure_bullrev = state == 1 and ta.crossunder(rsi[1], 66.67) and ta.crossover(rsi, 66.67)
failure_bearrev = state == 2 and ta.crossover(rsi[1], 33.33) and ta.crossunder(rsi, 33.33)

var float pause_bull = na
var float pause_bear = na
pause_bull := pause1 ? low : pause_bull[1]
pause_bear := pause2 ? high : pause_bear[1]

candles = plot_sel == 'Candles'
shapes = plot_sel == 'Shapes'
both = plot_sel == 'Both'

// ---------- PLOTS ---------- //

// Plot Decision Points
plot(showlines ? pause_bull : na, title = "Bull Decision Point Level", color = color.new(color.blue, 50), style = plot.style_circles)
plot(showlines ? pause_bear : na, title = "Bear Decision Point Level", color = color.new(color.red, 50), style = plot.style_circles)

plotshape(shapes or both ? pause1 : na, style=shape.square, location=location.belowbar, color=color.new(color.blue, 0))
plotshape(shapes or both ? pause2 : na, style=shape.square, location=location.abovebar, color=color.new(color.red, 0))

plotshape(shapes or both ? mom_bull : na, style=shape.triangleup, location=location.belowbar, color=color.new(color.blue, 0))
plotshape(shapes or both ? mom_bear : na, style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 0))

plotshape(shapes or both ? exit_bull : na, style=shape.xcross, location=location.abovebar, color=color.new(color.orange, 0))
plotshape(shapes or both ? exit_bear : na, style=shape.xcross, location=location.belowbar, color=color.new(color.orange, 0))

plotshape(shapes or both ? failure_bullrev : na, style=shape.triangleup, location=location.belowbar, color=color.new(color.lime, 0))
plotshape(shapes or both ? failure_bearrev : na, style=shape.triangledown, location=location.abovebar, color=color.new(color.purple, 0))

candle_col = pause1 or mom_bull ? color.blue : pause2 or mom_bear ? color.red : exit_bull or exit_bear ? color.orange : failure_bullrev ? color.lime : failure_bearrev ? color.purple : na
barcolor(candles or both ? candle_col : na)

// Trend
state_col = state == 1 ? color.new(color.blue, transp) : state == 2 ? color.new(color.red, transp) : na
bgcolor(showBG ? state_col : na, title='Trend State')

// 2:1 Momentum
BG_col = rsi > 66.67 ? color.new(color.blue, transp) : rsi < 33.33 ? color.new(color.red, transp) : na
bgcolor(showBG ? BG_col : na, title='Trend Momentum')

// 4:1 Momentum
OB = rsi > 80 ? color.new(color.red, transp) : na
OS = rsi < 20 ? color.new(color.blue, transp) : na

bgcolor(showBG ? OB : na, title='Overbought')
bgcolor(showBG ? OS : na, title='Oversold')


