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

//@version=5
indicator("Multiple RSI Strategies [MoonKoder]", overlay = false)


// ==============================================
// ==       GET USER GENERAL SETTINGS          ==
// ==============================================


// {

// ToolTips Strings to Help User
ToolTip0_Str    = "Choose The Strategy to Detect Signals \n\n"
ToolTip1_Str    = "01 - Traditional: \nActivates signal when crosses the Strat5_OverBought or the Strat5_OverSold limits \n\n"
ToolTip2_Str    = "02 - MidLevel Cross: \nActivates signal when crosses the 50 line \n"
ToolTip3_Str    = "03 - Histogram: \nActivates signal when histogram crosses the 0 line \n\n"
ToolTip4_Str    = "04 - Moving Average Cross: \nActivates signal when crosses the Moving Average \n\n"
ToolTip5_Str    = "05 - Divergence: \nActivates signal when a divergence is found between Price Action and Rsi\n\n"
ToolTip6_Str    = "06 - Multi Rsi: \nActivates signal when there's a cross from both Rsi's"
ToolTip_Str     = ToolTip0_Str + ToolTip1_Str + ToolTip2_Str + ToolTip3_Str + ToolTip4_Str + ToolTip5_Str
ToolTip_Alert   = "Check if you want Alerts Activated"


// Title of Window
gr_General_Str  = "GENERAL SETTINGS"


// Get General User Inputs
StrategyChoose  = input.string(title = "Strategy to Use", defval = "01 - Traditional", 
                  options = ["01 - Traditional", "02 - MidLevel Cross", "03 - Histogram", "04 - Moving Average Cross", "05 - Divergence", "06 - Multi Rsi"],
                  group = gr_General_Str, tooltip = ToolTip_Str)
UseAlerts       = input.bool(title = "Use Alerts", defval = true, group = gr_General_Str, tooltip = ToolTip_Alert)

// }


// ==============================================
// ==             IMPORTANT CALCS              ==
// ==============================================


// {


// Function to Determine Rsi Value
Function_Rsi(_Source, _Period) =>
    Rsi = ta.rsi(_Source, _Period)


// Get Heikin-Ashi Values
HeikinAshi_Open     = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, open)
HeikinAshi_High     = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, high)
HeikinAshi_Low      = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, low)
HeikinAshi_Close    = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close)


// Function to Determine Source
Function_SourceChange(_Source) =>
    SourceType  = _Source == "01 - Standard Open"  ? open  : 
                  _Source == "02 - Standard High"  ? high  : 
                  _Source == "03 - Standard Low"   ? low   : 
                  _Source == "04 - Standard Close" ? close : 
                  _Source == "05 - Standard HLC3"  ? hlc3  :
                  _Source == "06 - Standard HL2"   ? hl2   :
                  _Source == "07 - Heikin Ashi Open"  ? HeikinAshi_Open  : 
                  _Source == "08 - Heikin Ashi High"  ? HeikinAshi_High  :
                  _Source == "09 - Heikin Ashi Low"   ? HeikinAshi_Low   :
                  _Source == "10 - Heikin Ashi Close" ? HeikinAshi_Close : na


// Define Bullish and Bearish Signals for Alerts
bool BuySignal  = na
bool SellSignal = na


// General Function to Calculate Moving Average
Function_MovAvg(_Source, _Period, _Smoothing) =>
    MovAvg  = switch _Smoothing
        "EMA" => 
            ta.ema(_Source, _Period)
        "RMA" =>
            ta.rma(_Source, _Period)
        "SMA" =>
            ta.sma(_Source, _Period)
        "WMA" =>
            ta.wma(_Source, _Period)
        "HMA" =>
            ta.hma(_Source, _Period)


// }


// ==============================================
// ==                 OPTION 1                 ==
// ==============================================


// {

// ToolTips Strings to Help User
ToolTip_Op1_Period      = "Period: \nPeriod Value to Rsi Study \n\n"
ToolTip_Op1_Source      = "Source: \nSource Value to Rsi Study \n\n"
ToolTip_Op1_OS          = "Strat5_OverSold Level: \nChoose the Level for Strat5_OverSold Position \n\n"
ToolTip_Op1_OB          = "Strat5_OverBought Level: \nChoose the Level for Strat5_OverBought Position \n\n"
ToolTip_Op1_TimeFrame   = "TimeFrame: \nChoose the TimeFrame you want to study\n\n"
ToolTip_Op1_ClrChange   = "Color Change: \nCheck if you want to change to change the colour on limit Over areas"


// Title of Window
gr_Option1      = "01 - TRADITIONAL"


