Swing Points by MATA

matatraderid · study · 228 行 · 点赞 209 · TradingView 原页

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

Pine Script

//@version=6
// ═══════════════════════════════════════════════════════════════════════════════
//  Swing Points by MATA
//  HH · HL · LH · LL — every confirmed swing, clearly labelled.
//
//  Original work by MATA TRADER, written from scratch.
//  • Swing highs / lows confirmed by N bars on each side (no repaint after confirmation)
//  • Highs and lows always alternate: two highs in a row keep only the higher one
//    (two lows in a row keep only the lower one), so labels never double up
//  • Each high is compared with the previous high (HH / LH),
//    each low with the previous low (HL / LL)
//  • Trend read-out: HH + HL = uptrend, LH + LL = downtrend, mixed = range
//  • Swing support / resistance: levels from the latest swing lows / highs until a candle closes through them
// ═══════════════════════════════════════════════════════════════════════════════
indicator("Swing Points by MATA", shorttitle = "Swing Points by MATA", overlay = true,
     max_labels_count = 500, max_lines_count = 500)
 
// ───────────────────────────── Palette ─────────────────────────────
C_BULL = #000000   // HH / HL  - black
C_BEAR = #000000   // LH / LL  - black
C_TXT  = #000000   // panel text - black
C_SR   = #000000   // swing support / resistance - black
 
// ───────────────────────────── Inputs ─────────────────────────────
G_SW = "Swings"
swLen     = input.int(5, "Swing Strength", minval = 1, maxval = 50, group = G_SW, tooltip = "Bars on each side needed to confirm a swing high / low.\nLower = more (minor) swings, higher = only major swings. A swing is drawn after this many bars have closed, then it never moves.", display = display.none)
maxShow   = input.int(100, "Max Labels Shown", minval = 4, maxval = 250, group = G_SW, display = display.none)
 
G_LB = "Labels"
showHH    = input.bool(true, "HH", inline = "h", group = G_LB, display = display.none)
showLH    = input.bool(true, "LH", inline = "h", group = G_LB, display = display.none)
showHL    = input.bool(true, "HL", inline = "l", group = G_LB, display = display.none)
showLL    = input.bool(true, "LL", inline = "l", group = G_LB, display = display.none)
colBull   = input.color(C_BULL, "Bullish (HH / HL)", inline = "c", group = G_LB, display = display.none)
colBear   = input.color(C_BEAR, "Bearish (LH / LL)", inline = "c", group = G_LB, display = display.none)
showPrice = input.bool(false, "Show Price", group = G_LB, display = display.none)
lblSize   = input.string("Normal", "Text Size", options = ["Tiny", "Small", "Normal"], group = G_LB, display = display.none)
 
G_SR = "Swing Support / Resistance"
showSR    = input.bool(true, "Show Support / Resistance", group = G_SR, tooltip = "Resistance at the latest swing highs, support at the latest swing lows. A level stays until a candle CLOSES through it, then it is removed.", display = display.none)
srCount   = input.int(3, "Levels Each Side", minval = 1, maxval = 20, group = G_SR, display = display.none)
srStyle   = input.string("Dotted", "Line Style", options = ["Solid", "Dashed", "Dotted"], group = G_SR, display = display.none)
srWidth   = input.int(2, "Line Width", minval = 1, maxval = 4, group = G_SR, display = display.none)
colSR     = input.color(C_SR, "Color", group = G_SR, display = display.none)
srText    = input.bool(true, "Show Label", group = G_SR, display = display.none)
 
G_TR = "Trend Panel"
showTbl   = input.bool(true, "Show Trend Panel", group = G_TR, display = display.none)
tblPos    = input.string("Top Right", "Position", options = ["Top Right", "Bottom Right", "Top Left", "Bottom Left"], group = G_TR, display = display.none)
 
// ───────────────────────────── Helpers ─────────────────────────────
sizeOf(string s)  => s == "Tiny" ? size.tiny : s == "Normal" ? size.normal : size.small
styleOf(string s) => s == "Solid" ? line.style_solid : s == "Dashed" ? line.style_dashed : line.style_dotted
posOf(string s)   => s == "Top Left" ? position.top_left : s == "Bottom Left" ? position.bottom_left : s == "Bottom Right" ? position.bottom_right : position.top_right
 
