Skip to content
All library documents

Calculating Weighted Percentiles with Nearest Rank

Article TradingView scripts

Summary

This indicator calculates a weighted percentile from a rolling window of source values. It sorts the values while keeping their weights aligned, accumulates the sorted weights, then returns the first value whose cumulative weight reaches the requested share of total weight. The example uses a median percentile and walks through the calculation with sample data.

Weights can combine recency, which gives newer observations greater influence, with inferred volume, represented by the absolute difference between each bar’s close and open. Either weighting option can be disabled, yielding unit weights when both are off. The author says the implementation was tested and describes a later revision as improving computational complexity from quadratic to n log n. The document does not provide independent validation or trading-performance evidence. The result depends on the chosen source, window length, percentile, and weighting assumptions; inferred volume is a price-range proxy rather than reported traded volume.

Key ideas

  • Sort observations and their corresponding weights together before calculating a weighted percentile.
  • Use cumulative sorted weights and a percentile threshold to select the nearest-rank value.
  • The weighting scheme can emphasize recent bars, bars with larger open-to-close moves, or both.
  • With both weighting options disabled, the method reduces to an ordinary nearest-rank percentile.
  • The indicator describes a faster algorithm but provides no independent performance or trading results.

Tags

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