Warm tip: This article is reproduced from serverfault.com, please click

algorithmic trading-在 Pine Script TradingView 中带有 if 语句的 line.new 时的值

(algorithmic trading - n/a value when line.new with if statement in Pine Script TradingView)

发布于 2020-12-30 16:11:50

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

这是代码,这里的“trendLineCrossover”应该在交叉条上显示 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)

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

Questioner
pupsozeyde
Viewed
0
PineCoders-LucF 2021-01-01 21:05:59

在这里,我们在x23380号柱上绘制你的线,barstate.islast在每个柱上评估的条件中都包括在内,因为将你的线图调用包含在if块内将无法检测到交叉点:

版本1

//@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)

在此处输入图片说明

版本2

//@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)

在此处输入图片说明