Dow Swing Structure

Han0315 · study · 118 行 · 点赞 40 · TradingView 原页

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

Pine Script

//@version=6
indicator("Dow Swing Structure", shorttitle="Dow Swings", overlay=true, max_lines_count=500, max_labels_count=500)
 
// ─────────────────────────────────────────
// Inputs
// ─────────────────────────────────────────
pivotLen   = input.int(3, "Pivot length (N)", minval=1, group="Swings")
upCol      = input.color(#26A69A, "Up leg / pivot high", group="Style")
dnCol      = input.color(#EF5350, "Down leg / pivot low", group="Style")
transCol   = input.color(#F7A600, "Transition", group="Style")
lineW      = input.int(2, "Line width", minval=1, maxval=5, group="Style")
showLabels = input.bool(false, "Show HH / HL / LH / LL labels", group="Style")
showBox    = input.bool(true, "Show trend status box", group="Trend box")
boxPosStr  = input.string("Top right", "Box position", options=["Top right", "Top left", "Bottom right", "Bottom left"], group="Trend box")
 
// ─────────────────────────────────────────
// 1) Pivots: confirmed N bars after the pivot bar (no repaint)
// ─────────────────────────────────────────
ph = ta.pivothigh(high, pivotLen, pivotLen)
pl = ta.pivotlow(low, pivotLen, pivotLen)
 
// ─────────────────────────────────────────
// State
// ─────────────────────────────────────────
var int   lastType  = 0      // 1 = last swing is a high, -1 = low, 0 = none yet
var int   lastIdx   = na
var float lastPrice = na
var line  lastLine  = na
var label lastMark  = na
 
var float lastH   = na       // last swing high
var float prevH   = na       // swing high before that
var float lastL   = na       // last swing low
var float prevL   = na       // swing low before that
var bool  hBroken = false    // a close has broken above lastH
var bool  lBroken = false    // a close has broken below lastL
 
tagFor(bool isH, float p, float prevSame) =>
    na(prevSame) ? "" : isH ? (p > prevSame ? "HH" : "LH") : (p > prevSame ? "HL" : "LL")
 
// ─────────────────────────────────────────
// 2) Alternate highs and lows
//    Same type twice in a row -> keep only the more extreme one
// 3) Connect with lines (up leg = up color, down leg = down color)
// ─────────────────────────────────────────
for k = 0 to 1
    bool  isH = k == 0
    float p   = isH ? ph : pl
    if not na(p)
        int   b   = bar_index - pivotLen
        int   t   = isH ? 1 : -1
        color col = isH ? upCol : dnCol
        if lastType == t
            if isH ? p > lastPrice : p < lastPrice
                lastIdx   := b
                lastPrice := p
                if isH
                    lastH   := p
                    hBroken := false
                else
                    lastL   := p
                    lBroken := false
                if not na(lastLine)
                    line.set_xy2(lastLine, b, p)
                label.set_x(lastMark, b)
                label.set_text(lastMark, showLabels ? tagFor(isH, p, isH ? prevH : prevL) : "")
        else
            if isH
                prevH   := lastH
                lastH   := p
                hBroken := false
            else
                prevL   := lastL
                lastL   := p
                lBroken := false
            if not na(lastIdx)
                lastLine := line.new(lastIdx, lastPrice, b, p, color=col, width=lineW)
            lastMark := label.new(b, p, showLabels ? tagFor(isH, p, isH ? prevH : prevL) : "", yloc=isH ? yloc.abovebar : yloc.belowbar, style=isH ? label.style_triangledown : label.style_triangleup, color=col, textcolor=col, size=size.tiny)
            lastType  := t
            lastIdx   := b
            lastPrice := p
 
// ─────────────────────────────────────────
// 4) Dow trend state (closed bars only -> no repaint)
//    Up   : HH + HL, and no close below the last swing low
//    Down : LH + LL, and no close above the last swing high
//    Otherwise: Transition
// ─────────────────────────────────────────
var int trend = 0
if barstate.isconfirmed
    if not na(lastL) and close < lastL
        lBroken := true
    if not na(lastH) and close > lastH
        hBroken := true
    int tr = 0
    if not na(prevH) and not na(prevL)
        if lastH > prevH and lastL > prevL and not lBroken
            tr := 1
        else if lastH < prevH and lastL < prevL and not hBroken
            tr := -1
    trend := tr
 
// ─────────────────────────────────────────
// Trend status box
// ─────────────────────────────────────────
posOf(string s) =>
    switch s
        "Top left"     => position.top_left
        "Bottom right" => position.bottom_right
        "Bottom left"  => position.bottom_left
        => position.top_right
 
var table tbl = table.new(posOf(boxPosStr), 1, 1, border_width=1)
 
if barstate.islast and showBox
    string txt = trend == 1 ? "Trend: Up" : trend == -1 ? "Trend: Down" : "Trend: Transition"
    color  c   = trend == 1 ? upCol : trend == -1 ? dnCol : transCol
    table.cell(tbl, 0, 0, txt, text_color=c, bgcolor=color.new(c, 85), text_size=size.normal)

← 返回列表