Pine Script: How to move a label to the right of the last bar to a user selectable custom distance?

huangapple go评论88阅读模式
英文:

Pine Script: How to move a label to the right of the last bar to a user selectable custom distance?

问题

以下是您要翻译的内容:

Pine Script:我正在尝试将标签移到最后一根柱子的右侧。我有一个庞大的代码库,其中有许多绘图。我将其简化为仅用于排查此问题的必要内容。此特定问题与延伸到绘图右侧的线的标签相关。

意图是标签将位于线的上方,并偏移到最后一根柱子的右侧。该线将说明绘图/线所指示的内容。

问题是,无论我尝试了什么,都无法将标签移动到最后一根柱子的右侧(以便在图表上没有数据的区域更容易阅读)。某些策略会将标签/文本向左移动,但不会向右移动。

我尝试了许多不同的方法来移动标签/文本。在下面的代码中,我列出了三种不同的尝试。所有都使用“labeloffset”作为用户输入,以允许用户确定将标签移动多远到右边(在不同时间框架或使用空间的其他指标上使用时,如果不能调整标签可能占用的位置,则是必需的)。 "tl = "是我尝试用于移动标签/文本的代码。

我不确定这是否是附加在附加的代码底部附近的某种尝试问题,还是我弄乱了函数。我尝试了很多东西,但我记得第二次尝试的代码可以让我使用“labeloffset”的负整数将标签向左移动;但是,它不允许我将标签向右移动。

与偏移相关的代码位于顶部,而var/line/functions位于底部。

英文:

Pine Script: I'm attempting to move a label to the right of the last bar. I have a large code base with many plots. I pared this down to just what was needed to troubleshoot this one issue. This specific issue is related to the label for a line that extends to the right of a plot.
The intent is that a label will sit above the line and offset to the right of the last bar. The line would relate what the plot/line are designating.

The issue is that nothing I have tried will move the label to the right of the last bar (to make it easier to read in an area of the chart without any data). Some of the strategies will move the label/text to the left - but, not the right.

I tried many different methods to move the label/text. In the code below I list three different attempts. All use the "labeloffset" as a user input to allow the user to determine how far to the right to move the label (necessary in using the indicator on different time frames or with other indicators that use space the label might occupy if it couldn't be adjusted). The "tl =" is the code I attempted to use to move the label/text.

I'm not sure if it is an issue with one of my attempts listed at the top of the attached code or if I messed up the function near the bottom of the code. I've tried so many things - but, I do remember that the second attempt code would allow me to move the label to the left using a negative integer for "labeloffset"; however, it would not allow me to move the label to the right.

The code related to the offset is at the top and the var/line/functions are at the bottom.

  1. '//@version=5
  2. indicator(title='EMA1 label test', overlay=true)
  3. labeloffset = input.int(4, maxval=500, title="Label Offset", group = "====================== Main Settings ======================", inline = '0')
  4. tl = (bar_index + labeloffset)
  5. // first attmept: tl = time+(time-time[labeloffset])
  6. // second attempt: ms_in_one_minute = 60*1000
  7. // second attempt: tl = time + (labeloffset * ms_in_one_minute) // negative int will move back - but, positive int won't move forward.
  8. // third attempt: tl = (bar_index + labeloffset)
  9. var bool show_hlines = input(true, 'Show horizontal lines', group = "====================== Main Settings ======================", inline = '0')
  10. var bool show_extlines = not show_hlines
  11. // Format for EMA 1 (9, chart)
  12. res1 = input.timeframe(title='Timeframe', defval='1', group='═══════════ EMA 1 ═══════════')
  13. ema1_tit_lab = input.string('9 EMA - Chart', title = 'Line Label', group='═══════════ EMA 1 ═══════════')
  14. len1 = input(title='Length', defval=9, group='═══════════ EMA 1 ═══════════', inline = '11')
  15. src1 = input(close, title='Source',group='═══════════ EMA 1 ═══════════', inline = '11')
  16. col1 = input(color.rgb(19, 216, 1, 17), 'Color', group='═══════════ EMA 1 ═══════════', inline = '12')
  17. wid1 = input.int(1,'Plot Width', maxval = 4, group='═══════════ EMA 1 ═══════════', inline='12')
  18. // End of format for EMA 1
  19. // Function to define MA's to enable altering time frame for Extended Lines/Labels
  20. f_secureSecurity(_symbol, _res, _src) =>
  21. request.security(_symbol, _res, _src[1], lookahead = barmerge.lookahead_on)
  22. ema1 = f_secureSecurity(syminfo.tickerid, res1, ta.ema(src1, len1))//Non Repainting
  23. // End of Function
  24. // MA's Smoothing
  25. ema1Smooth = request.security(syminfo.tickerid, res1, ema1[barstate.isrealtime ? 1 : 0], gaps=barmerge.gaps_on)
  26. // End of Smoothing
  27. // MA's plots
  28. ema1_plot = plot(ema1Smooth, color = col1, linewidth=wid1, title = 'EMA 1')
  29. // End of MA's plots
  30. // --- Variables
  31. // Variables - color
  32. var color_ema1 = col1
  33. // End of Variables - color
  34. // Variables - lines
  35. var line_ema1 = line.new(x1=na, y1=na, x2=na, y2=na, xloc=xloc.bar_time, extend=extend.right, color=show_hlines ? color_ema1 : na, style=line.style_dashed)
  36. //End of Variables - lines
  37. //Variables - labels
  38. var label_ema1 = label.new(x=na, y=na, text="Test", xloc=xloc.bar_time, color=show_hlines ? color_ema1 : na, textcolor=show_hlines ? color_ema1 : na, style=label.style_none)
  39. // Attempted fix label by add "x=bar_index" without success)
  40. // End of Variables - labels
  41. // --- End of Variables
  42. // Function to move lines/lables
  43. f_moveLine(_id, _x, _y) =>
  44. line.set_xy1(_id, _x, _y)
  45. line.set_xy2(_id, _x + 1, _y)
  46. f_moveLabel(_id, _x, _y) =>
  47. label.set_xy(_id, _x, _y)
  48. // End of function
  49. // Move the lines/labels
  50. if barstate.islast
  51. f_moveLine(line_ema1, time, ema1)
  52. f_moveLabel(label_ema1, tl, ema1)
  53. // End of moving lines/labels

答案1

得分: 0

你试图使用bar_index来调整时间,同时将xloc设置为bar_time。我从这一行中移除了xloc = bar_time

  1. var label_ema1 = label.new(x=na, y=na, text="Test", color=show_hlines ? color_ema1 : na, textcolor=show_hlines ? color_ema1 : na, style=label.style_none)

现在,你的标签和偏移使用了你的第三次尝试:

  1. tl = (bar_index + labeloffset)

Pine Script: How to move a label to the right of the last bar to a user selectable custom distance?

英文:

You were attempting to use bar_index to adjust the time while having the xloc set to bar_time. I removed the xloc = bar_time from this line

  1. var label_ema1 = label.new(x=na, y=na, text="Test", color=show_hlines ? color_ema1 : na, textcolor=show_hlines ? color_ema1 : na, style=label.style_none)

and now your label and offset work using your third attempt

  1. tl = (bar_index + labeloffset)

Pine Script: How to move a label to the right of the last bar to a user selectable custom distance?

huangapple
  • 本文由 发表于 2023年6月1日 02:34:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定