Indicator by Mathi
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © stockist247

//@version=5
indicator("Indicator by Mathi", "Mathi Trading", overlay=true, max_lines_count=500,  max_labels_count=500)

AUTO = "Auto"
DAILY = "Daily"
WEEKLY = "Weekly"
MONTHLY = "Monthly"
QUARTERLY = "Quarterly"
YEARLY = "Yearly"
BIYEARLY = "Biyearly"
TRIYEARLY = "Triyearly"
QUINQUENNIALLY = "Quinquennially"
DECENNIALLY = "Decennially"

TRADITIONAL = "Traditional"
FIBONACCI = "Fibonacci"
WOODIE = "Woodie"
CLASSIC = "Classic"
DEMARK = "DM"
CAMARILLA = "Camarilla"

kind = input.string(title="Type", defval="Traditional", options=[TRADITIONAL, FIBONACCI, WOODIE, CLASSIC, DEMARK, CAMARILLA])
pivot_time_frame = input.string(title="Pivots Timeframe", defval=AUTO, options=[AUTO, DAILY, WEEKLY, MONTHLY, QUARTERLY, YEARLY, BIYEARLY, TRIYEARLY, QUINQUENNIALLY, DECENNIALLY])
look_back = input.int(title="Number of Pivots Back", defval=15, minval=1, maxval=5000)
is_daily_based = input.bool(title="Use Daily-based Values", defval=true, tooltip = "When this option is unchecked, Pivot Points will use intraday data while calculating on intraday charts. If Extended Hours are displayed on the chart, they will be taken into account during the pivot level calculation. If intraday OHLC values are different from daily-based values (normal for stocks), the pivot levels will also differ.")

show_labels = input.bool(title="Show Labels", defval=true, inline = "labels")
position_labels = input.string("Left", "", options = ["Left", "Right"], inline = "labels")

var DEF_COLOR = #FB8C00
var arr_time = array.new_int()
var p = array.new_float()
p_show = input(true, "P‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏", inline = "P")
p_color = input(DEF_COLOR, "", inline = "P")

var r1 = array.new_float()
var s1 = array.new_float()
s1r1_show = input(true, "S1/R1", inline = "S1/R1")
s1r1_color = input(DEF_COLOR, "", inline = "S1/R1")

var r2 = array.new_float()
var s2 = array.new_float()
s2r2_show = input(true, "S2/R2", inline = "S2/R2")
s2r2_color = input(DEF_COLOR, "", inline = "S2/R2")

var r3 = array.new_float()
var s3 = array.new_float()
s3r3_show = input(true, "S3/R3", inline = "S3/R3")
s3r3_color = input(DEF_COLOR, "", inline = "S3/R3")

var r4 = array.new_float()
var s4 = array.new_float()
s4r4_show = input(true, "S4/R4", inline = "S4/R4")
s4r4_color = input(DEF_COLOR, "", inline = "S4/R4")

var r5 = array.new_float()
var s5 = array.new_float()
s5r5_show = input(true, "S5/R5", inline = "S5/R5")
s5r5_color = input(DEF_COLOR, "", inline = "S5/R5")

pivotX_open = float(na)
pivotX_open := nz(pivotX_open[1],open)
pivotX_high = float(na)
pivotX_high := nz(pivotX_high[1],high)
pivotX_low = float(na)
pivotX_low := nz(pivotX_low[1],low)
pivotX_prev_open = float(na)
pivotX_prev_open := nz(pivotX_prev_open[1])
pivotX_prev_high = float(na)
pivotX_prev_high := nz(pivotX_prev_high[1])
pivotX_prev_low = float(na)
pivotX_prev_low := nz(pivotX_prev_low[1])
pivotX_prev_close = float(na)
pivotX_prev_close := nz(pivotX_prev_close[1])

