NSE Bank Nifty - Arms Index (TRIN)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Sharad_Gaikwad

//@version=5
indicator("NSE Bank Nifty - Arms Index (TRIN)", overlay=false)

tab = table.new(position=position.top_center, columns=7, rows=20,frame_color = color.yellow, frame_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)

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

method = input.string(title = 'Calculation method', defval='SAME-DAY', options = ['SAME-DAY', 'PREVIOUS-DAY'], tooltip = 'Used to derive advancing and declining stocks. SAME-DAY = uses days close - days open.  PREVIOUS-DAY = uses days close - previous days close')

plot_avg_trin = input.bool(title = 'Plot Average TRIN', defval = true)
avg_len = input.int(title = 'Length for Average TRIN', defval =  10)
plot_trin = input.bool(title = 'Plot TRIN', defval = false)
plot_AD_price_line = input.bool(title = 'Plot AD Price line', defval = false)
plot_AD_volume_line = input.bool(title = 'Plot AD Volume line', defval = false)
overbought_band = input(title = 'Overbought above', defval = 3.00)
oversold_band = input(title = 'Oversold below', defval = 0.5)


// varip float s1_wtg = 27.8 / 100
// varip float s2_wtg = 22.62 / 100
// varip float s3_wtg = 11.61 / 100
// varip float s4_wtg = 11.52 / 100
// varip float s5_wtg = 11.45 / 100
// varip float s6_wtg = 5.91 / 100
// varip float s7_wtg = 2.33 / 100
// varip float s8_wtg = 1.75 / 100
// varip float s9_wtg = 1.7 / 100
// varip float s10_wtg = 1.54 / 100
// varip float s11_wtg = 1.13 / 100
// varip float s12_wtg = 0.65 / 100

no_of_stocks = 12
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')

// var adv_stocks = int(0)
// var dec_stocks = int(0)
// var adv_volume = float(0)
// var dec_volume= float(0)

adv_stocks = int(0)
dec_stocks = int(0)
adv_volume = float(0)
dec_volume= float(0)


[s1_open, s1_close, s1_volume] = request.security(s1, timeframe.period, [open, close, volume])
[s2_open, s2_close, s2_volume] = request.security(s2, timeframe.period, [open, close, volume])
[s3_open, s3_close, s3_volume] = request.security(s3, timeframe.period, [open, close, volume])
[s4_open, s4_close, s4_volume] = request.security(s4, timeframe.period, [open, close, volume])
[s5_open, s5_close, s5_volume] = request.security(s5, timeframe.period, [open, close, volume])
[s6_open, s6_close, s6_volume] = request.security(s6, timeframe.period, [open, close, volume])
[s7_open, s7_close, s7_volume] = request.security(s7, timeframe.period, [open, close, volume])
[s8_open, s8_close, s8_volume] = request.security(s8, timeframe.period, [open, close, volume])
[s9_open, s9_close, s9_volume] = request.security(s9, timeframe.period, [open, close, volume])
[s10_open, s10_close, s10_volume] = request.security(s10, timeframe.period, [open, close, volume])
[s11_open, s11_close, s11_volume] = request.security(s11, timeframe.period, [open, close, volume])
[s12_open, s12_close, s12_volume] = request.security(s12, timeframe.period, [open, close, volume])


s1_adv = method == 'SAME-DAY' ? s1_close >= s1_open : s1_close >= s1_close[1]
s2_adv = method == 'SAME-DAY' ? s2_close >= s2_open : s2_close >= s2_close[1]
s3_adv = method == 'SAME-DAY' ? s3_close >= s3_open : s3_close >= s3_close[1]
s4_adv = method == 'SAME-DAY' ? s4_close >= s4_open : s4_close >= s4_close[1]
s5_adv = method == 'SAME-DAY' ? s5_close >= s5_open : s5_close >= s5_close[1]
s6_adv = method == 'SAME-DAY' ? s6_close >= s6_open : s6_close >= s6_close[1]
s7_adv = method == 'SAME-DAY' ? s7_close >= s7_open : s7_close >= s7_close[1]
s8_adv = method == 'SAME-DAY' ? s8_close >= s8_open : s8_close >= s8_close[1]
s9_adv = method == 'SAME-DAY' ? s9_close >= s9_open : s9_close >= s9_close[1]
s10_adv = method == 'SAME-DAY' ? s10_close >= s10_open : s10_close >= s10_close[1]
s11_adv = method == 'SAME-DAY' ? s11_close >= s11_open : s11_close >= s11_close[1]
s12_adv = method == 'SAME-DAY' ? s12_close >= s12_open : s12_close >= s12_close[1]

s1_dec = method == 'SAME-DAY' ? s1_close < s1_open : s1_close < s1_close[1]
s2_dec = method == 'SAME-DAY' ? s2_close < s2_open : s2_close < s2_close[1]
s3_dec = method == 'SAME-DAY' ? s3_close < s3_open : s3_close < s3_close[1]
s4_dec = method == 'SAME-DAY' ? s4_close < s4_open : s4_close < s4_close[1]
s5_dec = method == 'SAME-DAY' ? s5_close < s5_open : s5_close < s5_close[1]
s6_dec = method == 'SAME-DAY' ? s6_close < s6_open : s6_close < s6_close[1]
s7_dec = method == 'SAME-DAY' ? s7_close < s7_open : s7_close < s7_close[1]
s8_dec = method == 'SAME-DAY' ? s8_close < s8_open : s8_close < s8_close[1]
s9_dec = method == 'SAME-DAY' ? s9_close < s9_open : s9_close < s9_close[1]
s10_dec = method == 'SAME-DAY' ? s10_close < s10_open : s10_close < s10_close[1]
s11_dec = method == 'SAME-DAY' ? s11_close < s11_open : s11_close < s11_close[1]
s12_dec = method == 'SAME-DAY' ? s12_close < s12_open : s12_close < s12_close[1]

