High and low of last D, W, 4W and quater
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Remote Trading
//@version=5
indicator("D, W and 4W H/L", overlay=true, max_lines_count=500, max_bars_back=5000)

daily_width = input.int(1, title = "Daily - Width", inline = "daily line", group="Line config")
weekly_width = input.int(2, title = "Weekly - Width", inline="weekly line", group="Line config")
four_weekly_width = input.int(3, title = "4 Weekly - Width", inline="4 weekly line", group="Line config")
quarterly_width = input.int(3, title = "Quaterly - Width", inline="quaterly line", group="Line config")

daily_style_string = input.string("Solid", "Style", ["Solid", "Dashed", "Dotted"], inline = "daily line", group="Line config")
weekly_style_string = input.string("Dashed", "Style", ["Solid", "Dashed", "Dotted"], inline="weekly line", group="Line config")
four_weekly_style_string = input.string("Dotted", "Style", ["Solid", "Dashed", "Dotted"], inline="4 weekly line", group="Line config")
quaterly_style_string = input.string("Dashed", "Style", ["Solid", "Dashed", "Dotted"], inline="quaterly line", group="Line config")

daily_color = input.color(color.rgb(141, 20, 255), "", inline = "daily line", group="Line config")
weekly_color = input.color(#00a800, "", inline="weekly line", group="Line config")
four_weekly_color = input.color(#0734ff, "", inline="4 weekly line", group="Line config")
quaterly_color = input.color(#801922, "", inline="quaterly line", group="Line config")

parseStyle(input) =>
    (input == "Solid" ? line.style_solid : (input == "Dashed" ? line.style_dashed : (input == "Dotted" ? line.style_dotted : na)))


input_n_daily_sr = input.int(defval=3, minval=0, maxval=10,  title="Number of daily H/L", group="Number of lines to show")
input_n_weekly_sr = input.int(defval=3, minval=0, maxval=10,  title="Number of weekly H/L", group="Number of lines to show")
input_show_4weekly = input.bool(true, "Show 4 weekly H/L", group="Number of lines to show")
input_show_quaterly = input.bool(true, "Show quaterly H/L", group="Number of lines to show")

input_futur_extension = input.int(defval=10, minval=0, title="N bars to extend lines into future", group="Other things")
plot_in_radius_only = input.bool(defval=true, title="Plot within the range of X pip/ticks from current price:", inline="radius", group="Other things")
pip_radius = input.int(defval=1000, minval=1, title="", inline="radius", tooltip = "The default value usually works quite well but if you notice lines are missing and you would like to have them shown experiment with the value.", group="Other things")
skip_overlapping_lower_lines = input.bool(defval=true, title="Don't plot overlapping lines again", inline="overlap", group="Other things") 
line_skip_radius = input.float(defval=2, minval=0, step=0.5, title="Min pip/tick distance", inline="overlap", group="Other things")
showlabels = input.bool(true, "Show labels", inline="Labels")
highSymbol = input.string("▲", title="| High", options=["▲", "△"], inline="Labels")
lowSymbol = input.string("▽", title="Low", options=["▼", "▽"], inline="Labels")

daily_style = parseStyle(daily_style_string)
weekly_style = parseStyle(weekly_style_string)
four_weekly_style = parseStyle(four_weekly_style_string)
quarterly_style = parseStyle(quaterly_style_string)

var lines = array.new_line()
var labels = array.new_label()

for idx=0 to array.size(lines)
    if array.size(lines) > 0
        ln = array.shift(lines)
        line.delete(ln)

for idx=0 to array.size(labels)
    if array.size(labels) > 0
        lab = array.shift(labels)
        label.delete(lab)

//https://www.tradingview.com/pine-script-docs/en/v4/essential/Drawings.html#max-bars-back-of-time
max_bars_back(time, 5000)


check_for_existing_line(new_line_price) =>
    bool result = true
    for index=0 to array.size(lines)
        if array.size(lines) > index
            ln = array.get(lines, index)
            prev_line_y = line.get_y1(ln)
            if math.abs(new_line_price - prev_line_y) < syminfo.mintick * line_skip_radius
                result := false
                break
    result

log.info(str.tostring(syminfo.mintick*pip_radius))
generte_lines(index, timeframe, h_or_l, style, color_, width, labelText) =>
    dh = request.security(syminfo.tickerid, timeframe, h_or_l[index], barmerge.gaps_off, barmerge.lookahead_on)
    if barstate.islast and (math.abs(dh - close) < syminfo.mintick*pip_radius or not plot_in_radius_only)
        counter = 0
        for ii = 0 to 4990
            //specificDate = time[ii] < timestamp(year, month, dayofmonth-2, 18, 00)
            if h_or_l[ii] == dh
                counter := ii
                break
        
        if check_for_existing_line(dh) or not skip_overlapping_lower_lines
            lineID = line.new(bar_index-counter, dh, bar_index+input_futur_extension, dh, color=color_, style=style, width=width)
            array.push(lines, lineID)
            if showlabels
                labelID = label.new(x=bar_index+input_futur_extension, y=dh, color=color.rgb(179, 179, 179, 63), textcolor=color.black, style=label.style_label_left, size=size.small)
                label.set_text(labelID, text=labelText)
                array.push(labels, labelID)


apply_lines_for_HL(index, timeframe, style, color_, width) =>
    generte_lines(index, timeframe, high, style, color_, width, timeframe + " " + highSymbol + " -" + str.tostring(index))
    generte_lines(index, timeframe, low, style, color_, width, timeframe + " " + lowSymbol + " -" + str.tostring(index))

if input_show_quaterly
    apply_lines_for_HL(1, "3M", quarterly_style, quaterly_color, quarterly_width)

if input_show_4weekly
    apply_lines_for_HL(1, "4W", four_weekly_style,four_weekly_color, four_weekly_width)

if input_n_weekly_sr >= 1
    apply_lines_for_HL(2, "W", weekly_style, weekly_color, weekly_width)
if input_n_weekly_sr >= 2
    apply_lines_for_HL(3, "W", weekly_style, weekly_color, weekly_width)
if input_n_weekly_sr >= 3
    apply_lines_for_HL(4, "W", weekly_style, weekly_color, weekly_width)
if input_n_weekly_sr >= 4
    apply_lines_for_HL(5, "W", weekly_style, weekly_color, weekly_width)
if input_n_weekly_sr >= 5
    apply_lines_for_HL(6, "W", weekly_style, weekly_color, weekly_width)
if input_n_weekly_sr >= 6
    apply_lines_for_HL(7, "W", weekly_style, weekly_color, weekly_width)
if input_n_weekly_sr >= 7
    apply_lines_for_HL(8, "W", weekly_style, weekly_color, weekly_width)
if input_n_weekly_sr >= 8
    apply_lines_for_HL(9, "W", weekly_style, weekly_color, weekly_width)
if input_n_weekly_sr >= 9
    apply_lines_for_HL(10, "W", weekly_style, weekly_color, weekly_width)


if input_n_daily_sr >= 1
    apply_lines_for_HL(1, "D", daily_style, daily_color, daily_width)
if input_n_daily_sr >= 2    
    apply_lines_for_HL(2, "D", daily_style, daily_color, daily_width)
if input_n_daily_sr >= 3
    apply_lines_for_HL(3, "D", daily_style, daily_color, daily_width)
if input_n_daily_sr >= 4
    apply_lines_for_HL(4, "D", daily_style, daily_color, daily_width)
if input_n_daily_sr >= 5
    apply_lines_for_HL(5, "D", daily_style, daily_color, daily_width)
if input_n_daily_sr >= 6
    apply_lines_for_HL(6, "D", daily_style, daily_color, daily_width)
if input_n_daily_sr >= 7    
    apply_lines_for_HL(7, "D", daily_style, daily_color, daily_width)
if input_n_daily_sr >= 8
    apply_lines_for_HL(8, "D", daily_style, daily_color, daily_width)
if input_n_daily_sr >= 9
    apply_lines_for_HL(9, "D", daily_style, daily_color, daily_width)   
