Skip to content
All library documents

Streaming Percentile Estimation with the P-Square Algorithm

Article TradingView scripts

Summary

This indicator explains how to estimate a running percentile for a data stream using the P-Square algorithm. Instead of storing and repeatedly sorting the full history, it updates five marker values and their positions as observations arrive. The approach is intended to make percentile tracking practical across a long chart series when built-in window limits or execution costs make exact calculations difficult.

The script can process either the selected source or its percentage returns, and plots the estimated percentile alongside the mean, standard deviation, and input series. An optional exact nearest-rank percentile is provided as a reference, but the document notes that this operation is costly. Its chart discussion compares the 84.1st percentile of returns with one standard deviation and observes a difference, which it interprets as evidence against normality in that example. The indicator provides an approximation, not a trading signal; accuracy and usefulness may vary with the data stream, and no trading performance is reported.

Key ideas

  • P-Square estimates a selected percentile incrementally using a small set of markers rather than retaining the entire series.
  • The input can be raw source values or percentage returns.
  • The indicator plots the estimated percentile with mean and standard deviation for context.
  • An exact percentile calculation is available for comparison but may be computationally expensive.
  • The example’s percentile and standard deviation differ, but it does not establish a trading edge.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.