RSI Candle Advanced V2
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Dicargo_Beam

//@version=5
indicator('RSI Candle Advanced V2')
src = close
description = input.bool(false, "description about 'RSI Advanced'", tooltip = "Period below 14 reduce the volatility of RSI , and period above 14 increase the volatility of RSI, allowing overbought and oversold zones to work properly and give you a better view of the trend.\n\nby applying the custom algorithm so that the 'RSI Advanced' with period[ 168 (=14*12) ] on a 5-minute timeframe has the same value as the 'original RSI' with period[ 14 ] on a 60-minute timeframe.\n\nAs another example, an 'RSI Advanced' with a period[ 56 (=14*4) ] in a 60-minute time frame has the same value as an 'original RSI' with a period[ 14 ] in a 240-minute time frame.")

// fixed_security(_sym, _res, _src) => 
//     request.security(_sym, _res, _src[barstate.isrealtime ? 1 : 0])[barstate.isrealtime ? 0 : 1]

// another_timeframe_rsilen = input.int(14,"another_timeframe RSI Length")
// another_timeframe = input.timeframe("60", "another_timeframe")
// another_timeframe_rsi = fixed_security(syminfo.tickerid, another_timeframe, ta.rsi(close,another_timeframe_rsilen))
// plot(another_timeframe_rsi[1] != another_timeframe_rsi? another_timeframe_rsi:na)


len = input(14, 'RSI Length')
ad = input(true, 'RSI Advanced ?? (Recommend)')
cdob = input.string('Candle', title='Candle or Bar or Line?', options=['Candle', 'Bar', 'Line'])
uc = input(color.green, 'Up Color')
dc = input(color.red, 'Down Color')



u = math.max(src - src[1], 0)
d = math.max(src[1] - src, 0)

ru = ta.rma(u, len)
rd = ta.rma(d, len)

a = 1 / len

ruh = a * math.max(high - close[1], 0) + (1 - a) * ta.rma(u, len)[1]
rdh = a * math.max(close[1] - high, 0) + (1 - a) * ta.rma(d, len)[1]

rul = a * math.max(low - close[1], 0) + (1 - a) * ta.rma(u, len)[1]
rdl = a * math.max(close[1] - low, 0) + (1 - a) * ta.rma(d, len)[1]

ruo = a * math.max(open - close[1], 0) + (1 - a) * ta.rma(u, len)[1]
rdo = a * math.max(close[1] - open, 0) + (1 - a) * ta.rma(d, len)[1]



function(rsi, len) =>
    f = -math.pow(math.abs(math.abs(rsi - 50) - 50), 1 + math.pow(len / 14, 0.618) - 1) / math.pow(50, math.pow(len / 14, 0.618) - 1) + 50
    rsiadvanced = if rsi > 50
        f + 50
    else
        -f + 50
    rsiadvanced




rsiha = 100 - 100 / (1 + ruh / rdh)
rsila = 100 - 100 / (1 + rul / rdl)
rsia = 100 - 100 / (1 + ru / rd)
rsioa = 100 - 100 / (1 + ruo / rdo)

rsih = if ad
    function(rsiha, len)
else
    rsiha

rsil = if ad
    function(rsila, len)
else
    rsila
rsi = if ad
    function(rsia, len)
else
    rsia
rsio = if ad
    function(rsioa, len)
else
    rsioa

col = open<close ? uc : dc
colc = if cdob == 'Candle'
    col
colb = if cdob == 'Bar'
    col
cold = if cdob == 'Line'
    col

plotcandle(rsio, rsih, rsil, rsi, color=colc, wickcolor=colc, bordercolor=colc)
plotbar(rsio, rsih, rsil, rsi, color=colb)
plot(rsi, color=cold)


hline(50, 'Balance', linestyle = hline.style_dotted)
h1 = hline(70, 'Overbought', linestyle = hline.style_dotted)
h2 = hline(30, 'Oversold', linestyle = hline.style_dotted)
fill(h1, h2, color=color.new(#643779, 87))



bblen = input.int(20,"RSI BB Length")
bbplot = input.bool(false,"RSI BB display?")
[bbm, bbu, bbl] = ta.bb(rsi, bblen, 2)

plot(bbplot?bbm:na,'Middle line', color=color.blue)
plot(bbplot?bbu:na,"Upper BB", color=color.rgb(0, 162, 255))
plot(bbplot?bbl:na,'Lower BB', color=color.rgb(0, 162, 255))


emalen1 = input.int(20,"EMA 1 Length")
emalen2 = input.int(50,"EMA 2 Length")
emaplot = input.bool(false,"RSI EMA display?")
ema1 = ta.ema(rsi, emalen1)
ema2 = ta.ema(rsi, emalen2)

plot(emaplot?ema1:na,"EMA 1", color=color.rgb(255, 230, 0))
plot(emaplot?ema2:na,"EMA 2", color=color.rgb(68, 196, 255))

