NSE Bank Nifty - Constituent Strength
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Sharad_Gaikwad
//BEGIN {
//@version=5
indicator("NSE Bank Nifty - Constituent Strength", overlay=false)
// Input parameters {
src = input.source(title = 'Data Source', defval = close)
tf = input.timeframe(title = 'Timeframe', defval = 'D')
ma_type = input.string(title = 'Moving average type', defval = 'SMA', options = ['SMA', 'EMA'])
ma_len = input.int(title = 'Moving average length', defval = 20)
show_chart = input.string(title = 'Show line chart for', defval = 'S1', options = ['S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9', 'S10', 'S11', 'S12'])
s1 = input('NSE:HDFCBANK')
s2 = input('NSE:ICICIBANK')
s3 = input('NSE:KOTAKBANK')
s4 = input('NSE:AXISBANK')
s5 = input('NSE:SBIN')
s6 = input('NSE:INDUSINDBK')
s7 = input('NSE:AUBANK')
s8 = input('NSE:BANDHANBNK')
s9 = input('NSE:FEDERALBNK')
s10 = input('NSE:IDFCFIRSTB')
s11 = input('NSE:PNB')
s12 = input('NSE:RBLBANK')
// } Input parameters

//Common  functions {
tab = table.new(position=position.bottom_left, columns=2, rows=6,frame_color = color.yellow, frame_width = 1, border_color = color.blue, border_width = 1)
msg(int row, int col, string msg_str, clr=color.blue) =>
    table.cell(table_id=tab, column=col, row=row, text=msg_str, text_color=clr)

tab1 = table.new(position=position.top_center, columns=1, rows=1,frame_color = color.yellow, frame_width = 1, border_color = color.blue, border_width = 1)
msg1(int row, int col, string msg_str, clr=color.blue) =>
    table.cell(table_id=tab1, column=col, row=row, text=msg_str, text_color=clr)

t(_val) => 
    ret_val = str.tostring(_val)

strength(ind_price, element_price, symbol_code) =>
    cmp_strength = element_price / ind_price
    ma_strength = ma_type == 'SMA' ? ta.sma(element_price / ind_price, ma_len) : ta.ema(element_price / ind_price, ma_len)
    s_color = cmp_strength > ma_strength ? color.green : cmp_strength < ma_strength ? color.red : color.blue
    show_plot = show_chart ==  symbol_code ? true : false
    [s_color, cmp_strength, ma_strength, show_plot]
// } Common functions 

//Data collection {
ind_cmp = request.security("NSE:BANKNIFTY", tf, src)

[s1_strength, s1_cmp, s1_ma, s1_plot] = request.security(s1, tf, strength(ind_cmp, src, 'S1'))
[s2_strength, s2_cmp, s2_ma, s2_plot] = request.security(s2, tf, strength(ind_cmp, src, 'S2'))
[s3_strength, s3_cmp, s3_ma, s3_plot] = request.security(s3, tf, strength(ind_cmp, src, 'S3'))
[s4_strength, s4_cmp, s4_ma, s4_plot] = request.security(s4, tf, strength(ind_cmp, src, 'S4'))
[s5_strength, s5_cmp, s5_ma, s5_plot] = request.security(s5, tf, strength(ind_cmp, src, 'S5'))
[s6_strength, s6_cmp, s6_ma, s6_plot] = request.security(s6, tf, strength(ind_cmp, src, 'S6'))
[s7_strength, s7_cmp, s7_ma, s7_plot] = request.security(s7, tf, strength(ind_cmp, src, 'S7'))
[s8_strength, s8_cmp, s8_ma, s8_plot] = request.security(s8, tf, strength(ind_cmp, src, 'S8'))
[s9_strength, s9_cmp, s9_ma, s9_plot] = request.security(s9, tf, strength(ind_cmp, src, 'S9'))
[s10_strength, s10_cmp, s10_ma, s10_plot] = request.security(s10, tf, strength(ind_cmp, src, 'S10'))
[s11_strength, s11_cmp, s11_ma, s11_plot] = request.security(s11, tf, strength(ind_cmp, src, 'S11'))
[s12_strength, s12_cmp, s12_ma, s12_plot] = request.security(s12, tf, strength(ind_cmp, src, 'S12'))
// } Data collection

//Display data {
msg(0, 0, syminfo.ticker(s1), s1_strength)
msg(0, 1, syminfo.ticker(s2), s2_strength)

msg(1, 0, syminfo.ticker(s3), s3_strength)
msg(1, 1, syminfo.ticker(s4), s4_strength)

msg(2, 0, syminfo.ticker(s5), s5_strength)
msg(2, 1, syminfo.ticker(s6), s6_strength)

msg(3, 0, syminfo.ticker(s7), s7_strength)
msg(3, 1, syminfo.ticker(s8), s8_strength)

msg(4, 0, syminfo.ticker(s9), s9_strength)
msg(4, 1, syminfo.ticker(s10), s10_strength)

msg(5, 0, syminfo.ticker(s11), s11_strength)
msg(5, 1, syminfo.ticker(s12), s12_strength)
// Display data }

// Plots {
plot_for = s1_plot ? syminfo.ticker(s1) :
     s2_plot ? syminfo.ticker(s2) :
     s3_plot ? syminfo.ticker(s3) :
     s4_plot ? syminfo.ticker(s4) :
     s5_plot ? syminfo.ticker(s5) :
     s6_plot ? syminfo.ticker(s6) :
     s7_plot ? syminfo.ticker(s7) :
     s8_plot ? syminfo.ticker(s8) :
     s9_plot ? syminfo.ticker(s9) :
     s10_plot ? syminfo.ticker(s10) :
     s11_plot ? syminfo.ticker(s11) : syminfo.ticker(s12)

price = s1_plot ? s1_cmp :
     s2_plot ? s2_cmp :
     s3_plot ? s3_cmp :
     s4_plot ? s4_cmp :
     s5_plot ? s5_cmp :
     s6_plot ? s6_cmp :
     s7_plot ? s7_cmp :
     s8_plot ? s8_cmp :
     s9_plot ? s9_cmp :
     s10_plot ? s10_cmp :
     s11_plot ? s11_cmp : s12_cmp

ma = s1_plot ? s1_ma :
     s2_plot ? s2_ma :
     s3_plot ? s3_ma :
     s4_plot ? s4_ma :
     s5_plot ? s5_ma :
     s6_plot ? s6_ma :
     s7_plot ? s7_ma :
     s8_plot ? s8_ma :
     s9_plot ? s9_ma :
     s10_plot ? s10_ma :
     s11_plot ? s11_ma : s12_ma
     
clr = s1_plot ? s1_strength :
     s2_plot ? s2_strength :
     s3_plot ? s3_strength :
     s4_plot ? s4_strength :
     s5_plot ? s5_strength :
     s6_plot ? s6_strength :
     s7_plot ? s7_strength :
     s8_plot ? s8_strength :
     s9_plot ? s9_strength :
     s10_plot ? s10_strength :
     s11_plot ? s11_strength : s12_strength
     
plot(price, title = 'Price line', color = clr)
plot(ma, title = 'MA line', color = color.blue)
msg1(0, 0, plot_for)
// } Plots
// } END
