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

如何将线条绘制在Pivot Low的高点而非其低点?

问题

我希望将线条绘制在Pivot Low的高点,而非Pivot Low的低点,请问该如何实现?

原代码:

//@version=5
indicator("Pivot crosses", "", true)

int activeLevelsInput = input(10)
int pivotLegsInput = input(5)

// When a pivot occurs, create a line and save it.
float pLo = ta.pivotlow(pivotLegsInput, pivotLegsInput)

var pivotLines = array.new<line>(activeLevelsInput)

if not na(pLo)
    line newPivotLine = line.new(bar_index - pivotLegsInput, pLo, bar_index, pLo)
    array.push(pivotLines, newPivotLine)

    // Delete the oldest line.
    line.delete(array.shift(pivotLines))

// Extend the lines.
if barstate.islast and barstate.isnew
    for pivotLine in pivotLines
        line.set_x2(pivotLine, bar_index)

// Check for crosses.
bool cross = false

for pivotLine in pivotLines
    float linePrice = line.get_y1(pivotLine)
    bool newCrossUp = close[1] < linePrice and close > linePrice
    bool newCrossDn = close[1] > linePrice and close < linePrice
    cross := cross or newCrossUp or newCrossDn

// Alert.

if cross
    alert("A cross has occurred")

plotchar(cross, "Cross", "X")
解决方案

要实现把线条画在Pivot Low对应K线的高点,只需修改线条创建时的价格参数:

  • ta.pivotlow() 返回的是Pivot Low的低点价格,我们需要获取这个Pivot Low所在K线的高点。Pivot Low出现在当前K线往前pivotLegsInput根的位置,这个位置的高点可以用high[pivotLegsInput]直接获取。
  • 将创建线条时的pLo替换为high[pivotLegsInput]即可。

修改后的完整代码:

//@version=5
indicator("Pivot crosses", "", true)

int activeLevelsInput = input(10)
int pivotLegsInput = input(5)

// 检测Pivot Low
float pLo = ta.pivotlow(pivotLegsInput, pivotLegsInput)
var pivotLines = array.new<line>(activeLevelsInput)

if not na(pLo)
    // 获取Pivot Low所在K线的高点价格
    float pivotHighPrice = high[pivotLegsInput]
    // 基于高点价格创建水平线条
    line newPivotLine = line.new(bar_index - pivotLegsInput, pivotHighPrice, bar_index, pivotHighPrice)
    array.push(pivotLines, newPivotLine)

    // 删除最早的线条,保持数量不超过设定值
    line.delete(array.shift(pivotLines))

// 延长线条到最新K线
if barstate.islast and barstate.isnew
    for pivotLine in pivotLines
        line.set_x2(pivotLine, bar_index)

// 检测价格穿越线条
bool cross = false

for pivotLine in pivotLines
    float linePrice = line.get_y1(pivotLine)
    bool newCrossUp = close[1] < linePrice and close > linePrice
    bool newCrossDn = close[1] > linePrice and close < linePrice
    cross := cross or newCrossUp or newCrossDn

// 触发警报
if cross
    alert("价格穿越了Pivot Low高点水平线")

plotchar(cross, "Cross", "X")

注:我把警报文本改成了中文,方便识别,不需要的话可以改回原英文。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 07:40:24