如何在 Pine 脚本中为 line.new 函数创建警报?

How to create alert for line.new function in Pine Script?

我在 TradingView 中创建了一个 pine 脚本,用于在枢轴之间绘制线,就像具有斜率且非水平的趋势线。现在我想为这些线添加警报,以便当价格越过最后一条线时我可以得到警报。这是示例代码:

line upper = line.new(x1, y1, x2, y2, extend=extend.right, color=color1, width=width1)
line lower = line.new(x1, y1, x2, y2, extend=extend.right, color=color2, width=width1)

那么如何向这一行添加警报?

您可以使用 line.get_*() 函数获取该行的值。如果您希望获得特定柱线的价格值,您需要获得使用 line.get_y2() 函数计算的 y2 值。

这是一个示例代码:

//@version=5

indicator("My script", overlay=true)

upperY = high[1] * 1.01
lowerY = low[1] * 0.99

line upper = line.new(bar_index - 1, upperY[1], bar_index, upperY)
line lower = line.new(bar_index - 1, lowerY[1], bar_index, lowerY)

upperYvalue = line.get_y2(upper)
lowerYvalue = line.get_y2(lower)

if ta.crossover(close, upperYvalue)
    label.new(x=bar_index, y=high * 1.07, text=str.tostring(upperYvalue), color=color.green)


if ta.crossunder(close, lowerYvalue)
    label.new(x=bar_index, y=low * 0.93, text=str.tostring(upperYvalue), color=color.red, style=label.style_label_up)