Reputation: 3
In Tradingview, I want to get the last traded price of a stock in Pine Script as a fixed Variable. In Built "close" would not suffice as this changes for each bar. As an example in the tradingview charts we have "Last Price Line" which is a constant line of the last traded price as shown in the Image
https://www.tradingview.com/x/5KVDU8h7/
Upvotes: 0
Views: 8402
Reputation: 184
Built-in variable 'close' is the latest traded price. Here's a script that draws a yellow dotted line at last traded price and as you can see it is always identical to the default "last price line" drawn by tradingview. If you want to access last traded price of previous bars, you can do it by looking up close1..close[2]..close[n] where n is number of bars back from the current bar.
//@version=4
study("My Script", overlay=true)
var line _lpLine = line.new(0, 0, 0, 0, extend=extend.left, style=line.style_dashed, color=color.yellow)
_lastTradedPrice = close
line.set_xy1(_lpLine, bar_index-1, _lastTradedPrice)
line.set_xy2(_lpLine, bar_index, _lastTradedPrice)
Upvotes: 0