2021年3月13日星期六

Printing a Label with label.new in Pine Script

I am trying to print a dynamic Label on my chart depending on the price behavior. Basically I am trying to plot the total cumulative volume of that wave on chart when the direction changes. Below is the part code snippet where I try to use the label.new() function through if condition

    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)  

But I am getting script error that reads - "The 'resolution' argument is incompatible with functions that have side effects."

What could be the reason and what is the possible solution or workaround to achieve the same. Below is the entire code of my script. Any help is appreciated. I am almost stuck for couple of days now

//@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")  
https://stackoverflow.com/questions/66620420/printing-a-label-with-label-new-in-pine-script March 14, 2021 at 10:05AM

没有评论:

发表评论