0
votes

I am trying to write a pine script where an asset's price line is plotted, and the line's colour is either green or red depending on whether the relevant day's close is above or below the latest close.

I have code that works providing I hard code the most recent close price to a variable e.g. 10178 in my example below.

series = input(close, title="Source")

last_price = 10178

plot(series, color=last_price > close ? color.green : color.red)

But is there a way in Pine Script to automatically assign the latest close price?

I've tried close[0] but that appears to be an array of every day's close price (and so returns red for every day) but what I need is a single value.

1

1 Answers

0
votes

If by latest close you mean the close of the realtime bar, then the answer to your question is no; you cannot refer to the realtime bar's close from historical bars. Future time series values cannot be accessed in Pine. close[0] (same as close) refers to the close of the bar the script is currently executing on.

If by latest close you mean the previous bar's close, then that may be referred to using close[1].

For more information on referencing series (which are NOT arrays), see: https://www.tradingview.com/pine-script-docs/en/v4/language/Operators.html#history-reference-operator