How to get current price in pine script

How to get current price in pine script

The Correct Answer and Explanation is :

To get the current price of an asset in Pine Script, the programming language used in TradingView, you can utilize built-in variables that provide access to price data. The primary price data you can access includes the open, high, low, and close prices for each bar on your chart.

Here’s a simple example of how to get the current price in Pine Script:

//@version=5
indicator("Current Price Example", overlay=true)

currentClose = close
currentOpen = open
currentHigh = high
currentLow = low

label.new(bar_index, currentClose, str.tostring(currentClose), color=color.blue, style=label.style_label_down, size=size.small)

Explanation

  1. Pine Script Version: The script starts by specifying the version of Pine Script being used (in this case, version 5). This is essential for ensuring compatibility with the syntax and functions available in that version.
  2. Indicator Declaration: The indicator function is called to create a new indicator. The overlay=true parameter means that the indicator will be plotted over the price chart rather than in a separate pane.
  3. Getting Price Data:
  • close: This built-in variable represents the closing price of the current bar. The closing price is often used as it reflects the last price at which the asset was traded during that period.
  • open, high, and low: Similarly, these variables provide the opening, highest, and lowest prices for the current bar. They can be used depending on what specific price information is required.
  1. Displaying the Current Price: The label.new function is used to create a label on the chart. The label is positioned at the current bar’s index (bar_index) and displays the current closing price. The color, style, and size parameters customize the appearance of the label.
  2. Usage: This basic structure allows traders to see the current closing price directly on their charts, facilitating quick assessments of price movements and trends.

Using these built-in variables effectively allows you to gather real-time data and create informative visual indicators within TradingView charts.

Scroll to Top