// Get General User Inputs
Strat1_Period       = input.int(title = "Period", defval = 14, minval = 0, step = 1, group = gr_Option1, tooltip = ToolTip_Op1_Period)
Strat1_Source       = input.string(title = "Source", defval = "04 - Standard Close", 
                      options = ["01 - Standard Open", "02 - Standard High", "03 - Standard Low", "04 - Standard Close", "05 - Standard HLC3", "06 - Standard HL2", 
                      "07 - Heikin Ashi Open", "08 - Heikin Ashi High", "09 - Heikin Ashi Low", "10 - Heikin Ashi Close"], group = gr_Option1, tooltip = ToolTip_Op1_Source)
Strat1_OverS        = input.float(title = "Strat5_OverSold Level", defval = 30, minval = 0, maxval = 50, step = 1, group = gr_Option1, tooltip = ToolTip_Op1_OS)
Strat1_OverB        = input.float(title = "Strat5_OverBought Level", defval = 70, minval = 50, maxval = 100, step = 1, group = gr_Option1, tooltip = ToolTip_Op1_OB)
Strat1_TimeF        = input.timeframe(title = "TimeFrame", defval = "15", group = gr_Option1, tooltip = ToolTip_Op1_TimeFrame)
Strat1_ClrTrd       = input.bool(title = "Color Change", defval = true, group = gr_Option1, tooltip = ToolTip_Op1_ClrChange)


// Calculate [Option 1] RSI
Strat1_Rsi      = request.security(syminfo.tickerid, Strat1_TimeF, Function_Rsi(Function_SourceChange(Strat1_Source), Strat1_Period))
Strat1_Color    = ((Strat1_Rsi < Strat1_OverS) and Strat1_ClrTrd) ? color.teal : ((Strat1_Rsi > Strat1_OverB) and Strat1_ClrTrd) ? color.red : color.yellow


// Preparing Plot for [Option 1]
Strat1_PrePlot_Rsi  = StrategyChoose == "01 - Traditional" ? Strat1_Rsi : na
Strat1_PrePlot_OS   = StrategyChoose == "01 - Traditional" ? Strat1_OverS : na
Strat1_PrePlot_OB   = StrategyChoose == "01 - Traditional" ? Strat1_OverB : na
Strat1_PrePlot_Clr  = StrategyChoose == "01 - Traditional" ? Strat1_Color : na


// Plot [Option 1] Results
Plot_Strat1Rsi      = plot(Strat1_PrePlot_Rsi, color = Strat1_PrePlot_Clr, linewidth = 2)
Plot_StratOS        = plot(Strat1_PrePlot_OS, color = color.black, linewidth = 1)
Plot_StratOB        = plot(Strat1_PrePlot_OB, color = color.black, linewidth = 1)


// Set [Option 1] Signals for Alerts
BuySignal   := ta.crossover(Strat1_Rsi, Strat1_OverS) and StrategyChoose == "01 - Traditional"
SellSignal  := ta.crossunder(Strat1_Rsi, Strat1_OverB) and StrategyChoose == "01 - Traditional"


// }


// ==============================================
// ==                 OPTION 2                 ==
// ==============================================


// {

// ToolTips Strings to Help User
ToolTip_Op2_Period      = "Period: \nPeriod Value to Rsi Study \n\n"
ToolTip_Op2_Source      = "Source: \nSource Value to Rsi Study \n\n"
ToolTip_Op2_TimeFrame   = "TimeFrame: \nChoose the TimeFrame you want to study \n\n"
ToolTip_Op2_ClrChange   = "Color Change: \nCheck if you want to change to change the colour on limit Over areas"


// Title of Window
gr_Option2      = "02 - MIDLEVEL CROSS"


// Get General User Inputs
Strat2_Period       = input.int(title = "Period", defval = 14, minval = 0, step = 1, group = gr_Option2, tooltip = ToolTip_Op2_Period)
Strat2_Source       = input.string(title = "Source", defval = "04 - Standard Close", 
                      options = ["01 - Standard Open", "02 - Standard High", "03 - Standard Low", "04 - Standard Close", "05 - Standard HLC3", "06 - Standard HL2", 
                      "07 - Heikin Ashi Open", "08 - Heikin Ashi High", "09 - Heikin Ashi Low", "10 - Heikin Ashi Close"], group = gr_Option2, tooltip = ToolTip_Op2_Source)
Strat2_TimeF        = input.timeframe(title = "TimeFrame", defval = "15", group = gr_Option2, tooltip = ToolTip_Op2_TimeFrame)
Strat2_ClrTrd       = input.bool(title = "Color Change", defval = true, group = gr_Option2, tooltip = ToolTip_Op2_ClrChange)


// Calculate [Option 2] RSI
Strat2_Rsi          = request.security(syminfo.tickerid, Strat2_TimeF, Function_Rsi(Function_SourceChange(Strat2_Source), Strat2_Period))
Strat2_Color        = ((Strat2_Rsi > 50) and Strat2_ClrTrd) ? color.teal : color.red


