Outback RSI & Hull [TTF]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TheTrdFloor
//
// We built this indicator to help simplify some of the setup for our Outback strategy, was many were having trouble setting up the
// Hull Suite on top of the RSI using the Indicator on Indicator method.
//
//@version=5
indicator(title="Outback RSI & Hull [TTF]", shorttitle="Outback RSI & Hull [TTF]", format=format.price, precision=2, timeframe="", timeframe_gaps=true)

/////////////////////////////           Standard RSI           /////////////////////////////            

ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "Bollinger Bands" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

rsiLengthInput = input.int(618, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
rsitrigger = input.int(50, title="RSI Trigger Level", group="RSI Settings", tooltip="RSI Trigger Level for safe entry")
usersitrigger = input.bool(false, title="Display Safe Entries", group="RSI Settings", tooltip="If activated, background coloring will differentiate between risk entry and safe entry (RSI above/below RSI trigger level")
maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")
maLengthInput = input.int(200, title="MA Length", group="MA Settings")
bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="MA Settings")

up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maTypeInput)
isBB = maTypeInput == "Bollinger Bands"

plot(rsi, "RSI", color=#7E57C2)
plot(rsiMA, "RSI-based MA", color=color.white)
hline(50, "RSI Middle Band", color=color.new(#787B86, 50))



/////////////////////////////            Hull Suite by Insilico              ///////////////////////////////////

length3 = input(227, title='MA Length', group = "Hull Suite")
lengthMult = input(2.0, title="Length multiplier", group = "Hull Suite")

MHULL = ta.wma(2 * ta.wma(rsi,int(length3 * lengthMult) / 2) - ta.wma(rsi, int(length3 * lengthMult)), math.round(math.sqrt(int(length3 * lengthMult))))

plot(MHULL, "Hull", color=color.yellow)


/////////////////////////////            Background filled based on Outback Criteria              ///////////////////////////////////

riskentrylong = usersitrigger ? (rsi>rsiMA and rsi>MHULL and rsi<rsitrigger ? true : false) : rsi>rsiMA and rsi>MHULL ? true : false
entrylong = rsi>rsiMA and rsi>MHULL and rsi>rsitrigger ? true : false

riskentryshort = usersitrigger ? (rsi<rsiMA and rsi<MHULL and rsi>rsitrigger ? true : false) : rsi<rsiMA and rsi<MHULL ? true : false
entryshort = rsi<rsiMA and rsi<MHULL and rsi<rsitrigger ? true : false

bgcolor(riskentrylong ? color.new(#056656,50) : na, title="Risk Entry Long")
bgcolor(entrylong and usersitrigger ? color.new(#00332a,50) : na, title="Safe Entry Long")
bgcolor(riskentryshort ? color.new(#ff9800, 50) : na, title="Risk Entry Short")
bgcolor(entryshort and usersitrigger ? color.new(#e65100, 50) : na, title="Safe Entry Short")

/////  Adding alerts for risk entries
m_alertMsgBull = "ORSI - Bullish"
m_alertMsgBear = "ORSI - Bearish"
alertcondition(riskentrylong and not riskentrylong[1] ? true : false, title=m_alertMsgBull, message=m_alertMsgBull)
alertcondition(riskentryshort and not riskentryshort[1] ? true : false, title=m_alertMsgBear, message=m_alertMsgBear)
// if (riskentrylong)
//     alert(message=m_alertMsgBull, freq=alert.freq_once_per_bar)
// if (riskentryshort)
//     alert(message=m_alertMsgBear, freq=alert.freq_once_per_bar)
