You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Pine Script指标获取非近期的历史最高价(ATH)数据

修改思路

原有脚本仅存储最新一次ATH的价格和时间,要实现历史时间段ATH筛选,需要调整核心逻辑:

  • 用数组存储所有历史产生的ATH的价格与对应时间戳
  • 新增时间筛选规则,优先匹配距今5-10年区间的ATH,无符合结果时自动取距今1年以上的最近ATH
  • 保留原有绘图、表格展示、告警能力,适配筛选后的ATH参数

修改后完整脚本

indicator('历史区间 All-Time High/Low', shorttitle='区间ATH/ATL', overlay=true)

////////////
// INPUTS //
show_ath   = input(true,  "显示历史ATH?")
show_atl   = input(false, "显示历史ATL?")
show_table = input(true,  "显示数据统计表格?")
// 新增时间筛选参数
min_ath_age = input.int(365, title="ATH最小距今天数(至少1年)", minval=30)
prefer_ath_low_age = input.int(1825, title="优先筛选ATH最小距今天数(5年)", minval=365)
prefer_ath_high_age = input.int(3650, title="优先筛选ATH最大距今天数(10年)", minval=1825)

///////////////
// FUNCTIONS //
// 重写ATH获取函数,返回所有历史ATH数组
get_all_ath_list() =>
    var float ath_val = na
    var array<float> ath_prices = array.new_float()
    var array<int> ath_times = array.new_int()
    // 当新高出现时存入数组
    if bar_index == 0
        ath_val := high
        array.push(ath_prices, high)
        array.push(ath_times, time)
    else if high > ath_val
        ath_val := high
        array.push(ath_prices, high)
        array.push(ath_times, time)
    [ath_prices, ath_times]

// 原ATL函数可按需按相同逻辑修改,本次优先实现ATH需求
get_all_time_low() =>
    l = 0.0
    t = 0
    l := bar_index == 0 ? low  : low < l[1] ? low  : l[1]
    t := bar_index == 0 ? time : low < l[1] ? time : t[1]
    [l, t]

// 获取所有ATH数据
[all_ath_prices, all_ath_times] = request.security(syminfo.tickerid, 'D', get_all_ath_list())
[atl, atl_dt] = request.security(syminfo.tickerid, 'D', get_all_time_low())

// 筛选符合要求的ATH
selected_ath = na
selected_ath_dt = na
// 先遍历找5-10年区间的ATH
for i = array.size(all_ath_times) - 1 to 0 by -1
    dt = array.get(all_ath_times, i)
    days_ago = math.round((timenow - dt) / 86400000)
    if days_ago >= prefer_ath_low_age and days_ago <= prefer_ath_high_age
        selected_ath := array.get(all_ath_prices, i)
        selected_ath_dt := dt
        break
// 没有找到5-10年的,就找1年以上的最近的ATH
if na(selected_ath)
    for i = array.size(all_ath_times) - 1 to 0 by -1
        dt = array.get(all_ath_times, i)
        days_ago = math.round((timenow - dt) / 86400000)
        if days_ago >= min_ath_age
            selected_ath := array.get(all_ath_prices, i)
            selected_ath_dt := dt
            break

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

// plotting
if show_ath and not na(selected_ath)
    lATH=line.new(bar_index - 1, selected_ath, bar_index, selected_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(selected_ath_dt)) + "-" + str.tostring(month(selected_ath_dt)) + "-" + str.tostring(dayofmonth(selected_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, "产生时间",    bgcolor = #cccccc)
    table.cell(ATHtable, 2, 0, "距今天数", bgcolor = #cccccc)
    table.cell(ATHtable, 3, 0, "价格",    bgcolor = #cccccc)
    table.cell(ATHtable, 4, 0, "当前距离占比",   bgcolor = #cccccc)
    table.cell(ATHtable, 5, 0, "当前距离绝对值",   bgcolor = #cccccc)
    
    if (show_ath and not na(selected_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(selected_ath, format.mintick),                      bgcolor = color.new(color.green, transp = 50))
        table.cell(ATHtable, 4, 1, str.tostring(((selected_ath / close) - 1) * 100 , "#.##") + "%", bgcolor = color.new(color.green, transp = 50))
        table.cell(ATHtable, 5, 1, str.tostring(selected_ath - close , format.mintick),             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, format.mintick),                      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, format.mintick),              bgcolor = color.new(color.red, transp = 50))

// alerts 适配筛选后的ATH
alertcondition(ta.crossover(high, selected_ath), '突破历史区间ATH!', '突破历史区间ATH!')
alertcondition(ta.crossunder(low, atl), 'All-time Low!',  'All-time Low!')

核心调整说明

  • 新增了可自定义的时间筛选参数,可按需调整最小距今天数、优先筛选的时间区间
  • 用数组存储所有历史ATH记录,避免只保留最新值
  • 筛选逻辑默认优先取5-10年区间的最近一次ATH,无符合结果时自动取1年以上的最近一次ATH
  • 告警逻辑同步适配筛选后的ATH值,不会被新产生的短期ATH影响

内容的提问来源于stack exchange,提问作者Alex Fx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 22:54:06