PineScript/TradingView指标:价格线刻度显示价格难题
实现ATH/ATL价格线显示价格标签
要在ATH和ATL的水平线上显示价格标签(类似TradingView默认线条的效果),你需要使用label.new()函数添加文本标签,因为line.new()本身不支持内置价格显示。以下是修改后的完整代码,同时优化了线条创建逻辑以提升性能:
//@version=5 indicator('All-Time High/Low', shorttitle='ATH/ATL', overlay=true) //////////// // 输入参数 // show_ath = input(true, "显示历史最高价?") show_atl = input(true, "显示历史最低价?") show_table = input(false, "显示统计表格?") /////////////// // 自定义函数 // // 获取历史最高价及日期 get_all_time_high() => h = 0.0 t = 0 h := bar_index == 0 ? high : high > h[1] ? high : h[1] t := bar_index == 0 ? time : high > h[1] ? time : t[1] [h, t] // 获取历史最低价及日期 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, ath_dt] = request.security(syminfo.tickerid, 'D', get_all_time_high()) [atl, atl_dt] = request.security(syminfo.tickerid, 'D', get_all_time_low()) ath_days = math.round((timenow - ath_dt) / 86400000) atl_days = math.round((timenow - atl_dt) / 86400000) // 绘制线条及价格标签 if show_ath // 初始化ATH线条(仅执行一次) var line lATH = line.new(bar_index - 1, ath, bar_index, ath, extend = extend.both, color = color.orange, style = line.style_dashed) // 更新线条位置(当ATH变化时自动更新) line.set_xy1(lATH, bar_index - 1, ath) line.set_xy2(lATH, bar_index, ath) // 初始化ATH价格标签(仅执行一次) var label ath_label = label.new(bar_index, ath, str.tostring(ath, format.mintick), color=color.orange, textcolor=color.white, style=label.style_label_left, size=size.small) // 更新标签位置和文本 label.set_xy(ath_label, bar_index, ath) label.set_text(ath_label, str.tostring(ath, format.mintick)) if show_atl // 初始化ATL线条(仅执行一次) var line lATL = line.new(bar_index - 1, atl, bar_index, atl, extend = extend.both, color = color.orange, style = line.style_dashed) // 更新线条位置 line.set_xy1(lATL, bar_index - 1, atl) line.set_xy2(lATL, bar_index, atl) // 初始化ATL价格标签(仅执行一次) var label atl_label = label.new(bar_index, atl, str.tostring(atl, format.mintick), color=color.orange, textcolor=color.white, style=label.style_label_left, size=size.small) // 更新标签位置和文本 label.set_xy(atl_label, bar_index, atl) label.set_text(atl_label, str.tostring(atl, format.mintick)) 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)) // 表头 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) // 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, format.mintick), 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 , 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)) // 警报设置 alertcondition(ta.crossover(high, ath), '历史新高!', '历史新高!') alertcondition(ta.crossunder(low, atl), '历史新低!', '历史新低!')
关键修改说明
- 使用
var关键字初始化线条和标签,避免每根K线重复创建对象,提升脚本性能 - 为ATH/ATL线条添加对应的价格标签,标签会自动跟随线条位置更新,并以最小刻度格式显示价格
- 标签采用左对齐样式,固定在最新K线右侧,与TradingView默认线条的价格显示效果一致
内容的提问来源于stack exchange,提问作者KangarooJack77
相关产品推荐
相关产品推荐