// Preparing Plot for [Option 2]
Strat2_PrePlot_Rsi  = StrategyChoose == "02 - MidLevel Cross" ? Strat2_Rsi : na
Strat2_PrePlot_Clr  = StrategyChoose == "02 - MidLevel Cross" ? Strat2_Color : na
Strat2_PrePlot_Mid  = StrategyChoose == "02 - MidLevel Cross" ? 50 : na



// Plot [Option 2] Results
Plot_Strat2Rsi      = plot(Strat2_PrePlot_Rsi, color = Strat2_PrePlot_Clr, linewidth = 2)
Plot_Strat2MidPoint = plot(Strat2_PrePlot_Mid, color = color.black, linewidth = 1)


// Set [Option 2] Signals for Alerts
BuySignal   := ta.crossover(Strat2_Rsi, 50) and StrategyChoose == "02 - MidLevel Cross"
SellSignal  := ta.crossunder(Strat2_Rsi, 50) and StrategyChoose == "02 - MidLevel Cross"


// }



// ==============================================
// ==                 OPTION 3                 ==
// ==============================================


// {

// ToolTips Strings to Help User
ToolTip_Op3_RsiPeriod   = "RSI Period: \nRSI Period Value to Rsi Study \n\n"
ToolTip_Op3_FastPeriod  = "Fast Period: \nFast Period Value to Rsi Study \n\n"
ToolTip_Op3_SlowPeriod  = "Slow Period: \nSlow Period Value to Rsi Study \n\n"
ToolTip_Op3_SignPeriod  = "Signal Period: \nSignal Period Value to Rsi Study \n\n"
ToolTip_Op3_Smooth      = "Smooth: \nChoose the smooth value to the histogram calculation \n\n"
ToolTip_Op3_Source      = "Source: \nSource Value to Rsi Study \n\n"
ToolTip_Op3_TimeFrame   = "TimeFrame: \nChoose the TimeFrame you want to study"


// Title of Window
gr_Option3      = "03 - HISTOGRAM"


// Get General User Inputs
Strat3_RsiPeriod    = input.int(title = "RSI Period", defval = 14, minval = 0, step = 1, group = gr_Option3, tooltip = ToolTip_Op3_RsiPeriod)
Strat3_FastPeriod   = input.int(title = "Fast Period", defval = 9, minval = 0, step = 1, group = gr_Option3, tooltip = ToolTip_Op3_FastPeriod)
Strat3_SlowPeriod   = input.int(title = "Slow Period", defval = 14, minval = 0, step = 1, group = gr_Option3, tooltip = ToolTip_Op3_SlowPeriod)
Strat3_SignalPeriod = input.int(title = "Signal Period", defval = 9, minval = 0, step = 1, group = gr_Option3, tooltip = ToolTip_Op3_SignPeriod)
Strat3_Source       = input.string(title = "Source", defval = "04 - Standard Close", 
                      options = ["01 - Standard Open", "02 - Standard High", "03 - Standard Low", "04 - Standard Close", "05 - Standard HLC3", "06 - Standard HL2", 
                      "07 - Heikin Ashi Open", "08 - Heikin Ashi High", "09 - Heikin Ashi Low", "10 - Heikin Ashi Close"], group = gr_Option3, tooltip = ToolTip_Op3_Source)
Strat3_Smooth       = input.string(title = "Smoothing", defval = "EMA", options = ["EMA", "RMA", "SMA", "WMA", "HMA"], group = gr_Option3, tooltip = ToolTip_Op3_Smooth)
Strat3_TimeF        = input.timeframe(title = "TimeFrame", defval = "15", group = gr_Option3, tooltip = ToolTip_Op3_TimeFrame)


// Calculate [Option 3] Moving Average Convergence Divergence
Strat3_Rsi          = request.security(syminfo.tickerid, Strat3_TimeF, Function_Rsi(Function_SourceChange(Strat3_Source), Strat3_RsiPeriod))
Strat3_FastMovAvg   = request.security(syminfo.tickerid, Strat3_TimeF, Function_MovAvg(Strat3_Rsi, Strat3_FastPeriod, Strat3_Smooth))
Strat3_SlowMovAvg   = request.security(syminfo.tickerid, Strat3_TimeF, Function_MovAvg(Strat3_Rsi, Strat3_SlowPeriod, Strat3_Smooth))
Strat3_RsiMacd      = Strat3_FastMovAvg - Strat3_SlowMovAvg
Strat3_Signal       = request.security(syminfo.tickerid, Strat3_TimeF, Function_MovAvg(Strat3_RsiMacd, Strat3_SignalPeriod, Strat3_Smooth))
Strat3_Histogram    = Strat3_RsiMacd - Strat3_Signal
Strat3_MacdColor    = color.aqua
Strat3_SignalColor  = color.orange
Strat3_HistoColor   = (Strat3_Histogram > 0 and Strat3_Histogram > Strat3_Histogram[1]) ? color.teal : 
                      (Strat3_Histogram > 0 and Strat3_Histogram < Strat3_Histogram[1]) ? color.lime : 
                      (Strat3_Histogram < 0 and Strat3_Histogram < Strat3_Histogram[1]) ? color.red : 
                      (Strat3_Histogram < 0 and Strat3_Histogram > Strat3_Histogram[1]) ? color.maroon : na


