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

如何将Pine Script指标1标签的bar偏移量作为指标2的回归通道长度输入?

问题:结合Pine Script两个指标,用最低点bar间隔作为回归通道长度参数

需求说明:

  • 指标1:识别lowLB周期内的最低点,在对应K线位置绘制标签显示价格
  • 指标2:回归通道指标,需要将指标1中最低点所在K线与当前K线的间隔数量作为lengthInput参数

原指标代码

指标1(最低点标签)

//@version=5
indicator("Test", "", true)

lowLB = input(20, "Low Lookback")

lo = ta.lowest(lowLB)
lowestBarOffset = - ta.lowestbars(lowLB)

// Create label on bar zero only.
var lbl = label.new(na, na, "", color = color.rgb(190, 190, 190), style=label.style_label_upper_right)
// When a new low is found, move the label there and update its text and tooltip.
if ta.change(lo)
    // Build label and tooltip strings.
    labelText = "Low: " + str.tostring(lo, format.mintick)
    tooltipText = "Offest in bars: " + str.tostring(lowestBarOffset) + "\nLow: " + str.tostring(low[lowestBarOffset], format.mintick)
    // Update the label's position, text and tooltip.
    label.set_xy(lbl, bar_index[lowestBarOffset], lo)
    label.set_text(lbl, labelText)
    label.set_tooltip(lbl, tooltipText)

指标2(回归通道片段)

lengthInput = input(<INPUT_FROM_FUNCTION_1>, title="Length")    <------- ?
sourceInput = input.source(close, title="Source")

group1 = "Channel Settings"
useUpperDevInput = input.bool(true, title="Upper Deviation", inline = "Upper Deviation", group = group1)
upperMultInput = input.float(2.0, title="", inline = "Upper Deviation", group = group1) 
.....

解决方案

不需要使用barstate,核心是跟踪最新最低点的K线索引,再计算其与当前K线的间隔。

关键步骤

  1. 新增变量存储最新最低点的K线索引:用var关键字定义,确保只初始化一次,每次找到新低点时更新
  2. 计算间隔数:用当前K线索引减去存储的低点K线索引,得到两者的间隔
  3. 处理初始值:未找到低点时,给lengthInput设置默认值(比如用lowLB)

整合后的完整代码

//@version=5
indicator("Low-Based Regression Channel", "", true)

// --- 指标1:最低点标签模块 ---
lowLB = input(20, "Low Lookback")

lo = ta.lowest(lowLB)
lowestBarOffset = - ta.lowestbars(lowLB)

// 存储最新最低点的bar索引(用var保持状态)
var int lowestBarIndex = na
// 找到新低点时更新索引
if ta.change(lo)
    lowestBarIndex := bar_index[lowestBarOffset]
    // 更新标签逻辑(保留原代码)
    var lbl = label.new(na, na, "", color = color.rgb(190, 190, 190), style=label.style_label_upper_right)
    labelText = "Low: " + str.tostring(lo, format.mintick)
    tooltipText = "Offest in bars: " + str.tostring(lowestBarOffset) + "\nLow: " + str.tostring(low[lowestBarOffset], format.mintick)
    label.set_xy(lbl, lowestBarIndex, lo)
    label.set_text(lbl, labelText)
    label.set_tooltip(lbl, tooltipText)

// --- 指标2:回归通道模块 ---
// 计算间隔数,初始未找到低点时用lowLB作为默认值
lengthInput = na(lowestBarIndex) ? lowLB : bar_index - lowestBarIndex
// 可选:保留手动输入开关,让用户切换自动/手动模式
// useAutoLength = input.bool(true, "Use Auto Length from Lowest Bar")
// manualLength = input.int(20, "Manual Length")
// lengthInput = useAutoLength ? (na(lowestBarIndex) ? lowLB : bar_index - lowestBarIndex) : manualLength

sourceInput = input.source(close, title="Source")

group1 = "Channel Settings"
useUpperDevInput = input.bool(true, title="Upper Deviation", inline = "Upper Deviation", group = group1)
upperMultInput = input.float(2.0, title="", inline = "Upper Deviation", group = group1)

// 回归通道核心逻辑(补充完整你的原有代码)
// 示例:线性回归计算
linReg = ta.linreg(sourceInput, lengthInput, 0)
dev = ta.stdev(sourceInput, lengthInput)
upperChannel = useUpperDevInput ? linReg + upperMultInput * dev : na
plot(upperChannel, color=color.new(color.blue, 0), title="Upper Channel")
plot(linReg, color=color.new(color.orange, 0), title="Mid Line")

代码说明

  • var int lowestBarIndex = na:持久化存储最新低点的K线索引,避免每次K线刷新重置
  • lengthInput = na(lowestBarIndex) ? lowLB : bar_index - lowestBarIndex:处理初始状态,未找到低点时用lowLB作为默认长度,找到后自动计算间隔
  • 可选的手动输入开关:如果需要允许用户切换自动/手动长度,取消注释对应代码即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 02:20:27