Pivot Points. High & Lows By Reversal Percentage

This Pine Script library provides a robust function for identifying and tracking pivot points (reversal points) in price data, suitable for integration into custom trading indicators and strategies.
🛠️ Main Features:
- ✅ Identifies pivot highs and lows based on configurable price movement thresholds.
- ✅ Lightweight. No candle backtracing used. Much less computation heavy.
- ✅ Supports multiple calls (with different values) within a single script.
- ✅ Compatible with request.security for multi-timeframe analysis.
- ✅ Returns both confirmed and temporary pivots for flexible integration.
- ✅ Pinescript V5 and V6 compliant code.
Purpose:
The pivots library enables Pine Script developers to easily add pivot point detection to their scripts. It identifies significant price reversals by evaluating price movements against a minimum range threshold (min_range_pct) and confirming reversals based on a percentage (reversal_pct) of the prior trend’s magnitude. The library supports multiple simultaneous calls with different settings, making it ideal for multi-timeframe strategies.
How It Works:
The library’s f_calculatePivot function tracks price movements to detect pivot points:
- Minimum Range Threshold: A potential pivot is considered if the price moves beyond the min_range_pct percentage of the current high (for a high pivot) or low (for a low pivot), ensuring sufficient movement.
- Reversal Confirmation: A pivot is confirmed if the price reverses from the potential pivot by at least the reversal_pct percentage of the distance between the last confirmed pivot and the current potential pivot, measuring the retracement relative to the prior trend’s magnitude.
- The function alternates between tracking highs (in an uptrend) and lows (in a downtrend), updating the trend when a pivot is confirmed.
- State management uses an array of pivot_state objects, allowing independent calculations for different timeframes and min_range_pct values within the same script.
## Technical Reference
Functions:
- Parameters:
- _high: The high price series (e.g., high or math.max(open, close)).
- _low: The low price series (e.g., low or math.min(open, close)).
- _min_range_pct: The minimum percentage price movement to consider a potential pivot.
- _reversal_pct: The percentage of the prior trend’s distance required to confirm a pivot.
- Returns:
A tuple containing:
- isNewPivot: Boolean indicating if a new pivot was confirmed.
- last_confirmed_pivot: The most recent confirmed pivot (type pivot).
- temp_pivot: The current temporary pivot (type pivot).
Pivot type:
- idx (series int): Bar index of the pivot.
- typ (series int): Type of pivot (PIVOT_HIGH or PIVOT_LOW).
- prc (series float): Price of the pivot.
- tme (series int): Timestamp of the pivot.
Constants (internal):
- TREND_LONG, TREND_SHORT: Trend direction indicators (1, -1).
- PIVOT_HIGH, PIVOT_LOW: Pivot type indicators (1, -1).
✨ Example of Use:
## Release Notes
v1
- Initial release of the pivots library with f_calculatePivot function for detecting pivot points and supporting multiple configurations and timeframes.
v2
- Code is Pinescript V6 ready. Remains identified as V5, but changing the version number is the only thing that is required to be v6.
Библиотека Pine
В истинном духе TradingView автор опубликовал этот код Pine как библиотеку с открытым исходным кодом, чтобы другие программисты Pine из нашего сообщества могли её использовать. Браво автору! Вы можете использовать эту библиотеку для личного пользования или в других публикациях с открытым исходным кодом, но повторное использование этого кода в публикациях регулируется Правилами поведения.
Отказ от ответственности
Библиотека Pine
В истинном духе TradingView автор опубликовал этот код Pine как библиотеку с открытым исходным кодом, чтобы другие программисты Pine из нашего сообщества могли её использовать. Браво автору! Вы можете использовать эту библиотеку для личного пользования или в других публикациях с открытым исходным кодом, но повторное использование этого кода в публикациях регулируется Правилами поведения.