RS for VPA
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © karthikmarar

//@version=5
//Relative strength Indicator for use with VPA 5.0

indicator("RS for VPA")

i_sym = input.symbol(title='Index for Rel.strength', defval='NSE:cnx500')
len   = input.int(22, "Period", minval=10, maxval=66, step=1)
len1   = input.int(22, "Short MA for Index", minval=10, maxval=32, step=1)
len2   = input.int(60, "Mid MA for Index", minval=32, maxval=80, step=1)
len3   = input.int(130, "Long MA for Index", minval=80, maxval=150, step=1)
a = close
bs = request.security(i_sym, 'D', close)
zeroline = 0
crs = ((a/a[len])/(bs/bs[len]) - 1)
zero_color = (crs >= 0) ? color.green : color.orange
plot_color = (crs >= 0) ? color.green : color.red 

fill_color = (crs >= 0) ?  color.new(color.green, 80) : color.new(color.red, 80)
hzplot = plot(zeroline, title="Horiz. Axis", color=zero_color, linewidth=1)
crs_plot = plot(crs, title="CRS", color=plot_color, linewidth=2)
fill(plot1=crs_plot, plot2=hzplot, color=fill_color)

x1 = ta.ema(bs,5)
x2 = ta.ema(bs,len1)
x3 = ta.ema(bs,len2)
x4 = ta.ema(bs,len3)
xs = x1-x2 > 0 ? 1 : -1
xm = xs == 1 and x2-x3 > 0 ? 1 : -1
xl = xm == 1 and x3-x4 > 0 ? 1 : -1

bgcolor (xl > 0 ? color.new(color.yellow, 40) : (xm > 0 ? color.new(color.yellow, 60) : ( xs >0 ? color.new(color.yellow, 80) : ( xl < 0 ? color.new(color.blue, 80): (xm < 0 and xs > 0? color.new(color.blue,60):color.new(color.blue,40)   )))))