本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// ============================================================================
// Harmonic Patterns Gartley, Bat, Butterfly, Crab & PRZ [LunqFX]
// ----------------------------------------------------------------------------
// Finds the four classic harmonic patterns on confirmed swings, projects the
// potential reversal zone (PRZ) before point D forms, follows price into the
// zone, confirms D, projects the 0.382 and 0.618 AD targets, and keeps a
// record per pattern of how often the PRZ held on this symbol.
//
// NON-REPAINTING: everything is decided on closed bars from confirmed pivots.
// No request.security, no lookahead.
// ============================================================================
indicator("Harmonic Patterns Gartley, Bat, Butterfly, Crab & PRZ [LunqFX]", "Harmonic Patterns [LunqFX]",
overlay = true, max_lines_count = 300, max_labels_count = 400, max_boxes_count = 100, max_polylines_count = 100)
// ─────────────────────────────────────────────────────────────────────────
// INPUTS
// ─────────────────────────────────────────────────────────────────────────
gP_ = "Patterns"
useG = input.bool(true, "Gartley", group = gP_)
useB = input.bool(true, "Bat", group = gP_)
useBf = input.bool(true, "Butterfly", group = gP_)
useC = input.bool(true, "Crab", group = gP_)
side = input.string("Both", "Direction", options = ["Both", "Bullish", "Bearish"], group = gP_)
xdTol = input.float(0.05, "XD tolerance (± part of XA)", minval = 0.0, maxval = 0.15, step = 0.005, group = gP_,
tooltip = "How far the XA retracement of D may sit from the pattern's ratio. The PRZ is where this band meets the BC projection.")
przMin = input.float(0.5, "Min. PRZ height (× ATR)", minval = 0.0, maxval = 3.0, step = 0.1, group = gP_,
tooltip = "A thinner zone is widened around its middle to this height, so a single wick cannot break it.")
gS = "Swings"
pivLen = input.int(4, "Swing strength", minval = 2, maxval = 20, group = gS, tooltip = "Bars on each side that confirm a swing. Higher = larger patterns.")
legMin = input.float(1.0, "Min. swing size (× ATR)", minval = 0.3, maxval = 10.0, step = 0.1, group = gS)
xaMin = input.float(3.0, "Min. XA leg (× ATR)", minval = 1.0, maxval = 30.0, step = 0.5, group = gS)
maxWait = input.int(80, "Max. wait for D (bars)", minval = 10, maxval = 500, group = gS)
maxLeg = input.int(200, "Target expiry (bars)", minval = 20, maxval = 2000, group = gS)
gD = "Drawing"
showTgt = input.bool(true, "Targets after D", group = gD)
showRat = input.bool(true, "Ratios on point labels", group = gD)
keepN = input.int(4, "Patterns drawn", minval = 1, maxval = 20, group = gD, tooltip = "Finished patterns older than this are removed from the chart. The record keeps all of them.")
lblSize = input.string("Normal", "Label size", options = ["Small", "Normal", "Large"], group = gD)
gT = "Panel"
showTbl = input.bool(true, "Show panel", group = gT)
tblPos = input.string("Bottom Center", "Position", options = ["Bottom Center", "Bottom Left", "Bottom Right", "Top Center"], group = gT)
tblSize = input.string("Normal", "Size", options = ["Small", "Normal", "Large"], group = gT)
gA = "Alerts"
alForm = input.bool(true, "Alert: pattern forming (PRZ projected) and price in PRZ", group = gA)
alD = input.bool(true, "Alert: D confirmed, targets hit, PRZ broken", group = gA)
candOn_ = input.bool(true, "Neon candles", group = "Visuals")
candGlw = input.bool(true, "Glow candles", group = "Visuals", tooltip = "Each body is drawn in four bands that brighten towards the close, so the candle reads in the direction it moved. Off = flat translucent bodies.")
// ─────────────────────────────────────────────────────────────────────────
// THEME
// ─────────────────────────────────────────────────────────────────────────
bgLum = 0.2126 * color.r(chart.bg_color) + 0.7152 * color.g(chart.bg_color) + 0.0722 * color.b(chart.bg_color)
isLight = bgLum > 128
UP = isLight ? #2F7FD8 : #6FB7FF // cornflower: bullish patterns, rising candles
DN = isLight ? #C8329B : #FF6FCF // orchid: bearish patterns, falling candles
ACC = isLight ? #6A5CC9 : #E9E4FF // pale lilac: the PRZ
ACCX = isLight ? #4B3FA8 : #C9C0FF
GREY = isLight ? #8A8F9C : #5C6070
TXT = isLight ? #171A24 : #EEF0F7
MUTE = isLight ? #5F6575 : #9CA2B4
PANEL = isLight ? #FFFFFF : #0B0D16
ROW = isLight ? #F1F3F9 : #161927
INK = #0B0D16
NONE = color.new(color.black, 100)
lSize = lblSize == "Small" ? size.small : lblSize == "Large" ? size.large : size.normal
tSize = tblSize == "Small" ? size.small : tblSize == "Large" ? size.large : size.normal
px(float v) =>
str.tostring(v, format.mintick)
pct(int a, int b) =>
b > 0 ? str.tostring(a * 100.0 / b, "0") + "%" : "—"
// ── pattern ratios: Gartley, Bat, Butterfly, Crab
var array<string> NAME = array.from("GARTLEY", "BAT", "BUTTERFLY", "CRAB")
var array<float> AB_MIN = array.from(0.585, 0.382, 0.75, 0.382)
var array<float> AB_MAX = array.from(0.65, 0.5, 0.82, 0.618)
var array<float> XD = array.from(0.786, 0.886, 1.27, 1.618)
var array<float> CD_MIN = array.from(1.13, 1.618, 1.618, 2.24)
var array<float> CD_MAX = array.from(1.618, 2.618, 2.618, 3.618)
BC_MIN = 0.382
BC_MAX = 0.886
useP = array.from(useG, useB, useBf, useC)
// ─────────────────────────────────────────────────────────────────────────
// SERIES (computed on every bar)
// ─────────────────────────────────────────────────────────────────────────
atr = ta.atr(50)
ph = ta.pivothigh(high, pivLen, pivLen)
pl = ta.pivotlow(low, pivLen, pivLen)
pBar = bar_index - pivLen
// ─────────────────────────────────────────────────────────────────────────
// STATE
// stage 1 waiting for D · 2 price inside the PRZ · 3 D confirmed, targets
// dir 1 = bullish pattern (D is a low), -1 = bearish (D is a high)
// points 0..4 = X, A, B, C, D
// ─────────────────────────────────────────────────────────────────────────
var bool act = false
var int stage = 0
var int dir = 0
var int pat = -1
var float przHi = na
var float przLo = na
var float t1 = na
var float t2 = na
var int hitMax = 0
var int dBar = na
var bool dKnown = false
var string lastRes = ""
var array<float> zP = array.new_float() // zigzag of confirmed swings
var array<int> zB = array.new_int()
var array<bool> zH = array.new_bool()
var array<float> hp = array.new_float(5, na)
var array<int> hb = array.new_int(5, na)
// the last finished pattern, shown in the panel again when a newer candidate is dropped
var array<float> sP = array.new_float(5, na)
var array<int> sB = array.new_int(5, na)
var array<float> sV = array.new_float(8, na) // dir, pat, przHi, przLo, t1, t2, hitMax, dKnown
var box przBx = na
var polyline projPl = na
var line t1Ln = na
var line t2Ln = na
var label t1Lb = na
var label t2Lb = na
// record per pattern
var array<int> rD = array.new_int(4, 0) // D confirmed inside the PRZ
var array<int> rBr = array.new_int(4, 0) // PRZ broken before D
var array<int> rT1 = array.new_int(4, 0)
var array<int> rT2 = array.new_int(4, 0)
var array<int> rF = array.new_int(4, 0) // failed after D
evForm = false
evPrz = false
evD = false
evHit = 0
evDone = false
evFail = false
evBrk = false
evExp = false
evDrop = false
bump(array<int> a, int i) =>
array.set(a, i, array.get(a, i) + 1)
// ── drawing registry: objects of a pattern are filed under its id
var int mId = 0
var array<line> gLn = array.new<line>()
var array<int> gLnI = array.new_int()
var array<label> gLb = array.new<label>()
var array<int> gLbI = array.new_int()
var array<box> gBx = array.new<box>()
var array<int> gBxI = array.new_int()
var array<polyline> gPl = array.new<polyline>()
var array<int> gPlI = array.new_int()
regLn(line o) =>
array.push(gLn, o)
array.push(gLnI, mId)
regLb(label o) =>
array.push(gLb, o)
array.push(gLbI, mId)
regBx(box o) =>
array.push(gBx, o)
array.push(gBxI, mId)
regPl(polyline o) =>
array.push(gPl, o)
array.push(gPlI, mId)
prune(int upTo) =>
while array.size(gLnI) > 0 and array.get(gLnI, 0) <= upTo
line.delete(array.shift(gLn))
array.shift(gLnI)
while array.size(gLbI) > 0 and array.get(gLbI, 0) <= upTo
label.delete(array.shift(gLb))
array.shift(gLbI)
while array.size(gBxI) > 0 and array.get(gBxI, 0) <= upTo
box.delete(array.shift(gBx))
array.shift(gBxI)
while array.size(gPlI) > 0 and array.get(gPlI, 0) <= upTo
polyline.delete(array.shift(gPl))
array.shift(gPlI)
// removes a pattern that never reached its PRZ
dropTail(int id) =>
while array.size(gLnI) > 0 and array.last(gLnI) == id
line.delete(array.pop(gLn))
array.pop(gLnI)
while array.size(gLbI) > 0 and array.last(gLbI) == id
label.delete(array.pop(gLb))
array.pop(gLbI)
while array.size(gBxI) > 0 and array.last(gBxI) == id
box.delete(array.pop(gBx))
array.pop(gBxI)
while array.size(gPlI) > 0 and array.last(gPlI) == id
polyline.delete(array.pop(gPl))
array.pop(gPlI)
H(int i) =>
array.get(hp, i)
pt(int i) =>
chart.point.from_index(array.get(hb, i), H(i))
ptLb(int i, string t) =>
isHighPt = (i % 2 == 1) == (dir == 1)
c = dir == 1 ? UP : DN
regLb(label.new(array.get(hb, i), H(i), t, style = isHighPt ? label.style_label_down : label.style_label_up,
color = i == 4 ? ACC : c, textcolor = INK, size = lSize))
rat(string name, float v) =>
showRat ? name + " · " + str.tostring(v, "0.00") : name
// best pattern for the swings X, A, B, C: the one whose PRZ price reaches first
findPat(float x, float a, float b, float c, int dn, float minH) =>
xa = math.abs(a - x)
abl = math.abs(b - a)
bcl = math.abs(c - b)
ab = abl / xa
bc = bcl / abl
int best = -1
float bHi = na
float bLo = na
if bc >= BC_MIN and bc <= BC_MAX
for i = 0 to 3
if array.get(useP, i) and ab >= array.get(AB_MIN, i) and ab <= array.get(AB_MAX, i)
xd = array.get(XD, i)
z1a = a - dn * (xd - xdTol) * xa
z1b = a - dn * (xd + xdTol) * xa
z2a = c - dn * array.get(CD_MIN, i) * bcl
z2b = c - dn * array.get(CD_MAX, i) * bcl
hi = math.min(math.max(z1a, z1b), math.max(z2a, z2b))
lo = math.max(math.min(z1a, z1b), math.min(z2a, z2b))
if hi > lo
if hi - lo < minH
mid = (hi + lo) / 2
hi := mid + minH / 2
lo := mid - minH / 2
near = dn == 1 ? hi : lo
bNear = dn == 1 ? bHi : bLo
if best == -1 or (near - bNear) * dn > 0
best := i
bHi := hi
bLo := lo
[best, bHi, bLo]
// ─────────────────────────────────────────────────────────────────────────
// PATTERN — closed bars only
// ─────────────────────────────────────────────────────────────────────────
if barstate.isconfirmed and not na(atr)
zChanged = false
near = dir == 1 ? przHi : przLo
far = dir == 1 ? przLo : przHi
c = dir == 1 ? UP : DN
// ── bar checks
if act and stage == 1
tow = dir == 1 ? low : high
if (near - tow) * dir >= 0
stage := 2
evPrz := true
if (far - close) * dir > 0
evBrk := true
else if bar_index - array.get(hb, 3) > maxWait
evDrop := true
else if act and stage == 2
if (far - close) * dir > 0
evBrk := true
else if bar_index - array.get(hb, 3) > 2 * maxWait
evDrop := true
else if act and stage == 3
fav = dir == 1 ? high : low
prevHit = hitMax
if (fav - t2) * dir >= 0
hitMax := 2
else if hitMax < 1 and (fav - t1) * dir >= 0
hitMax := 1
if hitMax > prevHit
evHit := hitMax
if showTgt
if prevHit < 1
label.set_text(t1Lb, "✓ " + label.get_text(t1Lb))
if hitMax == 2
label.set_text(t2Lb, "✓ " + label.get_text(t2Lb))
if hitMax == 2
evDone := true
else if (far - close) * dir > 0
evFail := true
else if bar_index - dBar > maxLeg
evExp := true
// ── confirmed swings
lastIsHigh = array.size(zH) > 0 and array.last(zH)
for i = 0 to 1
evH = i == 0 ? not lastIsHigh : lastIsHigh
pv = evH ? ph : pl
if not na(pv)
accepted = false
n = array.size(zP)
if n == 0
array.push(zP, pv)
array.push(zB, pBar)
array.push(zH, evH)
accepted := true
else if array.last(zH) == evH
if (pv - array.last(zP)) * (evH ? 1 : -1) > 0
array.set(zP, n - 1, pv)
array.set(zB, n - 1, pBar)
accepted := true
else if (pv - array.last(zP)) * (evH ? 1 : -1) >= legMin * atr and pBar > array.last(zB)
array.push(zP, pv)
array.push(zB, pBar)
array.push(zH, evH)
accepted := true
if array.size(zP) > 8
array.shift(zP)
array.shift(zB)
array.shift(zH)
if accepted
zChanged := true
if accepted and act and stage <= 2 and not evDrop and not evBrk
isC = evH == (dir == 1) // a swing of the same kind as C
if isC
// C was exceeded, or price turned before reaching the PRZ
evDrop := true
else if stage == 2 and (near - pv) * dir >= 0
// D: the confirmed swing that ended the move into the PRZ
stage := 3
evD := true
dKnown := true
dBar := pBar
array.set(hp, 4, pv)
array.set(hb, 4, pBar)
ad = math.abs(H(1) - pv)
t1 := pv + dir * 0.382 * ad
t2 := pv + dir * 0.618 * ad
hitMax := 0
polyline.delete(projPl)
regPl(polyline.new(array.from(pt(2), pt(3), pt(4)), closed = true, line_color = color.new(c, 10), fill_color = color.new(c, 84), line_width = 2))
ptLb(4, rat("D", math.abs(H(1) - pv) / math.abs(H(1) - H(0))))
box.set_right(przBx, pBar + 3)
if showTgt
t1Ln := line.new(pBar, t1, bar_index + 4, t1, color = color.new(c, 10), width = 2)
t2Ln := line.new(pBar, t2, bar_index + 4, t2, color = color.new(c, 10), width = 2, style = line.style_dashed)
t1Lb := label.new(bar_index + 4, t1, "T1 · 0.382 AD " + px(t1), style = label.style_label_left, color = INK, textcolor = c, size = lSize)
t2Lb := label.new(bar_index + 4, t2, "T2 · 0.618 AD " + px(t2), style = label.style_label_left, color = INK, textcolor = c, size = lSize)
regLn(t1Ln)
regLn(t2Ln)
regLb(t1Lb)
regLb(t2Lb)
// ── endings
if evDrop
dropTail(mId)
act := false
stage := 0
for k = 0 to 4
array.set(hp, k, array.get(sP, k))
array.set(hb, k, array.get(sB, k))
if not na(array.get(sV, 0))
dir := int(array.get(sV, 0))
pat := int(array.get(sV, 1))
przHi := array.get(sV, 2)
przLo := array.get(sV, 3)
t1 := array.get(sV, 4)
t2 := array.get(sV, 5)
hitMax := int(array.get(sV, 6))
dKnown := array.get(sV, 7) > 0
else if evBrk
bump(rBr, pat)
regLb(label.new(bar_index, dir == 1 ? low : high, "✕ PRZ BROKEN", style = dir == 1 ? label.style_label_up : label.style_label_down, color = GREY, textcolor = color.white, size = lSize))
box.set_right(przBx, bar_index)
lastRes := "PRZ broken"
act := false
stage := 0
prune(mId - keepN)
else if evDone or evFail or evExp
bump(rD, pat)
if hitMax >= 1
bump(rT1, pat)
if hitMax >= 2
bump(rT2, pat)
if evFail
bump(rF, pat)
regLb(label.new(bar_index, dir == 1 ? low : high, "✕ FAILED", style = dir == 1 ? label.style_label_up : label.style_label_down, color = GREY, textcolor = color.white, size = lSize))
if showTgt
line.set_x2(t1Ln, bar_index)
line.set_x2(t2Ln, bar_index)
label.set_x(t1Lb, bar_index + 1)
label.set_x(t2Lb, bar_index + 1)
lastRes := evDone ? "T2 reached" : evFail ? "failed after D" : hitMax >= 1 ? "T1 reached" : "expired"
act := false
stage := 0
prune(mId - keepN)
if (evBrk or evDone or evFail or evExp)
for k = 0 to 4
array.set(sP, k, array.get(hp, k))
array.set(sB, k, array.get(hb, k))
array.set(sV, 0, dir)
array.set(sV, 1, pat)
array.set(sV, 2, przHi)
array.set(sV, 3, przLo)
array.set(sV, 4, t1)
array.set(sV, 5, t2)
array.set(sV, 6, hitMax)
array.set(sV, 7, dKnown ? 1 : 0)
// ── searching: the last four swings may be X, A, B, C
if not act and zChanged and array.size(zP) >= 4
n = array.size(zP)
x = array.get(zP, n - 4)
a = array.get(zP, n - 3)
b = array.get(zP, n - 2)
cc = array.get(zP, n - 1)
dn = array.get(zH, n - 1) ? 1 : -1
allowed = side == "Both" or (side == "Bullish" and dn == 1) or (side == "Bearish" and dn == -1)
if allowed and math.abs(a - x) >= xaMin * atr
[best, hi, lo] = findPat(x, a, b, cc, dn, przMin * atr)
farN = dn == 1 ? lo : hi
if best >= 0 and (farN - close) * dn <= 0
mId += 1
act := true
stage := 1
dir := dn
pat := best
przHi := hi
przLo := lo
dKnown := false
hitMax := 0
evForm := true
for k = 0 to 3
array.set(hp, k, array.get(zP, n - 4 + k))
array.set(hb, k, array.get(zB, n - 4 + k))
array.set(hp, 4, na)
array.set(hb, 4, na)
cN = dn == 1 ? UP : DN
regPl(polyline.new(array.from(pt(0), pt(1), pt(2)), closed = true, line_color = color.new(cN, 10), fill_color = color.new(cN, 84), line_width = 2))
regLn(line.new(array.get(hb, 2), b, array.get(hb, 3), cc, color = color.new(cN, 10), width = 2))
ptLb(0, "X")
ptLb(1, "A")
ptLb(2, rat("B", math.abs(b - a) / math.abs(a - x)))
ptLb(3, rat("C", math.abs(cc - b) / math.abs(a - b)))
cBar = array.get(hb, 3)
span = math.max(cBar - array.get(hb, 2), 3)
przBx := box.new(cBar, hi, bar_index + 5, lo, border_color = color.new(ACC, 35), border_width = 2, bgcolor = color.new(ACC, 86),
text = "PRZ", text_color = ACCX, text_size = size.small,
text_halign = text.align_right, text_valign = dn == 1 ? text.align_bottom : text.align_top)
regBx(przBx)
projPl := polyline.new(array.from(chart.point.from_index(cBar, cc), chart.point.from_index(cBar + span, (hi + lo) / 2)), line_color = color.new(cN, 40), line_width = 2, line_style = line.style_dashed)
regPl(projPl)
// running drawings follow the current bar
if act and stage <= 2
box.set_right(przBx, bar_index + 5)
if act and stage == 3 and showTgt
line.set_x2(t1Ln, bar_index + 4)
line.set_x2(t2Ln, bar_index + 4)
label.set_x(t1Lb, bar_index + 4)
label.set_x(t2Lb, bar_index + 4)
// ─────────────────────────────────────────────────────────────────────────
// PANEL — one strip along the bottom: the real shape of the pattern in
// table cells, then a row of tiles
// ─────────────────────────────────────────────────────────────────────────
pos = tblPos == "Bottom Left" ? position.bottom_left : tblPos == "Bottom Right" ? position.bottom_right : tblPos == "Top Center" ? position.top_center : position.bottom_center
vSize = tblSize == "Small" ? size.normal : tblSize == "Large" ? size.huge : size.large
cellW = tblSize == "Small" ? 0.42 : tblSize == "Large" ? 0.72 : 0.56 // % of the chart
cellH = tblSize == "Small" ? 0.95 : tblSize == "Large" ? 1.55 : 1.2
tileW = tblSize == "Small" ? 6.5 : tblSize == "Large" ? 10.5 : 8.5
MAP_C = 22
MAP_R = 10
N_T = 5
var table tb = na
rowOf(float p, float top, float bot) =>
1 + int(math.round((top - p) / (top - bot) * (MAP_R - 3)))
// a tile: caption on top, value below, a transparent column before it as the gap
tile(int k, string cap, string val, color fg, float wMul = 1.0) =>
c = MAP_C + 1 + 2 * k
table.cell(tb, c - 1, 0, "", bgcolor = NONE, width = 0.25)
table.merge_cells(tb, c - 1, 0, c - 1, MAP_R - 1)
table.cell(tb, c, 0, cap, text_color = MUTE, bgcolor = PANEL, text_size = tSize, width = tileW * wMul, text_valign = text.align_bottom)
table.merge_cells(tb, c, 0, c, 3)
table.cell(tb, c, 4, val, text_color = fg, bgcolor = PANEL, text_size = vSize, width = tileW * wMul, text_valign = text.align_top)
table.merge_cells(tb, c, 4, c, MAP_R - 1)
if barstate.islast and showTbl
table.delete(tb) // rebuilt each time: merged cells cannot be merged twice
has = not na(H(0))
mc = not has ? GREY : dir == 1 ? UP : DN
tb := table.new(pos, MAP_C + 2 * N_T, MAP_R, bgcolor = NONE, border_width = 0, frame_color = NONE, frame_width = 0)
for r = 0 to MAP_R - 1
for cl = 0 to MAP_C - 1
table.cell(tb, cl, r, "", bgcolor = PANEL, width = cellW, height = cellH)
// mini shape: X-A-B-C-D in its real proportions, the PRZ as a band
if has
dP = dKnown ? H(4) : (przHi + przLo) / 2
dB = dKnown ? array.get(hb, 4) : array.get(hb, 3) + math.max(array.get(hb, 3) - array.get(hb, 2), 3)
ps = array.from(H(0), H(1), H(2), H(3), dP)
bs = array.from(array.get(hb, 0), array.get(hb, 1), array.get(hb, 2), array.get(hb, 3), dB)
top = math.max(array.max(ps), przHi)
bot = math.min(array.min(ps), przLo)
cols = array.new_int()
for k = 0 to 4
cl = 1 + int(math.round((array.get(bs, k) - array.get(bs, 0)) * 1.0 / math.max(dB - array.get(bs, 0), 1) * (MAP_C - 3)))
if k > 0
cl := math.max(cl, array.get(cols, k - 1) + 1)
array.push(cols, math.min(cl, MAP_C - 2))
przC = math.min(array.get(cols, 3) + 1, MAP_C - 2)
for cl = przC to MAP_C - 2
for rr = rowOf(przHi, top, bot) to rowOf(przLo, top, bot)
table.cell_set_bgcolor(tb, cl, rr, color.new(ACC, isLight ? 70 : 80))
int prevR = na
for k = 1 to 4
c0 = array.get(cols, k - 1)
c1 = array.get(cols, k)
r0 = rowOf(array.get(ps, k - 1), top, bot)
r1 = rowOf(array.get(ps, k), top, bot)
col = not act ? color.new(mc, 45) : k == 4 and not dKnown ? color.new(mc, 62) : mc
if c1 > c0
for cl = c0 to c1
rNow = int(math.round(r0 + (r1 - r0) * (cl - c0) * 1.0 / (c1 - c0)))
fromR = na(prevR) ? rNow : prevR
for rr = math.min(fromR, rNow) to math.max(fromR, rNow)
table.cell_set_bgcolor(tb, cl, rr, col)
prevR := rNow
// tiles
nD = has ? array.get(rD, pat) : 0
nZ = has ? nD + array.get(rBr, pat) : 0
stg = not has ? "searching" : act ? (stage == 1 ? "wait D" : stage == 2 ? "in PRZ" : hitMax == 2 ? "T2 ✓" : hitMax == 1 ? "T1 ✓" : "D ✓") :
lastRes == "T2 reached" ? "T2 ✓" : lastRes == "T1 reached" ? "T1 ✓" : lastRes == "PRZ broken" ? "broken" : lastRes == "failed after D" ? "failed" : "expired"
tile(0, act or not has ? "PATTERN" : "LAST PATTERN", has ? array.get(NAME, pat) + (dir == 1 ? " ▲" : " ▼") : "—", has ? mc : MUTE)
tile(1, "STAGE", stg, act ? TXT : MUTE)
tile(2, "PRZ", has ? px(przHi) + "\n" + px(przLo) : "—", has ? ACCX : MUTE, 1.35)
tile(3, "HELD · " + str.tostring(nZ), has ? pct(nD, nZ) : "—", TXT)
tile(4, "T2", has ? pct(array.get(rT2, pat), nD) : "—", TXT)
// ─────────────────────────────────────────────────────────────────────────
// ALERTS
// ─────────────────────────────────────────────────────────────────────────
pName = pat >= 0 ? (dir == 1 ? "bullish " : "bearish ") + array.get(NAME, pat) : ""
if alForm and evForm
alert(syminfo.ticker + " Harmonic: " + pName + " forming, PRZ " + px(przLo) + " – " + px(przHi), alert.freq_once_per_bar_close)
if alForm and evPrz
alert(syminfo.ticker + " Harmonic: price inside the PRZ of the " + pName, alert.freq_once_per_bar_close)
if alD and evD
alert(syminfo.ticker + " Harmonic: " + pName + " D confirmed · T1 " + px(t1) + " · T2 " + px(t2), alert.freq_once_per_bar_close)
if alD and evHit > 0
alert(syminfo.ticker + " Harmonic: " + pName + " target T" + str.tostring(evHit) + " hit", alert.freq_once_per_bar_close)
if alD and evBrk
alert(syminfo.ticker + " Harmonic: " + pName + " PRZ broken", alert.freq_once_per_bar_close)
alertcondition(evForm, "Pattern forming", "{{ticker}} Harmonic pattern forming, PRZ projected")
alertcondition(evPrz, "Price in PRZ", "{{ticker}} Harmonic: price inside the PRZ")
alertcondition(evD, "D confirmed", "{{ticker}} Harmonic: D confirmed")
alertcondition(evBrk, "PRZ broken", "{{ticker}} Harmonic: PRZ broken")
// ─────────────────────────────────────────────────────────────────────────
// GLOW CANDLES (LunqFX house style)
// ─────────────────────────────────────────────────────────────────────────
GLOW_UP_DARK = #6FB7FF
GLOW_DN_DARK = #FF6FCF
GLOW_UP_LIGHT = #2F7FD8
GLOW_DN_LIGHT = #C8329B
glowUp = isLight ? GLOW_UP_LIGHT : GLOW_UP_DARK
glowDn = isLight ? GLOW_DN_LIGHT : GLOW_DN_DARK
glowEdge = close >= open ? glowUp : glowDn
glowOn = candOn_ and candGlw
GLOW_NONE = color.new(color.black, 100)
plotcandle(candOn_ and not glowOn ? open : na, high, low, close, "Candles", color = color.new(glowEdge, 24), wickcolor = color.new(glowEdge, 18), bordercolor = glowEdge, display = display.pane)
gQ = (close - open) / 4.0
plotcandle(glowOn ? open : na, glowOn ? math.max(open, close) : na, glowOn ? math.min(open, close) : na, glowOn ? close : na, "Glow mask", color = chart.bg_color, wickcolor = GLOW_NONE, bordercolor = chart.bg_color, display = display.pane)
plotcandle(glowOn ? open : na, glowOn ? math.max(open, open + gQ) : na, glowOn ? math.min(open, open + gQ) : na, glowOn ? open + gQ : na, "Glow band 1", color = color.new(glowEdge, 78), wickcolor = GLOW_NONE, bordercolor = GLOW_NONE, display = display.pane)
plotcandle(glowOn ? open + gQ : na, glowOn ? math.max(open + gQ, open + 2 * gQ) : na, glowOn ? math.min(open + gQ, open + 2 * gQ) : na, glowOn ? open + 2 * gQ : na, "Glow band 2", color = color.new(glowEdge, 58), wickcolor = GLOW_NONE, bordercolor = GLOW_NONE, display = display.pane)
plotcandle(glowOn ? open + 2 * gQ : na, glowOn ? math.max(open + 2 * gQ, open + 3 * gQ) : na, glowOn ? math.min(open + 2 * gQ, open + 3 * gQ) : na, glowOn ? open + 3 * gQ : na, "Glow band 3", color = color.new(glowEdge, 36), wickcolor = GLOW_NONE, bordercolor = GLOW_NONE, display = display.pane)
plotcandle(glowOn ? open + 3 * gQ : na, glowOn ? math.max(open + 3 * gQ, close) : na, glowOn ? math.min(open + 3 * gQ, close) : na, glowOn ? close : na, "Glow band 4", color = color.new(glowEdge, 8), wickcolor = GLOW_NONE, bordercolor = GLOW_NONE, display = display.pane)
plotcandle(glowOn ? open : na, glowOn ? high : na, glowOn ? low : na, glowOn ? close : na, "Glow wick", color = GLOW_NONE, wickcolor = color.new(glowEdge, 10), bordercolor = color.new(glowEdge, 30), display = display.pane)