// Preparing Plot for [Option 3]
Strat3_PrePlot_Macd = StrategyChoose == "03 - Histogram" ? Strat3_RsiMacd : na
Strat3_PrePlot_Sign = StrategyChoose == "03 - Histogram" ? Strat3_Signal : na
Strat3_PrePlot_Hist = StrategyChoose == "03 - Histogram" ? Strat3_Histogram : na
Strat3_PrePlot_Mid  = StrategyChoose == "03 - Histogram" ? 0 : na



// Plot [Option 3] Results
Plot_Strat3Macd     = plot(Strat3_PrePlot_Macd, color = Strat3_MacdColor, linewidth = 2, title = "Option 3 - Macd Rsi")
Plot_Strat3Signal   = plot(Strat3_PrePlot_Sign, color = Strat3_SignalColor, linewidth = 2, title = "Option 3 - Signal")
Plot_Strat3MidPoint = plot(Strat3_PrePlot_Mid, color = color.black, linewidth = 1, title = "Option 3 - Middle Line")
Plot_Strat3Histo    = plot(Strat3_PrePlot_Hist, style = plot.style_columns, color = Strat3_HistoColor, title = "Option 3 - Histogram")


// Set [Option 3] Signals for Alerts
BuySignal   := ta.crossover(Strat3_Histogram, 0) and (Strat3_RsiMacd < 0) and (Strat3_Signal < 0) and StrategyChoose == "03 - Histogram"
SellSignal  := ta.crossunder(Strat3_Histogram, 0) and (Strat3_RsiMacd < 0) and (Strat3_Signal < 0) and StrategyChoose == "03 - Histogram"


// }


// ==============================================
// ==                 OPTION 4                 ==
// ==============================================


// {

// ToolTips Strings to Help User
ToolTip_Op4_Period      = "RSI Period: \nPeriod Value to Rsi Study \n\n"
ToolTip_Op4_AvgPeriod   = "Moving Average Period: \nPeriod Value to the Moving Average Study \n\n"
ToolTip_Op4_Smooth      = "Smooth: \nChoose the smooth value to the Moving Average calculation \n\n"
ToolTip_Op4_Source      = "Source: \nSource Value to Rsi Study \n\n"
ToolTip_Op4_TimeFrame   = "TimeFrame: \nChoose the TimeFrame you want to study"


// Title of Window
gr_Option4      = "04 - MOVING AVERAGE CROSS"


// Get General User Inputs
Strat4_RsiPeriod    = input.int(title = "RSI Period", defval = 14, minval = 0, step = 1, group = gr_Option4, tooltip = ToolTip_Op4_Period)
Strat4_AvgPeriod    = input.int(title = "Moving Average Period", defval = 14, minval = 0, step = 1, group = gr_Option3, tooltip = ToolTip_Op4_AvgPeriod)
Strat4_Source       = input.string(title = "Source", defval = "04 - Standard Close", 
                      options = ["01 - Standard Open", "02 - Standard High", "03 - Standard Low", "04 - Standard Close", "05 - Standard HLC3", "06 - Standard HL2", 
                      "07 - Heikin Ashi Open", "08 - Heikin Ashi High", "09 - Heikin Ashi Low", "10 - Heikin Ashi Close"], group = gr_Option4, tooltip = ToolTip_Op4_Source)
Strat4_Smooth       = input.string(title = "Smoothing", defval = "EMA", options = ["EMA", "RMA", "SMA", "WMA", "HMA"], group = gr_Option4, tooltip = ToolTip_Op4_Smooth)
Strat4_TimeF        = input.timeframe(title = "TimeFrame", defval = "15", group = gr_Option4, tooltip = ToolTip_Op4_TimeFrame)


// Calculate [Option 4] RSI and Respective Moving Average
Strat4_Rsi          = request.security(syminfo.tickerid, Strat4_TimeF, Function_Rsi(Function_SourceChange(Strat4_Source), Strat4_RsiPeriod))
Strat4_MovAvg       = request.security(syminfo.tickerid, Strat4_TimeF, Function_MovAvg(Strat4_Rsi, Strat4_AvgPeriod, Strat4_Smooth))


