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

Pine Script 5:如何添加可自定义的历史线条显示控制参数?

解决Pine Script 5中控制十字星历史线条显示数量的问题

以下是修正后的完整代码,可实现自定义显示历史十字星线条数量的需求:

//@version=5
indicator("D Lines", shorttitle="D Lines", overlay=true)

// 输入参数
LineLongInput = input.int(25, title="Line Length")
LineColorInput = input.color(color.rgb(19, 132, 117), title="Line Color")
LineWidthInput = input.int(1, title="Line Width")
HistoryLength = input.int(20, title="Historical Bars", minval=1)

// 存储线条对象的持久化数组
var line[] dojiLines = array.new_line()

drawHorizontalLineFromDoji(lineLong, lineColor, lineWidth, historyLimit) =>
    // 判断当前K线是否为十字星
    dojiSize = math.abs(open - close)
    isDoji = dojiSize < (high - low) * 0.1

    if isDoji
        dojiBodyMid = (open + close) / 2
        // 绘制新的水平线条
        newLine = line.new(x1=bar_index, y1=dojiBodyMid, x2=bar_index + lineLong, y2=dojiBodyMid, width=lineWidth, color=lineColor)
        // 将新线条加入数组
        array.push(dojiLines, newLine)
        
        // 当线条数量超过限制时,删除最旧的线条
        if array.size(dojiLines) > historyLimit
            oldestLine = array.shift(dojiLines)
            line.delete(oldestLine)

// 调用绘制函数,传入正确的参数
drawHorizontalLineFromDoji(LineLongInput, LineColorInput, LineWidthInput, HistoryLength)

关键修改说明

  • 用数组管理线条生命周期:创建持久化数组存储所有绘制的线条对象,实现对线条的追踪和批量管理。
  • 修正参数传递逻辑:将LineLongInput作为线条长度传入绘制函数,HistoryLength作为历史线条数量限制传入,解决原代码参数传错的问题。
  • 动态清理旧线条:每次绘制新线条后,检查数组长度是否超过设定的历史数量,若超过则删除最旧的线条,确保界面只保留最近的N条十字星线条。
  • 精简冗余变量:移除原函数中无需持久化的lastDojiIndex和lastDojiBodyMid变量,简化逻辑结构。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 11:13:17