Auto Trendline Channel

SahilTradeSchool · study · 51 行 · 点赞 95 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("Auto Trendline Channel", shorttitle="ATC", overlay=true, max_lines_count=10, max_bars_back=5000)
 
// ── Inputs ──────────────────────────────────────────
src       = input.source(close, "Trend channel source", group="AUTO TRENDLINES")
len       = input.int(200, "Trend channel lookback", minval=2, maxval=5000, group="AUTO TRENDLINES")
mult      = input.float(2.0, "Channel width (x deviation)", minval=0.1, step=0.1, group="AUTO TRENDLINES")
extendR   = input.bool(true, "Extend lines to the right", group="AUTO TRENDLINES")
showFill  = input.bool(true, "Fill channel", group="AUTO TRENDLINES")
colMid    = input.color(color.blue, "Middle line color", group="STYLE")
colBand   = input.color(color.blue, "Upper/Lower line color", group="STYLE")
colFill   = input.color(color.new(color.blue, 90), "Fill color", group="STYLE")
widthMid  = input.int(1, "Middle line width", minval=1, group="STYLE")
widthBand = input.int(1, "Band line width", minval=1, group="STYLE")
 
// ── Linear regression line (via WMA & SMA) ──────────
w = ta.wma(src, len)
s = ta.sma(src, len)
A = 4 * s - 3 * w          // line value at the start of the window
B = 3 * w - 2 * s          // line value at the current bar
m = (A - B) / (len - 1)    // slope per bar
 
// ── Draw only on the last bar ───────────────────────
var line upL  = na
var line midL = na
var line lowL = na
var linefill lf = na
 
if barstate.islast and bar_index >= len
    // Deviation of price around the regression line
    float sumSq = 0.0
    for i = 0 to len - 1
        float linePrice = B + m * i
        sumSq += math.pow(src[i] - linePrice, 2)
    float dev = math.sqrt(sumSq / (len - 1)) * mult
 
    line.delete(upL)
    line.delete(midL)
    line.delete(lowL)
    linefill.delete(lf)
 
    ext = extendR ? extend.right : extend.none
    x1 = bar_index - len + 1
    x2 = bar_index
 
    upL  := line.new(x1, A + dev, x2, B + dev, extend=ext, color=colBand, width=widthBand)
    midL := line.new(x1, A,       x2, B,       extend=ext, color=colMid,  width=widthMid)
    lowL := line.new(x1, A - dev, x2, B - dev, extend=ext, color=colBand, width=widthBand)
 
    if showFill
        lf := linefill.new(upL, lowL, colFill)

← 返回列表