Pivot Points. High & Lows By Reversal Percentage

📚 Library: pivots
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.
- ✅ Supports multiple configurations (timeframes and range percentages) within a single script.
- ✅ Compatible with request.security for multi-timeframe analysis.
- ✅ Configurable to use candle body (open/close) or full candle range (high/low) with ignore_wick.
- ✅ Returns both confirmed and temporary pivots for flexible integration.
🎯 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 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.
Typical Use Cases:
- Detecting swing highs and lows for trend-following or reversal strategies.
- Visualizing pivot points across multiple timeframes using request.security.
- Enhancing custom indicators with reliable pivot detection.
- Building strategies that use confirmed pivots as entry/exit signals.
Who Should Use It:
This library is designed for Pine Script developers who want to incorporate robust pivot point detection into their indicators or strategies without managing complex state logic.
Important Note:
This library does not generate buy/sell signals on its own. It provides pivot detection tools to be used alongside your existing trading logic.
⚠️ Disclaimer:
This library is for educational and informational purposes only. It does not constitute financial advice.
## 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).
Types:
- pivot
- Fields:
- 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.
✨ 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.
pivots Library
📚 Library: pivots
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.
✅ Supports multiple calls (with different values) within a single script.
✅ Compatible with request.security for multi-timeframe analysis.
✅ Lightweight. No candle backtracing simplifies computations.
✅ 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.
Typical Use Cases:
Detecting swing highs and lows for trend-following or reversal strategies.
Visualizing pivot points across multiple timeframes using request.security.
No repainting. This is not an entry point detector.
Who Should Use It:
This library is designed for Pine Script developers who want to incorporate robust pivot point detection into their indicators or strategies without managing complex state logic.
Important Note:
This library does not generate buy/sell signals on its own. It provides pivot detection tools to be used alongside your existing trading logic.
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.
Libreria Pine
In pieno spirito TradingView, l'autore ha pubblicato questo codice Pine come libreria open-source in modo che altri programmatori Pine della nostra comunità possano riutilizzarlo. Complimenti all'autore! È possibile utilizzare questa libreria privatamente o in altre pubblicazioni open-source, ma il riutilizzo di questo codice in una pubblicazione è regolato dal nostro Regolamento.
Declinazione di responsabilità
Libreria Pine
In pieno spirito TradingView, l'autore ha pubblicato questo codice Pine come libreria open-source in modo che altri programmatori Pine della nostra comunità possano riutilizzarlo. Complimenti all'autore! È possibile utilizzare questa libreria privatamente o in altre pubblicazioni open-source, ma il riutilizzo di questo codice in una pubblicazione è regolato dal nostro Regolamento.