Average Daily Range% 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%", shorttitle = "ADR%", overlay = false, format = format.percent)


i_comparative_stats_max = input.int(240, title = "Max for Comparative Statistics", group = "Tables")
i_comparative_stats_min = input.int(10, title = "Max for Comparative Statistics", group = "Tables")

i_hline_w = input.int(1, title = "Hline Width")

i_position = input.string("Top-Right", "Table position", options = ["Top-Right", "Center-Right", "Bottom-Right"], group = "Text")
table_position = i_position == "Top-Right" ? position.top_right : i_position == "Tiny" ? position.middle_right : position.bottom_right
i_its = input.string("Auto", "Axis text size", options = ["Auto", "Tiny", "Small", "Normal"], group = "Text")
axis_ts = i_its == "Auto" ? size.auto : i_its == "Tiny" ? size.tiny : i_its == "Small" ? size.small : size.normal
i_header_width = input.int(16, title = "Width for table statistics description", group = "Text", tooltip = "You don't need to edit any settings in this group, unless the tables are malfunctioning when you use multiple charts per tab")
i_content_width = input.int(4, title = "Width for table values box", group = "Text")
i_box_height = input.int(8, title = "Height for table boxes", group = "Text")

c_hlines = input.color(color.new(color.gray, 80), title = "Hline", group  = "Colour")
c_tabletext = input.color(color.new(color.yellow, 0), title = "Table Text", group  = "Colour")
c_indicator = input.color(color.new(color.fuchsia, 0), title = "Indicator", group  = "Colour")


d_0 = request.security(syminfo.tickerid, "D", high[0]-low[0], lookahead=barmerge.lookahead_on)
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

ADR_normalized = d_0 / ADR * 100




plot(ADR_normalized, title = "Average Daily Range", color = c_indicator, linewidth = 1)

hline(100,title="100", color = c_hlines, linewidth = i_hline_w)
hline(80,title="80", color = c_hlines, linewidth = i_hline_w)
hline(50,title="50", color = c_hlines, linewidth = i_hline_w)
hline(0,title="100", color = c_hlines, linewidth = i_hline_w)

statistics = table.new(table_position, 3, 32, bgcolor = na, frame_color = na, frame_width = 0, border_color = na, border_width = 0)


fill_table(row,txt_header,txt_content) =>
    table.cell(statistics,0,row,text = txt_header, text_color = c_tabletext, height = i_box_height, width = i_header_width, text_size = axis_ts, bgcolor = na)
    table.cell(statistics,1,row,text = txt_content, text_color = c_tabletext, height = i_box_height, width = i_content_width, text_size = axis_ts, bgcolor = na)
//90 day stats

//Number of Days Over 100


above_100_total(min,max) =>
    int above_100_total = 0
    for counter = min to max
        if ADR_normalized[counter] > 100
            above_100_total += 1
        above_100_total

fill_table(0,"Above 100 Ratio",str.tostring(above_100_total(i_comparative_stats_min,i_comparative_stats_max)/i_comparative_stats_max,  format.percent))

//plotshape(ADR_normalized > 100, location = location.top)


//Max Days In a Row under 100


under_100_inrow(min,max) =>
    int below_100_inrow = 0, int below_100_current = 0, int continuous_cycle = max
    for counter = min to max
        if ADR_normalized[counter] < 100 and counter < max
            below_100_current += 1
        else if counter == max and ADR_normalized[counter] < 100
            while ADR_normalized[continuous_cycle] < 100
                below_100_current += 1
                continuous_cycle += 1
        else
            below_100_current := 0
        if below_100_current > below_100_inrow
            below_100_inrow := below_100_current
    below_100_inrow

fill_table(1,"Under 100 In a Row",str.tostring(under_100_inrow(i_comparative_stats_min,i_comparative_stats_max)))

//Remaining

fill_table(2,"Today Move",str.tostring(ADR_normalized/100, format.percent))