Search code examples
labelpine-scriptpine-script-v5moving-average

how to plot a label next to the last visible value of the moving average?


I want to plot a text label next to a moving average, but i want the label to continue being shown next to the moving average while I move the chart to left

I tried

//@version=5
indicator("My script", overlay = true)

ema = ta.sma(close, 200)
_text = label.new(chart.right_visible_bar_time, ema, "media", style = label.style_label_right, xloc = xloc.bar_time, color = color.rgb(36, 145, 63), textcolor = color.white)
label.delete(_text[1])
plot(ema)

the label is shown at the last price of the moving average, but as I move the chart to the left, the label does not follow the Y average position

how it works

how I would like it to work


Solution

  • What you are asking is not possible as you cannot get the moving average value of the last visible bar.

    However, you can get a similar effect if you enable "Indicators and financials name" and "Indicators and financials value" from the settings.

    enter image description here