Average Daily Range (ADR) (Multi Timeframe, Multi Period)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Arun_K_Bhaskar

//@version=5
indicator(title="Average Day Range (ADR) (Multi Timeframe, Multi Period)", shorttitle="ADR", overlay=true)

//__________________________ Menu General Inputs Start
g_adr_s = 'General Inputs'
tt_l2 = 'default 14' + '\n' + 'input same lengths to appear the levels as a line '
int i_adr_length_2 = input.int(defval=7, minval=0, title='Length', inline='1', group=g_adr_s)
int i_adr_length_1 = input.int(defval=14, minval=0, title='', tooltip=tt_l2, inline='1', group=g_adr_s)
//__________________________ Menu General Inputs End

//__________________________ Menu ADR 1 Start
g_adr_1 = 'Average Day Range 1'
string i_adr_1_tf = input.timeframe(defval='D', title='Timeframe', inline='1', group=g_adr_1)
int i_adr_1_showlast = input.int(defval=1, minval=0, title='Show Last', inline='1', group=g_adr_1) + 1

bool i_adr_1_multi_show_1 = input.bool(defval=true, title='M1  ', inline='2', group=g_adr_1)
float i_adr_1_multi_1 = input.float(defval=0.33, title='', minval=0, inline='2', group=g_adr_1)
bool i_adr_1_multi_show_2 = input.bool(defval=false, title='M2  ', inline='2', group=g_adr_1)
float i_adr_1_multi_2 = input.float(defval=2.64, title='', minval=0, inline='2', group=g_adr_1) // 1.75, 2 for Index
bool i_adr_1_multi_show_3 = input.bool(defval=false, title='M3  ', inline='3', group=g_adr_1)
float i_adr_1_multi_3 = input.float(defval=4.62, title='', minval=0, inline='3', group=g_adr_1)
bool i_adr_1_multi_show_4 = input.bool(defval=false, title='M4  ', inline='3', group=g_adr_1)
float i_adr_1_multi_4 = input.float(defval=7.59, title='', minval=0, inline='3', group=g_adr_1)

