本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Absorption Trap Logic", shorttitle = "Absorption Trap Logic", overlay = true, max_boxes_count = 500, max_lines_count = 500, max_labels_count = 50)
// ── General settings ───────────────────────────────────────
minTicks = input.int(1, "Min sweep, ticks", minval = 1)
lookback = input.int(1000, "Bars back to calculate (0 = all history)", minval = 0, tooltip = "Calculation runs only on the last N bars of the current chart. Fewer bars = faster indicator.")
skipIns = input.bool(true, "Skip inside bars", tooltip = "Compare with the mother window instead of the previous one: windows fully inside the reference window (equal High/Low counts as inside) are skipped, including long runs of them, e.g. after news.")
motherH = input.int(24, "Mother bar search depth, hours", minval = 1, maxval = 48)
liveDraw = input.bool(true, "Draw live (on unclosed 1m bar)", tooltip = "If an absorption has already formed on the current unclosed 1m bar, it is drawn immediately and updated until that minute closes. The final version stays after the close.")
grpVis = "Style (common)"
drawMode = input.string("Auto", "Style", options = ["Auto", "Box", "Brackets"], group = grpVis, tooltip = "Box: outline with fill. Brackets: only High and Low lines, no vertical sides, so they are not confused with wicks. Auto: brackets when the window spans no more than 2 chart bars, box otherwise.")
colBear = input.color(#C2185B, "Bearish", group = grpVis)
colBull = input.color(#2E7D32, "Bullish", group = grpVis)
colDoji = input.color(#7E57C2, "Doji", group = grpVis)
fillTr = input.int(90, "Fill transparency, %", minval = 0, maxval = 100, group = grpVis)
prevCol = input.color(color.gray, "Debug: mother window (what we compare against)", group = grpVis)
liveCol = input.color(color.blue, "Debug: windows being compared now", group = grpVis)
colDone = input.color(color.gray, "Mitigated candle color (50% retest / full overlap)", group = grpVis)
g1 = "Absorption #1"
on1 = input.bool(true, "Enable", group = g1)
tf1 = input.timeframe("15", "Absorption timeframe", group = g1)
st1 = input.string("Solid", "Line", options = ["Solid", "Dashed", "Dotted"], group = g1)
w1 = input.int(1, "Width", minval = 1, maxval = 4, group = g1)
mid1 = input.bool(true, "Draw midline", group = g1, tooltip = "Line at 50% of the absorption candle. Drawn only for a closed (confirmed) candle, not while live.")
dim1 = input.bool(true, "Dim after 50% retest", group = g1, tooltip = "If price returns to the midpoint (50%) after the absorption candle closes, the outline, midline and brackets switch to the mitigated color and the fill is removed.")
full1 = input.bool(false, "Dim after full overlap", group = g1, tooltip = "If after the absorption candle closes price trades both above its High and below its Low (fully overlapped), the outline, midline and brackets switch to the mitigated color and the fill is removed.")
hide1 = input.bool(false, "Hide after full overlap", group = g1, tooltip = "If after the absorption candle closes price trades both above its High and below its Low, the candle is removed from the chart (outline, brackets, midline). Works together with the 50% dimming: first the candle is dimmed, then it disappears after a full overlap.")
dbg1 = input.bool(false, "Debug mode (captured bars)", group = g1)
g2 = "Absorption #2"
on2 = input.bool(true, "Enable", group = g2)
tf2 = input.timeframe("5", "Absorption timeframe", group = g2)
st2 = input.string("Dashed", "Line", options = ["Solid", "Dashed", "Dotted"], group = g2)
w2 = input.int(1, "Width", minval = 1, maxval = 4, group = g2)
mid2 = input.bool(true, "Draw midline", group = g2, tooltip = "Line at 50% of the absorption candle. Drawn only for a closed (confirmed) candle, not while live.")
dim2 = input.bool(true, "Dim after 50% retest", group = g2, tooltip = "If price returns to the midpoint (50%) after the absorption candle closes, the outline, midline and brackets switch to the mitigated color and the fill is removed.")
full2 = input.bool(false, "Dim after full overlap", group = g2, tooltip = "If after the absorption candle closes price trades both above its High and below its Low (fully overlapped), the outline, midline and brackets switch to the mitigated color and the fill is removed.")
hide2 = input.bool(false, "Hide after full overlap", group = g2, tooltip = "If after the absorption candle closes price trades both above its High and below its Low, the candle is removed from the chart (outline, brackets, midline). Works together with the 50% dimming: first the candle is dimmed, then it disappears after a full overlap.")
dbg2 = input.bool(false, "Debug mode (captured bars)", group = g2)
grpPB = "Pullbacks"
pbShow = input.bool(true, "Show pullbacks", group = grpPB, tooltip = "Pullback zigzag of the selected timeframe. Candles of the selected timeframe are built from chart bars, so pullbacks are shown on charts with a timeframe not higher than the selected one.")
pbTf = input.timeframe("60", "Pullback timeframe", group = grpPB)
pbIns = input.bool(false, "Include inside bars", group = grpPB, tooltip = "Off: inside bars are skipped and the mother bar stays the reference. On: pullbacks are built from every bar, each bar becomes the reference for the next one.")
pbCol = input.color(color.gray, "Color", group = grpPB, inline = "pb")
pbSt = input.string("Solid", "", options = ["Solid", "Dashed", "Dotted"], group = grpPB, inline = "pb")
pbW = input.int(1, "Width", minval = 1, maxval = 4, group = grpPB)
pbCount = input.int(100, "Max lines kept", minval = 1, maxval = 300, group = grpPB)
grpAl = "Alerts"
alMode = input.string("Instantly (live)", "When to send", options = ["Instantly (live)", "On 1m close"], group = grpAl, tooltip = "Instantly: as soon as the absorption has formed, even if the 1m bar is not closed yet. On 1m close: when the 1m bar on which the absorption formed has closed. Either way, one alert per absorption candle.")
alUpDn = input.bool(true, "High swept first, then low", group = grpAl)
alDnUp = input.bool(true, "Low swept first, then high", group = grpAl)
alBoth = input.bool(true, "Both sweeps in the same minute", group = grpAl)
alPbNew = input.bool(true, "Pullbacks: last point appeared or redrawn", group = grpAl, tooltip = "A new leg started (including a tentative one on an unclosed pullback-timeframe candle) or a tentative leg was cancelled.")
alPbMove = input.bool(true, "Pullbacks: last point moved", group = grpAl, tooltip = "Price made a new high/low of the current leg and the point moved. At most once per chart bar.")
// ── Helpers ────────────────────────────────────────────────
chartMin = math.max(1, int(timeframe.in_seconds() / 60))
styleOf(string s) =>
s == "Dashed" ? line.style_dashed : s == "Dotted" ? line.style_dotted : line.style_solid
// Open time of the chart bar that contains moment t.
// TradingView places a point with a non-bar-aligned time on the next bar, so a window starting inside a bar
// collapsed into a line. Anchor the edges to the bar that contains the minute.
max_bars_back(time, 5000)
barFloor(int t) =>
int r = time
if t < time
int k = 0
while k < 4999 and not na(time[k + 1]) and time[k] > t
k += 1
r := time[k]
r
ticksBeyond(float a, float b) =>
int(math.round((a - b) / syminfo.mintick))
// One absorption search layer with its own window.
// 1m bars are kept in a ring buffer: minute number g is stored in cell g % cap.
// rH/rL: High/Low of the n-minute window ending at this minute.
type Layer
int n
int D // how many windows back to search for the mother window
int cap // buffer size = D*n + n
bool bracket
string lineStyle
int width
bool dbg
bool filled // fill only when window TF = chart TF
bool mid // draw midline
bool dim // dim after 50% retest
bool dimFull // dim after full overlap
bool hideFull // remove after full overlap
array<int> t
array<float> h
array<float> l
array<float> o
array<float> cl
array<float> rH
array<float> rL
int idx = -1
int nextCheck = 0
varip int lastAlert = 0
box liveCur
box livePrev
label liveLbl
// Confirmed candles that are not mitigated yet
array<float> pMid
array<int> pSide // 1 = close above midline, -1 = below, 0 = exactly on it
array<float> pH // candle High and Low, for full-overlap tracking
array<float> pL
array<bool> pUp // price has already traded above High
array<bool> pDn // price has already traded below Low
array<bool> pMidDone // already dimmed by the 50% retest
array<box> pBox
array<line> pL1
array<line> pL2
array<line> pLM
newLayer(string tf, string st, int w, bool dbg, bool mid, bool dim, bool dimFull, bool hideFull) =>
int n = math.max(1, int(timeframe.in_seconds(tf == "" ? timeframe.period : tf) / 60))
int D = skipIns ? math.max(1, int(math.floor(motherH * 60 / n))) : 1
int cap = D * n + n
bool br = drawMode == "Brackets" or (drawMode == "Auto" and n <= 2 * chartMin)
Layer.new(n, D, cap, br, styleOf(st), w, dbg, n == chartMin, mid, dim, dimFull, hideFull,
array.new<int>(cap, 0), array.new<float>(cap, na), array.new<float>(cap, na), array.new<float>(cap, na), array.new<float>(cap, na), array.new<float>(cap, na), array.new<float>(cap, na),
-1, 2 * n - 1, 0, na, na, na, array.new<float>(), array.new<int>(), array.new<float>(), array.new<float>(), array.new<bool>(), array.new<bool>(), array.new<bool>(), array.new<box>(), array.new<line>(), array.new<line>(), array.new<line>())
method gT(Layer this, int g) =>
array.get(this.t, g % this.cap)
method gH(Layer this, int g) =>
array.get(this.h, g % this.cap)
method gL(Layer this, int g) =>
array.get(this.l, g % this.cap)
method gRH(Layer this, int g) =>
array.get(this.rH, g % this.cap)
method gRL(Layer this, int g) =>
array.get(this.rL, g % this.cap)
// Mother window for the current window ending at minute g.
// Walk from the oldest window to the newest (step n minutes): a window fully inside the reference is skipped,
// otherwise it becomes the reference. Returns [High, Low, windows back (1 = immediately previous)].
method mother(Layer this, int g) =>
int n = this.n
int K = math.min(this.D, int(math.floor((g - n + 1) / n)))
int refK = K
float refH = this.gRH(g - K * n)
float refL = this.gRL(g - K * n)
if K >= 2
for k = K - 1 to 1
float bh = this.gRH(g - k * n)
float bl = this.gRL(g - k * n)
bool inside = ticksBeyond(bh, refH) <= 0 and ticksBeyond(refL, bl) <= 0
if not inside
refK := k
refH := bh
refL := bl
[refH, refL, refK]
// Check the minute against all not-yet-mitigated candles: has price returned to the midline
// and/or fully overlapped the candle (traded both above High and below Low)
method pRemove(Layer this, int k) =>
array.remove(this.pMid, k)
array.remove(this.pSide, k)
array.remove(this.pH, k)
array.remove(this.pL, k)
array.remove(this.pUp, k)
array.remove(this.pDn, k)
array.remove(this.pMidDone, k)
array.remove(this.pBox, k)
array.remove(this.pL1, k)
array.remove(this.pL2, k)
array.remove(this.pLM, k)
method recolor(Layer this, int k) =>
box b = array.get(this.pBox, k)
line a1 = array.get(this.pL1, k)
line a2 = array.get(this.pL2, k)
line am = array.get(this.pLM, k)
if not na(b)
box.set_border_color(b, colDone)
box.set_bgcolor(b, color.new(colDone, 100))
if not na(a1)
line.set_color(a1, colDone)
if not na(a2)
line.set_color(a2, colDone)
if not na(am)
line.set_color(am, colDone)
k
method erase(Layer this, int k) =>
box.delete(array.get(this.pBox, k))
line.delete(array.get(this.pL1, k))
line.delete(array.get(this.pL2, k))
line.delete(array.get(this.pLM, k))
k
method checkDone(Layer this, float mh, float ml) =>
int k = array.size(this.pMid) - 1
while k >= 0
float m = array.get(this.pMid, k)
int s = array.get(this.pSide, k)
bool up = array.get(this.pUp, k) or ticksBeyond(mh, array.get(this.pH, k)) >= 1
bool dn = array.get(this.pDn, k) or ticksBeyond(array.get(this.pL, k), ml) >= 1
array.set(this.pUp, k, up)
array.set(this.pDn, k, dn)
bool midDone = array.get(this.pMidDone, k)
bool full = up and dn
// 50% retest: dim (once)
if this.dim and not midDone and (s > 0 ? ml <= m : s < 0 ? mh >= m : true)
this.recolor(k)
midDone := true
array.set(this.pMidDone, k, true)
bool remove = false
if full and this.hideFull
this.erase(k)
remove := true
else if full and this.dimFull
this.recolor(k)
remove := true
// nothing left to track
else if midDone and not this.hideFull and not this.dimFull
remove := true
if remove
this.pRemove(k)
k -= 1
// Add a minute and check the condition. Returns [type (0 none, 1 high→low, 2 low→high, 3 same minute), signal inside a single chart bar, color]
method step(Layer this, int mt, float mo, float mh, float ml, float mc, bool closed) =>
int n = this.n
this.idx += 1
int g = this.idx
int p = g % this.cap
array.set(this.t, p, mt)
array.set(this.h, p, mh)
array.set(this.l, p, ml)
array.set(this.o, p, mo)
array.set(this.cl, p, mc)
// High/Low of the n-minute window ending at this minute
if g >= n - 1
float wh = mh
float wl = ml
if n > 1
for j = 1 to n - 1
wh := math.max(wh, this.gH(g - j))
wl := math.min(wl, this.gL(g - j))
array.set(this.rH, p, wh)
array.set(this.rL, p, wl)
if this.dim or this.dimFull or this.hideFull
this.checkDone(mh, ml)
int kind = 0
bool inBar = false
color c = na
if g >= this.nextCheck and g >= 2 * n - 1
[prevH, prevL, refK] = this.mother(g)
float curH = array.get(this.rH, p)
float curL = array.get(this.rL, p)
if ticksBeyond(curH, prevH) >= minTicks and ticksBeyond(prevL, curL) >= minTicks
int s0 = g - n + 1 // first minute of the current window
int firstUp = -1
int firstDn = -1
for j = 0 to n - 1
if firstUp < 0 and ticksBeyond(this.gH(s0 + j), prevH) >= minTicks
firstUp := j
if firstDn < 0 and ticksBeyond(prevL, this.gL(s0 + j)) >= minTicks
firstDn := j
kind := firstUp < firstDn ? 1 : firstDn < firstUp ? 2 : 3
// Direction of the merged candle: Open of the first minute vs Close of the last
float wOpen = array.get(this.o, s0 % this.cap)
float wClose = mc
int dirT = ticksBeyond(wClose, wOpen)
c := dirT < 0 ? colBear : dirT > 0 ? colBull : colDoji
int prevEnd = this.gT(g - refK * n)
int prevStart = this.gT(g - refK * n - n + 1)
int curStart = this.gT(s0)
int curEnd = mt // edges at the centers of the first and last minutes
// Chart coordinates: chart bars that contain the first and last minutes of the window
int xS = barFloor(curStart)
int xE = barFloor(curEnd)
int xPS = barFloor(prevStart)
int xPE = barFloor(prevEnd)
box bx = na
line b1 = na
line b2 = na
line lm = na
float m = (curH + curL) / 2
if xS == xE and chartMin > 1
inBar := true // window lies inside one chart bar: the box would have zero width, so nothing is drawn
else if this.bracket
b1 := line.new(xS, curH, xE, curH, xloc = xloc.bar_time, color = c, width = this.width + 1, style = this.lineStyle)
b2 := line.new(xS, curL, xE, curL, xloc = xloc.bar_time, color = c, width = this.width + 1, style = this.lineStyle)
else
bx := box.new(xS, curH, xE, curL, xloc = xloc.bar_time, border_color = c, border_width = this.width, border_style = this.lineStyle, bgcolor = color.new(c, this.filled ? fillTr : 100))
// Midline only for a closed absorption candle (not drawn live)
if this.mid and closed and not inBar
lm := line.new(xS, m, xE, m, xloc = xloc.bar_time, color = c, width = this.width, style = this.lineStyle)
// Track 50% retest / full overlap only for a confirmed candle, starting from the next minute
if (this.dim or this.dimFull or this.hideFull) and closed and not inBar
array.push(this.pMid, m)
array.push(this.pSide, wClose > m ? 1 : wClose < m ? -1 : 0)
array.push(this.pH, curH)
array.push(this.pL, curL)
array.push(this.pUp, false)
array.push(this.pDn, false)
array.push(this.pMidDone, false)
array.push(this.pBox, bx)
array.push(this.pL1, b1)
array.push(this.pL2, b2)
array.push(this.pLM, lm)
if array.size(this.pMid) > 200 // stop tracking the oldest ones
this.pRemove(0)
if this.dbg
box.new(xPS, prevH, xPE, prevL, xloc = xloc.bar_time, border_color = prevCol, border_width = 1, border_style = line.style_dashed, bgcolor = color.new(prevCol, 92))
line.new(xPS, prevH, xE, prevH, xloc = xloc.bar_time, color = prevCol, style = line.style_dotted)
line.new(xPS, prevL, xE, prevL, xloc = xloc.bar_time, color = prevCol, style = line.style_dotted)
// The next candle starts from the next minute; first check in n minutes, against the absorption candle itself
this.nextCheck := g + n
bool want = (kind == 1 and alUpDn) or (kind == 2 and alDnUp) or (kind == 3 and alBoth)
bool timeOk = alMode == "Instantly (live)" or closed
if want and timeOk and curEnd > this.lastAlert
this.lastAlert := curEnd
string kindS = kind == 1 ? "high swept first, then low" : kind == 2 ? "low swept first, then high" : "both sweeps in the same minute"
string dirS = dirT < 0 ? "bearish" : dirT > 0 ? "bullish" : "doji"
string msg = syminfo.ticker + ": absorption " + str.tostring(n) + "m, " + dirS + " (" + kindS + ")" +
"\nWindow: " + str.format_time(curStart, "HH:mm", syminfo.timezone) + "–" + str.format_time(curEnd + 60000, "HH:mm", syminfo.timezone) +
"\nHigh " + str.tostring(curH, format.mintick) + " (swept " + str.tostring(prevH, format.mintick) + ")" +
"\nLow " + str.tostring(curL, format.mintick) + " (swept " + str.tostring(prevL, format.mintick) + ")" +
(closed ? "" : "\n1m bar not closed yet, High/Low may extend")
alert(msg, alert.freq_all)
[kind, inBar, c]
// Live debug windows: what is being compared right now
method drawLive(Layer this) =>
int n = this.n
int g = this.idx
if this.dbg and g >= 2 * n - 1
box.delete(this.liveCur)
box.delete(this.livePrev)
label.delete(this.liveLbl)
[pH, pL, refK] = this.mother(g)
int pS = barFloor(this.gT(g - refK * n - n + 1))
int pE = barFloor(this.gT(g - refK * n))
int cS = barFloor(this.gT(g - n + 1))
int cE = barFloor(this.gT(g))
float cH = this.gRH(g)
this.livePrev := box.new(pS, pH, pE, pL, xloc = xloc.bar_time, border_color = liveCol, border_style = line.style_dashed, bgcolor = color.new(liveCol, 95))
this.liveCur := box.new(cS, cH, cE, this.gRL(g), xloc = xloc.bar_time, border_color = liveCol, border_style = line.style_dotted, bgcolor = color.new(liveCol, 90))
int waitMin = this.nextCheck - g
string mTxt = refK > 1 ? ", mother window " + str.tostring(refK) + " windows back" : ""
string txt = (waitMin > 0 ? str.tostring(n) + "m: locked, " + str.tostring(waitMin) + " min left" : str.tostring(n) + "m: checking") + mTxt
this.liveLbl := label.new(cE, cH, txt, xloc = xloc.bar_time, style = label.style_label_lower_left, color = color.new(liveCol, 80), textcolor = liveCol, size = size.small)
// ── Main calculation ───────────────────────────────────────
var Layer L1 = newLayer(tf1, st1, w1, dbg1, mid1, dim1, full1, hide1)
var Layer L2 = newLayer(tf2, st2, w2, dbg2, mid2, dim2, full2, hide2)
// Show only absorptions of the chart timeframe and higher
bool act1 = on1 and L1.n >= chartMin
bool act2 = on2 and L2.n >= chartMin
// All 1m bars inside the current chart bar (always regular candles, even on Heikin Ashi)
[mT, mO, mH, mL, mC] = request.security_lower_tf(ticker.standard(syminfo.tickerid), "1", [time, open, high, low, close])
bool sigUpDn = false
bool sigDnUp = false
bool sigBoth = false
bool inBar1 = false
color inCol1 = na
bool inBar2 = false
color inCol2 = na
int cnt = array.size(mT)
// On the live bar the last minute is not closed yet. In live mode it is used as is (on every tick
// the current bar is recalculated from scratch, so the final version remains after the close),
// otherwise it is skipped until it closes.
bool lastOpen = barstate.isrealtime and not barstate.isconfirmed
if lastOpen and not liveDraw
cnt -= 1
bool inRange = lookback == 0 or bar_index >= last_bar_index - lookback
if cnt > 0 and (act1 or act2) and inRange
for i = 0 to cnt - 1
int mt = array.get(mT, i)
float mh = array.get(mH, i)
float ml = array.get(mL, i)
float mo = array.get(mO, i)
float mc = array.get(mC, i)
bool closed = not (lastOpen and i == array.size(mT) - 1)
if act1
[k1, ib1, c1] = L1.step(mt, mo, mh, ml, mc, closed)
sigUpDn := sigUpDn or k1 == 1
sigDnUp := sigDnUp or k1 == 2
sigBoth := sigBoth or k1 == 3
if ib1
inBar1 := true
inCol1 := c1
if act2
[k2, ib2, c2] = L2.step(mt, mo, mh, ml, mc, closed)
sigUpDn := sigUpDn or k2 == 1
sigDnUp := sigDnUp or k2 == 2
sigBoth := sigBoth or k2 == 3
if ib2
inBar2 := true
inCol2 := c2
if barstate.islast
if act1
L1.drawLive()
if act2
L2.drawLive()
// ── Pullbacks of the selected timeframe ──
// A pullback-timeframe candle is built from chart bars. Inside bars (High ≤ reference and Low ≥ reference)
// are skipped: the last non-inside bar stays the reference.
type Swing
bool init = false
bool up = false
float pH = 0.0
float pL = 0.0
int pHT = 0
int pLT = 0
float prevV = 0.0
int prevT = 0
float refH = 0.0
float refL = 0.0
array<line> lines
line openLn
line tentLn
line tentLn2
bool dry = false // dry run on an unclosed candle: lines are stored, not drawn
int nSeg = 0 // number of legs created during the dry run (up to two)
int aT1 = 0
float aV1 = 0.0
int aT2 = 0
float aV2 = 0.0
int bT1 = 0
float bV1 = 0.0
int bT2 = 0
float bV2 = 0.0
method addLine(Swing this, int t1, float v1, int t2, float v2) =>
if this.dry
this.nSeg += 1
if this.nSeg == 1
this.aT1 := t1
this.aV1 := v1
this.aT2 := t2
this.aV2 := v2
else
this.bT1 := t1
this.bV1 := v1
this.bT2 := t2
this.bV2 := v2
else
// if both points are in the same candle and the second one is earlier in time, do not draw a line backwards
array.push(this.lines, line.new(t1, v1, math.max(t1, t2), v2, xloc = xloc.bar_time, color = pbCol, width = pbW, style = styleOf(pbSt)))
while array.size(this.lines) > pbCount
line.delete(array.shift(this.lines))
this.nSeg // common return value so the if branches do not conflict
// A pullback-timeframe candle closed: o/h/l/c and the chart-bar times of its High and Low
method process(Swing this, float o, float h, float l, float c, int hT, int lT) =>
bool inside = not pbIns and this.init and h <= this.refH and l >= this.refL
if not this.init
this.init := true
this.up := c >= o
this.pH := h
this.pL := l
this.pHT := hT
this.pLT := lT
this.prevV := this.up ? l : h
this.prevT := this.up ? lT : hT
this.refH := h
this.refL := l
else if not inside
bool bH = h > this.refH
bool bL = l < this.refL
// Order inside a candle that broke both sides: by the times of the extremes on the lower timeframe,
// or, if they are on the same chart bar, by candle color (green: low first, then high)
bool lowFirst = lT < hT or (lT == hT and c >= o)
if this.up
if bH and bL and pbIns and lowFirst
// first a pullback down from the previous high to this candle's low, then a reversal up from that low
this.addLine(this.prevT, this.prevV, this.pHT, this.pH)
this.prevV := this.pH
this.prevT := math.max(this.prevT, this.pHT)
this.addLine(this.prevT, this.prevV, lT, l)
this.prevV := l
this.prevT := math.max(this.prevT, lT)
this.pH := h
this.pHT := hT
this.pL := l
this.pLT := lT
else if bH and bL
if h > this.pH
this.pH := h
this.pHT := hT
this.addLine(this.prevT, this.prevV, this.pHT, this.pH)
this.prevV := this.pH
this.prevT := math.max(this.prevT, this.pHT)
this.pL := l
this.pLT := lT
this.pH := h
this.pHT := hT
this.up := false
else if bH
if h >= this.pH
this.pH := h
this.pHT := hT
this.pL := l
this.pLT := lT
else if bL
// skipping inside bars: must break the low of the pivot candle; including them: breaking the previous bar's low is enough
if pbIns or l < this.pL
this.addLine(this.prevT, this.prevV, this.pHT, this.pH)
this.prevV := this.pH
this.prevT := math.max(this.prevT, this.pHT)
this.pL := l
this.pLT := lT
this.pH := h
this.pHT := hT
this.up := false
else
if bH and bL and pbIns and not lowFirst
// first a bounce up from the previous low to this candle's high, then a reversal down from that high
this.addLine(this.prevT, this.prevV, this.pLT, this.pL)
this.prevV := this.pL
this.prevT := math.max(this.prevT, this.pLT)
this.addLine(this.prevT, this.prevV, hT, h)
this.prevV := h
this.prevT := math.max(this.prevT, hT)
this.pH := h
this.pHT := hT
this.pL := l
this.pLT := lT
else if bH and bL
if l < this.pL
this.pL := l
this.pLT := lT
this.addLine(this.prevT, this.prevV, this.pLT, this.pL)
this.prevV := this.pL
this.prevT := math.max(this.prevT, this.pLT)
this.pH := h
this.pHT := hT
this.pL := l
this.pLT := lT
this.up := true
else if bL
if l <= this.pL
this.pL := l
this.pLT := lT
this.pH := h
this.pHT := hT
else if bH
// skipping inside bars: must break the high of the pivot candle; including them: breaking the previous bar's high is enough
if pbIns or h > this.pH
this.addLine(this.prevT, this.prevV, this.pLT, this.pL)
this.prevV := this.pL
this.prevT := math.max(this.prevT, this.pLT)
this.pH := h
this.pHT := hT
this.pL := l
this.pLT := lT
this.up := true
this.refH := h
this.refL := l
int pbMin = math.max(1, int(timeframe.in_seconds(pbTf) / 60))
bool pbActive = pbShow and chartMin <= pbMin
var Swing SW = Swing.new(lines = array.new<line>())
// Pullback-timeframe candle being built
var float aO = na
var float aH = na
var float aL = na
var float aC = na
var int aHT = 0
var int aLT = 0
var bool aPend = false
int pbStart = time(pbTf)
bool pbNew = pbStart != pbStart[1]
if pbActive
// a new candle started but the previous one was not processed yet (e.g. the market closed early)
if pbNew and aPend
SW.process(aO, aH, aL, aC, aHT, aLT)
aPend := false
if pbNew or na(aO)
aO := open
aH := high
aL := low
aHT := time
aLT := time
else
if high > aH
aH := high
aHT := time
if low < aL
aL := low
aLT := time
aC := close
aPend := true
// the last chart bar of the period has closed, so the candle is complete
if barstate.isconfirmed and time_close == time_close(pbTf)
SW.process(aO, aH, aL, aC, aHT, aLT)
aPend := false
// Live zigzag tail: the unclosed pullback-timeframe candle is processed as a dry run, as if it closed now.
// So a new leg appears as soon as a high/low breaks out and may repaint until the candle closes.
varip int pbLastPrevT = -1
varip int pbLastET = 0
varip float pbLastEV = 0.0
varip int pbLastMoveBar = -1
if pbActive and barstate.islast and SW.init
line.delete(SW.openLn)
line.delete(SW.tentLn)
line.delete(SW.tentLn2)
Swing cur = SW
if aPend
cur := SW.copy()
cur.dry := true
cur.nSeg := 0
cur.process(aO, aH, aL, aC, aHT, aLT)
if cur.nSeg >= 1
SW.tentLn := line.new(cur.aT1, cur.aV1, math.max(cur.aT1, cur.aT2), cur.aV2, xloc = xloc.bar_time, color = pbCol, width = pbW, style = styleOf(pbSt))
if cur.nSeg >= 2
SW.tentLn2 := line.new(cur.bT1, cur.bV1, math.max(cur.bT1, cur.bT2), cur.bV2, xloc = xloc.bar_time, color = pbCol, width = pbW, style = styleOf(pbSt))
float eV = cur.up ? cur.pH : cur.pL
int eT = cur.up ? cur.pHT : cur.pLT
SW.openLn := line.new(cur.prevT, cur.prevV, math.max(cur.prevT, eT), eV, xloc = xloc.bar_time, color = pbCol, width = pbW, style = styleOf(pbSt))
// Last-point alerts, realtime only
if barstate.isrealtime
string tfTxt = timeframe.in_seconds(pbTf) >= 3600 and timeframe.in_seconds(pbTf) % 3600 == 0 ? str.tostring(int(timeframe.in_seconds(pbTf) / 3600)) + "h" : str.tostring(pbMin) + "m"
string what = cur.up ? "high" : "low"
string tent = aPend ? " (" + tfTxt + " candle not closed, may repaint)" : ""
if pbLastPrevT == -1
pbLastPrevT := cur.prevT
pbLastET := eT
pbLastEV := eV
else if cur.prevT != pbLastPrevT
if alPbNew
alert(syminfo.ticker + ": pullbacks " + tfTxt + ", new last point: " + what + " " + str.tostring(eV, format.mintick) +
", leg from " + str.tostring(cur.prevV, format.mintick) + tent, alert.freq_all)
pbLastPrevT := cur.prevT
pbLastET := eT
pbLastEV := eV
else if eT != pbLastET or eV != pbLastEV
if alPbMove and bar_index != pbLastMoveBar
alert(syminfo.ticker + ": pullbacks " + tfTxt + ", last point moved: " + what + " " + str.tostring(eV, format.mintick) + tent, alert.freq_all)
pbLastMoveBar := bar_index
pbLastET := eT
pbLastEV := eV
alertcondition(sigUpDn or sigDnUp or sigBoth, "Absorption: any", "{{ticker}}: absorption candle")
alertcondition(sigUpDn, "Absorption: high first, then low", "{{ticker}}: absorption candle (high swept first, then low)")
alertcondition(sigDnUp, "Absorption: low first, then high", "{{ticker}}: absorption candle (low swept first, then high)")