Rares P
Rares P

Reputation: 313

A mystery of accessing variables

I clearly am a noob so I am grateful if anyone could explain why this labels numbers?

//@version=4
study(title="Test", shorttitle="Test", overlay=true, max_labels_count = 500)

ma = sma(close,50)
label.new(bar_index, close,tostring(ma, "#.##"), yloc = yloc.belowbar, color = color.red, textcolor = color.black, style = label.style_none)

plot(na)

While this one labels NaN

//@version=4
study(title="Test", shorttitle="Test", overlay=true, max_labels_count = 500)

ma = sma(close,50)
var txtt=tostring(ma, "#.##")
label.new(bar_index, close,txtt, yloc = yloc.belowbar, color = color.red, textcolor = color.black, style = label.style_none)

plot(na)

Upvotes: 0

Views: 82

Answers (1)

Bjorn Mistiaen
Bjorn Mistiaen

Reputation: 6865

You declared txtt as var.
A var is only initialized once at the start of a script, and keeps its value throughout the script.
Remove the var from txtt and it should work fine.

txtt = tostring(ma, "#.##")

Or you could do

var string txtt = na
txtt := tostring(ma, "#.##")

Both should work.

Upvotes: 2

Related Questions