// ───────────────────────────── State ─────────────────────────────
// last swing:  1 = high, -1 = low, 0 = none yet
var int   lastType = 0
// latest and previous confirmed high / low
var float h1 = na
var float h0 = na
var float l1 = na
var float l0 = na
// label of the latest point (so it can be moved if a better point of the same type appears)
var label lastLbl = na
var array<label> lbls = array.new<label>()
// latest high / low classification for the trend panel
var string hiTag = ""
var string loTag = ""
 
// swing support / resistance levels
type Level
    line  ln
    label lb
    float price
    int   bar
 
var array<Level> resLv = array.new<Level>()
var array<Level> supLv = array.new<Level>()
 
// alert flags
bool aHH = false
bool aLH = false
bool aHL = false
bool aLL = false
 
// ───────────────────────────── Drawing ─────────────────────────────
tagText(string tag, float p) => showPrice ? tag + "\n" + str.tostring(p, format.mintick) : tag
 
tagShown(string tag) => tag == "HH" ? showHH : tag == "LH" ? showLH : tag == "HL" ? showHL : showLL
 
newLabel(int b, float p, string tag, bool isHigh) =>
    label lb = na
    if tagShown(tag)
        color c = tag == "HH" or tag == "HL" ? colBull : colBear
        lb := label.new(b, p, tagText(tag, p), xloc = xloc.bar_index, color = color(na), textcolor = c,
             style = isHigh ? label.style_label_down : label.style_label_up, size = sizeOf(lblSize))
        lbls.push(lb)
        if lbls.size() > maxShow
            label old = lbls.shift()
            old.delete()
    lb
 
addLevel(array<Level> arr, int b, float p, bool isRes) =>
    if showSR
        line  ln = line.new(b, p, bar_index, p, xloc = xloc.bar_index, extend = extend.right, color = colSR, style = styleOf(srStyle), width = srWidth)
        label lb = na
        if srText
            // text sits ON TOP of the line, in capitals
            lb := label.new(bar_index, p, isRes ? "RESISTANCE" : "SUPPORT", xloc = xloc.bar_index, color = color(na), textcolor = colSR,
                 style = label.style_label_lower_left, size = sizeOf(lblSize))
        arr.push(Level.new(ln, lb, p, b))
        if arr.size() > srCount
            Level old = arr.shift()
            old.ln.delete()
            old.lb.delete()
 
// two swings of the same type in a row: move the newest level to the better price
moveLevel(array<Level> arr, int b, float p) =>
    if showSR and arr.size() > 0
        Level lv = arr.last()
        lv.price := p
        lv.bar   := b
        lv.ln.set_xy1(b, p)
        lv.ln.set_y2(p)
        lv.lb.set_y(p)
 
// ───────────────────────────── Swing detection ─────────────────────────────
ph = ta.pivothigh(high, swLen, swLen)
pl = ta.pivotlow(low, swLen, swLen)
int pBar = bar_index - swLen
 
// --- swing high ---
if not na(ph)
    if lastType == 1
        // two highs in a row: keep only the higher one (move the last point up)
        if ph > h1
            h1 := ph
            string tag = na(h0) ? "HH" : ph > h0 ? "HH" : "LH"
            hiTag := tag
            lastLbl.delete()
            lastLbl := newLabel(pBar, ph, tag, true)
            moveLevel(resLv, pBar, ph)
            aHH := tag == "HH"
            aLH := tag == "LH"
    else
        h0 := h1
        h1 := ph
        string tag = na(h0) ? "HH" : ph > h0 ? "HH" : "LH"
        hiTag := tag
        lastLbl   := newLabel(pBar, ph, tag, true)
        addLevel(resLv, pBar, ph, true)
        lastType  := 1
        aHH := tag == "HH"
        aLH := tag == "LH"
 
