Home » Technical Analysis Functions » Understanding ta.percentile_linear_interpolation() Function in Pine Script

Understanding ta.percentile_linear_interpolation() Function in Pine Script

Photo of author
Published on

This article delves into the syntax, application, and key considerations of this function, providing a clear guide for incorporating it into your Pine Script strategies.

Syntax Overview

The ta.percentile_linear_interpolation() function is structured as follows:

ta.percentile_linear_interpolation(dataStream, reviewSpan, targetPercent) → series float

Arguments Explained

  • dataStream (series int/float): This represents the series of values you wish to analyze. It can be any series of numbers, such as closing prices, trading volumes, or even computed indicators.
  • reviewSpan (series int): Specifies the number of bars to look back from the current bar to calculate the percentile. This length determines the dataset’s size over which the percentile is computed.
  • targetPercent (simple int/float): The percentile you want to calculate, expressed as a number from 0 to 100. This defines the position within the sorted data set for which the value is sought.

Return Value

  • The function returns the value at the P-th percentile of the dataStream series for the last reviewSpan bars, calculated via linear interpolation between the two nearest ranks.

Example

Let’s apply ta.percentile_linear_interpolation() to a simple scenario: calculating the 90th percentile of the closing prices over the past 20 bars.

//@version=5
indicator("90th Percentile Closing Price", overlay=true)
closingPrices = close
length = 20
percentile = 90

percentileValue = ta.percentile_linear_interpolation(closingPrices, length, percentile)

plot(percentileValue, color=color.red, linewidth=2, title="90th Percentile Line")
Example

Code Walkthrough

  • Indicator Declaration:
    • //@version=5: This line specifies the version of Pine Script being used, in this case, version 5. It’s essential for compatibility and accessing the latest features.
    • indicator("90th Percentile Closing Price", overlay=true): This line declares a new indicator titled “90th Percentile Closing Price”. The overlay=true argument means that the indicator will be plotted directly on the price chart, overlaying the price action.
  • Variable Initialization:
    • closingPrices = close: Initializes a variable closingPrices to hold the series of closing prices (close). This variable is then used as the source data for the percentile calculation.
    • length = 20: Sets the length variable to 20, indicating that the percentile calculation will consider the past 20 bars.
    • percentile = 90: Assigns the value 90 to the percentile variable, specifying that the calculation aims to find the 90th percentile value.
  • Percentile Calculation:
    • percentileValue = ta.percentile_linear_interpolation(closingPrices, length, percentile): This line is where the percentile calculation happens. The ta.percentile_linear_interpolation function is called with three arguments: the series of closing prices (closingPrices), the number of bars to look back (length), and the target percentile (percentile). The function returns the 90th percentile value of the closing prices over the last 20 bars, which is then stored in the percentileValue variable.
  • Plotting the Percentile Value:
    • plot(percentileValue, color=color.red, linewidth=2, title="90th Percentile Line"): This line plots the calculated 90th percentile value on the chart. The percentileValue is plotted with specific styling options: the line color is set to red (color=color.red), the line width is set to 2 (linewidth=2), and the title of the plot is “90th Percentile Line” (title="90th Percentile Line"). This makes the 90th percentile line easily identifiable on the chart.

Key Features and Takeaways

  • Function Useability: The ta.percentile_linear_interpolation() function is versatile and can be applied to any numerical series, offering a detailed percentile calculation that goes beyond simple ranking.
  • Syntax and Application: Understanding the syntax is crucial for accurate implementation. The function’s parameters allow for dynamic analyses over variable periods and conditions.
  • Interpolation vs. Actual Data Points: It’s important to note that the function may return values not present in the original data, offering interpolated insights that could be more relevant for certain analyses.

This exploration of ta.percentile_linear_interpolation() in Pine Script highlights its utility in financial analysis, providing a foundation for incorporating sophisticated statistical measures into your trading strategies.

Leave a Comment