Search code examples
labelpine-scriptlineoffset

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


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

Solution

  • 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)
    

    label printing