52 Weeks High/Low Widget
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © QuantNomad

//@version=5
indicator('52 weeks High/Low', shorttitle='52W H/L', overlay=true)

////////////
// INPUTS //

weeks      = input(52,    "Number of Weeks")
show_ath   = input(true,  "Show High?")
show_atl   = input(false, "Show Low?")
show_table = input(true,  "Show table with stats?")

///////////////
// FUNCTIONS //

// get 52 weeks high/low
get_week_high_low() =>
    var time_arr = array.new_int(0)
    var high_arr = array.new_float(0)
    var low_arr  = array.new_float(0)
    
    array.unshift(time_arr, time)
    array.unshift(high_arr, high)
    array.unshift(low_arr,  low)
    
    ms_limit = 604800000 * weeks
    
    while (array.get(time_arr, array.size(time_arr) - 1) < time - ms_limit)
        array.pop(time_arr)
        array.pop(high_arr)
        array.pop(low_arr)
    
    // getting all-time high/low    
    ath = array.max(high_arr)
    atl = array.min(low_arr)
    
    hi = 0
    while true
        if array.get(high_arr, hi) == ath
            break
        hi += 1
    
    li = 0
    while true
        if array.get(low_arr, li) == atl
            break
        li += 1
    
    ath_dt = array.get(time_arr, hi)
    atl_dt = array.get(time_arr, li)
    
    [ath, atl, ath_dt, atl_dt]


[ath, atl, ath_dt, atl_dt] = request.security(syminfo.tickerid, '1D', get_week_high_low())

ath_days = math.round((timenow - ath_dt) / 86400000)
atl_days = math.round((timenow - atl_dt) / 86400000)

// plotting
if show_ath
    lATH=line.new(bar_index - 1, ath, bar_index, ath, extend = extend.both, color = color.green)
    line.delete(lATH[1])
    
if show_atl
    lATL=line.new(bar_index - 1, atl, bar_index, atl, extend = extend.both, color = color.red)
    line.delete(lATL[1])

if show_table
    var table ATHtable = table.new(position.bottom_right, 6, 3, frame_color = color.gray, bgcolor = color.gray, border_width = 1, frame_width = 1, border_color = color.white)

    ath_time = str.tostring(year(ath_dt)) + "-" + str.tostring(month(ath_dt)) + "-" + str.tostring(dayofmonth(ath_dt))
    atl_time = str.tostring(year(atl_dt)) + "-" + str.tostring(month(atl_dt)) + "-" + str.tostring(dayofmonth(atl_dt))

    // Header
    table.cell(ATHtable, 0, 0, "",         bgcolor = #cccccc)
    table.cell(ATHtable, 1, 0, "When?",    bgcolor = #cccccc)
    table.cell(ATHtable, 2, 0, "Days ago", bgcolor = #cccccc)
    table.cell(ATHtable, 3, 0, "Price",    bgcolor = #cccccc)
    table.cell(ATHtable, 4, 0, "% away",   bgcolor = #cccccc)
    table.cell(ATHtable, 5, 0, "$ away",   bgcolor = #cccccc)
    
    if (show_ath)
        // ATH
        table.cell(ATHtable, 0, 1, "ATH",                                                  bgcolor = #cccccc)
        table.cell(ATHtable, 1, 1, ath_time,                                               bgcolor = color.new(color.green, transp = 50))
        table.cell(ATHtable, 2, 1, str.tostring(ath_days),                                 bgcolor = color.new(color.green, transp = 50))
        table.cell(ATHtable, 3, 1, str.tostring(ath),                                      bgcolor = color.new(color.green, transp = 50))
        table.cell(ATHtable, 4, 1, str.tostring(((ath / close) - 1) * 100 , "#.##") + "%", bgcolor = color.new(color.green, transp = 50))
        table.cell(ATHtable, 5, 1, str.tostring(ath - close , "#.##") + "$",               bgcolor = color.new(color.green, transp = 50))
    
    if (show_atl)
        // ATL
        table.cell(ATHtable, 0, 2, "ATL",                                                  bgcolor = #cccccc)
        table.cell(ATHtable, 1, 2, atl_time,                                               bgcolor = color.new(color.red, transp = 50))
        table.cell(ATHtable, 2, 2, str.tostring(atl_days),                                 bgcolor = color.new(color.red, transp = 50))
        table.cell(ATHtable, 3, 2, str.tostring(atl),                                      bgcolor = color.new(color.red, transp = 50))
        table.cell(ATHtable, 4, 2, str.tostring(((atl / close) - 1) * 100 , "#.##") + "%", bgcolor = color.new(color.red, transp = 50))
        table.cell(ATHtable, 5, 2, str.tostring(atl - close, "#.##") + "$",                bgcolor = color.new(color.red, transp = 50))

// alerts
alertcondition(ta.crossover(high, ath), 'All-time High!', 'All-time High!')
alertcondition(ta.crossunder(low, atl), 'All-time Low!',  'All-time Low!')