// Preparing Plot for [Option 4]
Strat4_PrePlot_Rsi  = StrategyChoose == "04 - Moving Average Cross" ? Strat4_Rsi : na
Strat4_PrePlot_Avg  = StrategyChoose == "04 - Moving Average Cross" ? Strat4_MovAvg : na
Strat4_PrePlot_Mid  = StrategyChoose == "04 - Moving Average Cross" ? 50 : na


// Plot [Option 4] Results
Plot_Strat4_Rsi     = plot(Strat4_PrePlot_Rsi, color = color.purple, linewidth = 2, title = "Option 4 - Rsi")
Plot_Strat4_Avg     = plot(Strat4_PrePlot_Avg, color = color.orange, linewidth = 2, title = "Option 4 - Moving Average")
Plot_Strat4MidPoint = plot(Strat4_PrePlot_Mid, color = color.black, linewidth = 1, title = "Option 4 - Middle Line")


// Set [Option 4] Signals for Alerts
BuySignal   := ta.crossover(Strat4_Rsi, Strat4_MovAvg) and StrategyChoose == "04 - Moving Average Cross"
SellSignal  := ta.crossunder(Strat4_Rsi, Strat4_MovAvg) and StrategyChoose == "04 - Moving Average Cross"


// }



// ==============================================
// ==                 OPTION 5                 ==
// ==============================================


// {


// ToolTips Strings to Help User
ToolTip_Op5_Period          = "Period: \nPeriod Value to Rsi Study \n\n"
ToolTip_Op5_Source          = "Source: \nSource Value to Rsi Study \n\n"
ToolTip_Op5_LookRight       = "Pivot Lookback Right: \nNumber of candles that must exist on right to detect pivot \n\n"
ToolTip_Op5_LookLeft        = "Pivot Lookback Left: \nNumber of candles that must exist on left to detect pivot \n\n"
ToolTip_Op5_MaxRange        = "Max of Lookback Range: \nMaximum number of candles to lookback to detect a divergence"
ToolTip_Op5_MinRange        = "Min of Lookback Range: \nMinimum number of candles to lookback to detect a divergence"
ToolTip_Op5_BullDiv         = "Plot Bullish Divergence: \nShow Standard Bullish Divergence"
ToolTip_Op5_MediumBullDiv   = "Plot Middle Bullish Divergence: \nShow Medium Point Bullish Divergence"
ToolTip_Op5_BearDiv         = "Plot Bearish Divergence: \nShow Standard Bearish Divergence"
ToolTip_Op5_MediumBearDiv   = "Plot Middle Bearish Divergence: \nShow Medium Point Bearish Divergence"
ToolTip_Op5_OS              = "Strat5_OverSold Level: \nChoose the Level for Strat5_OverSold Position \n\n"
ToolTip_Op5_OB              = "Strat5_OverBought Level: \nChoose the Level for Strat5_OverBought Position \n\n"
ToolTip_Op5_BearColour      = "Bearish Color: \nColor from the Bearish Labels \n\n"
ToolTip_Op5_BullColour      = "Bullish Color: \nColor from the Bullish Labels \n\n"
ToolTip_Op5_LabelTextColour = "Text Label Color: \nColor from the labl's text \n\n"


// Title of Window
gr_Option5      = "05 - DIVERGENCE"


// Get General User Inputs
Strat5_RsiPeriod            = input.int(title = "Period", minval = 1, defval = 5, tooltip = ToolTip_Op5_Period, group = gr_Option5)
Strat5_RsiSource            = input.string(title = "Source", defval = "04 - Standard Close", 
                              options = ["01 - Standard Open", "02 - Standard High", "03 - Standard Low", "04 - Standard Close", "05 - Standard HLC3", "06 - Standard HL2", 
                              "07 - Heikin Ashi Open", "08 - Heikin Ashi High", "09 - Heikin Ashi Low", "10 - Heikin Ashi Close"], tooltip = ToolTip_Op5_Source, group = gr_Option5)
