Advanced RSI with Volatility Bands [RedWhite]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mrojas-mx

//@version=5

// # ========================================================================= #
// #                   |   Indicator  |
// # ========================================================================= #
indicator(
 title                = "Advanced RSI with Volatility Bands (VB)"   ,
 shorttitle           = "Advanced RSI"                              ,
 overlay              = false                                       ,
 precision            = 2                                           ,
 scale                = scale.right                                 ,
 timeframe            = ""                                          ,
 timeframe_gaps       = true                                        ,
 explicit_plot_zorder = false                                       ,
 max_lines_count      = 500                                         ,
 max_labels_count     = 500                                         ,
 max_boxes_count      = 500
 )
// # ========================================================================= #
// #                   |   Indicator  |
// # ========================================================================= #




//******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ********
// # ======================================================================================================================== #
//
//  ⬜  C O N S T A N T    V A L U E S
//
// # ======================================================================================================================== #
//******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ********


//*-[ Parameters: titles ]-*
string Txt_Hdg_1    = "Moving Average: Tom Tillson (T3)"
string Txt_Hdg_2    = "Moving Average: Arnaud Legoux (ALMA)"
string Txt_Hdg_3    = "Relative Strength Index (RSI)"
string Txt_Hdg_5    = "Moving Average Bands"
string Txt_Hdg_6    = "Band crossing (maximum and minimum)"
string Txt_Hdg_4    = "Moving Average Indicator"
// *-[ ******** ]-*


