4

i write pine with Tradingview and each time there is a new bar my text is being duplicated. i need the label to move to the new bar only.

//@version=4
study("My Script")

plot(close)

label.new (barstate.islast ? time : na,close,text="test",xloc=xloc.bar_time)
ArunPratap
  • 4,816
  • 7
  • 25
  • 43
Oren Teich
  • 47
  • 1
  • 4

4 Answers4

6

This code first deletes the last label created and then creates a new one, which will in turn be deleted on the next bar, so you're always left with only one label:

//@version=4
study("")
var label lbl = na
label.delete(lbl)
// Original line.
// lbl := label.new (barstate.islast ? time : na,close,text="test",xloc=xloc.bar_time)
// Place label 4 bars back.
lbl := label.new (barstate.islast ? bar_index - 4 : na,close,text="test",xloc=xloc.bar_index)
PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
  • Another question you might help me, how do i get the last candle index number ? – Oren Teich Nov 05 '19 at 09:46
  • You can use `last = not barstate.isconfirmed ? bar_index : na`. The index starts at 0 on the dataset's first bar, so actual number of bars in dataset is that value + 1. – PineCoders-LucF Nov 05 '19 at 14:57
  • THANKS :-) im trying to display label 5 bars back from the active bar. i tried: last = not barstate.isconfirmed ? bar_index : na label.new (bar_index==last-5 ? bar_index:na,0,text="X",xloc = xloc.bar_index) – Oren Teich Nov 05 '19 at 18:44
  • Very cool !another question, how do turn a specific value in series to be single variable ? – Oren Teich Nov 06 '19 at 19:35
  • You can't. Impossible to achieve the equivalent of passing an argument as a value instead of a pointer, let's say, except when "de-serializing" a series value by using it as a base with constant `[x]` index in a `for` loop and indexing other series values using the `for` loop's counter. – PineCoders-LucF Nov 06 '19 at 22:57
0

If anyone comes across this trying to draw a line or something else on only the last bar, like me, and getting frustrated with barstate.islast, here you go...

if bar_index == last_bar_index
    line.new(bar_index, y1, bar_index+1, y2)
    if array.size(line.all) > 1
        line.delete(array.get(line.all, array.size(line.all)-1))
Clark Baker
  • 91
  • 1
  • 4
0

It turns out this works:

indicator("test",overlay=true,max_labels_count=1)
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
-2

solved like hell :-) Asking for help, clarification, or responding to other answers.

Oren Teich
  • 47
  • 1
  • 4