Strat5_LookBackRight        = input.int(title = "Pivot Lookback Right", defval = 3, tooltip = ToolTip_Op5_LookRight, group = gr_Option5)
Strat5_LookBackLeft         = input.int(title = "Pivot Lookback Left", defval = 1, tooltip = ToolTip_Op5_LookLeft, group = gr_Option5)
Strat5_MaxLookBackRange     = input.int(title = "Max of Lookback Range", defval = 60, tooltip = ToolTip_Op5_MaxRange, group = gr_Option5)
Strat5_MinLookBackRange     = input.int(title = "Min of Lookback Range", defval = 5, tooltip = ToolTip_Op5_MinRange, group = gr_Option5)
Strat5_ShowBullDiv          = input.bool(title = "Plot Bullish Divergence", defval = true, tooltip = ToolTip_Op5_BullDiv, group = gr_Option5)
Strat5_ShowHiddenBullDiv    = input.bool(title = "Plot Hidden Bullish", defval = true, tooltip = ToolTip_Op5_MediumBullDiv, group = gr_Option5)
Strat5_ShowBearDiv          = input.bool(title = "Plot Bearish", defval = true, tooltip = ToolTip_Op5_BearDiv, group = gr_Option5)
Strat5_ShowHiddenBear       = input.bool(title = "Plot Hidden Bearish", defval = false, tooltip = ToolTip_Op5_MediumBearDiv, group = gr_Option5)
Strat5_OverSold             = input.float(title = "Strat5_OverSold Level", defval = 30, minval = 0, maxval = 100, tooltip = ToolTip_Op5_OS, group = gr_Option5)
Strat5_OverBought           = input.float(title = "Strat5_OverBought Level", defval = 70, minval = 50, maxval = 100, tooltip = ToolTip_Op5_OB, group = gr_Option5)
Strat5_BearishColour        = input.color(title = "Bearish Color", defval = color.red, tooltip = ToolTip_Op5_BearColour, group = gr_Option5)
Strat5_BullishColour        = input.color(title = "Bullish Color", defval = color.teal, tooltip = ToolTip_Op5_BullColour, group = gr_Option5)
Strat5_TextLabelColour      = input.color(title = "Text Label Color", defval = color.white, tooltip = ToolTip_Op5_LabelTextColour, group = gr_Option5)


// Function to Check if Study Candles Are In The Range
FunctionRangeChecker(RangeChecker) =>
    RangeBars   = ta.barssince(RangeChecker == true)
    IsInRange   = Strat5_MinLookBackRange <= RangeBars and RangeBars <= Strat5_MaxLookBackRange


// Calculate Rsi
Strat5_Rsi      = request.security(syminfo.tickerid, timeframe.period, Function_Rsi(Function_SourceChange(Strat5_RsiSource), Strat5_RsiPeriod))


// Check For Pivots On Rsi
IsPivotLow      = na(ta.pivotlow(Strat5_Rsi, Strat5_LookBackLeft, Strat5_LookBackRight)) ? false : true
IsPivotHigh     = na(ta.pivothigh(Strat5_Rsi, Strat5_LookBackLeft, Strat5_LookBackRight)) ? false : true


// Calculate Standard Bullish Divergence (Rsi Higher Low + Price Lower Low)
Rsi_HigherLow   = Strat5_Rsi[Strat5_LookBackRight] > ta.valuewhen(IsPivotLow, Strat5_Rsi[Strat5_LookBackRight], 1) and FunctionRangeChecker(IsPivotLow[1])
Price_LowerLow  = low[Strat5_LookBackRight] < ta.valuewhen(IsPivotLow, low[Strat5_LookBackRight], 1)
IsBullDiv       = Strat5_ShowBullDiv and Price_LowerLow and Rsi_HigherLow and IsPivotLow and StrategyChoose == "05 - Divergence"


// Calculate Medium Bullish Divergence (Rsi Lower Low + Price Higher Low)
Rsi_LowerLow    = Strat5_Rsi[Strat5_LookBackRight] < ta.valuewhen(IsPivotLow, Strat5_Rsi[Strat5_LookBackRight], 1) and FunctionRangeChecker(IsPivotLow[1])
Price_HigherLow = low[Strat5_LookBackRight] > ta.valuewhen(IsPivotLow, low[Strat5_LookBackRight], 1)
IsMediumBullDiv = Strat5_ShowHiddenBullDiv and Price_HigherLow and Rsi_LowerLow and IsPivotLow and StrategyChoose == "05 - Divergence"


// Calculate Standard Bearish Divergence (Rsi Lower High + Price Higher High)
Rsi_LowerHigh       = Strat5_Rsi[Strat5_LookBackRight] < ta.valuewhen(IsPivotHigh, Strat5_Rsi[Strat5_LookBackRight], 1) and FunctionRangeChecker(IsPivotHigh[1])
Price_HigherHigh    = high[Strat5_LookBackRight] > ta.valuewhen(IsPivotHigh, high[Strat5_LookBackRight], 1)
IsBearDiv           = Strat5_ShowBearDiv and Price_HigherHigh and Rsi_LowerHigh and IsPivotHigh and StrategyChoose == "05 - Divergence"


// Calculate Medium Bearish Divergence (Rsi Higher High + Price Lower High)
Rsi_HigherHigh      = Strat5_Rsi[Strat5_LookBackRight] > ta.valuewhen(IsPivotHigh, Strat5_Rsi[Strat5_LookBackRight], 1) and FunctionRangeChecker(IsPivotHigh[1])
Price_LowerHigh     = high[Strat5_LookBackRight] < ta.valuewhen(IsPivotHigh, high[Strat5_LookBackRight], 1)
IsMediumBearDiv     = Strat5_ShowHiddenBear and Price_LowerHigh and Rsi_HigherHigh and IsPivotHigh and StrategyChoose == "05 - Divergence"


