P-Square - Estimation of the Nth percentile of a series

Estimation of the Nth percentile of a series
When working with built-in functions in TradingView we have to limit our length parameters to max 4999. In case we want to use a function on the whole available series (bar 0 all the way to the current bar), we can usually not do this without manually creating these calculations in our code. For things like mean or standard deviation, this is quite trivial, but for things like percentiles, this is usually very costly. In more complex scripts, this becomes impossible because of resource restrictions from the Pine Script execution servers.

One solution to this is to use an estimation algorithm to get close to the true percentile value. Therefore, I have ported this implementation of the P-Square algorithm to Pine Script. P-Square is a fast algorithm that does a good job at estimating percentiles in data streams. Here's the algorithms original paper.

The chart
On the chart we see:
  • The returns of the series (blue scatter plot)
  • The mean of the returns of the series (orange line)
  • The standard deviation of the returns of the series (yellow line)
  • The actual 84.1th percentile of the returns (white line)
  • The estimatedl 84.1th percentile of the returns using the P-Square algorithm (green line)

Note: We can see that the returns are not normally distributed as we can see that one standard deviation is higher than the 84.1th percentile. One standard deviation should equal the 84.1th percentile if the data is normally distributed.
Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.

Want to use this script on a chart?


500 coins
Thank you for sharing this awesome study. However I guess me how to take advantage from this .
+1 Svara
lejmer gmeneguzzo
@gmeneguzzo, I guess you could use this as is for measuring risk. For instance, use this on the daily time frame with the percentile setting set to 90 and you'll know approximately how far price moves 90% of the days that TradingView has data for. This can help you with risk management and money management when you day trade the opening of a session for example.
+5 Svara
@lejmer, Thank you I'll try it.