本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Tri-Tier Swing Tracker", shorttitle = "TTST", overlay = true,
max_lines_count = 500, max_labels_count = 500)
var string GRP_S = "── Tier 1 (Minor Swings)"
var string GRP_M = "── Tier 2 (Intermediate Swings)"
var string GRP_L = "── Tier 3 (Major Swings)"
var string GRP_A = "── Alerts"
s_on = input.bool(true, title = "Show", group = GRP_S, inline = "sv")
s_ln = input.bool(true, title = "Lines", group = GRP_S, inline = "sv",
tooltip = "Toggle the swing lines on/off independently of labels")
s_lb = input.bool(true, title = "H/L Labels", group = GRP_S, inline = "sv")
s_ext = input.bool(true, title = "Extend Last", group = GRP_S, inline = "sv",
tooltip = "Draw a live dashed line from the last confirmed pivot to the current bar (repaints)")
s_lft = input.int(5, title = "Left", group = GRP_S, inline = "sp", minval = 1)
s_rgt = input.int(3, title = "Right", group = GRP_S, inline = "sp", minval = 1,
tooltip = "Left = bars before the pivot | Right = bars after the pivot\nHigher values = fewer, larger swings")
s_clr = input.color(color.new(#2962FF, 0), title = "Color", group = GRP_S, inline = "ss")
s_wid = input.int(1, title = "Width", group = GRP_S, inline = "ss", minval = 1, maxval = 4)
m_on = input.bool(true, title = "Show", group = GRP_M, inline = "mv")
m_ln = input.bool(true, title = "Lines", group = GRP_M, inline = "mv",
tooltip = "Toggle the swing lines on/off independently of labels")
m_lb = input.bool(true, title = "H/L Labels", group = GRP_M, inline = "mv")
m_ext = input.bool(true, title = "Extend Last", group = GRP_M, inline = "mv")
m_lft = input.int(13, title = "Left", group = GRP_M, inline = "mp", minval = 1)
m_rgt = input.int(5, title = "Right", group = GRP_M, inline = "mp", minval = 1)
m_clr = input.color(color.new(#FF1744, 0), title = "Color", group = GRP_M, inline = "ms")
m_wid = input.int(2, title = "Width", group = GRP_M, inline = "ms", minval = 1, maxval = 4)
l_on = input.bool(true, title = "Show", group = GRP_L, inline = "lv")
l_ln = input.bool(true, title = "Lines", group = GRP_L, inline = "lv",
tooltip = "Toggle the swing lines on/off independently of labels")
l_lb = input.bool(true, title = "H/L Labels", group = GRP_L, inline = "lv")
l_ext = input.bool(true, title = "Extend Last", group = GRP_L, inline = "lv")
l_lft = input.int(34, title = "Left", group = GRP_L, inline = "lp", minval = 1)
l_rgt = input.int(8, title = "Right", group = GRP_L, inline = "lp", minval = 1)
l_clr = input.color(color.new(#00C853, 0), title = "Color", group = GRP_L, inline = "ls")
l_wid = input.int(3, title = "Width", group = GRP_L, inline = "ls", minval = 1, maxval = 4)
alertHi = input.bool(true, title = "Alert — New High Pivot", group = GRP_A)
alertLo = input.bool(true, title = "Alert — New Low Pivot", group = GRP_A)
swingTracker(leftBars, rightBars, clr, wid, showLabels, showExt, showTier, showLines) =>
var float anchorPx = na
var int anchorIx = na
var float tipPx = na
var int tipIx = na
var int tipDir = 0
var line tipLine = na
var label tipLabel = na
var line liveLine = na
canDraw = showTier and showLines
hiPivot = ta.pivothigh(high, leftBars, rightBars)
loPivot = ta.pivotlow(low, leftBars, rightBars)
hasHi = not na(hiPivot)
hasLo = not na(loPivot)
pivIx = bar_index - rightBars
newHi = false
newLo = false
if hasHi
if tipDir == 1
if hiPivot > tipPx
if not na(tipLine)
line.delete(tipLine)
if not na(tipLabel)
label.delete(tipLabel)
tipPx := hiPivot
tipIx := pivIx
if canDraw and not na(anchorIx)
tipLine := line.new(anchorIx, anchorPx, pivIx, hiPivot,
color = clr, width = wid, style = line.style_solid)
if showTier and showLabels
tipLabel := label.new(pivIx, hiPivot, "H",
style = label.style_label_down,
color = color.new(clr, 70),
textcolor = clr, size = size.tiny)
newHi := true
else
anchorPx := tipPx
anchorIx := tipIx
tipLine := na
tipLabel := na
tipPx := hiPivot
tipIx := pivIx
tipDir := 1
if canDraw and not na(anchorIx)
tipLine := line.new(anchorIx, anchorPx, pivIx, hiPivot,
color = clr, width = wid, style = line.style_solid)
if showTier and showLabels
tipLabel := label.new(pivIx, hiPivot, "H",
style = label.style_label_down,
color = color.new(clr, 70),
textcolor = clr, size = size.tiny)
newHi := true
if hasLo
if tipDir == -1
if loPivot < tipPx
if not na(tipLine)
line.delete(tipLine)
if not na(tipLabel)
label.delete(tipLabel)
tipPx := loPivot
tipIx := pivIx
if canDraw and not na(anchorIx)
tipLine := line.new(anchorIx, anchorPx, pivIx, loPivot,
color = clr, width = wid, style = line.style_solid)
if showTier and showLabels
tipLabel := label.new(pivIx, loPivot, "L",
style = label.style_label_up,
color = color.new(clr, 70),
textcolor = clr, size = size.tiny)
newLo := true
else
anchorPx := tipPx
anchorIx := tipIx
tipLine := na
tipLabel := na
tipPx := loPivot
tipIx := pivIx
tipDir := -1
if canDraw and not na(anchorIx)
tipLine := line.new(anchorIx, anchorPx, pivIx, loPivot,
color = clr, width = wid, style = line.style_solid)
if showTier and showLabels
tipLabel := label.new(pivIx, loPivot, "L",
style = label.style_label_up,
color = color.new(clr, 70),
textcolor = clr, size = size.tiny)
newLo := true
if canDraw and showExt and not na(tipIx) and tipDir != 0
if not na(liveLine)
line.delete(liveLine)
liveTip = tipDir == 1 ? low : high
liveLine := line.new(tipIx, tipPx, bar_index, liveTip,
color = color.new(clr, 55), width = wid,
style = line.style_dashed)
if not canDraw and not na(liveLine)
line.delete(liveLine)
liveLine := na
[newHi, newLo]
[sHi, sLo] = swingTracker(s_lft, s_rgt, s_clr, s_wid, s_lb, s_ext, s_on, s_ln)
[mHi, mLo] = swingTracker(m_lft, m_rgt, m_clr, m_wid, m_lb, m_ext, m_on, m_ln)
[lHi, lLo] = swingTracker(l_lft, l_rgt, l_clr, l_wid, l_lb, l_ext, l_on, l_ln)
alertcondition(alertHi and (sHi or mHi or lHi),
title = "Any Tier — New High Pivot",
message = "TTST ▲ New HIGH pivot confirmed (any tier)")
alertcondition(alertLo and (sLo or mLo or lLo),
title = "Any Tier — New Low Pivot",
message = "TTST ▼ New LOW pivot confirmed (any tier)")
alertcondition(alertHi and sHi,
title = "Tier 1 — New High Pivot",
message = "TTST ▲ Tier 1 (Minor) — new HIGH pivot confirmed")
alertcondition(alertLo and sLo,
title = "Tier 1 — New Low Pivot",
message = "TTST ▼ Tier 1 (Minor) — new LOW pivot confirmed")
alertcondition(alertHi and mHi,
title = "Tier 2 — New High Pivot",
message = "TTST ▲ Tier 2 (Intermediate) — new HIGH pivot confirmed")
alertcondition(alertLo and mLo,
title = "Tier 2 — New Low Pivot",
message = "TTST ▼ Tier 2 (Intermediate) — new LOW pivot confirmed")
alertcondition(alertHi and lHi,
title = "Tier 3 — New High Pivot",
message = "TTST ▲ Tier 3 (Major) — new HIGH pivot confirmed")
alertcondition(alertLo and lLo,
title = "Tier 3 — New Low Pivot",
message = "TTST ▼ Tier 3 (Major) — new LOW pivot confirmed")