OPEN-SOURCE SCRIPT

Rolling Segment [LuxAlgo]

25 545
The Rolling Segment indicator provides a dynamic, trend-following reference line that adapts its slope and direction based on price volatility and distance from the market.

🔶 USAGE

The indicator is designed to filter out minor price fluctuations while remaining responsive to significant trend shifts. It appears as a continuous segmented line on the chart, changing color based on the current trend (Green for bullish, Red for bearish).

A gradient fill is applied between the price and the segment to visualize the "stretch" or deviation of the current price action from the trend line. This visual aid helps traders identify when price is moving in lockstep with the trend versus when it is overextending.

🔹 Reversal Signals

The indicator identifies trend reversals when the distance between the price and the segment exceeds a user-defined ATR-based threshold. These reversals are marked on the chart with triangle labels:

  • A bullish triangle (▲) appears below the segment when a new uptrend begins.
  • A bearish triangle (▼) appears above the segment when a new downtrend begins.


🔹 Speed Modes

The segment operates in two speed modes: "Fast" and "Slow." When price moves significantly away from the segment (crossing the Fast Threshold), the indicator switches to a faster slope to catch up with the momentum. When price is closer to the segment, it maintains a slower, more stable slope to avoid premature trend changes during consolidation.

🔶 MARKET INTERPRETATION

The Rolling Segment acts as a measure of trend health. Because the slope is derived from the Average True Range (ATR), the "speed" of the line is normalized to the asset's current volatility.

  • Trend Momentum: When the price stays consistently above a green segment (or below a red one) without triggering the "Fast" mode, it indicates a stable, sustainable trend.
  • Price Exhaustion: A sudden jump into "Fast" mode often highlights a surge in momentum. If the price continues to stretch significantly beyond the segment even in fast mode, it may signal an overextended move prone to mean reversion.
  • Consolidation: Horizontal or shallow segments indicate a period where the market is lacking a clear directional driver relative to its recent volatility.


🔶 DETAILS

The core of the script relies on a state-machine logic that governs a unique slope-freezing mechanism. Unlike traditional moving averages that recalculate every bar based on shifting data, this indicator calculates a linear slope and "freezes" it until a specific state change occurs.

The state is defined by the combination of Trend Direction and Speed Mode. The ATR value used to calculate the slope is only updated when:

  1. A trend reversal is detected (Price crosses the Reverse Threshold).
  2. The distance between price and the segment crosses the "Fast Threshold," triggering a shift in speed mode.
  3. The initial calculation begins at the start of the data series.


By freezing the slope, the indicator produces perfectly linear segments. This mathematical intuition ensures that the reference line moves at a constant "volatility-adjusted rate" per bar, providing a cleaner visual representation of trend momentum and making it easier to spot price exhaustion when the market deviates too far from this constant rate.

🔶 SETTINGS

🔹 Slopes

  • Fast Slope Length: The period used to determine the slope speed when the price is trending strongly. Lower values result in a steeper, more aggressive slope.
  • Slow Slope Length: The period used to determine the slope speed during standard market conditions. Higher values create a flatter, more trailing segment.


🔹 ATR Settings

  • ATR Length: The lookback period for the Average True Range calculation, which scales the slope and thresholds to current market volatility.


🔹 Thresholds

  • Fast Slope Threshold (ATR Multiplier): The distance (in ATR units) the price must reach from the segment to trigger the "Fast" slope mode.
  • Reverse Threshold (ATR Multiplier): The distance (in ATR units) the price must cross against the current trend to trigger a reversal and change the segment direction.

คำจำกัดสิทธิ์ความรับผิดชอบ

ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมใน ข้อกำหนดการใช้งาน