ADR Percent
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © vtrader321

//@version=5
indicator('ADR Percent', overlay=true)

LIGHTTRANSP = 90
AVGTRANSP = 80
HEAVYTRANSP = 70

var string _gp = 'Display'
string i_tableYpos = input.string('top', 'Panel position', inline='11', options=['top', 'middle', 'bottom'], group=_gp)
string i_tableXpos = input.string('center', '', inline='11', options=['left', 'center', 'right'], group=_gp)

var string _gp1 = 'Lookback Time Range(Days)'
string i_time_interval = input.string('5', '', inline='11', options=['5', '10', '15', '20'], group=_gp1)

_open = request.security(syminfo.tickerid, 'D', open)

dayrange = high - low

_r1 = request.security(syminfo.tickerid, 'D', dayrange[1])
_r2 = request.security(syminfo.tickerid, 'D', dayrange[2])
_r3 = request.security(syminfo.tickerid, 'D', dayrange[3])
_r4 = request.security(syminfo.tickerid, 'D', dayrange[4])
_r5 = request.security(syminfo.tickerid, 'D', dayrange[5])
_r6 = request.security(syminfo.tickerid, 'D', dayrange[6])
_r7 = request.security(syminfo.tickerid, 'D', dayrange[7])
_r8 = request.security(syminfo.tickerid, 'D', dayrange[8])
_r9 = request.security(syminfo.tickerid, 'D', dayrange[9])
_r10 = request.security(syminfo.tickerid, 'D', dayrange[10])
_r11 = request.security(syminfo.tickerid, 'D', dayrange[11])
_r12 = request.security(syminfo.tickerid, 'D', dayrange[12])
_r13 = request.security(syminfo.tickerid, 'D', dayrange[13])
_r14 = request.security(syminfo.tickerid, 'D', dayrange[14])
_r15 = request.security(syminfo.tickerid, 'D', dayrange[15])
_r16 = request.security(syminfo.tickerid, 'D', dayrange[16])
_r17 = request.security(syminfo.tickerid, 'D', dayrange[17])
_r18 = request.security(syminfo.tickerid, 'D', dayrange[18])
_r19 = request.security(syminfo.tickerid, 'D', dayrange[19])
_r20 = request.security(syminfo.tickerid, 'D', dayrange[20])

_adr = 0.0
if i_time_interval == '5'
    _adr_5 = (_r1 + _r2 + _r3 + _r4 + _r5) / 5
    _adr := _adr_5 / _open * 100
    _adr
else if i_time_interval == '10'
    _adr_10 = (_r1 + _r2 + _r3 + _r4 + _r5 + _r6 + _r7 + _r8 + _r9 + _r10) / 10
    _adr := _adr_10 / _open * 100
    _adr
else if i_time_interval == '15'
    _adr_15 = (_r1 + _r2 + _r3 + _r4 + _r5 + _r6 + _r7 + _r8 + _r9 + _r10 + _r11 + _r12 + _r13 + _r14 + _r15) / 15
    _adr := _adr_15 / _open * 100
    _adr
else if i_time_interval == '20'
    _adr_20 = (_r1 + _r2 + _r3 + _r4 + _r5 + _r6 + _r7 + _r8 + _r9 + _r10 + _r11 + _r12 + _r13 + _r14 + _r15 + _r16 + _r17 + _r18 + _r19 + _r20) / 20
    _adr := _adr_20 / _open * 100
    _adr

var table _table = table.new(i_tableYpos + '_' + i_tableXpos, 1, 1, border_width=3)
_c_color = color.rgb(38, 166, 154)
_transp = math.abs(_adr) > 2 ? HEAVYTRANSP : math.abs(_adr) > 1 ? AVGTRANSP : LIGHTTRANSP
_cellText = 'ADR' + '\n' + str.tostring(_adr, '0.00') + ' %'
table.cell(_table, 0, 0, _cellText, bgcolor=color.new(_c_color, _transp), text_color=_c_color, text_size=size.normal)

