本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Choliszt
//@version=6
indicator("Three Push Exhaustion", shorttitle="3Push", overlay=true, max_labels_count=500, max_lines_count=500)
// ════════════════════════════════════════════════════════════
// DETECTION
// ════════════════════════════════════════════════════════════
gDet = "Detection"
pivLen = input.int(5, "Swing sensitivity (bars each side)", minval=2, group=gDet, tooltip="Higher = only more prominent swings count, so fewer and cleaner signals. 3-8 is typical. This is the main tightness control.")
maxSpan = input.int(150,"Maximum span of the three pushes (bars)", minval=20, group=gDet, tooltip="If the first and third push are further apart than this, they are treated as unrelated structure and no pattern is formed.")
minPull = input.float(0.6, "Minimum pullback depth (ATR multiple)", minval=0, step=0.1, group=gDet, tooltip="Both pullbacks must retrace at least this much, so minor wobbles are not counted as separate pushes. Set to 0 to skip the check.")
showBull = input.bool(true, "Show bullish (three pushes to a low)", group=gDet)
showBear = input.bool(true, "Show bearish (three pushes to a high)", group=gDet)
cooldown = input.int(20, "Same-side cooldown (bars)", minval=0, group=gDet, tooltip="Prevents repeated signals from the same area.")
// ════════════════════════════════════════════════════════════
// MOMENTUM DIVERGENCE
// ════════════════════════════════════════════════════════════
gDiv = "Momentum divergence"
useDiv = input.bool(true, "Require momentum divergence (recommended)", group=gDiv, tooltip="The third push must show weaker RSI than the second. This is the defining feature of the pattern - turning it off leaves ordinary consecutive new highs or lows.")
rsiLen = input.int(14, "RSI length", minval=2, group=gDiv)
divStrict= input.bool(false,"Strict: momentum must weaken on every push", group=gDiv, tooltip="Requires RSI to decline across all three pushes rather than only on the last. Fewer but more textbook signals.")
showRsi = input.bool(false,"Show RSI value on labels", group=gDiv, tooltip="Prints the RSI reading at each push, so the divergence is visible directly on the chart.")
// ════════════════════════════════════════════════════════════
// SYMMETRY (OPTIONAL)
// ════════════════════════════════════════════════════════════
gSym = "Symmetry (optional)"
useSym = input.bool(false, "Require pushes of similar size", group=gSym, tooltip="The classic pattern is symmetrical. Enabling this enforces comparable push sizes - noticeably fewer signals.")
symTol = input.float(60, " Size tolerance (%)", minval=10, maxval=200, step=10, group=gSym, tooltip="Maximum difference allowed between two push sizes. 60% is fairly permissive.")
// ════════════════════════════════════════════════════════════
// DISPLAY
// ════════════════════════════════════════════════════════════
gDisp = "Display"
showSteps = input.bool(true, "Show push 1 and 2 (faded)", group=gDisp, tooltip="Turn off to leave only the completion marker for the cleanest chart.")
showPath = input.bool(true, "Show connecting path", group=gDisp)
numSize3 = input.string("normal", "Size of the 3", options=["small","normal","large"], group=gDisp)
numSize12 = input.string("small", "Size of the 1 and 2", options=["tiny","small","normal"], group=gDisp)
atrLen = input.int(14, "ATR length", minval=1, group=gDisp)
offMult = input.float(0.5,"Label offset (ATR multiple)", step=0.05, group=gDisp)
// ════════════════════════════════════════════════════════════
// COLORS
// ════════════════════════════════════════════════════════════
gCol = "Colors"
cBull = input.color(#00E676, "Bullish", group=gCol)
cBear = input.color(#FF1744, "Bearish", group=gCol)
stepTr = input.int(40, "Transparency of 1 and 2", minval=0, maxval=100, group=gCol, tooltip="Lower = pushes 1 and 2 stand out more. They stay behind the completion marker either way.")
pathTr = input.int(55, "Path transparency", minval=0, maxval=100, group=gCol)
// ════════════════════════════════════════════════════════════
// HELPERS
// ════════════════════════════════════════════════════════════
atrVal = ta.atr(atrLen)
rsiVal = ta.rsi(close, rsiLen)
f_size(s) =>
switch s
"tiny" => size.tiny
"small" => size.small
"normal" => size.normal
"large" => size.large
=> size.small
sz3 = f_size(numSize3)
sz12 = f_size(numSize12)
// ════════════════════════════════════════════════════════════
// PIVOT REGISTRY
// ════════════════════════════════════════════════════════════
ph = ta.pivothigh(high, pivLen, pivLen)
pl = ta.pivotlow(low, pivLen, pivLen)
var float[] hiPx = array.new<float>()
var int[] hiBar = array.new<int>()
var float[] hiRsi = array.new<float>()
var float[] loPx = array.new<float>()
var int[] loBar = array.new<int>()
var float[] loRsi = array.new<float>()
f_trim(_a, _b, _c) =>
while array.size(_a) > 40
array.shift(_a)
array.shift(_b)
array.shift(_c)
if not na(ph)
array.push(hiPx, ph)
array.push(hiBar, bar_index - pivLen)
array.push(hiRsi, rsiVal[pivLen])
f_trim(hiPx, hiBar, hiRsi)
if not na(pl)
array.push(loPx, pl)
array.push(loBar, bar_index - pivLen)
array.push(loRsi, rsiVal[pivLen])
f_trim(loPx, loBar, loRsi)
f_lowBetween(_a, _b) =>
float res = na
if array.size(loBar) > 0
for i = 0 to array.size(loBar) - 1
bi = array.get(loBar, i)
if bi > _a and bi < _b
px = array.get(loPx, i)
res := na(res) ? px : math.min(res, px)
res
f_highBetween(_a, _b) =>
float res = na
if array.size(hiBar) > 0
for i = 0 to array.size(hiBar) - 1
bi = array.get(hiBar, i)
if bi > _a and bi < _b
px = array.get(hiPx, i)
res := na(res) ? px : math.max(res, px)
res
f_symOK(_m1, _m2) =>
not useSym or (math.min(_m1, _m2) > 0 and math.abs(_m1 - _m2) / math.max(_m1, _m2) * 100 <= symTol)
// ════════════════════════════════════════════════════════════
// DRAWING
// ════════════════════════════════════════════════════════════
f_mark(_bar, _px, _txt, _col, _txtCol, _size, _isBull) =>
y = _isBull ? _px - atrVal * offMult : _px + atrVal * offMult
st = _isBull ? label.style_label_up : label.style_label_down
label.new(_bar, y, _txt, yloc=yloc.price, style=st, color=_col, textcolor=_txtCol, size=_size)
f_step(_bar, _px, _txt, _baseCol, _isBull) =>
y = _isBull ? _px - atrVal * offMult : _px + atrVal * offMult
st = _isBull ? label.style_label_up : label.style_label_down
label.new(_bar, y, _txt, yloc=yloc.price, style=st, color=color.new(_baseCol, stepTr), textcolor=color.new(color.white, 10), size=sz12)
// ════════════════════════════════════════════════════════════
// STATE
// ════════════════════════════════════════════════════════════
var int lastBullBar = na
var int lastBearBar = na
bool firedBull = false
bool firedBear = false
// ════════════════════════════════════════════════════════════
// BEARISH — three pushes to a high
// ════════════════════════════════════════════════════════════
if barstate.isconfirmed and showBear and not na(ph) and array.size(hiPx) >= 3
int n = array.size(hiPx)
float p1 = array.get(hiPx, n - 3)
float p2 = array.get(hiPx, n - 2)
float p3 = array.get(hiPx, n - 1)
int b1 = array.get(hiBar, n - 3)
int b2 = array.get(hiBar, n - 2)
int b3 = array.get(hiBar, n - 1)
float r1 = array.get(hiRsi, n - 3)
float r2 = array.get(hiRsi, n - 2)
float r3 = array.get(hiRsi, n - 1)
bool ascending = p1 < p2 and p2 < p3
bool spanOK = (b3 - b1) <= maxSpan
float pb1 = f_lowBetween(b1, b2)
float pb2 = f_lowBetween(b2, b3)
bool pullOK = not na(pb1) and not na(pb2) and (minPull == 0 or ((p1 - pb1) >= atrVal * minPull and (p2 - pb2) >= atrVal * minPull))
bool divOK = not useDiv or (divStrict ? (r1 > r2 and r2 > r3) : (r3 < r2))
bool symOK = f_symOK(p2 - nz(pb1, p1), p3 - nz(pb2, p2))
bool cdOK = na(lastBearBar) or (bar_index - lastBearBar) >= cooldown
if ascending and spanOK and pullOK and divOK and symOK and cdOK
lastBearBar := bar_index
firedBear := true
if showPath
line.new(b1, p1, b2, p2, color=color.new(cBear, pathTr), width=1, style=line.style_dotted)
line.new(b2, p2, b3, p3, color=color.new(cBear, pathTr), width=1, style=line.style_dotted)
if showSteps
f_step(b1, p1, showRsi ? "1 · " + str.tostring(r1, "#") : "1", cBear, false)
f_step(b2, p2, showRsi ? "2 · " + str.tostring(r2, "#") : "2", cBear, false)
f_mark(b3, p3, showRsi ? "3 · " + str.tostring(r3, "#") : "3", cBear, color.white, sz3, false)
// ════════════════════════════════════════════════════════════
// BULLISH — three pushes to a low
// ════════════════════════════════════════════════════════════
if barstate.isconfirmed and showBull and not na(pl) and array.size(loPx) >= 3
int n = array.size(loPx)
float p1 = array.get(loPx, n - 3)
float p2 = array.get(loPx, n - 2)
float p3 = array.get(loPx, n - 1)
int b1 = array.get(loBar, n - 3)
int b2 = array.get(loBar, n - 2)
int b3 = array.get(loBar, n - 1)
float r1 = array.get(loRsi, n - 3)
float r2 = array.get(loRsi, n - 2)
float r3 = array.get(loRsi, n - 1)
bool descending = p1 > p2 and p2 > p3
bool spanOK = (b3 - b1) <= maxSpan
float pb1 = f_highBetween(b1, b2)
float pb2 = f_highBetween(b2, b3)
bool pullOK = not na(pb1) and not na(pb2) and (minPull == 0 or ((pb1 - p1) >= atrVal * minPull and (pb2 - p2) >= atrVal * minPull))
bool divOK = not useDiv or (divStrict ? (r1 < r2 and r2 < r3) : (r3 > r2))
bool symOK = f_symOK(nz(pb1, p1) - p2, nz(pb2, p2) - p3)
bool cdOK = na(lastBullBar) or (bar_index - lastBullBar) >= cooldown
if descending and spanOK and pullOK and divOK and symOK and cdOK
lastBullBar := bar_index
firedBull := true
if showPath
line.new(b1, p1, b2, p2, color=color.new(cBull, pathTr), width=1, style=line.style_dotted)
line.new(b2, p2, b3, p3, color=color.new(cBull, pathTr), width=1, style=line.style_dotted)
if showSteps
f_step(b1, p1, showRsi ? "1 · " + str.tostring(r1, "#") : "1", cBull, true)
f_step(b2, p2, showRsi ? "2 · " + str.tostring(r2, "#") : "2", cBull, true)
f_mark(b3, p3, showRsi ? "3 · " + str.tostring(r3, "#") : "3", cBull, color.black, sz3, true)
// ════════════════════════════════════════════════════════════
// ALERTS
// ════════════════════════════════════════════════════════════
alertcondition(firedBull, title="Bullish three push complete", message="Three Push: three pushes to a low completed with momentum divergence")
alertcondition(firedBear, title="Bearish three push complete", message="Three Push: three pushes to a high completed with momentum divergence")
bool anyHit = firedBull or firedBear
if anyHit
alert(syminfo.ticker + " " + timeframe.period + " - " + (firedBull ? "Bullish three push" : "Bearish three push") + " @ " + str.tostring(close, format.mintick), alert.freq_once_per_bar)