Neuracap Price Widget
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © NeuraCap

//@version=5
indicator('Neuracap Price Widget', shorttitle='NeuraCap Px widget', overlay=true)

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

weeks      = input(52,    "Number of Weeks")
show_ath   = input(true,  "Show High?")
show_atl   = input(true, "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)

// get pre-covid high and covid low

get_covid_high_low() =>
    var time1_arr = array.new_int(0)
    var high1_arr = array.new_float(0)
    var low1_arr  = array.new_float(0)
    
    ms_limit1 = 604800000 * 112 //112 weeks difference
    ms_limit2 = 604800000 * 125 //125 
      
    time1 = time - ms_limit1
    array.unshift(time1_arr, time1)
    array.unshift(high1_arr, high)
    array.unshift(low1_arr,  low)
    
    
    while (array.get(time1_arr, array.size(time1_arr) -1) < time1 - ms_limit2)
        array.pop(time1_arr)
        array.pop(high1_arr)
        array.pop(low1_arr)
    
    // getting pre-covid high and covid low   
    cvh = array.max(high1_arr)
    cvl = array.min(low1_arr)
    
    hi = 0
    while true
        if array.get(high1_arr, hi) == cvh
            break
        hi += 1
    
    li = 0
    while true
        if array.get(low1_arr, li) == cvl
            break
        li += 1
    

    [cvh, cvl]

[cvh, cvl] = request.security(syminfo.tickerid, '1D', get_covid_high_low())
// 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.top_right, 6, 5, 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, "$ Diff",   bgcolor = #cccccc)
    
    if (show_ath)
        // ATH
        table.cell(ATHtable, 0, 1, "Dist from High",                                                  bgcolor = #cccccc)
        table.cell(ATHtable, 1, 1, ath_time,                                               bgcolor = color.new(color.green, transp = 25))
        table.cell(ATHtable, 2, 1, str.tostring(ath_days),                                 bgcolor = color.new(color.green, transp = 25))
        table.cell(ATHtable, 3, 1, str.tostring(ath),                                      bgcolor = color.new(color.green, transp = 25))
        table.cell(ATHtable, 4, 1, str.tostring((1- (close/ath )) * 100 , "#.##") + "%",   bgcolor = color.new(color.green, transp = 25))
        table.cell(ATHtable, 5, 1, str.tostring(ath - close , "#.##") + "$",               bgcolor = color.new(color.green, transp = 25))
    
    if (show_atl)
        // ATL
        table.cell(ATHtable, 0, 2, "Dist from Low",                                                  bgcolor = #cccccc)
        table.cell(ATHtable, 1, 2, atl_time,                                               bgcolor = color.new(color.red, transp = 25))
        table.cell(ATHtable, 2, 2, str.tostring(atl_days),                                 bgcolor = color.new(color.red, transp = 25))
        table.cell(ATHtable, 3, 2, str.tostring(atl),                                      bgcolor = color.new(color.red, transp = 25))
        table.cell(ATHtable, 4, 2, str.tostring(((close / atl) - 1) * 100 , "#.##") + "%", bgcolor = color.new(color.red, transp = 25))
        table.cell(ATHtable, 5, 2, str.tostring(atl - close, "#.##") + "$",                bgcolor = color.new(color.red, transp = 25))

        table.cell(ATHtable, 0, 3, "Returns L2H",                                                  bgcolor = #cccccc)
        table.cell(ATHtable, 1, 3, "",                                                     bgcolor = color.new(color.white, transp = 25))
        table.cell(ATHtable, 2, 3, str.tostring(""),                                       bgcolor = color.new(color.white, transp = 25))
        table.cell(ATHtable, 3, 3, str.tostring(""),                                       bgcolor = color.new(color.white, transp = 25))
        table.cell(ATHtable, 4, 3, str.tostring(((ath/ atl) - 1) * 100 , "#.##") + "%",    bgcolor = color.new(color.white, transp = 25))
        table.cell(ATHtable, 5, 3, str.tostring(ath - atl, "#.##") + "$",                  bgcolor = color.new(color.white, transp = 25))

        // table.cell(ATHtable, 0, 4, "COVID H/L",                                                  bgcolor = #cccccc)
        // table.cell(ATHtable, 1, 4, "pre-cov Hi",                                            bgcolor = color.new(color.white, transp = 25))
        // table.cell(ATHtable, 2, 4, str.tostring(cvh),                                        bgcolor = color.new(color.white, transp = 25))
        // table.cell(ATHtable, 3, 4, str.tostring("covid low"),                               bgcolor = color.new(color.white, transp = 25))
        // table.cell(ATHtable, 4, 4, str.tostring(cvl),                                        bgcolor = color.new(color.white, transp = 25))
        // table.cell(ATHtable, 5, 4, str.tostring(""),                                         bgcolor = color.new(color.white, transp = 25))
// alerts
alertcondition(ta.crossover(high, ath), 'All-time High!', 'All-time High!')
alertcondition(ta.crossunder(low, atl), 'All-time Low!',  'All-time Low!')

