The Harmonic Patterns indicator finds Gartley, Bat, Butterfly and Crab patterns on confirmed swings and projects the potential reversal zone (PRZ) as soon as X, A, B and C are in place, before point D exists. On one chart that is easy to follow. Across a whole list it is not: a pattern can wait weeks for price to reach its zone, and the moment that matters is when price actually gets there.
This screener runs the same engine over every stock in a list and returns the ones where, in the last few bars, price has just entered the PRZ of an active pattern or D has just been confirmed inside it. Those are the two moments when the stage shown on the indicator’s panel changes to “in PRZ” and to “D confirmed”.
pivLen bars on each side, with a minimum leg size in ATR, exactly as in the indicator.pivLen bars after it to be confirmed, D itself is pivLen bars old (4 by default) when it is listed.Everything else (expiry, a C that is exceeded before the zone, a new pattern replacing an older one) follows the indicator line for line. The screener keeps the full state machine on purpose: a simplified copy would list patterns the indicator has already discarded.
Sorting by Stage puts the confirmed D patterns on top. Sorting by Pattern groups bullish and bearish setups apart.
Measured on the current members of the S&P 500, daily bars from January 2019:
Harmonic patterns on daily bars are rare, so this is a short list by nature. The window of 3 bars is there so that a setup is not lost if you skip a day: price is usually still near the zone two bars later. Set maxAge to 1 to see only the last bar.
Keep the pattern settings matched to the ones you use on the chart, or the list and the chart will not agree.
side set to 1, the scan returns only bullish patterns, which fits a long-only watchlist.One limit worth knowing: ProScreener calculates on a limited history (1024 bars with a Premium licence, 255 with Complete), while a chart can hold many more. The swings and the patterns are built from that history, so the screener can occasionally be on a different pattern from the chart. With 255 bars the list will be shorter still. If a listed stock does not show the same zone on the chart, check how many bars the chart is loading.
//---------------------------------------------------------------
// PRC_Harmonic Patterns Screener
// version = 0
// 25.11.2026
// Iván González @ www.prorealcode.com
// Author: LunqFX (Harmonic Patterns indicator)
// Sharing ProRealTime knowledge
//--------------------------------------------------------------------//
// Lists the stocks where, in the last bars, price has just entered the PRZ
// of an active harmonic pattern (Gartley, Bat, Butterfly or Crab) or D has
// just been confirmed inside it. Same engine and defaults as the indicator.
//--------------------------------------------------------------------//
//=== INPUTS ===
//--------------------------------------------------------------------//
// --- Patterns ---
useG = 1 // Gartley
useB = 1 // Bat
useBf = 1 // Butterfly
useC = 1 // Crab
side = 0 // 0 = Both, 1 = Bullish only, 2 = Bearish only
xdTol = 0.05 // XD tolerance (+/- part of XA): the PRZ is where this band meets the BC projection
przMin = 0.5 // minimum PRZ height (x ATR): a thinner zone is widened around its middle
// --- Swings ---
pivLen = 4 // swing strength: bars on each side that confirm a swing
legMin = 1.0 // minimum swing size (x ATR)
xaMin = 3.0 // minimum XA leg (x ATR)
maxWait = 80 // maximum wait for D (bars after C)
maxLeg = 200 // target expiry (bars after D)
//--------------------------------------------------------------------//
//=== PATTERN RATIOS: 0 Gartley, 1 Bat, 2 Butterfly, 3 Crab ===
//--------------------------------------------------------------------//
$abMn[0] = 0.585
$abMn[1] = 0.382
$abMn[2] = 0.75
$abMn[3] = 0.382
$abMx[0] = 0.65
$abMx[1] = 0.5
$abMx[2] = 0.82
$abMx[3] = 0.618
$xdR[0] = 0.786
$xdR[1] = 0.886
$xdR[2] = 1.27
$xdR[3] = 1.618
$cdMn[0] = 1.13
$cdMn[1] = 1.618
$cdMn[2] = 1.618
$cdMn[3] = 2.24
$cdMx[0] = 1.618
$cdMx[1] = 2.618
$cdMx[2] = 2.618
$cdMx[3] = 3.618
$useP[0] = useG
$useP[1] = useB
$useP[2] = useBf
$useP[3] = useC
//--------------------------------------------------------------------//
//=== STATE ===
// Same engine as the indicator, bar by bar over the screener history.
// 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)
//--------------------------------------------------------------------//
ONCE act = 0
ONCE stage = 0
ONCE dir = 0
ONCE pat = 0 - 1
ONCE przHi = 0
ONCE przLo = 0
ONCE t1 = 0
ONCE t2 = 0
ONCE hitMax = 0
ONCE dBar = 0
ONCE dKnown = 0
ONCE nTot = 0
ONCE nA = 0
ONCE lvP = 0
ONCE lvB = 0
ONCE lvH = 0
ONCE sOk = 0
//--------------------------------------------------------------------//
//=== SERIES ===
//--------------------------------------------------------------------//
atrRaw = averagetruerange[50](close)
hhP = highest[pivLen](high)
llP = lowest[pivLen](low)
evPrz = 0
evD = 0
evDone = 0
evFail = 0
evBrk = 0
evExp = 0
evDrop = 0
zChanged = 0
// SCREENER: every bar is closed at the end of the day, so the engine reads bar [0]
// (the indicator reads [1] because its last bar can be the live one)
cIdx = barindex
cHigh = high
cLow = low
cClose = close
atrC = atrRaw
pBar = cIdx - pivLen
// swing confirmed on the closed bar: strict against the bars after it, >= against the bars before it
phOk = 0
plOk = 0
phV = 0
plV = 0
IF barindex >= 2 * pivLen + 1 THEN
IF high[pivLen] > hhP AND high[pivLen] >= hhP[pivLen + 1] THEN
phOk = 1
phV = high[pivLen]
ENDIF
IF low[pivLen] < llP AND low[pivLen] <= llP[pivLen + 1] THEN
plOk = 1
plV = low[pivLen]
ENDIF
ENDIF
//--------------------------------------------------------------------//
//=== PATTERN ENGINE (closed bars only) ===
//--------------------------------------------------------------------//
IF barindex >= 49 THEN
IF dir = 1 THEN
nearV = przHi
farV = przLo
ELSE
nearV = przLo
farV = przHi
ENDIF
// --- bar checks
IF act = 1 AND stage = 1 THEN
IF dir = 1 THEN
tow = cLow
ELSE
tow = cHigh
ENDIF
IF (nearV - tow) * dir >= 0 THEN
stage = 2
evPrz = 1
IF (farV - cClose) * dir > 0 THEN
evBrk = 1
ENDIF
ELSIF cIdx - hb3 > maxWait THEN
evDrop = 1
ENDIF
ELSIF act = 1 AND stage = 2 THEN
IF (farV - cClose) * dir > 0 THEN
evBrk = 1
ELSIF cIdx - hb3 > 2 * maxWait THEN
evDrop = 1
ENDIF
ELSIF act = 1 AND stage = 3 THEN
IF dir = 1 THEN
fav = cHigh
ELSE
fav = cLow
ENDIF
IF (fav - t2) * dir >= 0 THEN
hitMax = 2
ELSIF hitMax < 1 AND (fav - t1) * dir >= 0 THEN
hitMax = 1
ENDIF
IF hitMax = 2 THEN
evDone = 1
ELSIF (farV - cClose) * dir > 0 THEN
evFail = 1
ELSIF cIdx - dBar > maxLeg THEN
evExp = 1
ENDIF
ENDIF
// --- confirmed swings: first the opposite kind of the last one, then the same kind
lastIsHigh = 0
IF nTot > 0 AND lvH = 1 THEN
lastIsHigh = 1
ENDIF
FOR ii = 0 TO 1 DO
IF ii = 0 THEN
evH = 1 - lastIsHigh
ELSE
evH = lastIsHigh
ENDIF
pvOk = 0
pv = 0
IF evH = 1 AND phOk = 1 THEN
pvOk = 1
pv = phV
ELSIF evH = 0 AND plOk = 1 THEN
pvOk = 1
pv = plV
ENDIF
IF pvOk = 1 THEN
IF evH = 1 THEN
swSgn = 1
ELSE
swSgn = 0 - 1
ENDIF
accepted = 0
IF nTot = 0 THEN
lvP = pv
lvB = pBar
lvH = evH
nTot = 1
accepted = 1
ELSIF lvH = evH THEN
IF (pv - lvP) * swSgn > 0 THEN
lvP = pv
lvB = pBar
accepted = 1
ENDIF
ELSIF (pv - lvP) * swSgn >= legMin * atrC AND pBar > lvB THEN
// the last swing is archived in a NEW slot; the live one stays in scalars
$zP[nA] = lvP
$zB[nA] = lvB
$zH[nA] = lvH
nA = nA + 1
lvP = pv
lvB = pBar
lvH = evH
nTot = nTot + 1
accepted = 1
ENDIF
IF accepted = 1 THEN
zChanged = 1
ENDIF
IF accepted = 1 AND act = 1 AND stage <= 2 AND evDrop = 0 AND evBrk = 0 THEN
isC = 0
IF (evH = 1 AND dir = 1) OR (evH = 0 AND dir = 0 - 1) THEN
isC = 1
ENDIF
IF isC = 1 THEN
// C was exceeded, or price turned before reaching the PRZ
evDrop = 1
ELSIF stage = 2 AND (nearV - pv) * dir >= 0 THEN
// D: the confirmed swing that ended the move into the PRZ
stage = 3
evD = 1
dKnown = 1
dBar = pBar
hp4 = pv
hb4 = pBar
adL = abs(hp1 - pv)
t1 = pv + dir * 0.382 * adL
t2 = pv + dir * 0.618 * adL
hitMax = 0
ENDIF
ENDIF
ENDIF
NEXT
// --- endings
IF evDrop = 1 THEN
act = 0
stage = 0
IF sOk = 1 THEN
hp0 = sp0
hp1 = sp1
hp2 = sp2
hp3 = sp3
hp4 = sp4
hb0 = sb0
hb1 = sb1
hb2 = sb2
hb3 = sb3
hb4 = sb4
dir = sDir
pat = sPat
przHi = sHi
przLo = sLo
t1 = sT1
t2 = sT2
hitMax = sHit
dKnown = sDk
ENDIF
ELSIF evBrk = 1 THEN
act = 0
stage = 0
ELSIF evDone = 1 OR evFail = 1 OR evExp = 1 THEN
act = 0
stage = 0
ENDIF
IF evBrk = 1 OR evDone = 1 OR evFail = 1 OR evExp = 1 THEN
sOk = 1
sp0 = hp0
sp1 = hp1
sp2 = hp2
sp3 = hp3
sp4 = hp4
sb0 = hb0
sb1 = hb1
sb2 = hb2
sb3 = hb3
sb4 = hb4
sDir = dir
sPat = pat
sHi = przHi
sLo = przLo
sT1 = t1
sT2 = t2
sHit = hitMax
sDk = dKnown
ENDIF
// --- searching: the last four swings may be X, A, B, C
IF act = 0 AND zChanged = 1 AND nTot >= 4 THEN
pX = $zP[nA - 3]
pA = $zP[nA - 2]
pB = $zP[nA - 1]
pC = lvP
IF lvH = 1 THEN
dn = 1
ELSE
dn = 0 - 1
ENDIF
allowed = 0
IF side = 0 OR (side = 1 AND dn = 1) OR (side = 2 AND dn = 0 - 1) THEN
allowed = 1
ENDIF
IF allowed = 1 AND abs(pA - pX) >= xaMin * atrC THEN
// best pattern for X, A, B, C: the one whose PRZ price reaches first
xaL = abs(pA - pX)
abL = abs(pB - pA)
bcL = abs(pC - pB)
abR = abL / xaL
bcR = bcL / abL
best = 0 - 1
bHi = 0
bLo = 0
minH = przMin * atrC
IF bcR >= 0.382 AND bcR <= 0.886 THEN
FOR kk = 0 TO 3 DO
IF $useP[kk] = 1 AND abR >= $abMn[kk] AND abR <= $abMx[kk] THEN
xdK = $xdR[kk]
z1a = pA - dn * (xdK - xdTol) * xaL
z1b = pA - dn * (xdK + xdTol) * xaL
z2a = pC - dn * $cdMn[kk] * bcL
z2b = pC - dn * $cdMx[kk] * bcL
zHi = min(max(z1a, z1b), max(z2a, z2b))
zLo = max(min(z1a, z1b), min(z2a, z2b))
IF zHi > zLo THEN
IF zHi - zLo < minH THEN
zMid = (zHi + zLo) / 2
zHi = zMid + minH / 2
zLo = zMid - minH / 2
ENDIF
IF dn = 1 THEN
zNear = zHi
bNear = bHi
ELSE
zNear = zLo
bNear = bLo
ENDIF
IF best = 0 - 1 OR (zNear - bNear) * dn > 0 THEN
best = kk
bHi = zHi
bLo = zLo
ENDIF
ENDIF
ENDIF
NEXT
ENDIF
IF dn = 1 THEN
farN = bLo
ELSE
farN = bHi
ENDIF
IF best >= 0 AND (farN - cClose) * dn <= 0 THEN
act = 1
stage = 1
dir = dn
pat = best
przHi = bHi
przLo = bLo
dKnown = 0
hitMax = 0
hp0 = pX
hp1 = pA
hp2 = pB
hp3 = pC
hb0 = $zB[nA - 3]
hb1 = $zB[nA - 2]
hb2 = $zB[nA - 1]
hb3 = lvB
hp4 = 0
hb4 = 0
ENDIF
ENDIF
ENDIF
ENDIF
//--------------------------------------------------------------------//
//=== SCREENER ===
// Event: price entered the PRZ, or D was confirmed. A pattern broken on the
// same bar (close beyond the far side of the PRZ) is left out.
// The event is computed once, on its own bar; maxAge only says how many
// bars back it is still listed (capture window), with the columns of the
// bar that fired.
//--------------------------------------------------------------------//
maxAge = 3 // bars in which an event is still listed (1 = only today)
sigOk = 0
IF act = 1 AND (evPrz = 1 OR evD = 1) THEN
sigOk = 1
ENDIF
// Pattern: 1 Gartley, 2 Bat, 3 Butterfly, 4 Crab; positive bullish, negative bearish
patCode = (pat + 1) * dir
ageS = 0
colPat = 0
colStg = 0
colHi = 0
colLo = 0
IF barindex >= maxAge THEN
FOR n = 0 TO maxAge - 1 DO
IF ageS = 0 AND sigOk[n] = 1 THEN
ageS = n + 1
colPat = patCode[n]
colStg = stage[n]
colHi = przHi[n]
colLo = przLo[n]
ENDIF
NEXT
ENDIF
barsAgo = ageS - 1
SCREENER[ageS > 0](colPat AS "Pattern", colStg AS "Stage 2 PRZ 3 D", colHi AS "PRZ high", colLo AS "PRZ low", barsAgo AS "Bars ago")
Based on the Harmonic Patterns indicator by LunqFX.