Average Daily Range Expansion Remaindeer for Daytrading
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © greenmask9

//@version=5
indicator(title = "Average Daily Range Remaindeer", shorttitle = "ADRR", overlay = true)

i_full_style = input.string("Solid", title = "Line Style", options = ["Solid", "Dotted", "Dashed"], group="Full Expansion")
full_style = i_full_style == "Solid" ? line.style_solid : i_full_style == "Dotted" ? line.style_dotted : line.style_dashed
i_full_colour = input.color(color.new(#00bcd4,0), title = "Color", group="Full Expansion")
i_full_width = input.int(1, title = "Line Width", minval=1, group="Full Expansion")
i_full_enable = input.bool(true, "Enable", group="Full Expansion")

i_rem_style = input.string("Solid", title = "Line Style", options = ["Solid", "Dotted", "Dashed"], group="Remaining Expansion")
rem_style = i_rem_style == "Solid" ? line.style_solid : i_rem_style == "Dotted" ? line.style_dotted : line.style_dashed
i_rem_colour = input.color(color.new(#00bcd4,0), title = "Color", group="Remaining Expansion")
i_rem_width = input.int(1, title = "Line Width", minval=1, group="Remaining Expansion")
i_rem_enable = input.bool(true, "Enable", group="Remaining Expansion")

d_1 = request.security(syminfo.tickerid, "D", high[1]-low[1], lookahead=barmerge.lookahead_on)
d_2 = request.security(syminfo.tickerid, "D", high[2]-low[2], lookahead=barmerge.lookahead_on)
d_3 = request.security(syminfo.tickerid, "D", high[3]-low[3], lookahead=barmerge.lookahead_on)
d_4 = request.security(syminfo.tickerid, "D", high[4]-low[4], lookahead=barmerge.lookahead_on)
d_5 = request.security(syminfo.tickerid, "D", high[5]-low[5], lookahead=barmerge.lookahead_on)

ADR = (d_1 + d_2 + d_3 + d_4 + d_5) / 5

var new_day_x1 = 0
new_day = ADR != ADR[1]
if new_day
    new_day_x1 := time

var full_expansion_upwards = line.new(na,na,na,na, xloc = xloc.bar_time, style = full_style, color = i_full_colour,width = i_full_width)
var full_expansion_downwards = line.new(na,na,na,na, xloc = xloc.bar_time, style = full_style, color = i_full_colour,width = i_full_width)
var remaining_expansion_upwards = line.new(na,na,na,na, xloc = xloc.bar_time, style = rem_style, color = i_rem_colour,width = i_rem_width)
var remaining_expansion_downwards = line.new(na,na,na,na, xloc = xloc.bar_time, style = rem_style, color = i_rem_colour,width = i_rem_width)
var float intra_high = na
var float intra_low = na

if new_day
    if i_full_enable
        line.set_xy1(full_expansion_upwards, new_day_x1, open + ADR)
        line.set_xy2(full_expansion_upwards, time_close("1D"), open + ADR)
        line.set_xy1(full_expansion_downwards, new_day_x1, open - ADR)
        line.set_xy2(full_expansion_downwards, time_close("1D"), open - ADR)
    intra_high := high
    intra_low := low

if high > intra_high[1]
    intra_high := high
if low < intra_low[1]
    intra_low := low
remaining_adr = ADR - (intra_high - intra_low)

if i_rem_enable
    line.set_xy1(remaining_expansion_upwards, time[1], intra_high + remaining_adr)
    line.set_xy2(remaining_expansion_upwards, time_close("1D"), intra_high + remaining_adr)
    line.set_xy1(remaining_expansion_downwards, time[1], intra_low - remaining_adr)
    line.set_xy2(remaining_expansion_downwards, time_close("1D"), intra_low - remaining_adr)