Auto Trendlines

SahilTradeSchool · study · 96 行 · 点赞 44 · TradingView 原页

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

Pine Script

//@version=6
indicator("Auto Trendlines", overlay = true, max_lines_count = 500)
 
// ───────── Inputs ─────────
lenL       = input.int(10, "Pivot Left Bars", minval = 1, group = "Pivot Settings")
lenR       = input.int(10, "Pivot Right Bars", minval = 1, group = "Pivot Settings")
 
breakType  = input.string("Close", "Breakout Confirmation", options = ["Close", "Wick"], group = "Breaks")
showBreaks = input.bool(true, "Show Break Markers", group = "Breaks")
keepBroken = input.bool(true, "Keep Broken Lines (dotted)", group = "Breaks")
 
upCol      = input.color(color.green, "Uptrend Line Color", group = "Style")
dnCol      = input.color(color.red, "Downtrend Line Color", group = "Style")
lineWidth  = input.int(2, "Line Width", minval = 1, maxval = 5, group = "Style")
lineStyle  = input.string("Solid", "Line Style", options = ["Solid", "Dashed", "Dotted"], group = "Style")
 
f_style(s) =>
    s == "Dashed" ? line.style_dashed : s == "Dotted" ? line.style_dotted : line.style_solid
 
// ───────── State ─────────
var float ph1 = na
var float ph2 = na
var int   ph1Bar = na
var int   ph2Bar = na
 
var float pl1 = na
var float pl2 = na
var int   pl1Bar = na
var int   pl2Bar = na
 
var line dnLine = na
var line upLine = na
 
// ───────── Pivot Detection ─────────
phVal = ta.pivothigh(high, lenL, lenR)
plVal = ta.pivotlow(low, lenL, lenR)
 
if not na(phVal)
    ph2 := ph1
    ph2Bar := ph1Bar
    ph1 := phVal
    ph1Bar := bar_index - lenR
    if not na(dnLine)
        line.delete(dnLine)
        dnLine := na
    // Downtrend: latest high is lower than the previous high
    if not na(ph2) and ph1 < ph2
        dnLine := line.new(ph2Bar, ph2, ph1Bar, ph1, extend = extend.right, color = dnCol, width = lineWidth, style = f_style(lineStyle))
 
if not na(plVal)
    pl2 := pl1
    pl2Bar := pl1Bar
    pl1 := plVal
    pl1Bar := bar_index - lenR
    if not na(upLine)
        line.delete(upLine)
        upLine := na
    // Uptrend: latest low is higher than the previous low
    if not na(pl2) and pl1 > pl2
        upLine := line.new(pl2Bar, pl2, pl1Bar, pl1, extend = extend.right, color = upCol, width = lineWidth, style = f_style(lineStyle))
 
// ───────── Break Detection ─────────
bool dnBreak = false
bool upBreak = false
 
if barstate.isconfirmed
    if not na(dnLine)
        dnPrice = line.get_price(dnLine, bar_index)
        if (breakType == "Close" ? close : high) > dnPrice
            dnBreak := true
            if keepBroken
                line.set_extend(dnLine, extend.none)
                line.set_xy2(dnLine, bar_index, dnPrice)
                line.set_style(dnLine, line.style_dotted)
            else
                line.delete(dnLine)
            dnLine := na
 
    if not na(upLine)
        upPrice = line.get_price(upLine, bar_index)
        if (breakType == "Close" ? close : low) < upPrice
            upBreak := true
            if keepBroken
                line.set_extend(upLine, extend.none)
                line.set_xy2(upLine, bar_index, upPrice)
                line.set_style(upLine, line.style_dotted)
            else
                line.delete(upLine)
            upLine := na
 
plotshape(showBreaks and dnBreak, "Downtrend Break", shape.triangleup, location.belowbar, upCol, size = size.small, text = "BO")
plotshape(showBreaks and upBreak, "Uptrend Break", shape.triangledown, location.abovebar, dnCol, size = size.small, text = "BD")
 
// ───────── Alerts ─────────
alertcondition(dnBreak, "Downtrend Line Break (Bullish)", "Price broke above the downtrend line on {{ticker}} ({{interval}})")
alertcondition(upBreak, "Uptrend Line Break (Bearish)", "Price broke below the uptrend line on {{ticker}} ({{interval}})")

← 返回列表