如果 src [1] 已经是 pine 中的第一个,会发生什么情况

问题描述 投票:0回答:1
//@version=4
study(title="UT Bot Alerts", overlay = true)

// Inputs
a = input(1,     title = "Key Vaule. 'This changes the sensitivity'")
c = input(10,    title = "ATR Period")
h = input(false, title = "Signals from Heikin Ashi Candles")

xATR  = atr(c)
nLoss = a * xATR

src = h ? security(heikinashi(syminfo.tickerid), timeframe.period, close, lookahead = false) : close

> xATRTrailingStop = 0.0
> xATRTrailingStop := iff(src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), src - nLoss),
   iff(src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), src + nLoss), 
   iff(src > nz(xATRTrailingStop[1], 0), src - nLoss, src + nLoss)))
 
pos = 0   
pos :=  iff(src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0), 1,
   iff(src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0))) 
   
xcolor = pos == -1 ? color.red: pos == 1 ? color.green : color.blue 

ema   = ema(src,1)
above = crossover(ema, xATRTrailingStop)
below = crossover(xATRTrailingStop, ema)

buy  = src > xATRTrailingStop and above 
sell = src < xATRTrailingStop and below

barbuy  = src > xATRTrailingStop 
barsell = src < xATRTrailingStop 

plotshape(buy,  title = "Buy",  text = 'Buy',  style = shape.labelup,   location = location.belowbar, color= color.green, textcolor = color.white, transp = 0, size = size.tiny)
plotshape(sell, title = "Sell", text = 'Sell', style = shape.labeldown, location = location.abovebar, color= color.red,   textcolor = color.white, transp = 0, size = size.tiny)

barcolor(barbuy  ? color.green : na)
barcolor(barsell ? color.red   : na)

alertcondition(buy,  "UT Long",  "UT Long")
alertcondition(sell, "UT Short", "UT Short")

我在尝试将上述代码转换为本节中的Python代码时遇到问题<>xATRTrailingStop=0.0

XATRTrailingStop :=iff (src>nz (xATRTrailingStop [1, 0) 和 src [1]>nz (xATRTrailingStop [1, 0),> 我的问题是:如果 src 已经是初始值,那么访问 src [1] 会得到什么?这不是错误吗?

python pine-script tradingview-api quantitative-finance
1个回答
0
投票

如果是初始值,则引用其历史值将返回

NaN

您可以使用

fixnan()
NaN
值替换为之前最接近的非 NaN 值,或使用
nz()
NaN
值替换为一系列零(或给定值)。

这是一个演示:

//@version=5
indicator("My script", overlay=true)

src_1 = close[1]
src_2 = fixnan(close[1])
src_3 = nz(close[1])

plot(src_1, "src_1", color=color.green)
plot(src_2, "src_2", color=color.yellow)
plot(src_3, "src_3", color=color.red)

© www.soinside.com 2019 - 2024. All rights reserved.