在 Pine 脚本中使用 label.new 打印标签时出错
Error while Printing a Label with label.new in Pine Script
我正在尝试根据价格行为在图表上打印动态标签。基本上,当方向改变时,我试图在图表上绘制该波的总累积量。下面是我尝试通过 if condition
使用 label.new() 函数的部分代码片段
if directionHasChanged or barstate.islast and not barstate.isrealtime or barstate.isrealtime and not barstate.isconfirmed
plotChar := true
if directionIsDown and plotChar
label.new(bar_index,high,style=label.style_none,text=volString,color=color.white)
但我收到脚本错误,内容为 -“'resolution' 参数与具有副作用的函数不兼容。”
可能是什么原因以及实现相同目标的可能解决方案或变通方法是什么。下面是我的脚本的完整代码。任何帮助表示赞赏。我现在几乎被困了几天
//@version=4
study("Weis Wave Chart", shorttitle="Weis", overlay=true, resolution="")
lblOffset = input(5, title="Label Offset")
method = input(defval="ATR", options=["ATR", "Traditional", "Part of Price"], title="Renko Assignment Method")
methodvalue = input(defval=14.0, type=input.float, minval=0, title="Value")
pricesource = input(defval="Close", options=["Close", "High / Low", "Open / Close"], title="Price Source")
useClose = pricesource == "Close"
useOpenClose = pricesource == "Open / Close" or useClose
useTrueRange = input(defval="Auto", options=["Always", "Auto", "Never"], title="Use True Range instead of Volume")
isOscillating = input(defval=false, type=input.bool, title="Oscillating")
normalize = input(defval=false, type=input.bool, title="Normalize")
vol = useTrueRange == "Always" or useTrueRange == "Auto" and na(volume) ? tr : volume
op = useClose ? close : open
hi = useOpenClose ? close >= op ? close : op : high
lo = useOpenClose ? close <= op ? close : op : low
if method == "ATR"
methodvalue := atr(round(methodvalue))
if method == "Part of Price"
methodvalue := close / methodvalue
currclose = float(na)
prevclose = nz(currclose[1])
prevhigh = prevclose + methodvalue
prevlow = prevclose - methodvalue
currclose := hi > prevhigh ? hi : lo < prevlow ? lo : prevclose
direction = int(na)
direction := currclose > prevclose ? 1 : currclose < prevclose ? -1 : nz(direction[1])
directionHasChanged = change(direction) != 0
directionIsUp = direction > 0
directionIsDown = direction < 0
extr = float(na)
fixnan_1 = fixnan(extr[1])
prevextr = directionHasChanged ? currclose : fixnan_1
extr := directionIsUp and hi >= prevextr ? hi : directionIsDown and lo <= prevextr ? lo : prevextr
barcount = 1
barcount := not directionHasChanged and normalize ? barcount[1] + barcount : barcount
vol := not directionHasChanged ? vol[1] + vol : vol
res = barcount > 1 ? vol / barcount : vol
volString = tostring(res)
f_calc_bar_time(offset) => ret = time + ((time-time[1]) * offset)
//var label = label.new(bar_index, na)
//label.set_text(label, "green")
//label.set_size(label, "10")
//label.set_textcolor(label, color.white)
//label.set_yloc(label, yloc.belowbar)
//label.set_style(label, label.style_none)
mLow = security(syminfo.tickerid, '5m', low[1], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
mHigh = security(syminfo.tickerid, '5m', high[1], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
//f_print_low(_txt) => var _lbl = label(na), label.delete(_lbl), _lbl := label.new(time + (time-time[1]) * 5, low, style=label.style_none,text=_txt,color=color.white)
//f_print_high(_txt) => var _lbl = label(na), label.delete(_lbl), _lbl := label.new(bar_index[0], high, style=label.style_none,text=_txt,color=color.white)
//if directionHasChanged
//if directionIsUp
//label.new(f_calc_bar_time(lblOffset), mLow, xloc=xloc.bar_time, text=tostring(res), style=label.style_none, color=color.white)
//if directionIsDown
//label.new(f_calc_bar_time(lblOffset), mHigh, xloc=xloc.bar_time, text=tostring(res), style=label.style_none, color=color.white)
var plotChar = false
var up = false
var down = false
var upLbl = 0
var dwnLbl = 0
if directionHasChanged or barstate.islast and not barstate.isrealtime or barstate.isrealtime and not barstate.isconfirmed
plotChar := true
if directionIsDown and plotChar
label.new(bar_index,high,style=label.style_none,text=volString,color=color.white)
if directionIsUp and plotChar
down := true
dwnLbl := 1
//plotchar(series=plotChar, color=color.white,char='3')
plotchar(series=up,color=color.white,location=location.abovebar, text="3")
plotchar(series=down,color=color.white,location=location.belowbar, text="3")
//label.new(upLbl,high,style=label.style_none,text=volString,color=color.white)
//label.new(dwnLbl,low,style=label.style_none,text=volString,color=color.white)
plotChar := false
up := false
down := false
upLbl := 0
dwnLbl := 0
plot(directionHasChanged or barstate.islast and not barstate.isrealtime or barstate.isrealtime and not barstate.isconfirmed ? extr[1] : na, offset=-1, color=#1155CC, transp=50, linewidth=2, title="Wave Line")
原因是您的 study()
声明中的 resolution=""
参数。
study("Weis Wave Chart", shorttitle="Weis", overlay=true, resolution="")
当您删除它时,脚本编译没有错误。
您必须将其更改为:
study("Weis Wave Chart", shorttitle="Weis", overlay=true)
我正在尝试根据价格行为在图表上打印动态标签。基本上,当方向改变时,我试图在图表上绘制该波的总累积量。下面是我尝试通过 if condition
使用 label.new() 函数的部分代码片段 if directionHasChanged or barstate.islast and not barstate.isrealtime or barstate.isrealtime and not barstate.isconfirmed
plotChar := true
if directionIsDown and plotChar
label.new(bar_index,high,style=label.style_none,text=volString,color=color.white)
但我收到脚本错误,内容为 -“'resolution' 参数与具有副作用的函数不兼容。”
可能是什么原因以及实现相同目标的可能解决方案或变通方法是什么。下面是我的脚本的完整代码。任何帮助表示赞赏。我现在几乎被困了几天
//@version=4
study("Weis Wave Chart", shorttitle="Weis", overlay=true, resolution="")
lblOffset = input(5, title="Label Offset")
method = input(defval="ATR", options=["ATR", "Traditional", "Part of Price"], title="Renko Assignment Method")
methodvalue = input(defval=14.0, type=input.float, minval=0, title="Value")
pricesource = input(defval="Close", options=["Close", "High / Low", "Open / Close"], title="Price Source")
useClose = pricesource == "Close"
useOpenClose = pricesource == "Open / Close" or useClose
useTrueRange = input(defval="Auto", options=["Always", "Auto", "Never"], title="Use True Range instead of Volume")
isOscillating = input(defval=false, type=input.bool, title="Oscillating")
normalize = input(defval=false, type=input.bool, title="Normalize")
vol = useTrueRange == "Always" or useTrueRange == "Auto" and na(volume) ? tr : volume
op = useClose ? close : open
hi = useOpenClose ? close >= op ? close : op : high
lo = useOpenClose ? close <= op ? close : op : low
if method == "ATR"
methodvalue := atr(round(methodvalue))
if method == "Part of Price"
methodvalue := close / methodvalue
currclose = float(na)
prevclose = nz(currclose[1])
prevhigh = prevclose + methodvalue
prevlow = prevclose - methodvalue
currclose := hi > prevhigh ? hi : lo < prevlow ? lo : prevclose
direction = int(na)
direction := currclose > prevclose ? 1 : currclose < prevclose ? -1 : nz(direction[1])
directionHasChanged = change(direction) != 0
directionIsUp = direction > 0
directionIsDown = direction < 0
extr = float(na)
fixnan_1 = fixnan(extr[1])
prevextr = directionHasChanged ? currclose : fixnan_1
extr := directionIsUp and hi >= prevextr ? hi : directionIsDown and lo <= prevextr ? lo : prevextr
barcount = 1
barcount := not directionHasChanged and normalize ? barcount[1] + barcount : barcount
vol := not directionHasChanged ? vol[1] + vol : vol
res = barcount > 1 ? vol / barcount : vol
volString = tostring(res)
f_calc_bar_time(offset) => ret = time + ((time-time[1]) * offset)
//var label = label.new(bar_index, na)
//label.set_text(label, "green")
//label.set_size(label, "10")
//label.set_textcolor(label, color.white)
//label.set_yloc(label, yloc.belowbar)
//label.set_style(label, label.style_none)
mLow = security(syminfo.tickerid, '5m', low[1], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
mHigh = security(syminfo.tickerid, '5m', high[1], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
//f_print_low(_txt) => var _lbl = label(na), label.delete(_lbl), _lbl := label.new(time + (time-time[1]) * 5, low, style=label.style_none,text=_txt,color=color.white)
//f_print_high(_txt) => var _lbl = label(na), label.delete(_lbl), _lbl := label.new(bar_index[0], high, style=label.style_none,text=_txt,color=color.white)
//if directionHasChanged
//if directionIsUp
//label.new(f_calc_bar_time(lblOffset), mLow, xloc=xloc.bar_time, text=tostring(res), style=label.style_none, color=color.white)
//if directionIsDown
//label.new(f_calc_bar_time(lblOffset), mHigh, xloc=xloc.bar_time, text=tostring(res), style=label.style_none, color=color.white)
var plotChar = false
var up = false
var down = false
var upLbl = 0
var dwnLbl = 0
if directionHasChanged or barstate.islast and not barstate.isrealtime or barstate.isrealtime and not barstate.isconfirmed
plotChar := true
if directionIsDown and plotChar
label.new(bar_index,high,style=label.style_none,text=volString,color=color.white)
if directionIsUp and plotChar
down := true
dwnLbl := 1
//plotchar(series=plotChar, color=color.white,char='3')
plotchar(series=up,color=color.white,location=location.abovebar, text="3")
plotchar(series=down,color=color.white,location=location.belowbar, text="3")
//label.new(upLbl,high,style=label.style_none,text=volString,color=color.white)
//label.new(dwnLbl,low,style=label.style_none,text=volString,color=color.white)
plotChar := false
up := false
down := false
upLbl := 0
dwnLbl := 0
plot(directionHasChanged or barstate.islast and not barstate.isrealtime or barstate.isrealtime and not barstate.isconfirmed ? extr[1] : na, offset=-1, color=#1155CC, transp=50, linewidth=2, title="Wave Line")
原因是您的 study()
声明中的 resolution=""
参数。
study("Weis Wave Chart", shorttitle="Weis", overlay=true, resolution="")
当您删除它时,脚本编译没有错误。
您必须将其更改为:
study("Weis Wave Chart", shorttitle="Weis", overlay=true)