ATH ATL ATX Finder
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AlfaVisioner

//@version=5
indicator("ATH ATL ATX Finder", overlay=true)

in_src=hlc3 //input.source(hlc3, title="Data source", group="General")
in_use_initial_time=input.bool(defval=true, title="Start from date below", group="Period")
in_initial_time=input.time(defval=timestamp("01 JAN 2021"), title="Date", group="Period")
in_panel_alignment=input.string(defval="Top", options=["Top", "Bottom"], title ="Panel alignment", group="View")
in_show_labels=input.bool(defval=true, title="Show labels", group="View")

// Time filter
time_is_ok = not in_use_initial_time or time > in_initial_time

// Counters
var candles = 0
var average_price_total = 0.00
var ath = -1e10
var atl = 1e10

new_ath_found = false
new_atl_found = false

if time_is_ok
    if high > ath
        ath := high    
        new_ath_found := true

    if low < atl
        atl := low    
        new_atl_found := true
    
// Output 
//-- Colors
color_atx_label = color.green
color_atl_label = color.red
color_price_label = #b5c7fd
color_table = color.new(color.green,50)
color_envelope = color.purple

//-- Time
bgcolor(not time_is_ok? color.new(color.red, 80) : na)

//-- Labels
if in_show_labels and new_ath_found
    ath_label=label.new(bar_index, high, text='ATH', style=label.style_label_down, color=color_atx_label)
    label.delete(ath_label[1])

if in_show_labels and new_atl_found 
    atl_label=label.new(bar_index, low, text='ATL', style=label.style_label_up, color=color_atl_label)
    label.delete(atl_label[1])   

//--Math-
safe_div(a, b)=>
  result = (b != 0) ? a / b : 0

panel_alignment = switch in_panel_alignment 
    'Top' => position.top_right
    'Bottom' => position.bottom_right
    
result_table = table.new(panel_alignment, columns=1, rows=5, bgcolor=#00000000)

if barstate.islast
    atx = safe_div(ath, atl) 
    x_to_ath = safe_div(ath, in_src) 
    t_from_atl = safe_div(in_src, atl)     
    ath_text = 'ATH: ' + str.tostring(ath, format.mintick)
    atl_text = 'ATL: ' + str.tostring(atl, format.mintick)
    atx_text = 'ATX: ' + str.tostring(atx,"#.##")
    x_to_ath_text = 'X to ATH: ' + str.tostring(x_to_ath, "#.##")
    t_from_atl_text = 'X from ATL: ' + str.tostring(t_from_atl, "#.##")    

    table.cell(table_id=result_table, column=0, row=0, text=ath_text, height=0, text_color=color.black, text_halign=text.align_left, text_valign= text.align_center, bgcolor=color_table)
    table.cell(table_id=result_table, column=0, row=1, text=atl_text, height=0, text_color=color.black, text_halign=text.align_left, text_valign= text.align_center, bgcolor=color_table)
    table.cell(table_id=result_table, column=0, row=2, text=atx_text, height=0, text_color=color.black, text_halign=text.align_left, text_valign= text.align_center, bgcolor=color_table)
    table.cell(table_id=result_table, column=0, row=3, text=x_to_ath_text, height=0, text_color=color.black, text_halign=text.align_left, text_valign= text.align_center, bgcolor=color_table)    
    table.cell(table_id=result_table, column=0, row=4, text=t_from_atl_text, height=0, text_color=color.black, text_halign=text.align_left, text_valign= text.align_center, bgcolor=color_table)        