// --- swing low ---
if not na(pl)
    if lastType == -1
        // two lows in a row: keep only the lower one (move the last point down)
        if pl < l1
            l1 := pl
            string tag = na(l0) ? "LL" : pl < l0 ? "LL" : "HL"
            loTag := tag
            lastLbl.delete()
            lastLbl := newLabel(pBar, pl, tag, false)
            moveLevel(supLv, pBar, pl)
            aLL := tag == "LL"
            aHL := tag == "HL"
    else
        l0 := l1
        l1 := pl
        string tag = na(l0) ? "LL" : pl < l0 ? "LL" : "HL"
        loTag := tag
        lastLbl   := newLabel(pBar, pl, tag, false)
        addLevel(supLv, pBar, pl, false)
        lastType  := -1
        aLL := tag == "LL"
        aHL := tag == "HL"
 
// ───────────────────────────── Support / resistance breaks ─────────────────────────────
bool aResBrk = false
bool aSupBrk = false
if barstate.isconfirmed
    if resLv.size() > 0
        for i = resLv.size() - 1 to 0
            Level lv = resLv.get(i)
            if close > lv.price and bar_index > lv.bar + swLen
                lv.ln.delete()
                lv.lb.delete()
                resLv.remove(i)
                aResBrk := true
    if supLv.size() > 0
        for i = supLv.size() - 1 to 0
            Level lv = supLv.get(i)
            if close < lv.price and bar_index > lv.bar + swLen
                lv.ln.delete()
                lv.lb.delete()
                supLv.remove(i)
                aSupBrk := true
 
// keep the S / R labels at the right edge of the chart
if barstate.islast
    for lv in resLv
        lv.lb.set_x(bar_index + 3)
    for lv in supLv
        lv.lb.set_x(bar_index + 3)
 
// ───────────────────────────── Trend panel ─────────────────────────────
var table panel = table.new(posOf(tblPos), 2, 3, bgcolor = color.new(#000000, 100), frame_width = 0, border_width = 0)
 
if barstate.islast and showTbl
    string trendTxt = hiTag == "HH" and loTag == "HL" ? "Uptrend" : hiTag == "LH" and loTag == "LL" ? "Downtrend" : hiTag == "" or loTag == "" ? "—" : "Range"
    color  trendCol = trendTxt == "Uptrend" ? colBull : trendTxt == "Downtrend" ? colBear : C_TXT
    table.cell(panel, 0, 0, "Swing Structure", text_color = C_TXT, text_size = sizeOf(lblSize), text_halign = text.align_left)
    table.cell(panel, 1, 0, trendTxt, text_color = trendCol, text_size = sizeOf(lblSize), text_halign = text.align_right)
    table.cell(panel, 0, 1, "Last High", text_color = C_TXT, text_size = sizeOf(lblSize), text_halign = text.align_left)
    table.cell(panel, 1, 1, hiTag == "" ? "—" : hiTag + "  " + str.tostring(h1, format.mintick), text_color = hiTag == "HH" ? colBull : colBear, text_size = sizeOf(lblSize), text_halign = text.align_right)
    table.cell(panel, 0, 2, "Last Low", text_color = C_TXT, text_size = sizeOf(lblSize), text_halign = text.align_left)
    table.cell(panel, 1, 2, loTag == "" ? "—" : loTag + "  " + str.tostring(l1, format.mintick), text_color = loTag == "HL" ? colBull : colBear, text_size = sizeOf(lblSize), text_halign = text.align_right)
 
// ───────────────────────────── Alerts ─────────────────────────────
alertcondition(aHH, "New HH", "Swing Points by MATA: new Higher High")
alertcondition(aHL, "New HL", "Swing Points by MATA: new Higher Low")
alertcondition(aLH, "New LH", "Swing Points by MATA: new Lower High")
alertcondition(aLL, "New LL", "Swing Points by MATA: new Lower Low")
alertcondition(aResBrk, "Resistance Broken", "Swing Points by MATA: candle closed above a swing resistance")
alertcondition(aSupBrk, "Support Broken",    "Swing Points by MATA: candle closed below a swing support")

← 返回列表