//adv_stocks := 0 
adv_stocks := s1_adv ? adv_stocks + 1 : adv_stocks 
adv_stocks := s2_adv ? adv_stocks + 1 : adv_stocks 
adv_stocks := s3_adv ? adv_stocks + 1 : adv_stocks 
adv_stocks := s4_adv ? adv_stocks + 1 : adv_stocks 
adv_stocks := s5_adv ? adv_stocks + 1 : adv_stocks 
adv_stocks := s6_adv ? adv_stocks + 1 : adv_stocks 
adv_stocks := s7_adv ? adv_stocks + 1 : adv_stocks 
adv_stocks := s8_adv ? adv_stocks + 1 : adv_stocks 
adv_stocks := s9_adv ? adv_stocks + 1 : adv_stocks 
adv_stocks := s10_adv ? adv_stocks + 1 : adv_stocks 
adv_stocks := s11_adv ? adv_stocks + 1 : adv_stocks 
adv_stocks := s12_adv ? adv_stocks + 1 : adv_stocks 

//dec_stocks := 0
dec_stocks := s1_dec ? dec_stocks + 1 : dec_stocks 
dec_stocks := s2_dec ? dec_stocks + 1 : dec_stocks 
dec_stocks := s3_dec ? dec_stocks + 1 : dec_stocks 
dec_stocks := s4_dec ? dec_stocks + 1 : dec_stocks 
dec_stocks := s5_dec ? dec_stocks + 1 : dec_stocks 
dec_stocks := s6_dec ? dec_stocks + 1 : dec_stocks 
dec_stocks := s7_dec ? dec_stocks + 1 : dec_stocks 
dec_stocks := s8_dec ? dec_stocks + 1 : dec_stocks 
dec_stocks := s9_dec ? dec_stocks + 1 : dec_stocks 
dec_stocks := s10_dec ? dec_stocks + 1 : dec_stocks 
dec_stocks := s11_dec ? dec_stocks + 1 : dec_stocks 
dec_stocks := s12_dec ? dec_stocks + 1 : dec_stocks 

//adv_volume := 0
adv_volume := s1_adv  ? adv_volume + s1_volume : adv_volume 
adv_volume := s2_adv ? adv_volume + s2_volume : adv_volume 
adv_volume := s3_adv ? adv_volume + s3_volume : adv_volume 
adv_volume := s4_adv ? adv_volume + s4_volume : adv_volume 
adv_volume := s5_adv ? adv_volume + s5_volume : adv_volume 
adv_volume := s6_adv ? adv_volume + s6_volume : adv_volume 
adv_volume := s7_adv ? adv_volume + s7_volume : adv_volume 
adv_volume := s8_adv ? adv_volume + s8_volume : adv_volume 
adv_volume := s9_adv ? adv_volume + s9_volume : adv_volume 
adv_volume := s10_adv ? adv_volume + s10_volume : adv_volume 
adv_volume := s11_adv ? adv_volume + s11_volume : adv_volume 
adv_volume := s12_adv ? adv_volume + s12_volume : adv_volume 

//dec_volume := 0
dec_volume := s1_dec ? dec_volume + s1_volume : dec_volume 
dec_volume := s2_dec ? dec_volume + s2_volume : dec_volume 
dec_volume := s3_dec ? dec_volume + s3_volume : dec_volume 
dec_volume := s4_dec ? dec_volume + s4_volume : dec_volume 
dec_volume := s5_dec ? dec_volume + s4_volume : dec_volume 
dec_volume := s6_dec ? dec_volume + s6_volume : dec_volume 
dec_volume := s7_dec ? dec_volume + s7_volume : dec_volume 
dec_volume := s8_dec ? dec_volume + s8_volume : dec_volume 
dec_volume := s9_dec ? dec_volume + s9_volume : dec_volume 
dec_volume := s10_dec ? dec_volume + s10_volume : dec_volume 
dec_volume := s11_dec ? dec_volume + s11_volume : dec_volume
dec_volume := s12_dec ? dec_volume + s12_volume : dec_volume

trin = (nz(adv_stocks, 1) / nz(dec_stocks, 1)) / (nz(adv_volume, 1) / nz(dec_volume, 1))
avg_trin  = ta.sma(trin, avg_len)

plot(plot_trin ? trin : na, color = color.blue)
plot(plot_avg_trin ? avg_trin : na, color = color.purple)

plot(plot_AD_price_line ? adv_stocks : na, color = color.green)
plot(plot_AD_price_line ? dec_stocks : na, color = color.red)


plot(plot_AD_volume_line ? adv_volume : na, color = color.green)
plot(plot_AD_volume_line ? dec_volume : na, color = color.red)

hline(price = 1, title = 'Line at 1', color = color.gray, linestyle=hline.style_dashed)
hline(price = overbought_band, title = 'Top Line', color = color.gray, linestyle=hline.style_dashed)
hline(price = oversold_band, title = 'Low Line', color = color.gray, linestyle=hline.style_dashed)