// Plot Default Indicator Buffers

Strat5_PrePlot_Rsi  = StrategyChoose == "05 - Divergence" ? Strat5_Rsi : na
Strat5_PrePlot_Mid  = StrategyChoose == "05 - Divergence" ? 50 : na
Strat5_PrePlot_OS   = StrategyChoose == "05 - Divergence" ? Strat5_OverSold : na
Strat5_PrePlot_OB   = StrategyChoose == "05 - Divergence" ? Strat5_OverBought : na

Stra5_Rsi_Plot      = plot(Strat5_PrePlot_Rsi, title = "Option 5 - Rsi", linewidth = 2, color = color.purple)
Stra5_MidPoint_Plot = plot(Strat5_PrePlot_Mid, title = "Option 5 - MidPoint Line", linewidth = 1, color = color.black)
Stra5_OverS_Plot    = plot(Strat5_PrePlot_OS, title = "Option 5 - OverSold Line", linewidth = 1, color = color.black)
Stra5_OverB_Plot    = plot(Strat5_PrePlot_OB, title = "Option 5 - OverBought Line", linewidth = 1, color = color.black)


// Plot Standard Bullish Divergence
plot(IsPivotLow and StrategyChoose == "05 - Divergence" ? Strat5_Rsi[Strat5_LookBackRight] : na, title = "Standard Bullish Divergence", offset = -Strat5_LookBackRight, 
     linewidth = 2, color = (IsBullDiv ? Strat5_BullishColour : color.new(color.white, 100)))

plotshape(IsBullDiv and StrategyChoose == "05 - Divergence" ? Strat5_Rsi[Strat5_LookBackRight] : na, title = "Standard Bullish Divergence Label", offset = -Strat5_LookBackRight,
          text = " Bull \n Div ", style = shape.labelup, location = location.absolute, color = Strat5_BullishColour, textcolor = Strat5_TextLabelColour)


// Plot Medium Bullish Divergence
plot(IsPivotLow and StrategyChoose == "05 - Divergence" ? Strat5_Rsi[Strat5_LookBackRight] : na, title = "Medium Bullish Divergence", offset = -Strat5_LookBackRight, 
     linewidth=2, color=(IsMediumBullDiv ? Strat5_BullishColour : color.new(color.white, 100)))
     
plotshape(IsMediumBullDiv and StrategyChoose == "05 - Divergence" ? Strat5_Rsi[Strat5_LookBackRight] : na, title = "Medium Bullish Divergence Label", offset = -Strat5_LookBackRight,
          text =" M Bull \n Div", style = shape.labelup, location = location.absolute, color = Strat5_BullishColour, textcolor = Strat5_TextLabelColour)


// Plot Standard Bearish Divergence
plot(IsPivotHigh and StrategyChoose == "05 - Divergence" ? Strat5_Rsi[Strat5_LookBackRight] : na, title = "Standard Bearish Divergence", offset = -Strat5_LookBackRight,
     linewidth = 2, color = (IsBearDiv ? Strat5_BearishColour : color.new(color.white, 100)))

plotshape(IsBearDiv and StrategyChoose == "05 - Divergence" ? Strat5_Rsi[Strat5_LookBackRight] : na, title = "Standard Bearish Divergence Label", offset = -Strat5_LookBackRight,
          text =" Bear \n Div ", style = shape.labeldown, location = location.absolute, color = Strat5_BearishColour, textcolor = Strat5_TextLabelColour)


// Plot Medium Bearish Divergence
plot(IsPivotHigh and StrategyChoose == "05 - Divergence" ? Strat5_Rsi[Strat5_LookBackRight] : na, title = "Medium Bearish Divergence", offset = -Strat5_LookBackRight,
     linewidth = 2, color = (IsMediumBearDiv ? Strat5_BearishColour : color.new(color.white, 100)))

plotshape(IsMediumBearDiv and StrategyChoose == "05 - Divergence" ? Strat5_Rsi[Strat5_LookBackRight] : na, title = "Medium Bearish Divergence Label", offset = -Strat5_LookBackRight, 
          text = " M Bear \n Div", style = shape.labeldown, location = location.absolute, color = Strat5_BearishColour, textcolor = Strat5_TextLabelColour)



// Set [Option 4] Signals for Alerts
BuySignal   := StrategyChoose == "05 - Divergence" and (IsBullDiv or IsMediumBullDiv) ? Strat5_Rsi[Strat5_LookBackRight] : na
SellSignal  := StrategyChoose == "05 - Divergence" and (IsBearDiv or IsMediumBearDiv) ? Strat5_Rsi[Strat5_LookBackRight] : na

