ATR Table 2.0
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © gtabachi

//@version=5
indicator("ATR Table 3.0", overlay=true)

day2_close = request.security(syminfo.tickerid, "D", close[2])
day1_max = request.security(syminfo.tickerid, "D", high[1])
day1_min = request.security(syminfo.tickerid, "D", low[1])
day1_atr = if (day1_min > day2_close)
    day1_max - day2_close
else if (day1_max < day2_close)
    day2_close - day1_min
else 
    day1_max - day1_min

day3_close = request.security(syminfo.tickerid, "D", close[3])
day2_max = request.security(syminfo.tickerid, "D", high[2])
day2_min = request.security(syminfo.tickerid, "D", low[2])
day2_atr = if (day2_min > day3_close)
    day2_max - day3_close
else if (day2_max < day3_close)
    day3_close - day2_min
else 
    day2_max - day2_min

day4_close = request.security(syminfo.tickerid, "D", close[4])
day3_max = request.security(syminfo.tickerid, "D", high[3])
day3_min = request.security(syminfo.tickerid, "D", low[3])
day3_atr = if (day3_min > day4_close)
    day3_max - day4_close
else if (day3_max < day4_close)
    day4_close - day3_min
else 
    day3_max - day3_min

day5_close = request.security(syminfo.tickerid, "D", close[5])
day4_max = request.security(syminfo.tickerid, "D", high[4])
day4_min = request.security(syminfo.tickerid, "D", low[4])
day4_atr = if (day4_min > day5_close)
    day4_max - day5_close
else if (day4_max < day5_close)
    day5_close - day4_min
else 
    day4_max - day4_min


day6_close = request.security(syminfo.tickerid, "D", close[6])
day5_max = request.security(syminfo.tickerid, "D", high[5])
day5_min = request.security(syminfo.tickerid, "D", low[5])
day5_atr = if (day5_min > day6_close)
    day5_max - day6_close
else if (day5_max < day6_close)
    day6_close - day5_min
else 
    day5_max - day5_min


day7_close = request.security(syminfo.tickerid, "D", close[7])
day6_max = request.security(syminfo.tickerid, "D", high[6])
day6_min = request.security(syminfo.tickerid, "D", low[6])
day6_atr = if (day6_min > day7_close)
    day6_max - day7_close
else if (day6_max < day7_close)
    day7_close - day6_min
else 
    day6_max - day6_min



day8_close = request.security(syminfo.tickerid, "D", close[8])
day7_max = request.security(syminfo.tickerid, "D", high[7])
day7_min = request.security(syminfo.tickerid, "D", low[7])
day7_atr = if (day7_min > day8_close)
    day7_max - day8_close
else if (day7_max < day8_close)
    day8_close - day7_min
else 
    day7_max - day7_min

day9_close = request.security(syminfo.tickerid, "D", close[9])
day8_max = request.security(syminfo.tickerid, "D", high[8])
day8_min = request.security(syminfo.tickerid, "D", low[8])
day8_atr = if (day8_min > day9_close)
    day8_max - day9_close
else if (day8_max < day9_close)
    day9_close - day8_min
else 
    day8_max - day8_min

day10_close = request.security(syminfo.tickerid, "D", close[10])
day9_max = request.security(syminfo.tickerid, "D", high[9])
day9_min = request.security(syminfo.tickerid, "D", low[9])
day9_atr = if (day9_min > day10_close)
    day9_max - day10_close
else if (day9_max < day10_close)
    day10_close - day9_min
else 
    day9_max - day9_min


day11_close = request.security(syminfo.tickerid, "D", close[11])
day10_max = request.security(syminfo.tickerid, "D", high[10])
day10_min = request.security(syminfo.tickerid, "D", low[10])
day10_atr = if (day10_min > day11_close)
    day10_max - day11_close
else if (day10_max < day11_close)
    day11_close - day10_min
else 
    day10_max - day10_min


avg_atr = (day1_atr+day2_atr+day3_atr+day4_atr+day5_atr+day6_atr+day7_atr+day8_atr+day9_atr+day10_atr)/10

day1_close = request.security(syminfo.tickerid, "D", close[1])
day_max = request.security(syminfo.tickerid, "D", high)
day_min = request.security(syminfo.tickerid, "D", low)
day_atr = if (day_min > day1_close)
    day_max - day1_close
else if (day_max < day1_close)
    day1_close - day_min
else 
    day_max - day_min

perc_ma_atr = day_atr/avg_atr

max_zrange = avg_atr*0.12

var ADRTable = table.new(position = position.top_right, columns = 2, rows = 4, frame_color = color.black , frame_width = 2, border_color=color.silver, border_width = 1)


bgcolor_c=              input( defval=color.gray,   title="Head col color",group="Header columns",inline="Column color")
bgtrans_c=              input.float( defval=0,title="transparency",group="Header columns",inline="Column color",minval=0,maxval=100,step=10)
font_color_c=           input( defval=color.white,  title="Font color",group="Header columns",inline="Column font")
font_trans_c=           input.float( defval=0,title="transparency",group="Header columns",inline="Column font",minval=0,maxval=100,step=10)

bgcolor_r=              input( defval=color.white,  title="Head row color",group="Header rows",inline="Row color")
bgtrans_r=              input.float( defval=0,title="transparency",group="Header rows",inline="Row color",minval=0,   maxval=100,     step=10)
font_color_r=           input( defval=color.black,  title="Font color",group="Header rows",inline="Row font")
font_trans_r=           input.float( defval=0,title="transparency",group="Header rows",inline="Row font",minval=0,   maxval=100,     step=10)
  

if barstate.islast
    table.cell(table_id = ADRTable, column = 0, row = 0, text = "Day TR:", text_size=size.small, bgcolor = color.new(bgcolor_c,bgtrans_c), text_color=color.new (font_color_c,font_trans_c))
    table.cell(table_id = ADRTable, column = 1, row = 0, text = str.tostring(day_atr,'#'), text_size=size.small, bgcolor = color.new(bgcolor_r,bgtrans_r), text_color=color.new (font_color_r,font_trans_r))
    table.cell(table_id = ADRTable, column = 0, row = 1, text = "10 Day ATR:", text_size=size.small, bgcolor = color.new(bgcolor_c,bgtrans_c), text_color=color.new (font_color_c,font_trans_c))
    table.cell(table_id = ADRTable, column = 1, row = 1, text = str.tostring(avg_atr,'#'), text_size=size.small, bgcolor = color.new(bgcolor_r,bgtrans_r), text_color=color.new (font_color_r,font_trans_r))
    table.cell(table_id = ADRTable, column = 0, row = 2, text = "Max ZRange:", text_size=size.small, bgcolor = color.new(bgcolor_c,bgtrans_c), text_color=color.new (font_color_c,font_trans_c))
    table.cell(table_id = ADRTable, column = 1, row = 2, text = str.tostring(max_zrange,'#'), text_size=size.small, bgcolor = color.new(bgcolor_r,bgtrans_r), text_color=color.new (font_color_r,font_trans_r))
    table.cell(table_id = ADRTable, column = 0, row = 3, text = "Range Consumed:", text_size=size.small, bgcolor = color.new(bgcolor_c,bgtrans_c), text_color=color.new (font_color_c,font_trans_c))
    table.cell(table_id = ADRTable, column = 1, row = 3, text = str.tostring(perc_ma_atr,'#.##'), text_size=size.small, bgcolor = color.new(bgcolor_r,bgtrans_r), text_color=color.new (font_color_r,font_trans_r))