color i_adr_1_r_bx_color = input.color(defval=#F7525F40, title='Box', inline='4', group=g_adr_1)
color i_adr_1_s_bx_color = input.color(defval=#22AB9440, title='', inline='4', group=g_adr_1)
string i_adr_1_bx_style = input.string(defval=line.style_solid, title='', options=[line.style_solid, line.style_dashed, line.style_dotted], inline='4', group=g_adr_1)

color i_adr_1_multi_color = input.color(defval=#2A2E39, title='Multiplier  ', inline='5', group=g_adr_1)
string i_adr_1_ln_style = input.string(defval=line.style_solid, title='', options=[line.style_solid, line.style_dashed, line.style_dotted], inline='5', group=g_adr_1)
int i_adr_1_ln_width = input.int(defval=1, title='', inline='5', group=g_adr_1)

color i_adr_1_r_txt_color = input.color(defval=#F7525F, title='Text', inline='6', group=g_adr_1)
color i_adr_1_s_txt_color = input.color(defval=#22AB94, title='', inline='6', group=g_adr_1)
string i_adr_1_bx_size = input.string(defval=size.auto, title='', options=[size.auto, size.tiny, size.small, size.normal, size.large, size.huge], inline='6', group=g_adr_1)
string i_adr_1_bx_text_align = input.string(text.align_left,'', options = [text.align_right, text.align_center, text.align_left], inline='6', group=g_adr_1)
//__________________________ Menu ADR 1 End

//__________________________ Menu ADR 2 Start
g_adr_2 = 'Average Day Range 2'
string i_adr_2_tf = input.timeframe(defval='W', title='Timeframe', inline='1', group=g_adr_2)
int i_adr_2_showlast = input.int(defval=0, minval=0, title='Show Last', inline='1', group=g_adr_2) + 1

bool i_adr_2_multi_show_1 = input.bool(defval=true, title='M1  ', inline='2', group=g_adr_2)
float i_adr_2_multi_1 = input.float(defval=0.33, title='', minval=0, inline='2', group=g_adr_2)

color i_adr_2_r_bx_color = input.color(defval=#F7525F40, title='Box', inline='3', group=g_adr_2)
color i_adr_2_s_bx_color = input.color(defval=#22AB9440, title='', inline='3', group=g_adr_2)
string i_adr_2_bx_style = input.string(defval=line.style_dashed, title='', options=[line.style_solid, line.style_dashed, line.style_dotted], inline='3', group=g_adr_2)

color i_adr_2_multi_color = input.color(defval=#2A2E39, title='Multiplier  ', inline='4', group=g_adr_2)
string i_adr_2_ln_style = input.string(defval=line.style_dashed, title='', options=[line.style_solid, line.style_dashed, line.style_dotted], inline='4', group=g_adr_2)
int i_adr_2_ln_width = input.int(defval=1, title='', inline='4', group=g_adr_2)

color i_adr_2_r_txt_color = input.color(defval=#F7525F, title='Text', inline='5', group=g_adr_2)
color i_adr_2_s_txt_color = input.color(defval=#22AB94, title='', inline='5', group=g_adr_2)
string i_adr_2_bx_size = input.string(defval=size.auto, title='', options=[size.auto, size.tiny, size.small, size.normal, size.large, size.huge], inline='5', group=g_adr_2)
string i_adr_2_bx_text_align = input.string(text.align_left,'', options = [text.align_right, text.align_center, text.align_left], inline='5', group=g_adr_2)
//__________________________ Menu ADR 2 End

//__________________________ Menu ADR 3 Start
g_adr_3 = 'Average Day Range 3'
string i_adr_3_tf = input.timeframe(defval='M', title='Timeframe', inline='1', group=g_adr_3)
int i_adr_3_showlast = input.int(defval=0, minval=0, title='Show Last', inline='1', group=g_adr_3) + 1

bool i_adr_3_multi_show_1 = input.bool(defval=true, title='M1  ', inline='2', group=g_adr_3)
float i_adr_3_multi_1 = input.float(defval=0.33, title='', minval=0, inline='2', group=g_adr_3)

color i_adr_3_r_bx_color = input.color(defval=#F7525F40, title='Box', inline='3', group=g_adr_3)
color i_adr_3_s_bx_color = input.color(defval=#22AB9440, title='', inline='3', group=g_adr_3)
string i_adr_3_bx_style = input.string(defval=line.style_dotted, title='', options=[line.style_solid, line.style_dashed, line.style_dotted], inline='3', group=g_adr_3)

color i_adr_3_multi_color = input.color(defval=#2A2E39, title='Multiplier  ', inline='4', group=g_adr_3)
string i_adr_3_ln_style = input.string(defval=line.style_dotted, title='', options=[line.style_solid, line.style_dashed, line.style_dotted], inline='4', group=g_adr_3)
int i_adr_3_ln_width = input.int(defval=1, title='', inline='4', group=g_adr_3)

color i_adr_3_r_txt_color = input.color(defval=#F7525F, title='Text', inline='5', group=g_adr_3)
color i_adr_3_s_txt_color = input.color(defval=#22AB94, title='', inline='5', group=g_adr_3)
string i_adr_3_bx_size = input.string(defval=size.auto, title='', options=[size.auto, size.tiny, size.small, size.normal, size.large, size.huge], inline='5', group=g_adr_3)
string i_adr_3_bx_text_align = input.string(text.align_left,'', options = [text.align_right, text.align_center, text.align_left], inline='5', group=g_adr_3)
//__________________________ Menu ADR 3 End

//__________________________ Menu Table Start
g_tb = 'Table'
bool i_table_show = input.bool(defval=true, title='Table', inline='tb_1', group=g_tb)
color i_tbl_text_color = input.color(defval=#B2B5BE, title='', inline='tb_1', group=g_tb)
string i_tbl_position = input.string(defval=position.top_right, title='', options=[position.top_left, position.top_center, position.top_right, position.middle_left, position.middle_center, position.middle_right, position.bottom_left, position.bottom_center, position.bottom_right], inline='tb_1', group=g_tb)
string i_tbl_text_size = input.string(defval=size.auto, title='', options=[size.auto, size.tiny, size.small, size.normal, size.large, size.huge], inline='tb_1', group=g_tb)
//__________________________ Menu Table End

//__________________________ Get Data Start
get_ohlcv(timeframe, source) => 
  request.security(symbol=syminfo.tickerid, timeframe=timeframe, expression=source, gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)

// Get Time
adr_1_time = get_ohlcv(i_adr_1_tf, time)
adr_1_time_close = get_ohlcv(i_adr_1_tf, time_close)
adr_2_time = get_ohlcv(i_adr_2_tf, time)
adr_2_time_close = get_ohlcv(i_adr_2_tf, time_close)
adr_3_time = get_ohlcv(i_adr_3_tf, time)
adr_3_time_close = get_ohlcv(i_adr_3_tf, time_close)
//__________________________ Get Data End

//__________________________ ADR Calculations Start
// ADR Function
adr(timeframe, length, multiplier) =>
    day_open = get_ohlcv(timeframe, open)
    prev_high_sma = get_ohlcv(timeframe, ta.sma(high[1], length))
    prev_low_sma = get_ohlcv(timeframe, ta.sma(low[1], length))
    // ADR Formula
    adr_value = prev_high_sma - prev_low_sma
    adr_r = day_open + (adr_value / 2) * multiplier
    adr_s = day_open - (adr_value / 2) * multiplier
    [adr_r, adr_s]

// ADR 1
[adr_1_len_1_r, adr_1_len_1_s] = adr(i_adr_1_tf, i_adr_length_1, 1)
[adr_1_len_2_r, adr_1_len_2_s] = adr(i_adr_1_tf, i_adr_length_2, 1)

[adr_1_len_1_r_m_1, adr_1_len_1_s_m_1] = adr(i_adr_1_tf, i_adr_length_1, i_adr_1_multi_1)
[adr_1_len_1_r_m_2, adr_1_len_1_s_m_2] = adr(i_adr_1_tf, i_adr_length_1, i_adr_1_multi_2)
[adr_1_len_1_r_m_3, adr_1_len_1_s_m_3] = adr(i_adr_1_tf, i_adr_length_1, i_adr_1_multi_3)
[adr_1_len_1_r_m_4, adr_1_len_1_s_m_4] = adr(i_adr_1_tf, i_adr_length_1, i_adr_1_multi_4)

// ADR 2
[adr_2_len_1_r, adr_2_len_1_s] = adr(i_adr_2_tf, i_adr_length_1, 1)
[adr_2_len_2_r, adr_2_len_2_s] = adr(i_adr_2_tf, i_adr_length_2, 1)

[adr_2_len_1_r_m_1, adr_2_len_1_s_m_1] = adr(i_adr_2_tf, i_adr_length_1, i_adr_2_multi_1)

// ADR 3
[adr_3_len_1_r, adr_3_len_1_s] = adr(i_adr_3_tf, i_adr_length_1, 1)
[adr_3_len_2_r, adr_3_len_2_s] = adr(i_adr_3_tf, i_adr_length_2, 1)

[adr_3_len_1_r_m_1, adr_3_len_1_s_m_1] = adr(i_adr_3_tf, i_adr_length_1, i_adr_3_multi_1)
//__________________________ ADR Calculations End

//__________________________ Draw Functions Start
draw_line(left_time, right_time, float price, color _color, string _style, int _width) =>
    _line = line.new(x1=left_time, y1=price, x2=right_time, y2=price, xloc=xloc.bar_time, color=_color, style=_style, width=_width)

draw_box(left_time, right_time, float top_price, float bottom_price, color _color, int _border_width, string _border_style, string _text, string _text_size, color _text_color, string _text_halign) =>
    //_text = str.tostring(math.round_to_mintick(top_price))
    _box = box.new(left=left_time, top=top_price, right=right_time, bottom=bottom_price, border_color=_color, border_width=_border_width, border_style=_border_style, xloc=xloc.bar_time, bgcolor=_color, text=_text, text_size=_text_size, text_color=_text_color, text_halign=_text_halign, text_valign=text.align_center)//, text_wrap=text.wrap_auto)

//draw_label(float price, color _color, string _size) =>
//    _text = str.tostring(math.round_to_mintick(price))
//    label.new(point=adr_1_time, text=_text, xloc=xloc.bar_time, color=#00000000, style=label.style_label_right, textcolor=_color, size=_size, textalign=text.align_right)
//__________________________ Draw Functions End

//__________________________ Draw ADR 1 Levels Start
var adr_1_r = array.new_box(), var adr_1_s = array.new_box()
var adr_1_r_m_1 = array.new_line(), var adr_1_s_m_1 = array.new_line()
var adr_1_r_m_2 = array.new_line(), var adr_1_s_m_2 = array.new_line()
var adr_1_r_m_3 = array.new_line(), var adr_1_s_m_3 = array.new_line()
var adr_1_r_m_4 = array.new_line(), var adr_1_s_m_4 = array.new_line()

if(timeframe.change(i_adr_1_tf))
    adr_1_r_bx = draw_box(adr_1_time, adr_1_time_close, adr_1_len_1_r, adr_1_len_2_r, i_adr_1_r_bx_color, 1, i_adr_1_bx_style, str.tostring(i_adr_1_tf) + ': ' + str.tostring(math.round_to_mintick(adr_1_len_1_r)), i_adr_1_bx_size, i_adr_1_r_txt_color, i_adr_1_bx_text_align)
    array.push(adr_1_r, adr_1_r_bx)
    adr_1_s_bx = draw_box(adr_1_time, adr_1_time_close, adr_1_len_1_s, adr_1_len_2_s, i_adr_1_s_bx_color, 1, i_adr_1_bx_style, str.tostring(i_adr_1_tf) + ': ' + str.tostring(math.round_to_mintick(adr_1_len_1_s)), i_adr_1_bx_size, i_adr_1_s_txt_color, i_adr_1_bx_text_align)
    array.push(adr_1_s, adr_1_s_bx)
    if i_adr_1_multi_show_1
        adr_1_r_m_1_ln = draw_line(adr_1_time, adr_1_time_close, adr_1_len_1_r_m_1, i_adr_1_multi_color, i_adr_1_ln_style, i_adr_1_ln_width)
        array.push(adr_1_r_m_1, adr_1_r_m_1_ln)
        adr_1_s_m_1_ln = draw_line(adr_1_time, adr_1_time_close, adr_1_len_1_s_m_1, i_adr_1_multi_color, i_adr_1_ln_style, i_adr_1_ln_width)
        array.push(adr_1_s_m_1, adr_1_s_m_1_ln)
    if i_adr_1_multi_show_2
        adr_1_r_m_2_ln = draw_line(adr_1_time, adr_1_time_close, adr_1_len_1_r_m_2, i_adr_1_multi_color, i_adr_1_ln_style, i_adr_1_ln_width)
        array.push(adr_1_r_m_2, adr_1_r_m_2_ln)
        adr_1_s_m_2_ln = draw_line(adr_1_time, adr_1_time_close, adr_1_len_1_s_m_2, i_adr_1_multi_color, i_adr_1_ln_style, i_adr_1_ln_width)
        array.push(adr_1_s_m_2, adr_1_s_m_2_ln)
    if i_adr_1_multi_show_3
        adr_1_r_m_3_ln = draw_line(adr_1_time, adr_1_time_close, adr_1_len_1_r_m_3, i_adr_1_multi_color, i_adr_1_ln_style, i_adr_1_ln_width)
        array.push(adr_1_r_m_3, adr_1_r_m_3_ln)
        adr_1_s_m_3_ln = draw_line(adr_1_time, adr_1_time_close, adr_1_len_1_s_m_3, i_adr_1_multi_color, i_adr_1_ln_style, i_adr_1_ln_width)
        array.push(adr_1_s_m_3, adr_1_s_m_3_ln)
    if i_adr_1_multi_show_4
        adr_1_r_m_4_ln = draw_line(adr_1_time, adr_1_time_close, adr_1_len_1_r_m_4, i_adr_1_multi_color, i_adr_1_ln_style, i_adr_1_ln_width)
        array.push(adr_1_r_m_4, adr_1_r_m_4_ln)
        adr_1_s_m_4_ln = draw_line(adr_1_time, adr_1_time_close, adr_1_len_1_s_m_4, i_adr_1_multi_color, i_adr_1_ln_style, i_adr_1_ln_width)
        array.push(adr_1_s_m_4, adr_1_s_m_4_ln)

// Function to delete past lines & boxes
show_last_boxes(arr, showlast) =>
    if array.size(arr) >= showlast
        box = array.get(arr, 0)
        box.delete(box)
        array.remove(arr, 0)

show_last_lines(arr, showlast) =>
    if array.size(arr) >= showlast
        line = array.get(arr, 0)
        line.delete(line)
        array.remove(arr, 0)

show_last_boxes(adr_1_r, i_adr_1_showlast)
show_last_boxes(adr_1_s, i_adr_1_showlast)
show_last_lines(adr_1_r_m_1, i_adr_1_showlast)
show_last_lines(adr_1_s_m_1, i_adr_1_showlast)
show_last_lines(adr_1_r_m_2, i_adr_1_showlast)
show_last_lines(adr_1_s_m_2, i_adr_1_showlast)
show_last_lines(adr_1_r_m_3, i_adr_1_showlast)
show_last_lines(adr_1_s_m_3, i_adr_1_showlast)
show_last_lines(adr_1_r_m_4, i_adr_1_showlast)
show_last_lines(adr_1_s_m_4, i_adr_1_showlast)
//__________________________ Draw ADR 1 Levels End

//__________________________ Draw ADR 2 Levels Start
var adr_2_r = array.new_box(), var adr_2_s = array.new_box()
var adr_2_r_m_1 = array.new_line(), var adr_2_s_m_1 = array.new_line()

if(timeframe.change(i_adr_2_tf))
    adr_2_r_bx = draw_box(adr_2_time, adr_2_time_close, adr_2_len_1_r, adr_2_len_2_r, i_adr_2_r_bx_color, 1, i_adr_2_bx_style, str.tostring(i_adr_2_tf) + ': ' + str.tostring(math.round_to_mintick(adr_2_len_1_r)), i_adr_2_bx_size, i_adr_2_r_txt_color, i_adr_2_bx_text_align)
    array.push(adr_2_r, adr_2_r_bx)
    adr_2_s_bx = draw_box(adr_2_time, adr_2_time_close, adr_2_len_1_s, adr_2_len_2_s, i_adr_2_s_bx_color, 1, i_adr_2_bx_style, str.tostring(i_adr_2_tf) + ': ' + str.tostring(math.round_to_mintick(adr_2_len_1_s)), i_adr_2_bx_size, i_adr_2_s_txt_color, i_adr_2_bx_text_align)
    array.push(adr_2_s, adr_2_s_bx)
    if i_adr_2_multi_show_1
        adr_2_r_m_1_ln = draw_line(adr_2_time, adr_2_time_close, adr_2_len_1_r_m_1, i_adr_2_multi_color, i_adr_2_ln_style, i_adr_2_ln_width)
        array.push(adr_2_r_m_1, adr_2_r_m_1_ln)
        adr_2_s_m_1_ln = draw_line(adr_2_time, adr_2_time_close, adr_2_len_1_s_m_1, i_adr_2_multi_color, i_adr_2_ln_style, i_adr_2_ln_width)
        array.push(adr_2_s_m_1, adr_2_s_m_1_ln)

show_last_boxes(adr_2_r, i_adr_2_showlast)
show_last_boxes(adr_2_s, i_adr_2_showlast)
show_last_lines(adr_2_r_m_1, i_adr_2_showlast)
show_last_lines(adr_2_s_m_1, i_adr_2_showlast)
//__________________________ Draw ADR 2 Levels End

//__________________________ Draw ADR 3 Levels Start
var adr_3_r = array.new_box(), var adr_3_s = array.new_box()
var adr_3_r_m_1 = array.new_line(), var adr_3_s_m_1 = array.new_line()

if(timeframe.change(i_adr_3_tf))
    adr_3_r_bx = draw_box(adr_3_time, adr_3_time_close, adr_3_len_1_r, adr_3_len_2_r, i_adr_3_r_bx_color, 1, i_adr_3_bx_style,  str.tostring(i_adr_3_tf) + ': ' + str.tostring(math.round_to_mintick(adr_3_len_1_r)), i_adr_3_bx_size, i_adr_3_r_txt_color, i_adr_3_bx_text_align)
    array.push(adr_3_r, adr_3_r_bx)
    adr_3_s_bx = draw_box(adr_3_time, adr_3_time_close, adr_3_len_1_s, adr_3_len_2_s, i_adr_3_s_bx_color, 1, i_adr_3_bx_style,  str.tostring(i_adr_3_tf) + ': ' + str.tostring(math.round_to_mintick(adr_3_len_1_s)), i_adr_3_bx_size, i_adr_3_s_txt_color, i_adr_3_bx_text_align)
    array.push(adr_3_s, adr_3_s_bx)
    if i_adr_3_multi_show_1
        adr_3_r_m_1_ln = draw_line(adr_3_time, adr_3_time_close, adr_3_len_1_r_m_1, i_adr_3_multi_color, i_adr_3_ln_style, i_adr_3_ln_width)
        array.push(adr_3_r_m_1, adr_3_r_m_1_ln)
        adr_3_s_m_1_ln = draw_line(adr_3_time, adr_3_time_close, adr_3_len_1_s_m_1, i_adr_3_multi_color, i_adr_3_ln_style, i_adr_3_ln_width)
        array.push(adr_3_s_m_1, adr_3_s_m_1_ln)

show_last_boxes(adr_3_r, i_adr_3_showlast)
show_last_boxes(adr_3_s, i_adr_3_showlast)
show_last_lines(adr_3_r_m_1, i_adr_3_showlast)
show_last_lines(adr_3_s_m_1, i_adr_3_showlast)
//__________________________ Draw ADR 3 Levels End

//__________________________ Table Start
// Current Timeframe ADR
[adr_1_len_1_r_ctf, adr_1_len_1_s_ctf] = adr('', i_adr_length_1, 1)

// Caculate ADR Range
pchg_chg(prev_value, current_value) =>
    pchg = (prev_value / current_value - 1) * 100
    chg = prev_value - current_value
    [pchg, chg]

[adr_1_pchg, adr_1_chg] = pchg_chg(adr_1_len_1_r_ctf, adr_1_len_1_s_ctf)
[adr_2_pchg, adr_2_chg] = pchg_chg(adr_1_len_1_r, adr_1_len_1_s)

// Bg Color
bgcolor_1 = color.new(i_tbl_text_color, 80)
bgcolor_2 = color.new(i_tbl_text_color, 90)

// Plot Table
var table plot_table = table.new(position=i_tbl_position, columns=3, rows=2, border_width=2)

if barstate.islast and i_table_show
    table.cell(plot_table, column=0, row=0, text='Chart', text_color=i_tbl_text_color, text_halign=text.align_right, bgcolor=bgcolor_1, text_size=i_tbl_text_size)
    table.cell(plot_table, column=1, row=0, text=str.tostring(adr_1_chg, '#.##') + ' ₹', text_color=i_tbl_text_color, text_halign=text.align_right, bgcolor=bgcolor_1, text_size=i_tbl_text_size)
    table.cell(plot_table, column=2, row=0, text=str.tostring(adr_1_pchg, '#.##') + ' %', text_color=i_tbl_text_color, text_halign=text.align_right, bgcolor=bgcolor_1, text_size=i_tbl_text_size)

    table.cell(plot_table, column=0, row=1, text=str.tostring(i_adr_1_tf), text_color=i_tbl_text_color, text_halign=text.align_right, bgcolor=bgcolor_2, text_size=i_tbl_text_size)
    table.cell(plot_table, column=1, row=1, text=str.tostring(adr_2_chg, '#.##') + ' ₹', text_color=i_tbl_text_color, text_halign=text.align_right, bgcolor=bgcolor_2, text_size=i_tbl_text_size)
    table.cell(plot_table, column=2, row=1, text=str.tostring(adr_2_pchg, '#.##') + ' %', text_color=i_tbl_text_color, text_halign=text.align_right, bgcolor=bgcolor_2, text_size=i_tbl_text_size)
//__________________________ Table End
//__________________________ CODE END