Home » Drawing On Charts » Understanding line.get_price() Function in Pine Script

Understanding line.get_price() Function in Pine Script

Photo of author
Published on

The line.get_price() function in Pine Script is used to retrieve the price level of a graphical line at a specified bar index. It’s particularly useful for custom indicators or strategies that involve dynamic lines, such as trend lines or support/resistance levels. Let’s delve into its syntax, usage, and key features to understand how it can be leveraged in trading scripts.

Syntax

The syntax for line.get_price() is as follows:

line.get_price(id, x) → series float

Arguments

  • id (series line): This is the line object from which you want to get the price. The line must have been previously created in your script using functions like line.new().
  • x (series int): The bar index for which you want to obtain the price level of the line. This is an integer value that represents a specific bar on the chart.

Example

Here’s a practical example to illustrate how line.get_price() can be used within a Pine Script indicator:

//@version=5
indicator("DynamicPriceLevel", overlay=false)
var line trendLine = na
if bar_index == 10
    trendLine := line.new(0, high[5], bar_index, high)
plot(line.get_price(trendLine, bar_index), color=color.green)
Example

In this example, a dynamic price level is calculated and plotted on the chart. The key steps are:

  1. Initialization: A variable trendLine is declared to store the line object.
  2. Line Creation: At the 10th bar index on the chart, a new line (trendLine) is created using line.new(). This line extends from the 0th bar to the current bar, with its price level based on the high price 5 bars ago and the current high price.
  3. Plotting Price Level: The line.get_price() function is used to plot the price level of trendLine at each bar index. This is visualized on the chart in green.

Returns

  • The function returns the price value of the line id at the specified bar index x.

Remarks

  • The line is assumed to have been created with extend=extend.both, meaning it extends indefinitely to the left and right on the chart.
  • This function is applicable only to lines created with xloc.bar_index. Attempting to use it with lines created using xloc.bar_time will result in an error.

Key Features and Takeaways

  • Function Usability: line.get_price() is essential for scripts that require dynamic interaction with graphical lines, allowing for precise price level calculations at any given bar.
  • Syntax and Application: Understanding the syntax is crucial for effectively using this function in your trading scripts. The function’s ability to return a floating-point series makes it versatile for various trading strategies.
  • Limitations: The function’s application is limited to lines created with xloc.bar_index, emphasizing the importance of understanding Pine Script’s coordinate systems.

By incorporating line.get_price() into your Pine Script indicators or strategies, you can enhance their functionality and provide more dynamic analysis capabilities on TradingView charts.

Leave a Comment