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

Pine Script中如何实现水平线条单拖拽点同步调整时间与价格?

Pine Script 实现单拖拽点同步调整水平线的时间与价格

在Pine Script指标开发中,我需要用水平线计算交易信号,要求用户能随时放置、重新定位这条线。但Pine Script无法访问用户手动绘制的图形,所以我用价格输入和时间输入来绘制水平线,配置后可以通过图表上的拖拽点调整位置,但现在需要分两步分别调整价格(Y轴)和时间(X轴),想知道能不能改成单拖拽点同步调整两者?当前使用代码如下:

//@version=5
indicator(title="Horizontal line", overlay=true)

// HORIZONTAL LINE
hLinePrice                  = input.price( 0, title="Horizontal line price" )
hLineTimeStart              = input.time( timestamp("26 July 2024 15:00 -0500"), title="Horizontal line time")
hLineBarsBack               = (time - hLineTimeStart) / (1000 * timeframe.in_seconds(timeframe.period))

var line horizontalLine     = na

if not barstate.isconfirmed or (barstate.isrealtime and barstate.islast and not barstate.isconfirmed)
    horizontalLine := line.new(x1=bar_index[hLineBarsBack], y1=hLinePrice, x2=bar_index, y2=hLinePrice, width=1, extend=extend.right)
    
    line.set_color(id=horizontalLine, color=color.black)
    line.set_style(id=horizontalLine, style=line.style_solid)

if barstate.isconfirmed
    line.delete(id=horizontalLine)

解决方案

可以通过可拖拽锚点线条+实时坐标同步的方式实现单拖拽点调整,核心是用一个单点线条作为唯一操作锚点,拖拽它时同步更新时间和价格输入,再基于锚点位置绘制延伸水平线。修改后的代码如下:

//@version=5
indicator(title="Single Drag Horizontal Line", overlay=true)

// 初始化输入(仅作为初始值,后续会被拖拽结果覆盖)
initPrice = input.price(0, title="Initial Line Price")
initTime = input.time(timestamp("26 July 2024 15:00 -0500"), title="Initial Line Time")

// 创建可拖拽的锚点线条(起点终点重合,方便拖拽)
var line anchorLine = line.new(
    x1=ta.barssince(time == initTime), 
    y1=initPrice, 
    x2=ta.barssince(time == initTime), 
    y2=initPrice, 
    width=3, 
    color=color.new(color.blue, 0), 
    style=line.style_circles
)

// 实时同步锚点坐标到时间和价格变量
var float currentLinePrice = initPrice
var int currentLineBarIndex = ta.barssince(time == initTime)

if barstate.isrealtime and barstate.islast
    // 获取拖拽后的锚点坐标
    currentLineBarIndex := line.get_x1(anchorLine)
    currentLinePrice := line.get_y1(anchorLine)

// 绘制向右延伸的水平线
var line mainLine = na
if barstate.isrealtime and barstate.islast
    line.delete(mainLine)
    mainLine := line.new(
        x1=currentLineBarIndex, 
        y1=currentLinePrice, 
        x2=bar_index, 
        y2=currentLinePrice, 
        width=1, 
        color=color.black, 
        extend=extend.right
    )

// 确保锚点线条在周期切换后保持正确位置
if barstate.islast
    line.set_x1(anchorLine, currentLineBarIndex)
    line.set_x2(anchorLine, currentLineBarIndex)
    line.set_y1(anchorLine, currentLinePrice)
    line.set_y2(anchorLine, currentLinePrice)

关键逻辑说明

  • 锚点线条:用圆形样式的单点线条作为唯一拖拽操作点,用户拖拽这个点就能同时调整时间(X轴)和价格(Y轴)
  • 实时同步:在实时行情的最后K线,通过line.get_x1()和line.get_y1()获取拖拽后的坐标,直接同步到时间和价格变量
  • 动态更新水平线:基于锚点的最新坐标重新绘制向右延伸的水平线,保证线条位置与拖拽操作完全同步
  • 周期兼容性:最后一段代码确保切换图表周期后,锚点线条能自动保持在正确位置

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 06:35:59