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

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

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.

'//@version=5

indicator(title='EMA1 label test', overlay=true)

labeloffset = input.int(4, maxval=500, title="Label Offset", group = "====================== Main Settings ======================", inline = '0')
tl = (bar_index + labeloffset)

//   first attmept:  tl = time+(time-time[labeloffset])

//   second attempt:  ms_in_one_minute = 60*1000
//   second attempt:  tl = time + (labeloffset * ms_in_one_minute)  // negative int will move back - but, positive int won't move forward.

//   third attempt: tl = (bar_index + labeloffset)
 
var bool show_hlines = input(true, 'Show horizontal lines', group = "====================== Main Settings ======================", inline = '0')
var bool show_extlines = not show_hlines

// Format for EMA 1 (9, chart)
res1 = input.timeframe(title='Timeframe', defval='1', group='═══════════ EMA 1 ═══════════')
ema1_tit_lab = input.string('9 EMA - Chart', title = 'Line Label', group='═══════════ EMA 1 ═══════════')
len1 = input(title='Length', defval=9, group='═══════════ EMA 1 ═══════════', inline = '11')
src1 = input(close, title='Source',group='═══════════ EMA 1 ═══════════', inline = '11')
col1 = input(color.rgb(19, 216, 1, 17), 'Color', group='═══════════ EMA 1 ═══════════', inline = '12')
wid1 = input.int(1,'Plot Width', maxval = 4, group='═══════════ EMA 1 ═══════════', inline='12')
// End of format for EMA 1

// Function to define MA's to enable altering time frame for Extended Lines/Labels
f_secureSecurity(_symbol, _res, _src) =>
    request.security(_symbol, _res, _src[1], lookahead = barmerge.lookahead_on)

ema1 = f_secureSecurity(syminfo.tickerid, res1, ta.ema(src1, len1))//Non Repainting
// End of Function

//  MA's Smoothing
ema1Smooth = request.security(syminfo.tickerid, res1, ema1[barstate.isrealtime ? 1 : 0], gaps=barmerge.gaps_on)
// End of Smoothing

// MA's plots
ema1_plot = plot(ema1Smooth, color = col1, linewidth=wid1, title = 'EMA 1')
// End of MA's plots

// ---  Variables

// Variables - color
var color_ema1 = col1
// End of Variables - color

// Variables - lines
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)
//End of Variables - lines

//Variables - labels
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)
// Attempted fix label by add "x=bar_index" without success)
// End of Variables - labels

// ---  End of Variables

// Function to move lines/lables
f_moveLine(_id, _x, _y) =>
    line.set_xy1(_id, _x, _y)
    line.set_xy2(_id, _x + 1, _y)
f_moveLabel(_id, _x, _y) =>
    label.set_xy(_id, _x, _y)

// End of function

// Move the lines/labels
if barstate.islast
    f_moveLine(line_ema1, time, ema1)
    f_moveLabel(label_ema1, tl, ema1)
// End of moving lines/labels

答案1

得分: 0

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

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)

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

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

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

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:

确定