//*-[ Parameters: colors ]-*
color Clr_RSI       = color.new(color = #9F47E1 , transp = 0    )
color Clr_MA_RSI    = color.new(color = #FFF538 , transp = 0    )
color Clr_Up_1      = color.new(color = #00cc99 , transp = 0    )
color Clr_Dn_1      = color.new(color = #d90368 , transp = 0    )
// < --------------------------------------------------------------------------------------------------------- >
color Clr_Band      = color.new(color = #568CCE , transp = 50   )
color Clr_Ln_1      = color.new(color = #adb5bd , transp = 40   )
color Clr_Ln_2      = color.new(color = #adb5bd , transp = 60   )
color Clr_Ln_3      = color.new(color = #B573E8 , transp = 90   )
// *-[ ******** ]-*


//*-[ Parameters: descriptions ]-*
string Ttip_Grp_1   = "The T3 moving average was developed by Tom Tillson.\nThis moving average is designed to be smoother and more responsive than traditional moving averages, but it can overshoot the price or indicator to which it is applied.\nThe volume factor is usually 0.7 or 0.618."
string Ttip_Grp_2   = "The ALMA moving average was developed by Arnaud Legoux.\nThe ALMA moving average is more sensitive to recent price movements than conventional moving averages, allowing for faster identification of emerging trends."
// *-[ ******** ]-*




//******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ********
// # ======================================================================================================================== #
//
//  🟦  I N P U T    D A T A  :  G E N E R A L
//
// # ======================================================================================================================== #
//******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ********


//*-[ Input values: Parameters for Tom Tillson's MA ]-*
aa_T3_Val       = input.float   (defval = 0.70  , title = "Alpha"   , tooltip   = Ttip_Grp_1 , group = Txt_Hdg_1 )
// *-[ ******** ]-*


//*-[ Input values: Parameters for Arnaud Legoux's MA ]-*
ba_ALMA_Ofst    = input.float   (defval = 0.85  , title = "Offset"  , tooltip   = Ttip_Grp_2 , group = Txt_Hdg_2 )
ba_ALMA_Sgm     = input.float   (defval = 6     , title = "Sigma"   ,                          group = Txt_Hdg_2 )
// *-[ ******** ]-*




//******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ********
// # ======================================================================================================================== #
//
//  🟦  I N P U T    D A T A  :  A D V A N C E D    R S I
//
// # ======================================================================================================================== #
//******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ********


//*-[ Input values : Relative Strength Index (RSI) ]-*
ca_RSI_Lgth = input.int     (defval = 14            , title = ""    , minval = 1 , inline = "01"        , group = Txt_Hdg_3  )
ca_RSI_Src  = input.source  (defval = close         , title = ""                 , inline = "01"        , group = Txt_Hdg_3  )
ca_Clr_RSI  = input.color   (defval = Clr_RSI       , title = "Line color"                              , group = Txt_Hdg_3  )
// *-[ ******** ]-*


//*-[ Input values : Moving Average ]-*
da_MA_Lgth  = input.int     (defval = 14            , title = ""    , minval = 1 , inline = "01"        , group = Txt_Hdg_4 )
da_MA_Mtd   = input.string  (defval = "SMA"         , title = ""        , options = ["SMA", "EMA", "WMA", "HMA", "SMMA/RMA", "SWMA", "ALMA", "VWMA", "VWAP", "DEMA", "TEMA", "T3"], inline = "01"   , group = Txt_Hdg_4 )
da_Clr_MA   = input.color   (defval = Clr_MA_RSI    , title = "Line color"                              , group = Txt_Hdg_4 ) 
// *-[ ******** ]-*


//*-[ Input values : Moving Average Bands ]-*
ea_MA_Lgth  = input.int     (defval = 70            , title = "Length"              , minval = 1        , group = Txt_Hdg_5 )
ea_MA_Stdev = input.float   (defval = 1.0           , title = "Standard deviation"  , minval = 0.0      , group = Txt_Hdg_5 )
ea_MA_Mtd   = input.string  (defval = "EMA"         , title = "Method"  , options = ["SMA", "EMA", "WMA", "HMA", "SMMA/RMA", "SWMA", "ALMA", "VWMA", "VWAP", "DEMA", "TEMA", "T3"]                  , group = Txt_Hdg_5 )
ea_Clr_Bnd  = input.color   (defval = Clr_Band      , title = "Band color"                              , group = Txt_Hdg_5 )
// *-[ ******** ]-*


//*-[ Input values : Lower, middle and upper band ]-*
fa_RSI_Shw  = input.bool    (defval = true          , title = "Show fill color RSI"                             , group = Txt_Hdg_6 )
// < --------------------------------------------------------------------------------------------------------- >
fa_RSI_Up   = input.int     (defval = 70            , title = ""  , minval = 0, maxval = 100  , inline = "01"   , group = Txt_Hdg_6 )
fa_Clr_Up   = input.color   (defval = Clr_Up_1      , title = ""                              , inline = "01"   , group = Txt_Hdg_6 )
// < --------------------------------------------------------------------------------------------------------- >
fb_RSI_Dn   = input.int     (defval = 30            , title = ""  , minval = 0, maxval = 100  , inline = "02"   , group = Txt_Hdg_6 )
fb_Clr_Dn   = input.color   (defval = Clr_Dn_1      , title = ""                              , inline = "02"   , group = Txt_Hdg_6 )
// < --------------------------------------------------------------------------------------------------------- >
fc_RSI_Shw  = input.bool    (defval = true          , title = "Show background color"                           , group = Txt_Hdg_6 )
fc_Clr_Bgd  = input.color   (defval = Clr_Ln_3      , title = "Background color"                                , group = Txt_Hdg_6 )
// *-[ ******** ]-*




//******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ********
// # ======================================================================================================================== #
//
//  🟥  C U S T O M    F U N C T I O N S
//
// # ======================================================================================================================== #
//******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ********


//*-[ DEMA: Doble Exponencial Movil Average ]-*
z_Typ_DEMA          (xb_MA_Src, xb_MA_Lgth) => 
    float xb_DEMA_01    = ta.ema(source = xb_MA_Src     , length = xb_MA_Lgth )
    float xb_DEMA_02    = ta.ema(source = xb_DEMA_01    , length = xb_MA_Lgth )
    float xb_DEMA_Rslt  = 2 *   (xb_DEMA_01 - xb_DEMA_02 )
// *-[ ******** ]-*


//*-[ TEMA: Triple Exponential Moving Average ]-*
z_Typ_TEMA          (xc_MA_Src, xc_MA_Lgth) => 
    float xc_TEMA_01    = ta.ema(source = xc_MA_Src     , length = xc_MA_Lgth )
    float xc_TEMA_02    = ta.ema(source = xc_TEMA_01    , length = xc_MA_Lgth )
    float xc_TEMA_03    = ta.ema(source = xc_TEMA_02    , length = xc_MA_Lgth )
    float xc_TEMA_Rslt  = 3 *   (xc_TEMA_01 - xc_TEMA_02) + xc_TEMA_03  
// *-[ ******** ]-*


//*-[ T3: Tillson Moving Average ]-*
z_Typ_TILLSON       (xd_MA_Src, xd_MA_Lgth, xd_TT_Alpha) =>
    float xd_T3_01      = ta.ema(source = xd_MA_Src     , length = xd_MA_Lgth )
    float xd_T3_02      = xd_T3_01 * ( 1 + aa_T3_Val ) - ta.ema(source = xd_T3_01 , length = xd_MA_Lgth ) * aa_T3_Val
// < --------------------------------------------------------------------------------------------------------- >
z_Typ_T3            (xe_MA_Src, xe_MA_Lgth, xe_TT_Alpha = 0.7) => 
    float xd_T3_Rslt    = z_Typ_TILLSON(z_Typ_TILLSON(z_Typ_TILLSON(xe_MA_Src, xe_MA_Lgth, xe_TT_Alpha),xe_MA_Lgth, xe_TT_Alpha),xe_MA_Lgth, xe_TT_Alpha)
// *-[ ******** ]-*


//*-[ Type of Moving Averages ]-*
z_Slct_MA           (z_Selct_Typ_MA, z_MA_Src, z_MA_Lgth) =>
	float xy_Typ_MA = switch str.upper(z_Selct_Typ_MA)
        "SMA"       => ta.sma     (source = z_MA_Src , length = z_MA_Lgth                                                 )  // Simple Moving Average
        "EMA"       => ta.ema     (source = z_MA_Src , length = z_MA_Lgth                                                 )  // Exponential Moving Average
        "WMA"       => ta.wma     (source = z_MA_Src , length = z_MA_Lgth                                                 )  // Weighted Moving Average 
        "HMA"       => ta.hma     (source = z_MA_Src , length = z_MA_Lgth                                                 )  // Hull Moving Average 
        "SMMA/RMA"  => ta.rma     (source = z_MA_Src , length = z_MA_Lgth                                                 )  // Relative Moving Average or Smoothed Moving Average
        "SWMA"      => ta.swma    (source = z_MA_Src                                                                      )  // Symmetrically-Weighted Moving Average 
        "ALMA"      => ta.alma    (series = z_MA_Src , length = z_MA_Lgth , offset = ba_ALMA_Ofst , sigma = ba_ALMA_Sgm   )  // Arnaud Legoux Moving Average 
        "VWMA"      => ta.vwma    (source = z_MA_Src , length = z_MA_Lgth                                                 )  // Volume-Weighted Moving Average                         
        "VWAP"      => ta.vwap    (z_MA_Src                                                                               )  // Volume-Weighted Average Price
        "DEMA"      => z_Typ_DEMA (z_MA_Src          , z_MA_Lgth                                                          )  // Doble Exponencial Movil Average
        "TEMA"      => z_Typ_TEMA (z_MA_Src          , z_MA_Lgth                                                          )  // Triple Exponential Moving Average
        "T3"        => z_Typ_T3   (z_MA_Src          , z_MA_Lgth          , aa_T3_Val                                     )  // Tillson Moving Average
// *-[ ******** ]-*




//******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ********
// # ======================================================================================================================== #
//
//  🟩  A R I T H M E T I C    C A L C U L A T I O N S
//
// # ======================================================================================================================== #
//******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ********


//*-[ Arithmetic result: Relative Strength Index (RSI) ]-*
cr_RSI_Rslt     = ta.rsi    (source = ca_RSI_Src    , length = ca_RSI_Lgth  )
// *-[ ******** ]-*


//*-[ Arithmetic result: Moving Average ]-*
dr_MAB_Rslt     = z_Slct_MA (da_MA_Mtd , cr_RSI_Rslt , da_MA_Lgth           )
// *-[ ******** ]-*


//*-[ Arithmetic result: Moving Average Band ]-*
er_MA_Rslt      = z_Slct_MA (ea_MA_Mtd , cr_RSI_Rslt , ea_MA_Lgth           )
er_Stdev_Rslt   = ta.stdev  (source = cr_RSI_Rslt    , length = ea_MA_Lgth  )
// *-[ ******** ]-*


//*-[ Arithmetic result: Upper and lower bands ]-*
fr_MAB_Up       = er_MA_Rslt    +   ea_MA_Stdev *   er_Stdev_Rslt
fr_MAB_Dn       = er_MA_Rslt    -   ea_MA_Stdev *   er_Stdev_Rslt
// *-[ ******** ]-*


//*-[ Color gradient: Moving Average Band ]-*
Clr_Grdt_1      = color.from_gradient(
   value = cr_RSI_Rslt
 , bottom_value = fb_RSI_Dn
 , top_value    = fa_RSI_Up
 , bottom_color = fb_Clr_Dn
 , top_color    = fa_Clr_Up
 ) 
// *-[ ******** ]-*


//*-[ Arithmetic result: Trend patterns ]-*
color fr_Ptrn_Up  = color ( cr_RSI_Rslt  >   fr_MAB_Up   ?   Clr_Grdt_1 : na )
color fr_Ptrn_Dn  = color ( cr_RSI_Rslt  <   fr_MAB_Dn   ?   Clr_Grdt_1 : na )
color fr_Clr_Bgd  = color ( fc_RSI_Shw                   ?   fc_Clr_Bgd : na )
// *-[ ******** ]-*




//******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ********
// # ======================================================================================================================== #
//
//  🟪  G R A P H I C    R E P R E S E N T A T I O N
//
// # ======================================================================================================================== #
//******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ******** ********


f_Plot_RSI  = plot(series = cr_RSI_Rslt , title = "RSI"                   , color = ca_Clr_RSI  , linewidth = 1 , style = plot.style_line , trackprice = false, editable = true , display = display.all )
f_Plot_MA   = plot(series = dr_MAB_Rslt , title = "RSI moving average"    , color = da_Clr_MA   , linewidth = 1 , style = plot.style_line , trackprice = false, editable = true , display = display.all )
f_Plot_Up   = plot(series = fr_MAB_Up   , title = "Upper moving average"  , color = ea_Clr_Bnd  , linewidth = 1 , style = plot.style_line , trackprice = false, editable = true , display = display.all )
f_Plot_Dn   = plot(series = fr_MAB_Dn   , title = "Lower moving average"  , color = ea_Clr_Bnd  , linewidth = 1 , style = plot.style_line , trackprice = false, editable = true , display = display.all )
// < --------------------------------------------------------------------------------------------------------- >
h_Line_Up   = hline(price = 70          , title = "Upper band"            , color = Clr_Ln_1, linestyle = hline.style_dashed , linewidth = 1, editable = true )
h_Line_Md   = hline(price = 50          , title = "Middle band"           , color = Clr_Ln_2, linestyle = hline.style_dashed , linewidth = 1, editable = true )
h_Line_Dn   = hline(price = 30          , title = "Lower band"            , color = Clr_Ln_1, linestyle = hline.style_dashed , linewidth = 1, editable = true )
// < --------------------------------------------------------------------------------------------------------- >
fill(plot1  = f_Plot_Up , plot2 = f_Plot_RSI , color = fa_RSI_Shw ? fr_Ptrn_Up : na , title = "Background upper", editable = true )
fill(plot1  = f_Plot_Dn , plot2 = f_Plot_RSI , color = fa_RSI_Shw ? fr_Ptrn_Dn : na , title = "Background lower", editable = true )
// < --------------------------------------------------------------------------------------------------------- >
fill(hline1 = h_Line_Up , hline2 = h_Line_Dn , color = fr_Clr_Bgd  , title = "Background fill" , editable = true )