Ralph Gehteha
Ralph Gehteha

Reputation: 1

Storing a single Close price (NOT a series) in a variable - impossible?

is it really impossible to store a single price in a variable without turning it into a series variable? It's freaking annoying. Please see below example code, the comments explain the problem. Thank you!

//@version=5
indicator("test")

// three random stocks
sec_1 = request.security("AAPL", "D", close)
sec_2 = request.security("MSFT", "D", close)
sec_3 = request.security("DOX", "D", close)

// supposed to fetch previous day's Close price of the above stocks
// a single value for each stock, not a series!

ar = array.new_float(3)

array.set(ar, 0, sec_1[1])
array.set(ar, 1, sec_2[1])
array.set(ar, 2, sec_3[1])

// sort by those single values. only makes sense with single values anyway?!
// but no error message

array.sort(ar)

// array supposed to contain single values, not series -> plot a line
// ... like this

plot(200, color=color.red)

// but plotting series. WHY???

plot(array.get(ar, 0))
plot(array.get(ar, 1))
plot(array.get(ar, 2))

Upvotes: 0

Views: 299

Answers (1)

vitruvius
vitruvius

Reputation: 21294

Once a series, always a series. As stated by the user manual, using close in calculations in your case would yield a result of series.

Built-in variables such as open, close, high, time or volume are of “series” form, as would be the result of expressions calculated using them.

If you want to have one single line, and not plot the historical values (series), you can use the line.new() function.

Upvotes: 1

Related Questions