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.
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.