本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Auto Triangle Patterns [HexaTrades]", shorttitle = "HexaTrades Triangles", overlay = true, max_lines_count = 500, max_labels_count = 500, max_bars_back = 1000)
// ─────────────────────────────────────────────────────────────────────────────
// Inputs
// ─────────────────────────────────────────────────────────────────────────────
grpD = "Detection"
pivLeft = input.int(5, "Swing strength (bars before)", minval = 1, group = grpD, tooltip = "A swing high/low must be the highest/lowest point for this many bars before it.")
pivRight = input.int(5, "Swing confirmation (bars after)", minval = 1, group = grpD, tooltip = "A swing high/low is confirmed after this many bars, so patterns appear with this delay.")
touches = input.int(2, "Touches per side", minval = 2, maxval = 4, group = grpD, tooltip = "Number of swing highs / swing lows each trendline must pass through.")
flatPct = input.float(25, "Flat line tolerance (% of height)", minval = 5, maxval = 50, step = 5, group = grpD, tooltip = "A trendline counts as flat (horizontal) if it moves less than this % of the triangle's height over the whole pattern. Used for ascending (flat top) and descending (flat bottom) triangles.")
symRatio = input.float(3.0, "Symmetry ratio (max)", minval = 1.5, maxval = 10, step = 0.5, group = grpD, tooltip = "For a symmetrical triangle the steeper line may be at most this many times steeper than the other. Lower = stricter, more balanced triangles.")
fitTol = input.float(0.5, "Touch / containment tolerance (× ATR)", minval = 0.05, step = 0.05, group = grpD, tooltip = "How far swing points may sit off the line, and how far price may poke outside the pattern before it is rejected.")
minBars = input.int(15, "Min pattern length (bars)", minval = 5, group = grpD)
maxBars = input.int(200, "Max pattern length (bars)", minval = 20, maxval = 900, group = grpD)
atrLen = input.int(14, "ATR length", minval = 1, group = grpD)
grpT = "Pattern types"
showAsc = input.bool(true, "Ascending", group = grpT)
showDesc = input.bool(true, "Descending", group = grpT)
showSym = input.bool(true, "Symmetrical", group = grpT)
grpS = "Style"
colAsc = input.color(color.teal, "Ascending", group = grpS)
colDesc = input.color(color.red, "Descending", group = grpS)
colSym = input.color(color.blue, "Symmetrical", group = grpS)
lineW = input.int(2, "Line width", minval = 1, maxval = 5, group = grpS)
showFill = input.bool(true, "Fill pattern", group = grpS)
grpB = "Breakout"
useClose = input.bool(true, "Breakout requires a close beyond the line", group = grpB, tooltip = "Off = a wick through the line counts as a breakout.")
showBO = input.bool(true, "Show breakout arrows", group = grpB)
colBull = input.color(color.green, "Breakout arrow", group = grpB, inline = "arrows")
colBear = input.color(color.red, "Breakdown arrow", group = grpB, inline = "arrows")
// ─────────────────────────────────────────────────────────────────────────────
// Types
// ─────────────────────────────────────────────────────────────────────────────
type Pivot
int x
float y
type Triangle
string kind
int xu // anchor bar of upper line
float yu // anchor price of upper line
float su // upper slope (price per bar)
int xl
float yl
float sl
int startX
int lastPivotX
int apexX
int endX
line upper
line lower
linefill fill
label tag
bool active = true
// ─────────────────────────────────────────────────────────────────────────────
// Helpers
// ─────────────────────────────────────────────────────────────────────────────
// Fits a line through the first and last of the last `n` pivots and checks
// that every pivot in between lies within `tol` of it.
fitLine(array<Pivot> pts, int n, float tol) =>
int sz = pts.size()
bool ok = sz >= n
int x1 = na
float y1 = na
float s = na
if ok
Pivot a = pts.get(sz - n)
Pivot b = pts.get(sz - 1)
x1 := a.x
y1 := a.y
s := (b.y - a.y) / (b.x - a.x)
if n > 2
for i = sz - n + 1 to sz - 2
Pivot p = pts.get(i)
if math.abs(p.y - (y1 + s * (p.x - x1))) > tol
ok := false
[ok, x1, y1, s]
lineAt(int x0, float y0, float s, int x) => y0 + s * (x - x0)
kindColor(string kind) => kind == "Ascending" ? colAsc : kind == "Descending" ? colDesc : colSym
// ─────────────────────────────────────────────────────────────────────────────
// Swing high / low collection
// ─────────────────────────────────────────────────────────────────────────────
var array<Pivot> highs = array.new<Pivot>()
var array<Pivot> lows = array.new<Pivot>()
atr = ta.atr(atrLen)
ph = ta.pivothigh(high, pivLeft, pivRight)
pl = ta.pivotlow(low, pivLeft, pivRight)
if not na(ph)
highs.push(Pivot.new(bar_index - pivRight, ph))
if highs.size() > 10
highs.shift()
if not na(pl)
lows.push(Pivot.new(bar_index - pivRight, pl))
if lows.size() > 10
lows.shift()
// ─────────────────────────────────────────────────────────────────────────────
// Pattern detection (runs only when a new swing high / low is confirmed)
// ─────────────────────────────────────────────────────────────────────────────
var Triangle tri = na
bool newPattern = false
bool bullBreak = false
bool bearBreak = false
if (not na(ph) or not na(pl)) and highs.size() >= touches and lows.size() >= touches and not na(atr)
tol = fitTol * atr
[okU, xu, yu, su] = fitLine(highs, touches, tol)
[okL, xl, yl, sl] = fitLine(lows, touches, tol)
if okU and okL
int startX = math.min(xu, xl)
int lastX = math.max(highs.last().x, lows.last().x)
int span = lastX - startX
float uStart = lineAt(xu, yu, su, startX)
float lStart = lineAt(xl, yl, sl, startX)
float uNow = lineAt(xu, yu, su, bar_index)
float lNow = lineAt(xl, yl, sl, bar_index)
// Textbook shape rules, measured against the triangle's own height
// (widest point) so they don't depend on volatility or timeframe.
float height = uStart - lStart
bool upFlat = height > 0 and math.abs(su * span) <= flatPct / 100 * height
bool loFlat = height > 0 and math.abs(sl * span) <= flatPct / 100 * height
bool upFall = su < 0 and not upFlat
bool loRise = sl > 0 and not loFlat
// Symmetrical = both lines converge at similar angles
bool balanced = upFall and loRise and math.max(-su, sl) / math.min(-su, sl) <= symRatio
// Ascending : flat top, rising bottom
// Descending : falling top, flat bottom
// Symmetrical : falling top, rising bottom, balanced slopes
// Anything else (wedges, rectangles, lopsided shapes) is rejected.
string kind = upFlat and loRise ? "Ascending" : upFall and loFlat ? "Descending" : balanced ? "Symmetrical" : na
bool enabled = kind == "Ascending" ? showAsc : kind == "Descending" ? showDesc : kind == "Symmetrical" ? showSym : false
// Apex = where the two lines intersect
float apexF = su != sl ? (yl - sl * xl - yu + su * xu) / (su - sl) : na
// Highs and lows must interleave (both lines cover the same stretch of time)
bool overlap = xu < lows.last().x and xl < highs.last().x
bool valid = enabled and overlap and span >= minBars and not na(apexF) and apexF > bar_index and apexF - startX <= maxBars and uStart > lStart and uNow > lNow
// Price must have stayed inside the triangle from start until now
if valid
for k = 0 to bar_index - startX
int x = bar_index - k
float u = lineAt(xu, yu, su, x)
float l = lineAt(xl, yl, sl, x)
if high[k] > u + tol or low[k] < l - tol
valid := false
break
// Don't redraw over a pattern that already resolved
if valid and not na(tri) and not tri.active and startX < tri.endX
valid := false
if valid
color c = kindColor(kind)
// Last whole bar before the lines cross (rounding could land past the true apex)
int apexX = int(math.floor(apexF))
if not na(tri) and tri.active and startX <= tri.lastPivotX
// Same pattern got another touch — refine its geometry in place
tri.kind := kind
tri.xu := xu
tri.yu := yu
tri.su := su
tri.xl := xl
tri.yl := yl
tri.sl := sl
tri.startX := startX
tri.lastPivotX := lastX
tri.apexX := apexX
tri.upper.set_xy1(startX, uStart)
tri.upper.set_xy2(bar_index, uNow)
tri.upper.set_color(c)
tri.lower.set_xy1(startX, lStart)
tri.lower.set_xy2(bar_index, lNow)
tri.lower.set_color(c)
tri.fill.set_color(showFill ? color.new(c, 90) : na)
tri.tag.set_xy(startX, uStart)
tri.tag.set_text(kind + " Triangle")
tri.tag.set_color(c)
else
if not na(tri) and tri.active
// A newer pattern replaces one that never broke out
tri.active := false
tri.endX := bar_index
tri.upper.set_style(line.style_dashed)
tri.lower.set_style(line.style_dashed)
tri.tag.set_color(color.new(kindColor(tri.kind), 60))
tri.tag.set_text(tri.kind + " Triangle (no breakout)")
line up = line.new(startX, uStart, bar_index, uNow, color = c, width = lineW)
line lo = line.new(startX, lStart, bar_index, lNow, color = c, width = lineW)
linefill f = linefill.new(up, lo, showFill ? color.new(c, 90) : na)
label t = label.new(startX, uStart, kind + " Triangle", style = label.style_label_down, color = c, textcolor = color.white, size = size.small)
tri := Triangle.new(kind, xu, yu, su, xl, yl, sl, startX, lastX, apexX, na, up, lo, f, t)
newPattern := true
// ─────────────────────────────────────────────────────────────────────────────
// Extend active pattern & watch for breakout / expiry
// ─────────────────────────────────────────────────────────────────────────────
if not na(tri) and tri.active
float uNow = lineAt(tri.xu, tri.yu, tri.su, bar_index)
float lNow = lineAt(tri.xl, tri.yl, tri.sl, bar_index)
tri.upper.set_xy2(bar_index, uNow)
tri.lower.set_xy2(bar_index, lNow)
if barstate.isconfirmed
float upSrc = useClose ? close : high
float dnSrc = useClose ? close : low
color c = kindColor(tri.kind)
bool upHit = upSrc > uNow
bool dnHit = dnSrc < lNow
if upHit and dnHit
// Wick pierced both lines: let the close decide, or no breakout if it closed inside
upHit := close > uNow
dnHit := close < lNow
if upHit
bullBreak := true
tri.active := false
tri.endX := bar_index
alert(tri.kind + " triangle: bullish breakout on " + syminfo.ticker + " at " + str.tostring(close, format.mintick), alert.freq_once_per_bar_close)
else if dnHit
bearBreak := true
tri.active := false
tri.endX := bar_index
alert(tri.kind + " triangle: bearish breakdown on " + syminfo.ticker + " at " + str.tostring(close, format.mintick), alert.freq_once_per_bar_close)
else if bar_index >= tri.apexX or bar_index - tri.startX > maxBars
// Reached the apex without a breakout — pattern failed
tri.active := false
tri.endX := bar_index
tri.upper.set_style(line.style_dashed)
tri.lower.set_style(line.style_dashed)
tri.tag.set_color(color.new(c, 60))
tri.tag.set_text(tri.kind + " Triangle (expired)")
if newPattern
alert(tri.kind + " triangle detected on " + syminfo.ticker, alert.freq_once_per_bar_close)
// ─────────────────────────────────────────────────────────────────────────────
// Alert conditions (for the "Create Alert" dialog)
// ─────────────────────────────────────────────────────────────────────────────
alertcondition(newPattern, "Triangle detected", "Triangle pattern detected on {{ticker}}")
alertcondition(bullBreak, "Triangle bullish breakout", "Triangle bullish breakout on {{ticker}} at {{close}}")
alertcondition(bearBreak, "Triangle bearish breakdown", "Triangle bearish breakdown on {{ticker}} at {{close}}")
// Small arrows on breakout bars
plotshape(showBO and bullBreak, "Breakout", shape.triangleup, location.belowbar, colBull, size = size.tiny, display = display.all - display.status_line)
plotshape(showBO and bearBreak, "Breakdown", shape.triangledown, location.abovebar, colBear, size = size.tiny, display = display.all - display.status_line)