Yearly High & Low
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Safxxr

//@version=5
indicator("Yearly High & Low", overlay=true)

var start_year = input.int(2017, "Starting Year")
var end_year = input.int(2022, "Ending Year")
var show_high = input.bool(true, "Show Highs")
var show_low = input.bool(true, "Show Lows")
var show_price = input.bool(true, "Show Price")
var high_color = input(color.new(color.lime, 0), "High Colour")
var low_color = input(color.new(color.red, 0), "Low Colour")
var label_font_color = input(color.rgb(255, 255, 255), "Label Colour") // New input for label font color

var current_high = float(0)
var current_low = float(0)
var hl = line.new(bar_index, current_high, bar_index + 1, current_high, extend=extend.right, color=high_color)
var ll = line.new(bar_index, current_low, bar_index + 1, current_low, extend=extend.right, color=low_color)
var y = year(time)

if start_year > year(time) or year(time) > end_year
    line.delete(hl)
    line.delete(ll)

var hlabel_array = array.new_label(na)
var llabel_array = array.new_label(na)

if high > line.get_y1(hl)
    current_high := high
    line.set_y1(hl, current_high)
    line.set_y2(hl, current_high)
    line.set_x1(hl, bar_index)
    line.set_x2(hl, bar_index + 1)

if low < line.get_y1(ll)
    current_low := low
    line.set_y1(ll, current_low)
    line.set_y2(ll, current_low)
    line.set_x1(ll, bar_index)
    line.set_x2(ll, bar_index + 1)

if year(time) > y
    label_year = str.tostring(y)
    label_high = show_price ? "$" + str.tostring(current_high) : ""
    label_low = show_price ? "$" + str.tostring(current_low) : ""
    high_label = label_year + " - " + label_high
    low_label = label_year + " - " + label_low
    hlabel = label.new(bar_index, y=current_high, text=high_label, style=label.style_none, textcolor=label_font_color) // Use label_font_color for font color
    llabel = label.new(bar_index, y=current_low, text=low_label, style=label.style_none, textcolor=label_font_color) // Use label_font_color for font color
    if start_year >= year(time) or year(time) > end_year
        label.delete(hlabel)
        label.delete(llabel)
    else
        array.push(hlabel_array, hlabel)
        array.push(llabel_array, llabel)
    y := year(time)
    current_high := high
    current_low := low
    hl := line.new(bar_index, current_high, bar_index + 1, current_high, extend=extend.right, color=high_color)
    ll := line.new(bar_index, current_low, bar_index + 1, current_low, extend=extend.right, color=low_color)

if not show_low
    line.delete(ll)

if not show_high
    line.delete(hl)

if barstate.islast
    label_year = str.tostring(y)
    label_high = show_price ? "$" + str.tostring(current_high) : ""
    label_low = show_price ? "$" + str.tostring(current_low) : ""
    high_label = label_year + " - " + label_high
    low_label = label_year + " - " + label_low
    hlabel = label.new(bar_index, y=current_high, text=high_label, style=label.style_none, textcolor=label_font_color) // Use label_font_color for font color
    llabel = label.new(bar_index, y=current_low, text=low_label, style=label.style_none, textcolor=label_font_color) // Use label_font_color for font color
    if start_year >= year(time) or year(time) > end_year
        label.delete(hlabel)
        label.delete(llabel)
    else
        array.push(hlabel_array, hlabel)
        array.push(llabel_array, llabel)

if start_year > year(time) or year(time) > end_year
    line.delete(hl)

for i in hlabel_array
    if show_high
        label.set_x(i, bar_index + 10)
    else
        label.delete(i)

for i in llabel_array
    if show_low
        label.set_x(i, bar_index + 10)
    else
        label.delete(i)
