RSI Triple Time Frame
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Special credits: Matthew J. Slabosz / Zen & The Art of Trading
// Scipt Author: © LucasVivien

// last pbl. = v141 / V5 = v146

// @version=5
indicator('RSI Triple Time Frame', shorttitle='RSI TTF', overlay=false)

 
//////////////////////////////////////// User input //////////////////////////////////////

rsiLen = input.int  (title='Length'    , defval=14, group='RSI Parameters')
rsiOB  = input.float(title='Overbought', defval=70, group='RSI Parameters')
rsiN   = input.float(title='Neutral'   , defval=50, group='RSI Parameters')
rsiOS  = input.float(title='Oversold'  , defval=30, group='RSI Parameters')

OBcolor    = input.bool(title='Overbought'        , defval=false, group='Background Colors', tooltip='Color chart backgroud red when TF1 & TF2 & TF3 are overbought on the same tick')
OScolor    = input.bool(title='Oversold'          , defval=false, group='Background Colors', tooltip='Color chart backgroud green when TF1 & TF2 & TF3 are oversold on the same tick')
Nbullcolor = input.bool(title='Crossover neutral' , defval=false, group='Background Colors', tooltip='Color chart backgroud yellow when TF1 & TF2 & TF3 enter bull control zone on the same tick')
Nbearcolor = input.bool(title='Crossunder neutral', defval=false, group='Background Colors', tooltip='Color chart backgroud orange when TF1 & TF2 & TF3 enter bear control zone on the same tick')

on0 = input.bool     (title='Chart Timeframe', defval=true  , group='Timeframes', inline="0")
on1 = input.bool     (title='Timeframe 1'    , defval=false , group='Timeframes', inline="1")
TF1 = input.timeframe(title=''               , defval='30'  , group='Timeframes', inline="1")
on2 = input.bool     (title='Timeframe 2'    , defval=false , group='Timeframes', inline="2")
TF2 = input.timeframe(title=''               , defval='60'  , group='Timeframes', inline="2")
on3 = input.bool     (title='Timeframe 3'    , defval=false , group='Timeframes', inline="3")
TF3 = input.timeframe(title=''               , defval='120' , group='Timeframes', inline="3")

SMAplot = input(title='SMA / Lenght', defval=false, group='Signal Line', inline="4")
SMAlen  = input(title=''            , defval=10   , group='Signal Line', inline="4")



////////////////////////////////////// RSI timeframes ////////////////////////////////////

rsiact = ta.rsi(close, rsiLen)
SMArsi = ta.sma(rsiact, SMAlen)

rsiTF1 = request.security(syminfo.tickerid, TF1, rsiact[barstate.isrealtime ? 1 : 0])
rsiTF2 = request.security(syminfo.tickerid, TF2, rsiact[barstate.isrealtime ? 1 : 0])
rsiTF3 = request.security(syminfo.tickerid, TF3, rsiact[barstate.isrealtime ? 1 : 0])



///////////////////////////////// Check if OB/OS/neutral /////////////////////////////////

RSIOB1 = rsiTF1 >= rsiOB
RSIOB2 = rsiTF2 >= rsiOB
RSIOB3 = rsiTF3 >= rsiOB
RSIOS1 = rsiTF1 <= rsiOS
RSIOS2 = rsiTF2 <= rsiOS
RSIOS3 = rsiTF3 <= rsiOS

RSIN1bullcross = ta.crossover(rsiTF1, rsiN)
RSIN2bullcross = ta.crossover(rsiTF2, rsiN)
RSIN3bullcross = ta.crossover(rsiTF3, rsiN)
RSIN1bearcross = ta.crossunder(rsiTF1, rsiN)
RSIN2bearcross = ta.crossunder(rsiTF2, rsiN)
RSIN3bearcross = ta.crossunder(rsiTF3, rsiN)



///////////////////////////////// Conditions Validation  /////////////////////////////////
OBtrigger    = RSIOB1         and RSIOB2         and RSIOB3
OStrigger    = RSIOS1         and RSIOS2         and RSIOS3
Nbulltrigger = RSIN1bullcross and RSIN2bullcross and RSIN3bullcross
Nbeartrigger = RSIN1bearcross and RSIN2bearcross and RSIN3bearcross



//////////////////////////////////////// Plot RSIs ///////////////////////////////////////

plot(on0 == true ? rsiact : na, color=color.new(#ff0066     , 0), title='RSI Current Timeframe', linewidth=2)
plot(on1 == true ? rsiTF1 : na, color=color.new(#ff00ff     , 0), title='RSI Timeframe #1'     , linewidth=1)
plot(on2 == true ? rsiTF2 : na, color=color.new(#cc00cc     , 0), title='RSI Timeframe #2'     , linewidth=1)
plot(on3 == true ? rsiTF3 : na, color=color.new(#660066     , 0), title='RSI Timeframe #3'     , linewidth=1)
plot(SMAplot     ? SMArsi : na, color=color.new(color.yellow, 0), title="RSI SMA plot"         , linewidth=1)

rob = hline(rsiOB, color=#666666, title='RSI Overbought', linestyle=hline.style_dotted)
ros = hline(rsiOS, color=#666666, title='RSI Oversold'  , linestyle=hline.style_dotted)
mid = hline(rsiN , color=#666666, title='RSI Neutral'   , linestyle=hline.style_dashed)
top = hline(100  , color=#666666, title='RSI Min'       , linestyle=hline.style_solid)
bot = hline(0    , color=#666666, title='RSI Max'       , linestyle=hline.style_solid)

fill(top, bot, color=OBtrigger    and OBcolor    ? color.new(color.red, 50) : OStrigger    and OScolor    ? color.new(color.green, 50) : na)
fill(top, bot, color=Nbulltrigger and Nbullcolor ? color.new(#ffff1a  , 50) : Nbeartrigger and Nbearcolor ? color.new(#ff8000    , 50) : na)



//////////////////////////////////////// Alerts ///////////////////////////////////////

alertcondition(OBtrigger or OStrigger, title="Triple TF RSI Alert", message="{{ticker}}: RSI values at extreme levels on all 3 timeframes")

