Skip to content
All library documents

Robust Moving Median Calculation with Torben’s Search Algorithm

Article ProRealCode

Summary

The document describes a moving median indicator for price data and explains the underlying Torben search algorithm. For each window, the routine finds its minimum and maximum, tests a midpoint, and counts values below, above, or equal to that candidate. It repeatedly tightens the search bounds until the median can be selected. The example uses a 20-bar window and can use closed-bar prices.

This approach is useful when preserving the original input array matters, since it does not sort or modify that data and reads values in sequence. It may suit large read-only datasets where copying is costly. The trade-off is that it makes multiple passes and is not presented as the fastest median method. The document supplies an implementation, but no speed comparison, trading signals, backtest, or evidence of profitability. It also does not discuss parameter selection or how the indicator should be incorporated into a trading system.

Key ideas

  • The indicator computes a median over a rolling window of price observations.
  • Torben’s algorithm searches between window minimum and maximum using repeated midpoint tests.
  • The algorithm does not modify or sort the input values.
  • Sequential repeated passes can reduce copying overhead for large read-only inputs.
  • The implementation uses a 20-bar window but provides no trading performance evidence.

Tags

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