OPEN-SOURCE SCRIPT
Automatic Trendline [Metrify]

Metrify Automatic Trendlines is an auto-drawing support/resistance channel built around pivot clustering + scoring, not “connect two perfect points”. The script continuously collects swing pivots (high/low) over a configurable lookback window, then searches for the best single support line and the best single resistance line that behave like a human-drawn trendline: multiple interactions, controlled slope, limited break-throughs, and (most importantly) still relevant to the current price. (configurable in "Max Relevance Distance" input)
The fundamental problem with algorithmic trendlines is subjectivity. To solve this mathematically, we treat trendlines as a statistical regression problem with specific constraints. We do not use linear regression on all candles, instead, we use a brute-force iterative approach on specific "Pivot Points."
The logic operates on a simple premise: Generate every possible line between past swing points, validate them against price history, score them based on fit, and render only the winner.
The Calculation Engine (f_find_best_line)
This function contains the primary computational load. It performs a nested loop operation:
For every pair of pivots (P1,P2), we calculate the slope (m) and the y-intercept concept. This gives us a tentative trendline equation:
y=mx+c
The Scoring Matrix
We assign a score to each candidate line based on weighted heuristics:
The line with the highest localBest score is returned as the dominant trendline.
What you can use it for?
This is a structure visualizer that tries to keep a clean, current S/R channel on screen with volatility-aware rules. It’s not a signal generator, it doesn’t predict breakouts, and it won’t always draw something, if the market is messy and no line survives the filters, it will show none instead of hallucinating geometry. If you need more lines (multiple concurrent channels), that’s a different design tradeoff (and usually becomes clutter + false confidence fast).
The fundamental problem with algorithmic trendlines is subjectivity. To solve this mathematically, we treat trendlines as a statistical regression problem with specific constraints. We do not use linear regression on all candles, instead, we use a brute-force iterative approach on specific "Pivot Points."
The logic operates on a simple premise: Generate every possible line between past swing points, validate them against price history, score them based on fit, and render only the winner.
The Calculation Engine (f_find_best_line)
This function contains the primary computational load. It performs a nested loop operation:
- Outer Loop (newer): Iterates through recent pivots.
- Inner Loop (older): Iterates through older pivots to form a candidate line segment.
For every pair of pivots (P1,P2), we calculate the slope (m) and the y-intercept concept. This gives us a tentative trendline equation:
y=mx+c
The Scoring Matrix
We assign a score to each candidate line based on weighted heuristics:
- Touch Count (touches * 2.8): The primary driver. More touches = higher statistical significance.
- Recency (recency * 1.2): Lines originating closer to the current price action are weighted higher.
- Tightness (avgErr): We calculate the average distance of all touches from the line. A "tighter" fit (lower error) increases the score.
Penalties: - violations * 2.2: False breaks heavily penalize the score.
- barBreakRatio * 2.0: If the line cuts through candle bodies (even if pivots are fine).
The line with the highest localBest score is returned as the dominant trendline.
What you can use it for?
This is a structure visualizer that tries to keep a clean, current S/R channel on screen with volatility-aware rules. It’s not a signal generator, it doesn’t predict breakouts, and it won’t always draw something, if the market is messy and no line survives the filters, it will show none instead of hallucinating geometry. If you need more lines (multiple concurrent channels), that’s a different design tradeoff (and usually becomes clutter + false confidence fast).
开源脚本
秉承TradingView的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。
开源脚本
秉承TradingView的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。