OPEN-SOURCE SCRIPT
Trendline Indicator with Dynamic Breakpoint Tracking

Trendline Indicator with Dynamic Breakpoint Tracking
This indicator automates the process of manually drawing and redrawing trendlines as price structure evolves, rather than plotting a single static line.
How it works
The script tracks confirmed swing highs and lows (pivots). It starts in downtrend mode, watching the most recent swing low:
When that low is broken, the line resets. It anchors to the top of the prior move and snaps its second point to the next confirmed swing high that's lower than that top — drawing a fresh descending resistance line.
As price continues down, each new broken low re-anchors the line the same way, so the resistance line keeps stepping down along the sequence of lower highs.
If price closes at or above the current resistance level, the script flips to uptrend mode and begins drawing an ascending support line using the mirror-image logic: anchoring from the bottom of the move and snapping to the next confirmed higher low.
The two modes alternate automatically as market structure shifts between making lower highs and higher lows.
Dynamic breakpoint tracking
Unlike a fixed trendline, the breakpoints this script uses to define the line are re-evaluated continuously:
If a transition is only halfway confirmed (e.g. resistance broke but no higher low has formed yet) and price reverses back through the original anchor before that confirmation completes, the script recognizes the transition failed and reverts to the prior trend immediately, rather than waiting on a reversal that isn't materializing.
A stale-anchor safeguard prevents the line from getting permanently locked onto an old price level if the market structure it's tracking becomes irrelevant.
Inputs
Pivot Left/Right Bars — sensitivity of swing detection (smaller = faster but noisier, larger = smoother but more lag)
Max bars to search for anchor point — how far back the script looks when re-anchoring
Line colors/width, signal colors, and toggles for trend labels, pivot markers, and the debug status table
Notes
Pivot-based detection is inherently lagging by design (a swing can only be confirmed once bars exist on both sides of it), so the line reacts after structure is confirmed, not in real time.
Built for visualizing trend structure and potential trend-change points — not a standalone buy/sell signal generator. Combine with your own confirmation and risk management.
This indicator automates the process of manually drawing and redrawing trendlines as price structure evolves, rather than plotting a single static line.
How it works
The script tracks confirmed swing highs and lows (pivots). It starts in downtrend mode, watching the most recent swing low:
When that low is broken, the line resets. It anchors to the top of the prior move and snaps its second point to the next confirmed swing high that's lower than that top — drawing a fresh descending resistance line.
As price continues down, each new broken low re-anchors the line the same way, so the resistance line keeps stepping down along the sequence of lower highs.
If price closes at or above the current resistance level, the script flips to uptrend mode and begins drawing an ascending support line using the mirror-image logic: anchoring from the bottom of the move and snapping to the next confirmed higher low.
The two modes alternate automatically as market structure shifts between making lower highs and higher lows.
Dynamic breakpoint tracking
Unlike a fixed trendline, the breakpoints this script uses to define the line are re-evaluated continuously:
If a transition is only halfway confirmed (e.g. resistance broke but no higher low has formed yet) and price reverses back through the original anchor before that confirmation completes, the script recognizes the transition failed and reverts to the prior trend immediately, rather than waiting on a reversal that isn't materializing.
A stale-anchor safeguard prevents the line from getting permanently locked onto an old price level if the market structure it's tracking becomes irrelevant.
Inputs
Pivot Left/Right Bars — sensitivity of swing detection (smaller = faster but noisier, larger = smoother but more lag)
Max bars to search for anchor point — how far back the script looks when re-anchoring
Line colors/width, signal colors, and toggles for trend labels, pivot markers, and the debug status table
Notes
Pivot-based detection is inherently lagging by design (a swing can only be confirmed once bars exist on both sides of it), so the line reacts after structure is confirmed, not in real time.
Built for visualizing trend structure and potential trend-change points — not a standalone buy/sell signal generator. Combine with your own confirmation and risk management.
Skrip sumber terbuka
Dalam semangat TradingView sebenar, pencipta skrip ini telah menjadikannya sumber terbuka, jadi pedagang boleh menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupuan anda boleh menggunakan secara percuma, ingat bahawa penerbitan semula kod ini tertakluk kepada Peraturan Dalaman.
Penafian
Maklumat dan penerbitan adalah tidak bertujuan, dan tidak membentuk, nasihat atau cadangan kewangan, pelaburan, dagangan atau jenis lain yang diberikan atau disahkan oleh TradingView. Baca lebih dalam Terma Penggunaan.
Skrip sumber terbuka
Dalam semangat TradingView sebenar, pencipta skrip ini telah menjadikannya sumber terbuka, jadi pedagang boleh menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupuan anda boleh menggunakan secara percuma, ingat bahawa penerbitan semula kod ini tertakluk kepada Peraturan Dalaman.
Penafian
Maklumat dan penerbitan adalah tidak bertujuan, dan tidak membentuk, nasihat atau cadangan kewangan, pelaburan, dagangan atau jenis lain yang diberikan atau disahkan oleh TradingView. Baca lebih dalam Terma Penggunaan.