// }


// ==============================================
// ==                 OPTION 6                 ==
// ==============================================


// {


// Title of Window
gr_Option6      = "06 - MULTI RSI"


// ToolTips Strings to Help User
ToolTip_Op6_FastPeriod      = "Fast Period: \nPeriod Value to Rsi Study \n\n"
ToolTip_Op6_FastSource      = "Fast Source: \nSource Value to Rsi Study \n\n"
ToolTip_Op6_FastTimeFrame   = "Fast TimeFrame: \nChoose the TimeFrame you want to study \n\n"
ToolTip_Op6_SlowPeriod      = "Slow Period: \nPeriod Value to Rsi Study \n\n"
ToolTip_Op6_SlowSource      = "Slow Source: \nSource Value to Rsi Study \n\n"
ToolTip_Op6_SlowTimeFrame   = "Slow TimeFrame: \nChoose the TimeFrame you want to study \n\n"


// Get General User Inputs
Strat6_FastPeriod   = input.int(title = "Fast Period", defval = 9, minval = 0, step = 1, group = gr_Option6, tooltip = ToolTip_Op6_FastPeriod)
Strat6_FastSource   = input.string(title = "Fast Source", defval = "04 - Standard Close", 
                      options = ["01 - Standard Open", "02 - Standard High", "03 - Standard Low", "04 - Standard Close", "05 - Standard HLC3", "06 - Standard HL2", 
                      "07 - Heikin Ashi Open", "08 - Heikin Ashi High", "09 - Heikin Ashi Low", "10 - Heikin Ashi Close"], group = gr_Option6, tooltip = ToolTip_Op6_FastSource)
Strat6_FastTimeF    = input.timeframe(title = "TimeFrame", defval = "15", group = gr_Option6, tooltip = ToolTip_Op6_FastTimeFrame)
Strat6_SlowPeriod   = input.int(title = "Slow Period", defval = 9, minval = 0, step = 1, group = gr_Option6, tooltip = ToolTip_Op6_SlowPeriod)
Strat6_SlowSource   = input.string(title = "Slow Source", defval = "04 - Standard Close", 
                      options = ["01 - Standard Open", "02 - Standard High", "03 - Standard Low", "04 - Standard Close", "05 - Standard HLC3", "06 - Standard HL2", 
                      "07 - Heikin Ashi Open", "08 - Heikin Ashi High", "09 - Heikin Ashi Low", "10 - Heikin Ashi Close"], group = gr_Option6, tooltip = ToolTip_Op6_SlowSource)
Strat6_SlowTimeF    = input.timeframe(title = "TimeFrame", defval = "120", group = gr_Option6, tooltip = ToolTip_Op6_SlowTimeFrame)


// Calculate [Option 6] RSI
Strat6_FastRsi      = request.security(syminfo.tickerid, Strat6_FastTimeF, Function_Rsi(Function_SourceChange(Strat6_FastSource), Strat6_FastPeriod))
Strat6_SlowRsi      = request.security(syminfo.tickerid, Strat6_SlowTimeF, Function_Rsi(Function_SourceChange(Strat6_SlowSource), Strat6_SlowPeriod))


// Preparing Plot for [Option 6]
Strat6_PrePlot_FastRsi  = StrategyChoose == "06 - Multi Rsi" ? Strat6_FastRsi : na
Strat6_PrePlot_SlowRsi  = StrategyChoose == "06 - Multi Rsi" ? Strat6_SlowRsi : na
Strat6_PrePlot_Mid      = StrategyChoose == "06 - Multi Rsi" ? 50 : na


// Plot [Option 2] Results
Plot_Strat6FastRsi      = plot(Strat6_PrePlot_FastRsi, color = color.green, linewidth = 2)
Plot_Strat6SlowRsi      = plot(Strat6_PrePlot_SlowRsi, color = color.red, linewidth = 2)
Plot_Strat6MidPoint     = plot(Strat6_PrePlot_Mid, color = color.black, linewidth = 1)


// Set [Option 2] Signals for Alerts
BuySignal   := ta.crossover(Strat6_FastRsi, Strat6_SlowRsi) and (Strat6_SlowRsi > 50) and StrategyChoose == "06 - Multi Rsi"
SellSignal  := ta.crossunder(Strat6_FastRsi, Strat6_SlowRsi) and (Strat6_SlowRsi < 50) and StrategyChoose == "06 - Multi Rsi"


// ==============================================
// ==             ALERT GENERATOR              ==
// ==============================================


// {

StringMessageAlert  = BuySignal ? "A Long Signal has been generated" : SellSignal ? "A Short Signal has been generated" : na

if ((BuySignal or SellSignal) and barstate.isconfirmed)
    alert(StringMessageAlert, alert.freq_once_per_bar_close)


// }