OPEN-SOURCE SCRIPT
Pivot Length Percentiles Oscillator

# Pivot Length Percentiles Oscillator: Technical Mechanics Explained
## Introduction
The Pivot Length Percentiles Oscillator is a statistical approach to identifying potential market reversals by analyzing the distribution of price movements relative to pivot points. This publication explains the technical mechanics behind the indicator.
## Core Mechanics
### 1. Pivot Point Detection
The indicator begins by identifying significant pivot highs and lows using a user-defined lookback period:
- `lft`: Number of bars to the left of potential pivot point
- `rht`: Number of bars to the right of potential pivot point
These parameters determine how "significant" a pivot needs to be to qualify for analysis.
### 2. Distance Measurement & Historical Database
For each new pivot point identified, the indicator:
- Calculates the absolute price distance from the previous pivot of the same type
- Records the number of candles between consecutive pivots
- Stores these measurements in dynamic arrays that build a historical database
### 3. Statistical Distribution Analysis
Rather than using fixed values, the oscillator analyzes the complete distribution of historical pivot distances and calculates key percentile values:
- `lw` (Low Percentile): Lower boundary for statistical significance
- `md` (Mid Percentile): Median statistical boundary
- `hi` (High Percentile): Upper boundary for statistical extremes
### 4. Oscillator Construction
Two primary oscillator lines are calculated:
- Green line (`osc1`): Measures current price's fall below recent highs with `low - ta.highest(high, lft)`
- Red line (`osc2`): Measures current price's rise above recent lows with `high - ta.lowest(low, lft)`
### 5. Threshold Generation
The percentile values from the historical distribution create dynamic threshold lines:
- For downside movements: Scaled versions of the low percentile (`lw_distance_low`) and high percentile (`hi_distance_low`)
- For upside movements: Scaled versions of the low percentile (`lw_distance_high`) and high percentile (`hi_distance_high`)
### 6. Signal Logic
Entry signals are generated when:
- **Bullish Signal**: The downside oscillator crosses below a statistical threshold while price continues showing downward momentum (close < previous close AND close < previous open)
- **Bearish Signal**: The upside oscillator crosses above a statistical threshold while price continues showing upward momentum (close > previous close AND close > previous open)
### 7. Visualization Options
Users can toggle between:
- Standard view: Shows the oscillator and threshold lines
- Percentile view: Displays the current movement's percentile rank within the historical distribution
## Implementation Notes
- The indicator scales threshold values by 0.9 to create a slight buffer that reduces false signals
- The movement's continuation is confirmed by checking both close-to-close and close-to-open relationships
- Arrays dynamically update throughout the chart's history, making the indicator increasingly accurate as more data is processed
## Mathematical Framework
The core statistical function calculates percentiles using linear interpolation between values when needed:
```
calculate_percentile(array, percentile) =
sortedValue[floor(index)] +
fraction * (sortedValue[ceil(index)] - sortedValue[floor(index)])
```
where `index = (array.size - 1) * percentile / 100`
This mathematical approach ensures the thresholds adapt dynamically to changing market conditions rather than relying on fixed values.
## Introduction
The Pivot Length Percentiles Oscillator is a statistical approach to identifying potential market reversals by analyzing the distribution of price movements relative to pivot points. This publication explains the technical mechanics behind the indicator.
## Core Mechanics
### 1. Pivot Point Detection
The indicator begins by identifying significant pivot highs and lows using a user-defined lookback period:
- `lft`: Number of bars to the left of potential pivot point
- `rht`: Number of bars to the right of potential pivot point
These parameters determine how "significant" a pivot needs to be to qualify for analysis.
### 2. Distance Measurement & Historical Database
For each new pivot point identified, the indicator:
- Calculates the absolute price distance from the previous pivot of the same type
- Records the number of candles between consecutive pivots
- Stores these measurements in dynamic arrays that build a historical database
### 3. Statistical Distribution Analysis
Rather than using fixed values, the oscillator analyzes the complete distribution of historical pivot distances and calculates key percentile values:
- `lw` (Low Percentile): Lower boundary for statistical significance
- `md` (Mid Percentile): Median statistical boundary
- `hi` (High Percentile): Upper boundary for statistical extremes
### 4. Oscillator Construction
Two primary oscillator lines are calculated:
- Green line (`osc1`): Measures current price's fall below recent highs with `low - ta.highest(high, lft)`
- Red line (`osc2`): Measures current price's rise above recent lows with `high - ta.lowest(low, lft)`
### 5. Threshold Generation
The percentile values from the historical distribution create dynamic threshold lines:
- For downside movements: Scaled versions of the low percentile (`lw_distance_low`) and high percentile (`hi_distance_low`)
- For upside movements: Scaled versions of the low percentile (`lw_distance_high`) and high percentile (`hi_distance_high`)
### 6. Signal Logic
Entry signals are generated when:
- **Bullish Signal**: The downside oscillator crosses below a statistical threshold while price continues showing downward momentum (close < previous close AND close < previous open)
- **Bearish Signal**: The upside oscillator crosses above a statistical threshold while price continues showing upward momentum (close > previous close AND close > previous open)
### 7. Visualization Options
Users can toggle between:
- Standard view: Shows the oscillator and threshold lines
- Percentile view: Displays the current movement's percentile rank within the historical distribution
## Implementation Notes
- The indicator scales threshold values by 0.9 to create a slight buffer that reduces false signals
- The movement's continuation is confirmed by checking both close-to-close and close-to-open relationships
- Arrays dynamically update throughout the chart's history, making the indicator increasingly accurate as more data is processed
## Mathematical Framework
The core statistical function calculates percentiles using linear interpolation between values when needed:
```
calculate_percentile(array, percentile) =
sortedValue[floor(index)] +
fraction * (sortedValue[ceil(index)] - sortedValue[floor(index)])
```
where `index = (array.size - 1) * percentile / 100`
This mathematical approach ensures the thresholds adapt dynamically to changing market conditions rather than relying on fixed values.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
For quick access on a chart, add this script to your favorites — learn more here.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
For quick access on a chart, add this script to your favorites — learn more here.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.