Pine script Pine Script TradingView中带有if语句的line.new时不适用值

Pine script Pine Script TradingView中带有if语句的line.new时不适用值,pine-script,algorithmic-trading,Pine Script,Algorithmic Trading,我想创建一个指标,显示收盘价何时越过趋势线 这是密码, 此处“趋势线交叉”应在交叉条上显示1.00,在其他条上显示0.00。但实际上,这只在最后一条(和连续区域)上显示0.00,在其他条上显示n/a lineObj = if (syminfo.tickerid == "BINANCE:SRMUSDT") if (barstate.islast) line.new(x1=3380-89, y1=0.9609, x2=3380 , y2=1.0216)

我想创建一个指标,显示收盘价何时越过趋势线

这是密码, 此处“趋势线交叉”应在交叉条上显示1.00,在其他条上显示0.00。但实际上,这只在最后一条(和连续区域)上显示0.00,在其他条上显示n/a

lineObj = if (syminfo.tickerid == "BINANCE:SRMUSDT")
    if (barstate.islast)
        line.new(x1=3380-89, y1=0.9609, x2=3380 , y2=1.0216)

line.set_extend(id=lineObj, extend=extend.right)
trendLine = line.get_price(lineObj, 0)

trendLineCrossover = crossover(close, trendLine)
plotshape(trendLineCrossover, title="trendLineCrossover", color=color.purple, style=shape.xcross)

如何修复代码以显示预期结果?谢谢。

在这里,我们在编号为3380的
x2
条上绘制您的线条,并在每个条上评估的条件中包括
barstate.islast
,因为将您的线条绘制调用包含在
if
块内将无法检测到交叉:

版本1

谢谢@pinecoders-lucf,这应该是可行的,但在图表上,12月27日23:00,1h BINANCE:SRMUSDT,这实际上是跨越了趋势线,正如你也可以看到的,但“trendLineCrossover”在条形图上显示我0。您能解释一下为什么会发生这种情况吗?这是因为您的
trendLineCrossover
变量包含
barstate.islast
的要求为true,因此只会检测数据集最后一个条带中出现的交叉。您可以尝试删除
barstate.islast
,看看您是否更喜欢这样的脚本。thansk@pinecoders lucf,但我将行更改为
trendLineCrossover:=交叉(关闭,趋势线)
,但现在我只能在bar_索引==3380(以及后面的那些)上看到0,而对于包括过度交叉条在内的以前的行,我只能看到n/a。对不起,也许是我这边的问题?我真的很感谢你的帮助,我帮了你一个忙。简化了一些内容,但添加了一些调试图,以帮助显示发生了什么。请记住,当您绘制线时,会从89条开始往回画,因此,尽管在该点之前的89条线上有一条线,
line.get\u price()
无法在那里返回值,因为脚本在这些条上计算时,该线不存在。银色背景现在只在你画线时开始,而不是从线的开始。谢谢@pinecoders lucf,我理解这一点并解决了我的问题
plotchar(bar\u index,“bar\u index”,“location.top,size=size.tiny)
bgcolor(trendLine?color.silver:na)
对我帮助很大。所以基本上,绘制的趋势线可以用于前向条带,但不能用于后向条带,是吗?我发现这有点复杂,但不管怎样还是解决了。
//@version=4
study("", "", true, max_bars_back = 5000)
var line lineObj = na
trendLineCrossover = false
if (syminfo.tickerid == "BINANCE:SRMUSDT") and bar_index == 3380
    lineObj := line.new(x1=bar_index-89, y1=0.9609, x2=bar_index , y2=1.0216, extend=extend.right)
        
trendLine = line.get_price(lineObj, bar_index)
trendLineCrossover := barstate.islast and crossover(close, trendLine)
plotshape(trendLineCrossover, title="trendLineCrossover", color=color.purple, style=shape.xcross)

// For validation only.
c = close < trendLine
plotchar(c, "c", "•", location.top, size = size.tiny)

// Show bg starting at the bar where we draw our line.
bgcolor(bar_index > 3380-89 ? color.silver : na)
//@version=4
study("", "", true)
var line lineObj = na
if (syminfo.tickerid == "BINANCE:SRMUSDT") and bar_index == 3380
    lineObj := line.new(x1=bar_index-89, y1=0.9609, x2=bar_index , y2=1.0216, extend=extend.right)
        
trendLine = line.get_price(lineObj, bar_index)
trendLineCrossover = crossover(close, trendLine)
plotshape(trendLineCrossover, title="trendLineCrossover", color=color.purple, style=shape.xcross)

// —————— For validation only.
// Bar no.
plotchar(bar_index, "bar_index", "", location.top, size = size.tiny)
// Plots the value returned by line.get_price() so you can see when it becomes available.
plot(trendLine, "trendLine", color.blue, 6, transp = 60)
// Show bg starting at the bar where where the line is drawn.
bgcolor(bar_index > 3380 ? color.silver : na)