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

如何在Pine Script v5中基于选中K线绘制动态水平线(Close+ATR(10))?

解决Pine Script v5中基于选中K线绘制动态水平线的问题

用plot()直接实现出现杂乱效果,是因为它会为每根K线计算并绘制对应值,而我们只需要基于光标选中的特定K线显示单条固定水平线。以下是针对性的实现方案:

代码实现与说明

//@version=5
indicator("选中K线的Close+ATR水平线", overlay=true)

// 1. 计算ATR(10)指标
atrValue = ta.atr(10)

// 2. 创建时间选择器,点击图表K线即可关联选中时间戳
selectedBarTime = input.time(title="选中K线时间", timestamp=timestamp("2024-01-01T00:00"), tooltip="点击目标K线完成选择")

// 3. 获取选中K线的收盘价和对应ATR值
var float selectedClose = na
var float selectedATR = na
if time == selectedBarTime
    selectedClose := close
    selectedATR := atrValue

// 4. 计算目标水平线数值
levelValue = selectedClose + selectedATR

// 5. 绘制水平线:用hline实现全图显示的固定线,或用plot实现从选中K线向右延伸的线
hline(na(levelValue) ? na : levelValue, title="Close + ATR(10)", color=color.red, linestyle=hline.style_dashed, linewidth=2)
// 若需要从选中K线开始向右延伸,替换上面的hline为下面的plot
// plot(na(levelValue) ? na : levelValue, title="Close + ATR(10)", color=color.red, style=plot.style_linebr, linewidth=2)

核心要点

  • 用input.time()绑定光标选中的K线时间戳,精准定位目标K线。
  • 通过变量缓存选中K线的收盘价和ATR值,避免重复计算导致的杂乱线条。
  • hline()会在整个图表范围内显示水平线,plot.style_linebr则仅从选中K线位置向右延伸,可根据需求选择。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 07:15:37