Ticker vs Index
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Lantzwat

//@version=5
indicator("Ticker vs Index", "TvI")

RefTickerId = input.symbol("SPX", title="Reference index", tooltip = "e.g. SPX/NDX/RUT/DWCPF or combinations of it")
sma = input.int(20,"SMA")

ref_close = request.security(RefTickerId, timeframe.period, close)

pclose      = ((close * 100) / close[1]) - 1
prefclose   = ((ref_close * 100) / ref_close[1]) - 1

diff  = ta.sma(pclose - prefclose,sma)
div   = 100 / (ta.highest(diff,250)-ta.lowest(diff,250))
diff := diff * div

p0          = plot(0, color=color.new(color.white,90))
diffcolor   = diff >= 0 ? color.blue : color.red
pDiff       = plot(diff, color = diffcolor)
fill(p0,pDiff,color = color.new(diffcolor,60))

