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.
Mã nguồn mở

Với tinh thần của TradingView, tác giả đã xuất bản tập lệnh theo mã nguồn mở, vì thế trader có thể dễ dàng hiểu và tùy chỉnh được. Bạn có thể sử dụng miễn phí, hoặc tùy chỉnh lại mã đã được cấp phép bởi Quy tắc Chung. Bạn có thể sử dụng nó trên biểu đồ.

Bạn muốn sử dụng tập lệnh này trên biểu đồ?

Bình luận

500 xu
Phản hồi
Thank you for sharing this awesome study. However I guess me how to take advantage from this .
+1 Phản hồi
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 Phản hồi