Weighted Relative Strength Index
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Lazylady

//@version=5
indicator(title="Weighted Relative Strength Index", shorttitle="WRSI", format=format.price, precision=2, timeframe="", timeframe_gaps=false)

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(27, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
rsiMAInput = input.int(28, minval=1, title="RSI SMA Length", group="RSI 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))
///////////////

//TimeFrame Inputs
tf1 = input.timeframe(title="Time Frame 1", defval="5", group="RSI TimeFrame")
tf2 = input.timeframe(title="Time Frame 2", defval="15", group="RSI TimeFrame")
tf3 = input.timeframe(title="Time Frame 3", defval="60", group="RSI TimeFrame")
tfw1 = input(9, title="Time Frame 1 Weight", group="RSI TimeFrame")
tfw2 = input(4, title="Time Frame 2 Weight", group="RSI TimeFrame")
tfw3 = input(1, title="Time Frame 3 Weight", group="RSI TimeFrame")
total = tfw1 + tfw2  + tfw3

//pre-plot

rsi1 = request.security(syminfo.tickerid, tf1, rsi)
rsi2 = request.security(syminfo.tickerid, tf2, rsi)
rsi3 = request.security(syminfo.tickerid, tf3, rsi)

rsif = rsi1*(tfw1/total) + rsi2*(tfw2/total) + rsi3*(tfw3/total)

rsifma = ta.sma(rsif,rsiMAInput)




///////////////
plot(rsif, "RSI", color=#7E57C2)
rsiUpperBand = hline(60, "RSI Upper Band", color=#787B86)
hline(50, "RSI Middle Band", color=color.new(#787B86, 50))
rsiLowerBand = hline(40, "RSI Lower Band", color=#787B86)
fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")
plot(rsifma, title = "RSI MA",color=color.red)