本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Recent Order Blocks", "ROB", overlay = true, max_lines_count = 50, max_labels_count = 50, max_bars_back = 500)
// ───────────────────────── Inputs ─────────────────────────
grpD = "Detection"
swingLen = input.int(5, "Swing strength (bars each side)", minval = 1, group = grpD,
tooltip = "Size of the swings whose break counts as a break of structure. Lower = smaller, more frequent OBs.")
mitMode = input.string("Touch", "OB is mitigated when price…", options = ["Touch", "50%", "Closes through"], group = grpD,
tooltip = "Touch: price trades back into the zone.\n50%: price reaches the middle of the zone.\nCloses through: a candle closes beyond the far side of the zone.")
replaceNewer = input.bool(true, "Replace with a newer OB before mitigation", group = grpD,
tooltip = "On: a fresh break of structure always moves the OB to the newest one.\nOff: the current OB stays until it is mitigated.\nEither way, when the shown OB is mitigated it falls back to the most recent unmitigated OB of that side.")
maxLook = 300
maxStore = 30
grpV = "Visuals"
bullCol = input.color(#089981, "Buy OB colour", group = grpV)
bearCol = input.color(#F23645, "Sell OB colour", group = grpV)
lineW = input.int(2, "Line width", minval = 1, maxval = 5, group = grpV)
showFill = input.bool(true, "Shade the zone", group = grpV)
showLabel = input.bool(true, "Show labels", group = grpV)
// ───────────────────────── Helpers ─────────────────────────
drawOB(int obBar, float top, float bot, color col, string txt, bool isBull) =>
l1 = line.new(obBar, top, bar_index, top, extend = extend.right, color = col, width = lineW)
l2 = line.new(obBar, bot, bar_index, bot, extend = extend.right, color = col, width = lineW)
linefill f = showFill ? linefill.new(l1, l2, color.new(col, 85)) : na
label lb = showLabel ? label.new(obBar, isBull ? bot : top, txt, style = isBull ? label.style_label_upper_left : label.style_label_lower_left, color = color.new(col, 100), textcolor = col, size = size.small) : na
[l1, l2, f, lb]
clearOB(line l1, line l2, linefill f, label lb) =>
linefill.delete(f)
line.delete(l1)
line.delete(l2)
label.delete(lb)
// ───────────────────────── Swings ─────────────────────────
ph = ta.pivothigh(high, swingLen, swingLen)
pl = ta.pivotlow(low, swingLen, swingLen)
var float swH = na
var int swHBar = na
var bool swHUsed = true
var float swL = na
var int swLBar = na
var bool swLUsed = true
if not na(ph)
swH := ph
swHBar := bar_index - swingLen
swHUsed := false
if not na(pl)
swL := pl
swLBar := bar_index - swingLen
swLUsed := false
// ───────────────────────── Stored OBs (all unmitigated ones) ─────────────────────────
var array<float> bTop = array.new<float>()
var array<float> bBot = array.new<float>()
var array<int> bBar = array.new<int>()
var array<int> bBos = array.new<int>()
var array<float> sTop = array.new<float>()
var array<float> sBot = array.new<float>()
var array<int> sBar = array.new<int>()
var array<int> sBos = array.new<int>()
// Currently displayed OB (identified by its break-of-structure bar)
var int bShown = na
var line bL1 = na
var line bL2 = na
var linefill bF = na
var label bLb = na
var int sShown = na
var line sL1 = na
var line sL2 = na
var linefill sF = na
var label sLb = na
bool newBull = false
bool newBear = false
bool bullMit = false
bool bearMit = false
// ───────────────────────── Mitigation (remove tapped OBs) ─────────────────────────
if bBos.size() > 0
for i = bBos.size() - 1 to 0
if bar_index > bBos.get(i)
t = bTop.get(i)
b = bBot.get(i)
hit = mitMode == "Touch" ? low <= t : mitMode == "50%" ? low <= (t + b) / 2 : close < b
if hit
if bBos.get(i) == bShown
bullMit := true
bTop.remove(i)
bBot.remove(i)
bBar.remove(i)
bBos.remove(i)
if sBos.size() > 0
for i = sBos.size() - 1 to 0
if bar_index > sBos.get(i)
t = sTop.get(i)
b = sBot.get(i)
hit = mitMode == "Touch" ? high >= b : mitMode == "50%" ? high >= (t + b) / 2 : close > t
if hit
if sBos.get(i) == sShown
bearMit := true
sTop.remove(i)
sBot.remove(i)
sBar.remove(i)
sBos.remove(i)
// ───────────────────────── New Buy OB (close above swing high) ─────────────────────────
if not swHUsed and close > swH
swHUsed := true
span = math.min(bar_index - swHBar, maxLook)
float bestLow = na
int bestOff = na
float anyLow = na
int anyOff = na
for i = 1 to span
if na(anyLow) or low[i] < anyLow
anyLow := low[i]
anyOff := i
if close[i] < open[i] and (na(bestLow) or low[i] < bestLow)
bestLow := low[i]
bestOff := i
int off = na(bestOff) ? anyOff : bestOff
if not na(off)
bTop.push(high[off])
bBot.push(low[off])
bBar.push(bar_index - off)
bBos.push(bar_index)
if bBos.size() > maxStore
bTop.shift()
bBot.shift()
bBar.shift()
bBos.shift()
// ───────────────────────── New Sell OB (close below swing low) ─────────────────────────
if not swLUsed and close < swL
swLUsed := true
span = math.min(bar_index - swLBar, maxLook)
float bestHigh = na
int bestOff = na
float anyHigh = na
int anyOff = na
for i = 1 to span
if na(anyHigh) or high[i] > anyHigh
anyHigh := high[i]
anyOff := i
if close[i] > open[i] and (na(bestHigh) or high[i] > bestHigh)
bestHigh := high[i]
bestOff := i
int off = na(bestOff) ? anyOff : bestOff
if not na(off)
sTop.push(high[off])
sBot.push(low[off])
sBar.push(bar_index - off)
sBos.push(bar_index)
if sBos.size() > maxStore
sTop.shift()
sBot.shift()
sBar.shift()
sBos.shift()
// ───────────────────────── Pick which Buy OB to show ─────────────────────────
int bTarget = na
int bIdx = na
if bBos.size() > 0
keep = not replaceNewer and not na(bShown) and bBos.includes(bShown)
bIdx := keep ? bBos.indexof(bShown) : bBos.size() - 1
bTarget := bBos.get(bIdx)
bChanged = (na(bTarget) and not na(bShown)) or (not na(bTarget) and (na(bShown) or bTarget != bShown))
if bChanged
clearOB(bL1, bL2, bF, bLb)
bShown := bTarget
if not na(bTarget)
[l1, l2, f, lb] = drawOB(bBar.get(bIdx), bTop.get(bIdx), bBot.get(bIdx), bullCol, "Buy OB", true)
bL1 := l1
bL2 := l2
bF := f
bLb := lb
newBull := bTarget == bar_index
// ───────────────────────── Pick which Sell OB to show ─────────────────────────
int sTarget = na
int sIdx = na
if sBos.size() > 0
keep = not replaceNewer and not na(sShown) and sBos.includes(sShown)
sIdx := keep ? sBos.indexof(sShown) : sBos.size() - 1
sTarget := sBos.get(sIdx)
sChanged = (na(sTarget) and not na(sShown)) or (not na(sTarget) and (na(sShown) or sTarget != sShown))
if sChanged
clearOB(sL1, sL2, sF, sLb)
sShown := sTarget
if not na(sTarget)
[l1, l2, f, lb] = drawOB(sBar.get(sIdx), sTop.get(sIdx), sBot.get(sIdx), bearCol, "Sell OB", false)
sL1 := l1
sL2 := l2
sF := f
sLb := lb
newBear := sTarget == bar_index
// ───────────────────────── Alerts ─────────────────────────
alertcondition(newBull, "New Buy OB", "{{ticker}} {{interval}}: new bullish order block formed")
alertcondition(newBear, "New Sell OB", "{{ticker}} {{interval}}: new bearish order block formed")
alertcondition(bullMit, "Buy OB mitigated", "{{ticker}} {{interval}}: price tapped the bullish order block")
alertcondition(bearMit, "Sell OB mitigated", "{{ticker}} {{interval}}: price tapped the bearish order block")