get_pivot_resolution() =>
    resolution = "M"
    if pivot_time_frame == AUTO
        if timeframe.isintraday
            resolution := timeframe.multiplier <= 15 ? "D" : "W"
        else if timeframe.isweekly or timeframe.ismonthly
            resolution := "12M"
    else if pivot_time_frame == DAILY
        resolution := "D"
    else if pivot_time_frame == WEEKLY
        resolution := "W"
    else if pivot_time_frame == MONTHLY
        resolution := "M"
    else if pivot_time_frame == QUARTERLY
        resolution := "3M"
    else if pivot_time_frame == YEARLY or pivot_time_frame == BIYEARLY or pivot_time_frame == TRIYEARLY or pivot_time_frame == QUINQUENNIALLY or pivot_time_frame == DECENNIALLY
        resolution := "12M"
    resolution

var lines = array.new_line()
var labels = array.new_label()

draw_line(i, pivot, col) =>
    if array.size(arr_time) > 1
        array.push(lines, line.new(array.get(arr_time, i), array.get(pivot, i), array.get(arr_time, i + 1), array.get(pivot, i), color=col, xloc=xloc.bar_time))

draw_label(i, y, txt, txt_color) =>
    if show_labels
        offset = '‏  ‏  ‏  ‏  ‏'
        labels_align_str_left= position_labels == "Left" ? txt + offset : offset + txt 
        x = position_labels == "Left" ? array.get(arr_time, i) : array.get(arr_time, i + 1)
        array.push(labels, label.new(x = x, y=y, text=labels_align_str_left, textcolor=txt_color, style=label.style_label_center, color=#00000000, xloc=xloc.bar_time))
        
traditional() =>
    pivotX_Median = (pivotX_prev_high + pivotX_prev_low + pivotX_prev_close) / 3
    array.push(p, pivotX_Median)
    array.push(r1, pivotX_Median * 2 - pivotX_prev_low)
    array.push(s1, pivotX_Median * 2 - pivotX_prev_high)
    array.push(r2, pivotX_Median + 1 * (pivotX_prev_high - pivotX_prev_low))
    array.push(s2, pivotX_Median - 1 * (pivotX_prev_high - pivotX_prev_low))
    array.push(r3, pivotX_Median * 2 + (pivotX_prev_high - 2 * pivotX_prev_low))
    array.push(s3, pivotX_Median * 2 - (2 * pivotX_prev_high - pivotX_prev_low))
    array.push(r4, pivotX_Median * 3 + (pivotX_prev_high - 3 * pivotX_prev_low))
    array.push(s4, pivotX_Median * 3 - (3 * pivotX_prev_high - pivotX_prev_low))
    array.push(r5, pivotX_Median * 4 + (pivotX_prev_high - 4 * pivotX_prev_low))
    array.push(s5, pivotX_Median * 4 - (4 * pivotX_prev_high - pivotX_prev_low))

fibonacci() =>
    pivotX_Median = (pivotX_prev_high + pivotX_prev_low + pivotX_prev_close) / 3
    pivot_range = pivotX_prev_high - pivotX_prev_low
    array.push(p, pivotX_Median)
    array.push(r1, pivotX_Median + 0.382 * pivot_range)
    array.push(s1, pivotX_Median - 0.382 * pivot_range)
    array.push(r2, pivotX_Median + 0.618 * pivot_range)
    array.push(s2, pivotX_Median - 0.618 * pivot_range)
    array.push(r3, pivotX_Median + 1 * pivot_range)
    array.push(s3, pivotX_Median - 1 * pivot_range)

woodie() =>
    pivotX_Woodie_Median = (pivotX_prev_high + pivotX_prev_low + pivotX_open * 2)/4
    pivot_range = pivotX_prev_high - pivotX_prev_low
    array.push(p, pivotX_Woodie_Median)
    array.push(r1, pivotX_Woodie_Median * 2 - pivotX_prev_low)
    array.push(s1, pivotX_Woodie_Median * 2 - pivotX_prev_high)
    array.push(r2, pivotX_Woodie_Median + 1 * pivot_range)
    array.push(s2, pivotX_Woodie_Median - 1 * pivot_range)
    
    pivot_point_r3 = pivotX_prev_high + 2 * (pivotX_Woodie_Median - pivotX_prev_low)
    pivot_point_s3 = pivotX_prev_low - 2 * (pivotX_prev_high - pivotX_Woodie_Median)
    array.push(r3, pivot_point_r3)
    array.push(s3, pivot_point_s3)
    array.push(r4, pivot_point_r3 + pivot_range)
    array.push(s4, pivot_point_s3 - pivot_range)

classic() =>
    pivotX_Median = (pivotX_prev_high + pivotX_prev_low + pivotX_prev_close)/3
    pivot_range = pivotX_prev_high - pivotX_prev_low
    array.push(p, pivotX_Median)
    array.push(r1, pivotX_Median * 2 - pivotX_prev_low)
    array.push(s1, pivotX_Median * 2 - pivotX_prev_high)
    array.push(r2, pivotX_Median + 1 * pivot_range)
    array.push(s2, pivotX_Median - 1 * pivot_range)
    array.push(r3, pivotX_Median + 2 * pivot_range)
    array.push(s3, pivotX_Median - 2 * pivot_range)
    array.push(r4, pivotX_Median + 3 * pivot_range)
    array.push(s4, pivotX_Median - 3 * pivot_range)

demark() =>
    pivotX_Demark_X = pivotX_prev_high + pivotX_prev_low * 2 + pivotX_prev_close
    if pivotX_prev_close == pivotX_prev_open
        pivotX_Demark_X := pivotX_prev_high + pivotX_prev_low + pivotX_prev_close * 2
    if pivotX_prev_close > pivotX_prev_open
        pivotX_Demark_X := pivotX_prev_high * 2 + pivotX_prev_low + pivotX_prev_close
    array.push(p, pivotX_Demark_X / 4)
    array.push(r1, pivotX_Demark_X / 2 - pivotX_prev_low)
    array.push(s1, pivotX_Demark_X / 2 - pivotX_prev_high)
    
camarilla() =>
    pivotX_Median = (pivotX_prev_high + pivotX_prev_low + pivotX_prev_close) / 3
    pivot_range = pivotX_prev_high - pivotX_prev_low
    array.push(p, pivotX_Median)
    array.push(r1, pivotX_prev_close + pivot_range * 1.1 / 12.0)
    array.push(s1, pivotX_prev_close - pivot_range * 1.1 / 12.0)
    array.push(r2, pivotX_prev_close + pivot_range * 1.1 / 6.0)
    array.push(s2, pivotX_prev_close - pivot_range * 1.1 / 6.0)
    array.push(r3, pivotX_prev_close + pivot_range * 1.1 / 4.0)
    array.push(s3, pivotX_prev_close - pivot_range * 1.1 / 4.0)
    array.push(r4, pivotX_prev_close + pivot_range * 1.1 / 2.0)
    array.push(s4, pivotX_prev_close - pivot_range * 1.1 / 2.0)

resolution = get_pivot_resolution()

[sec_open, sec_high, sec_low, sec_close] = request.security(syminfo.tickerid, resolution, [open, high, low, close], lookahead = barmerge.lookahead_on)
sec_open_gaps_on = request.security(syminfo.tickerid, resolution, open, gaps = barmerge.gaps_on, lookahead = barmerge.lookahead_on)

var number_of_years = 0
is_change_years = false
var custom_years_resolution = pivot_time_frame == BIYEARLY or pivot_time_frame == TRIYEARLY or pivot_time_frame == QUINQUENNIALLY or pivot_time_frame == DECENNIALLY
if  custom_years_resolution and ta.change(time(resolution))
    number_of_years += 1
    if pivot_time_frame == BIYEARLY and number_of_years % 2 == 0
        is_change_years := true
        number_of_years := 0
    else if pivot_time_frame == TRIYEARLY and number_of_years % 3 == 0
        is_change_years := true
        number_of_years := 0
    else if pivot_time_frame == QUINQUENNIALLY and number_of_years % 5 == 0
        is_change_years := true
        number_of_years := 0
    else if pivot_time_frame == DECENNIALLY and number_of_years % 10 == 0
        is_change_years := true
        number_of_years := 0

var is_change = false
var uses_current_bar = timeframe.isintraday and kind == WOODIE  
var change_time = int(na)
is_time_change = (ta.change(time(resolution)) and not custom_years_resolution) or is_change_years
if is_time_change
    change_time := time


if (not uses_current_bar and is_time_change) or (uses_current_bar and not na(sec_open_gaps_on))
    if is_daily_based
        pivotX_prev_open := sec_open[1]
        pivotX_prev_high := sec_high[1]
        pivotX_prev_low := sec_low[1]
        pivotX_prev_close := sec_close[1]
        pivotX_open := sec_open
        pivotX_high := sec_high
        pivotX_low := sec_low
    else
        pivotX_prev_high := pivotX_high
        pivotX_prev_low := pivotX_low
        pivotX_prev_open := pivotX_open
        pivotX_open := open
        pivotX_high := high
        pivotX_low := low
        pivotX_prev_close := close[1]
    
    if barstate.islast and not is_change and  array.size(arr_time) > 0 
        array.set(arr_time, array.size(arr_time) - 1, change_time)
    else
        array.push(arr_time, change_time)
    
    if kind == TRADITIONAL
        traditional()
    else if kind == FIBONACCI
        fibonacci()
    else if kind == WOODIE
        woodie()
    else if kind == CLASSIC
        classic()
    else if kind == DEMARK
        demark()
    else if kind == CAMARILLA
        camarilla()
    
    if array.size(arr_time) > look_back
        if array.size(arr_time) > 0
            array.shift(arr_time)
        if array.size(p) > 0 and p_show
            array.shift(p)
        if array.size(r1) > 0 and s1r1_show
            array.shift(r1)
        if array.size(s1) > 0 and s1r1_show
            array.shift(s1)
        if array.size(r2) > 0 and s2r2_show
            array.shift(r2)
        if array.size(s2) > 0 and s2r2_show
            array.shift(s2)
        if array.size(r3) > 0 and s3r3_show
            array.shift(r3)
        if array.size(s3) > 0 and s3r3_show
            array.shift(s3)
        if array.size(r4) > 0 and s4r4_show
            array.shift(r4)
        if array.size(s4) > 0 and s4r4_show
            array.shift(s4)
        if array.size(r5) > 0 and s5r5_show
            array.shift(r5)
        if array.size(s5) > 0 and s5r5_show
            array.shift(s5)
    is_change := true
else
    if is_daily_based
        pivotX_high := math.max(pivotX_high, sec_high)
        pivotX_low := math.min(pivotX_low, sec_low)
    else
        pivotX_high := math.max(pivotX_high, high)
        pivotX_low := math.min(pivotX_low, low)

if barstate.islast and array.size(arr_time) > 0 and is_change
    is_change := false
    if array.size(arr_time) > 2 and custom_years_resolution
        last_pivot_time = array.get(arr_time, array.size(arr_time) - 1)
        prev_pivot_time = array.get(arr_time, array.size(arr_time) - 2)
        estimate_pivot_time = last_pivot_time - prev_pivot_time
        array.push(arr_time, last_pivot_time + estimate_pivot_time)
    else 
        array.push(arr_time, time_close(resolution))
        
    for i = 0 to array.size(lines) - 1
        if array.size(lines) > 0 
            line.delete(array.shift(lines))
        if array.size(lines) > 0
            label.delete(array.shift(labels))
    
    for i = 0 to array.size(arr_time) - 2
        if array.size(p) > 0 and p_show
            draw_line(i, p, p_color)
            draw_label(i, array.get(p, i), "P", p_color)
        if array.size(r1) > 0 and s1r1_show
            draw_line(i, r1, s1r1_color)
            draw_label(i, array.get(r1, i), "R1", s1r1_color)
        if array.size(s1) > 0 and s1r1_show
            draw_line(i, s1, s1r1_color)
            draw_label(i, array.get(s1, i), "S1", s1r1_color)
        if array.size(r2) > 0 and s2r2_show
            draw_line(i, r2, s2r2_color)
            draw_label(i, array.get(r2, i), "R2", s2r2_color)
        if array.size(s2) > 0 and s2r2_show
            draw_line(i, s2, s2r2_color)
            draw_label(i, array.get(s2, i), "S2", s2r2_color)
        if array.size(r3) > 0 and s3r3_show
            draw_line(i, r3, s3r3_color)
            draw_label(i, array.get(r3, i), "R3", s3r3_color)
        if array.size(s3) > 0 and s3r3_show
            draw_line(i, s3, s3r3_color)
            draw_label(i, array.get(s3, i), "S3", s3r3_color)
        if array.size(r4) > 0 and s4r4_show
            draw_line(i, r4, s4r4_color)
            draw_label(i, array.get(r4, i), "R4", s4r4_color)
        if array.size(s4) > 0 and s4r4_show
            draw_line(i, s4, s4r4_color)
            draw_label(i, array.get(s4, i), "S4", s4r4_color)
        if array.size(r5) > 0 and s5r5_show
            draw_line(i, r5, s5r5_color)
            draw_label(i, array.get(r5, i), "R5", s5r5_color)
        if array.size(s5) > 0 and s5r5_show
            draw_line(i, s5, s5r5_color)
            draw_label(i, array.get(s5, i), "S5", s5r5_color)


// EMA
//EMA - 1
len4 = input.int(20, minval=1, title="Length")
src4 = input.source(close, title="Source")
out4 = ta.ema(src4, len4)
plot(out4, color=#0000FF, title="EMA20")
//End of format

//EMA - 2
len5 = input.int(50, minval=1, title="Length")
src5 = input.source(close, title="Source")
out5 = ta.ema(src5, len5)
plot(out5, color=#006400, title="EMA50")
//End of format

//EMA - 3
len6 = input.int(100, minval=1, title="Length")
src6 = input.source(close, title="Source")
out6 = ta.ema(src6, len6)
plot(out6, color=#FFFF00, title="EMA100")
//End of format

//EMA - 4
len7 = input.int(200, minval=1, title="Length")
src7 = input.source(close, title="Source")
out7 = ta.ema(src7, len7)
plot(out7, color=#ff0000, title="EMA200")
//End of format

//SMA
//SMA - 1
len8 = input.int(20, minval=1, title="Length")
src8 = input.source(close, title="Source")
out8 = ta.sma(src8, len8)
plot(out8, color=#800080, title="SMA20")

//SMA - 2
len9 = input.int(50, minval=1, title="Length")
src9 = input.source(close, title="Source")
out9 = ta.sma(src9, len9)
plot(out9, color=#000000, title="SMA50")

//SMA - 3
len10 = input.int(200, minval=1, title="Length")
src10 = input.source(close, title="Source")
out10 = ta.sma(src10, len10)
plot(out10, color=#A52A2A, title="SMA200")

//MA20
len11 = input.int(20, minval=1, title="Length")
src11 = input(close, title="Source")
offset = input.int(title="Offset", defval=0, minval=-500, maxval=500)
out11 = ta.sma(src11, len11)
plot(out11, color=#8F00FF, title="MA", offset=offset)

//RSI
rsiob = input.int(60, minval=50, title= "RSI Overbought")
rsios = input.int(40, maxval=50, title= "RSI Oversold")
myrsi = ta.rsi(close, 14)
overboughtrsi = myrsi > rsiob
barcolor(overboughtrsi? #ffffff: na)
oversoldrsi = myrsi < rsios
barcolor(oversoldrsi? #131722: na)

//-----------------------------------------------------------------------------------------------------------------------------

//TABLES by MATHI

//SuperTrend

source = close
hilow = ((high - low)*100)
openclose = ((close - open)*100)
vol = (volume / hilow)
spreadvol = (openclose * vol)
VPT = spreadvol + ta.cum(spreadvol)
window_len = 28

v_len = 14
price_spread = ta.stdev(high-low, window_len)

v =  spreadvol + ta.cum(spreadvol)
smooth = ta.sma(v, v_len)
v_spread = ta.stdev(v - smooth, window_len)
shadow = (v - smooth) / v_spread * price_spread

out = shadow > 0 ? high + shadow : low + shadow

len=input(10)
vpt=math.round(ta.ema(out,len),4)

// INPUTS //
st_mult   = input.float(1,   title = 'SuperTrend Multiplier', minval = 0, maxval = 100, step = 0.01)
st_period = input.int(100, title = 'SuperTrend Period',     minval = 1)

// CALCULATIONS //
up_lev = vpt - (st_mult * ta.atr(st_period))
dn_lev = vpt + (st_mult * ta.atr(st_period))

up_trend   = 0.0
up_trend   := close[1] > up_trend[1]   ? math.max(up_lev, up_trend[1])   : up_lev

down_trend = 0.0
down_trend := close[1] < down_trend[1] ? math.min(dn_lev, down_trend[1]) : dn_lev

// Calculate trend var
trend = "BUY"
trend := close > down_trend[1] ? "BUY" : close < up_trend[1] ? "SELL" : "BUY"

// Calculate SuperTrend Line
st_line = trend =="BUY" ? up_trend : down_trend
//-------------------------------------------------------------------------------------

//RSI
rsiob1 = input.int(60, minval=50, title= "RSI Overbought")
rsios1 = input.int(40, maxval=50, title= "RSI Oversold")
myrsi1 = math.round(ta.rsi(close, 14),2)
overboughtrsi1 = myrsi1 > rsiob1
oversoldrsi1 = myrsi1 < rsios1
trend2 = "UP"
trend2 := myrsi1 < rsiob1 and  myrsi1 > rsios1 ? "FLAT" : myrsi1 > rsiob1 ? "UP" : "DOWN"

//EMA AND SMA
ema1 = math.round(ta.ema(close, 20),3)
trend3 = "BUY"
trend3 := ema1 < close ? "BUY" : "SELL"

ema2 = math.round(ta.ema(close, 50),3)
trend4 = "BUY"
trend4 := ema2 < close ? "BUY" : "SELL"

sma = math.round(ta.sma(close, 50),3)
trend5 = "BUY"
trend5 := sma < close ? "BUY" : "SELL"

//------------------------------------------------------------------------------------------------------

//ADX for Table
lenadx = input(14)
th = input(20)

TrueRange = math.max(math.max(high-low, math.abs(high-nz(close[1]))), math.abs(low-nz(close[1])))
DirectionalMovementPlus = high-nz(high[1]) > nz(low[1])-low ? math.max(high-nz(high[1]), 0): 0
DirectionalMovementMinus = nz(low[1])-low > high-nz(high[1]) ? math.max(nz(low[1])-low, 0): 0

SmoothedTrueRange = 0.0
SmoothedTrueRange := nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/lenadx) + TrueRange

SmoothedDirectionalMovementPlus = 0.0
SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1])/lenadx) + DirectionalMovementPlus

SmoothedDirectionalMovementMinus = 0.0
SmoothedDirectionalMovementMinus := nz(SmoothedDirectionalMovementMinus[1]) - (nz(SmoothedDirectionalMovementMinus[1])/lenadx) + DirectionalMovementMinus

DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100
DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100
DX = math.round(math.abs(DIPlus-DIMinus) / (DIPlus+DIMinus)*100, 2)
ADX = math.round(ta.sma(DX, lenadx), 2)

trend6 = "BUY"
trend6 := DIPlus > DIMinus ? (ADX >= 20 ? "Strong BUY" : "BUY") : (ADX > 20 ? "Strong SELL" : "SELL")


//------------------------------------------------------------------------------------------------------

//Table
var table t = table.new(position.middle_right, 10, 10, border_width = 1)
 
if (barstate.islast)          
    
    table.cell(t, 0, 0, "Indicator", width = 6, text_size = size.normal, bgcolor = #aaaaaa)
    table.cell(t, 1, 0, "Value", width = 6, text_size = size.normal, bgcolor = #aaaaaa)
    table.cell(t, 2, 0, "Trend", width = 6, text_size = size.normal, bgcolor = #aaaaaa)
    table.cell(t, 0, 1, "ST", width = 6, text_size = size.normal, bgcolor = #aaaaaa)
    table.cell(t, 0, 2, "RSI", width = 6, text_size = size.normal, bgcolor = #aaaaaa)
    table.cell(t, 0, 3, "EMA20", width = 6, text_size = size.normal, bgcolor = #aaaaaa)
    table.cell(t, 0, 4, "EMA50", width = 6, text_size = size.normal, bgcolor = #aaaaaa)
    table.cell(t, 0, 5, "SMA50", width = 6, text_size = size.normal, bgcolor = #aaaaaa)
    table.cell(t, 0, 6, "ADX", width = 6, text_size = size.normal, bgcolor = #aaaaaa)
    
    table.cell(t, 1, 1, str.tostring(vpt), width = 5, text_size = size.normal, bgcolor = trend == "BUY" ? color.green : color.red)
    table.cell(t, 1, 2, str.tostring(myrsi1), width = 5, text_size = size.normal, bgcolor = trend2 == "UP" ? color.green : trend2 == "FLAT" ? color.silver : color.red)
    table.cell(t, 1, 3, str.tostring(ema1), width = 5, text_size = size.normal, bgcolor = trend3 == "BUY" ? color.green : color.red)
    table.cell(t, 1, 4, str.tostring(ema2), width = 5, text_size = size.normal, bgcolor = trend4 == "BUY" ? color.green : color.red)
    table.cell(t, 1, 5, str.tostring(sma), width = 5, text_size = size.normal, bgcolor = trend5 == "BUY" ? color.green : color.red)
    table.cell(t, 1, 6, str.tostring(ADX), width = 5, text_size = size.normal, bgcolor = ADX > 20 ? color.green : color.red)
    
    table.cell(t, 2, 1, str.tostring(trend), width = 5, text_size = size.normal, bgcolor  = trend == "BUY" ? color.green : color.red)
    table.cell(t, 2, 2, str.tostring(trend2), width = 5, text_size = size.normal, bgcolor = trend2 == "UP" ? color.green : trend2 == "FLAT" ? color.silver : color.red) 
    table.cell(t, 2, 3, str.tostring(trend3), width = 5, text_size = size.normal, bgcolor = trend3 == "BUY" ? color.green : color.red)
    table.cell(t, 2, 4, str.tostring(trend4), width = 5, text_size = size.normal, bgcolor = trend4 == "BUY" ? color.green : color.red)
    table.cell(t, 2, 5, str.tostring(trend5), width = 5, text_size = size.normal, bgcolor = trend5 == "BUY" ? color.green : color.red)
    table.cell(t, 2, 6, str.tostring(trend6), width = 6, text_size = size.normal, bgcolor = trend6 == "BUY" or trend6 == "Strong BUY" ? color.green : color.red)
    
//------------------------------------------------------------------------------------------------------

//Watchlist table
i_sym1 = input.symbol("NIFTY", "Symbol")
i1 = math.round(request.security(i_sym1, 'D', close),2) 
o1 = math.round(request.security(i_sym1, 'D', open),2)
h1 = math.round(request.security(i_sym1, 'D', high),2)
c1 = math.round(request.security(i_sym1, 'D', 100*(close-open)/open), 2)

i_sym2 = input.symbol("BANKNIFTY", "Symbol")
i2 = math.round(request.security(i_sym2, 'D', close),2)
o2 = math.round(request.security(i_sym2, 'D', open),2)
h2 = math.round(request.security(i_sym2, 'D', high),2)
c2 = math.round(request.security(i_sym2, 'D', 100*(close-open)/open), 2)

i_sym3 = input.symbol("SENSEX", "Symbol")
i3 = math.round(request.security(i_sym3, 'D', close),2)
o3 = math.round(request.security(i_sym3, 'D', open),2)
h3 = math.round(request.security(i_sym3, 'D', high),2)
c3 = math.round(request.security(i_sym3, 'D', 100*(close-open)/open), 2)

i_sym4 = input.symbol("BTCUSDT", "Symbol")
i4 = math.round(request.security(i_sym4, 'D', close),2)
o4 = math.round(request.security(i_sym4, 'D', open),2)
h4 = math.round(request.security(i_sym4, 'D', high),2)
c4 = math.round(request.security(i_sym4, 'D', 100*(close-open)/open), 2)

var table t1 = table.new(position.top_right, 10, 10, border_width = 1)
 
if (barstate.islast)          
    
    table.cell(t1, 0, 0, "SYMBOL", width = 6, text_size = size.normal, bgcolor = #aaaaaa)
    table.cell(t1, 1, 0, "LTP", width = 6, text_size = size.normal, bgcolor = #aaaaaa)
    table.cell(t1, 2, 0, "OPEN", width = 6, text_size = size.normal, bgcolor = #aaaaaa)
    table.cell(t1, 3, 0, "HIGH", width = 6, text_size = size.normal, bgcolor = #aaaaaa)
    table.cell(t1, 4, 0, "%CHANGE", width = 6, text_size = size.normal, bgcolor = #aaaaaa)
    table.cell(t1, 0, 1, "NIFTY", width = 6, text_size = size.normal, bgcolor = #aaaaaa)
    table.cell(t1, 0, 2, "BN", width = 6, text_size = size.normal, bgcolor = #aaaaaa)
    table.cell(t1, 0, 3, "SENSEX", width = 6, text_size = size.normal, bgcolor = #aaaaaa)
    table.cell(t1, 0, 4, "BTC", width = 6, text_size = size.normal, bgcolor = #aaaaaa)
    
    
    table.cell(t1, 1, 1, str.tostring(i1), width = 5, text_size = size.normal, bgcolor = c1 > 0 ? color.green : color.red)
    table.cell(t1, 1, 2, str.tostring(i2), width = 5, text_size = size.normal, bgcolor = c2 > 0 ? color.green : color.red)
    table.cell(t1, 1, 3, str.tostring(i3), width = 5, text_size = size.normal, bgcolor = c3 > 0 ? color.green : color.red)
    table.cell(t1, 1, 4, str.tostring(i4), width = 5, text_size = size.normal, bgcolor = c4 > 0 ? color.green : color.red)
    
    table.cell(t1, 2, 1, str.tostring(o1), width = 5, text_size = size.normal, bgcolor = c1 > 0 ? color.green : color.red)
    table.cell(t1, 2, 2, str.tostring(o2), width = 5, text_size = size.normal, bgcolor = c2 > 0 ? color.green : color.red)
    table.cell(t1, 2, 3, str.tostring(o3), width = 5, text_size = size.normal, bgcolor = c3 > 0 ? color.green : color.red)
    table.cell(t1, 2, 4, str.tostring(o4), width = 5, text_size = size.normal, bgcolor = c4 > 0 ? color.green : color.red)
    
    table.cell(t1, 3, 1, str.tostring(h1), width = 5, text_size = size.normal, bgcolor = c1 > 0 ? color.green : color.red)
    table.cell(t1, 3, 2, str.tostring(h2), width = 5, text_size = size.normal, bgcolor = c2 > 0 ? color.green : color.red)
    table.cell(t1, 3, 3, str.tostring(h3), width = 5, text_size = size.normal, bgcolor = c3 > 0 ? color.green : color.red)
    table.cell(t1, 3, 4, str.tostring(h4), width = 5, text_size = size.normal, bgcolor = c4 > 0 ? color.green : color.red)
    
    table.cell(t1, 4, 1, str.tostring(c1), width = 5, text_size = size.normal, bgcolor = c1 > 0 ? color.green : color.red)
    table.cell(t1, 4, 2, str.tostring(c2), width = 5, text_size = size.normal, bgcolor = c2 > 0 ? color.green : color.red)
    table.cell(t1, 4, 3, str.tostring(c3), width = 5, text_size = size.normal, bgcolor = c3 > 0 ? color.green : color.red)
    table.cell(t1, 4, 4, str.tostring(c4), width = 5, text_size = size.normal, bgcolor = c4 > 0 ? color.green : color.red)
    