In this blog, we will delve into the **array.percentile_linear_interpolation
**function in Pine Script, a popular domain-specific language used for creating custom indicators and strategies on the TradingView platform. This powerful function allows you to calculate the percentile value of an array based on a specified percentage, using linear interpolation.
Understanding Array.percentile_linear_interpolation
Array.percentile_linear_interpolation
is a built-in Pine Script function used to calculate the value at a given percentile of an array of numerical values. The function employs linear interpolation to determine the value at the specified percentile.
Syntax and Parameters
The syntax for this function is as follows:
array.percentile_linear_interpolation(array, percentage)
Parameters:
array
: An array containing numerical values. This array must be sorted in either ascending or descending order prior to passing it to the function.percentage
: A numerical value between 0 and 100, representing the percentile to calculate.
Return value:
The function returns the value at the specified percentile in the given array.
Use Case Examples
To better understand the usage of the array.percentile_linear_interpolation
function in Pine Script, let’s explore two unique use case examples.
<a name=”example-1-bollinger-band-percentiles”></a>
Example: Bollinger Band Percentiles
In this example, we will create an indicator that calculates the 20-day Bollinger Band percentile for the closing price of a trading instrument.
//@version=5 indicator('Bollinger Band Percentiles', shorttitle='BB Percentiles', overlay=true) length = input.int(20, minval=1, title='Length') src = close mult = input(2.0, title='Multiplier') percentage = input(50, title='Percentile') // Calculate Bollinger Bands basis = ta.sma(src, length) dev = mult * ta.stdev(src, length) upper = basis + dev lower = basis - dev // Calculate the distance between the upper and lower bands band_range = upper - lower // Initialize the array to store the band ranges band_range_array = array.new_float(0) // Add the current band range to the array array.unshift(band_range_array, band_range) // Keep the array length equal to the specified length if array.size(band_range_array) > length array.pop(band_range_array) // Sort the array in ascending order array.sort(band_range_array, order.ascending) // Calculate the percentile value using the array.percentile_linear_interpolation function percentile_value = array.percentile_linear_interpolation(band_range_array, percentage) // Plot the Bollinger Bands and the percentile value plot(basis, color=color.new(color.blue, 0), linewidth=2, title='Basis') plot(upper, color=color.new(color.red, 0), linewidth=1, title='UpperBand') plot(lower, color=color.new(color.red, 0), linewidth=1, title='Lower Band') plot(close + percentile_value, "Percentile Value", color=color.green, linewidth=2)

Explanation:
- We start by defining our input parameters:
length
,mult
,src
, andpercentage
. - Next, we calculate the Bollinger Bands using the
sma()
andstdev()
functions. - We then calculate the distance between the upper and lower bands, which is the
band_range
. - We initialize an empty array
band_range_array
to store the band ranges. - We add the current
band_range
to the beginning of the array usingarray.unshift()
. - To maintain the desired array size, we remove the oldest value from the array using
array.pop()
when the array size exceeds the specifiedlength
. - We sort the
band_range_array
in ascending order usingarray.sort()
. - The
array.percentile_linear_interpolation()
function is used to calculate the percentile value based on thepercentage
input. - Lastly, we plot the Bollinger Bands and the percentile value on the chart.
Key Takeaways
- The
array.percentile_linear_interpolation
function is a powerful tool in Pine Script that allows you to calculate the value at a specified percentile in a given array using linear interpolation.
- Before passing an array to the function, it must be sorted in either ascending or descending order.
- The
array.percentile_linear_interpolation
function can be used in a variety of scenarios, such as determining Bollinger Band or relative volume percentiles. - It is essential to maintain the desired array size by adding new values to the beginning of the array and removing the oldest values when the size exceeds the specified length.
Conclusion
In this blog, we have explored the array.percentile_linear_interpolation
function in Pine Script, including its syntax, parameters, and two unique use case examples that demonstrate its practical application. With this knowledge, you can now incorporate percentile calculations into your custom indicators and strategies on the TradingView platform, providing you with valuable insights into your trading analysis