本页源码来自 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/
// © PhenLabs
//@version=6
indicator("Smart Money Order Block & Volume Profile [PhenLabs]", shorttitle = "SMOBVP [PhenLabs]", overlay = true, max_boxes_count = 500, max_lines_count = 500, max_labels_count = 500, max_bars_back = 500)
// ─────────────────────────────────────────────────────────────────────────────
// ENUMS
// ─────────────────────────────────────────────────────────────────────────────
enum FilterPreset
strict = "Strict"
balanced = "Balanced"
aggressive = "Aggressive"
enum TblPos
br = "Bottom Right"
bl = "Bottom Left"
tl = "Top Left"
enum TblSize
tiny = "Tiny"
small = "Small"
normal = "Normal"
// ─────────────────────────────────────────────────────────────────────────────
// INPUTS (13)
// ─────────────────────────────────────────────────────────────────────────────
string gSig = "Order Block Signals"
string gProf = "Buyer vs Seller Volume Profile"
string gVis = "Visuals & Dashboard"
FilterPreset presetIn = input.enum(FilterPreset.balanced, "Signal Filter", group = gSig, tooltip = "Strict: OB must sit in a heavier volume node (30% of POC) with 48% defender pressure. Balanced: 25% of POC with 48% defender pressure. Aggressive: 25% of POC, no defender-pressure requirement.")
int swingLen = input.int(5, "Swing Length", minval = 2, maxval = 20, group = gSig, tooltip = "Bars on each side used to confirm the swing high or low that price must break to create an order block.")
float dispMult = input.float(1.0, "Displacement Body (x ATR)", minval = 0.3, maxval = 3.0, step = 0.1, group = gSig, tooltip = "Minimum candle body, in ATR multiples, for the structure break that creates an order block.")
bool useTrend = input.bool(true, "Trend + POC Filter", group = gSig, tooltip = "Longs only above the 200 EMA and not far below the profile POC. Shorts only below the 200 EMA and not far above the POC.")
float t1R = input.float(1.5, "Targets T1 / T2 (R)", minval = 0.5, maxval = 10.0, step = 0.1, inline = "tp", group = gSig)
float t2R = input.float(3.0, "", minval = 0.5, maxval = 10.0, step = 0.1, inline = "tp", group = gSig)
int profLen = input.int(100, "Profile Lookback (bars)", minval = 30, maxval = 300, group = gProf, tooltip = "Rolling window used for the 24-row buyer vs seller volume profile and POC.")
bool showProf = input.bool(true, "Show Buyer / Seller Profile", group = gProf)
int profWidth = input.int(30, "Profile Width (bars)", minval = 10, maxval = 80, group = gProf, active = showProf)
int maxOBShow = input.int(3, "Active Order Blocks Shown", minval = 0, maxval = 5, group = gVis, tooltip = "Number of nearest untested order blocks drawn on the chart.")
color bullCol = input.color(#22C3A6, "Buyer / Seller Colors", inline = "col", group = gVis)
color bearCol = input.color(#F0506E, "", inline = "col", group = gVis)
TblPos tblPosIn = input.enum(TblPos.br, "Dashboard Position / Size", inline = "tbl", group = gVis)
TblSize tblSizeIn = input.enum(TblSize.small, "", inline = "tbl", group = gVis)
// ─────────────────────────────────────────────────────────────────────────────
// CONSTANTS & PRESETS
// ─────────────────────────────────────────────────────────────────────────────
int BINS = 24
int MAX_AGE = 60
int TIME_STOP = 80
color pocCol = #F5C451
color txtCol = chart.fg_color
float volGate = switch presetIn
FilterPreset.strict => 0.30
FilterPreset.balanced => 0.25
FilterPreset.aggressive => 0.25
float defGate = switch presetIn
FilterPreset.strict => 0.48
FilterPreset.balanced => 0.48
FilterPreset.aggressive => 0.0
// ─────────────────────────────────────────────────────────────────────────────
// TYPES
// ─────────────────────────────────────────────────────────────────────────────
type OB
int dir
float top
float bot
int ci
int tOrig
bool used
float vr
float defp
// ─────────────────────────────────────────────────────────────────────────────
// CORE SERIES
// ─────────────────────────────────────────────────────────────────────────────
float atr = ta.atr(14)
float ema200 = ta.ema(close, 200)
float rngRaw = math.max(high - low, 1e-9)
bool hasVol = not na(volume) and volume > 0
var bool anyVol = false
anyVol := anyVol or hasVol
float effVol = hasVol ? volume : rngRaw
float buyFrac = math.min(math.max((close - low) / rngRaw, 0.0), 1.0)
float buyVol = effVol * buyFrac
float typP = hlc3
float wH = ta.highest(high, profLen)
float wL = ta.lowest(low, profLen)
float stepP = (wH - wL) / BINS
float pivWinH = ta.highest(high, 2 * swingLen + 1)
float pivWinL = ta.lowest(low, 2 * swingLen + 1)
float bodySz = math.abs(close - open)
bool ready = bar_index >= math.max(205, profLen + 5) and not na(atr) and atr > 0 and stepP > 0
bool confirmed = barstate.isconfirmed
max_bars_back(time, 320)
max_bars_back(high, 320)
max_bars_back(low, 320)
max_bars_back(open, 320)
max_bars_back(close, 320)
max_bars_back(effVol, 320)
max_bars_back(buyVol, 320)
max_bars_back(typP, 320)
// ─────────────────────────────────────────────────────────────────────────────
// ROLLING 24-ROW BUYER vs SELLER PROFILE (closed-bar values on history)
// ─────────────────────────────────────────────────────────────────────────────
array<float> binTot = array.new<float>(BINS, 0.0)
array<float> binBuy = array.new<float>(BINS, 0.0)
int pocIdx = 0
float pocVol = 1e-9
float pocPx = na
if ready
for j = 0 to profLen - 1
int b = math.floor((typP[j] - wL) / stepP)
b := math.min(math.max(b, 0), BINS - 1)
binTot.set(b, binTot.get(b) + effVol[j])
binBuy.set(b, binBuy.get(b) + buyVol[j])
for b = 0 to BINS - 1
if binTot.get(b) > pocVol
pocVol := binTot.get(b)
pocIdx := b
pocPx := wL + (pocIdx + 0.5) * stepP
// ─────────────────────────────────────────────────────────────────────────────
// STATE
// ─────────────────────────────────────────────────────────────────────────────
var array<OB> obs = array.new<OB>()
var float lph = na
var float lpl = na
var int lphi = -1
var int lpli = -1
var int tDir = 0
var float tE = na
var float tSL = na
var float tR = na
var float tT1 = na
var float tT2 = na
var bool tH1 = false
var int tBar = -1
var int lastExit = -1
var int nTr = 0
var int nWin = 0
var float netR = 0.0
var line lnE = na
var line lnSL = na
var line lnT1 = na
var line lnT2 = na
var label lbSL = na
var label lbT1 = na
var label lbT2 = na
bool longSig = false
bool shortSig = false
float sigDefp = na
float lastResR = na
// ─────────────────────────────────────────────────────────────────────────────
// 1. TRADE MANAGEMENT (closed-bar evaluation; stop checked first = conservative)
// ─────────────────────────────────────────────────────────────────────────────
if ready and tDir != 0 and bar_index > tBar
lnE.set_x2(time)
lnSL.set_x2(time)
lnT1.set_x2(time)
lnT2.set_x2(time)
lbSL.set_x(time)
lbT1.set_x(time)
lbT2.set_x(time)
if confirmed
bool done = false
float ro = na
string how = ""
if tDir == 1
if low <= tSL
ro := tH1 ? 0.5 * t1R - 0.5 : -1.0
how := "SL"
done := true
else
if not tH1 and high >= tT1
tH1 := true
lbT1.set_text("T1 ✓")
if high >= tT2
ro := 0.5 * t1R + 0.5 * t2R
how := "TP"
done := true
else if bar_index - tBar >= TIME_STOP
float cr = (close - tE) / tR
ro := tH1 ? 0.5 * t1R + 0.5 * cr : cr
how := "TIME"
done := true
else
if high >= tSL
ro := tH1 ? 0.5 * t1R - 0.5 : -1.0
how := "SL"
done := true
else
if not tH1 and low <= tT1
tH1 := true
lbT1.set_text("T1 ✓")
if low <= tT2
ro := 0.5 * t1R + 0.5 * t2R
how := "TP"
done := true
else if bar_index - tBar >= TIME_STOP
float cr = (tE - close) / tR
ro := tH1 ? 0.5 * t1R + 0.5 * cr : cr
how := "TIME"
done := true
if done
nTr += 1
nWin += ro > 0 ? 1 : 0
netR += ro
lastResR := ro
if how == "TP"
lbT2.set_text("T2 ✓ " + (ro >= 0 ? "+" : "") + str.tostring(ro, "#.##") + "R")
else if how == "SL"
lbSL.set_text((tH1 ? "STOP after T1 " : "STOP ") + (ro >= 0 ? "+" : "") + str.tostring(ro, "#.##") + "R")
else
lbT2.set_text("TIME EXIT " + (ro >= 0 ? "+" : "") + str.tostring(ro, "#.##") + "R")
tDir := 0
lastExit := bar_index
// ─────────────────────────────────────────────────────────────────────────────
// 2. ORDER BLOCK MITIGATION & CLOSED-BAR SIGNALS
// ─────────────────────────────────────────────────────────────────────────────
bool upOk = not useTrend or (close > ema200 and close >= pocPx - 0.75 * atr)
bool dnOk = not useTrend or (close < ema200 and close <= pocPx + 0.75 * atr)
if ready and confirmed
array<OB> keep = array.new<OB>()
if obs.size() > 0
for ob in obs
bool drop = bar_index - ob.ci > MAX_AGE
if not drop
int b0 = math.min(math.max(math.floor((ob.bot - wL) / stepP), 0), BINS - 1)
int b1 = math.min(math.max(math.floor((ob.top - wL) / stepP), 0), BINS - 1)
int lob = math.min(b0, b1)
int hib = math.max(b0, b1)
float zt = 0.0
float zb = 0.0
float zm = 0.0
for b = lob to hib
zt += binTot.get(b)
zb += binBuy.get(b)
zm := math.max(zm, binTot.get(b))
float bs = zb / (zt > 1e-9 ? zt : 1e-9)
ob.vr := zm / pocVol
ob.defp := ob.dir == 1 ? bs : 1.0 - bs
float mid = 0.5 * (ob.top + ob.bot)
if ob.dir == 1
if close < ob.bot
drop := true
else if bar_index > ob.ci and low <= ob.top and not ob.used
bool recTop = close > ob.top and close > open
bool recMid = close >= mid and close > open and (close - low) >= 0.45 * rngRaw
bool recWick = close >= ob.bot and (close - low) >= 0.55 * rngRaw
if recTop or recMid or recWick
ob.used := true
if recMid and tDir == 0 and bar_index > lastExit and upOk and ob.vr >= volGate and ob.defp >= defGate
float sl = math.min(ob.bot, low) - 0.35 * atr
float r = close - sl
if r >= 0.3 * atr and r <= 3.5 * atr
tDir := 1
tE := close
tSL := sl
tR := r
tT1 := close + t1R * r
tT2 := close + t2R * r
tH1 := false
tBar := bar_index
longSig := true
sigDefp := ob.defp
box.new(ob.tOrig, ob.top, time, ob.bot, xloc = xloc.bar_time, border_color = color.new(bullCol, 30), bgcolor = color.new(bullCol, 82), text = "BULL OB · " + str.tostring(math.round(ob.defp * 100)) + "%", text_color = color.new(bullCol, 0), text_size = size.small, text_halign = text.align_left, text_valign = text.align_bottom)
else
if close > ob.top
drop := true
else if bar_index > ob.ci and high >= ob.bot and not ob.used
bool recTop = close < ob.bot and close < open
bool recMid = close <= mid and close < open and (high - close) >= 0.45 * rngRaw
bool recWick = close <= ob.top and (high - close) >= 0.55 * rngRaw
if recTop or recMid or recWick
ob.used := true
if recMid and tDir == 0 and bar_index > lastExit and dnOk and ob.vr >= volGate and ob.defp >= defGate
float sl = math.max(ob.top, high) + 0.35 * atr
float r = sl - close
if r >= 0.3 * atr and r <= 3.5 * atr
tDir := -1
tE := close
tSL := sl
tR := r
tT1 := close - t1R * r
tT2 := close - t2R * r
tH1 := false
tBar := bar_index
shortSig := true
sigDefp := ob.defp
box.new(ob.tOrig, ob.top, time, ob.bot, xloc = xloc.bar_time, border_color = color.new(bearCol, 30), bgcolor = color.new(bearCol, 82), text = "BEAR OB · " + str.tostring(math.round(ob.defp * 100)) + "%", text_color = color.new(bearCol, 0), text_size = size.small, text_halign = text.align_left, text_valign = text.align_top)
if ob.used
drop := true
if not drop
keep.push(ob)
while keep.size() > 5
keep.shift()
obs := keep
// Signal drawings (event-time anchored, Lesson: xloc.bar_time)
if longSig or shortSig
color sc = longSig ? bullCol : bearCol
label.new(time, longSig ? low - 0.25 * atr : high + 0.25 * atr, longSig ? "BUY" : "SELL", xloc = xloc.bar_time, yloc = yloc.price, style = longSig ? label.style_label_up : label.style_label_down, color = sc, textcolor = color.white, size = size.normal)
lnE := line.new(time, tE, time, tE, xloc = xloc.bar_time, color = color.new(txtCol, 45), width = 1, style = line.style_dotted)
lnSL := line.new(time, tSL, time, tSL, xloc = xloc.bar_time, color = color.new(bearCol, 0), width = 2)
lnT1 := line.new(time, tT1, time, tT1, xloc = xloc.bar_time, color = color.new(bullCol, 25), width = 1, style = line.style_dashed)
lnT2 := line.new(time, tT2, time, tT2, xloc = xloc.bar_time, color = color.new(bullCol, 0), width = 2)
lbSL := label.new(time, tSL, "STOP", xloc = xloc.bar_time, yloc = yloc.price, style = label.style_label_left, color = color.new(color.black, 100), textcolor = bearCol, size = size.small)
lbT1 := label.new(time, tT1, "T1", xloc = xloc.bar_time, yloc = yloc.price, style = label.style_label_left, color = color.new(color.black, 100), textcolor = bullCol, size = size.small)
lbT2 := label.new(time, tT2, "T2", xloc = xloc.bar_time, yloc = yloc.price, style = label.style_label_left, color = color.new(color.black, 100), textcolor = bullCol, size = size.small)
// ─────────────────────────────────────────────────────────────────────────────
// 3. CONFIRMED SWING PIVOTS (pivot at bar_index - swingLen, no lookahead)
// ─────────────────────────────────────────────────────────────────────────────
if ready and confirmed
if high[swingLen] == pivWinH
lph := high[swingLen]
lphi := bar_index - swingLen
if low[swingLen] == pivWinL
lpl := low[swingLen]
lpli := bar_index - swingLen
// ─────────────────────────────────────────────────────────────────────────────
// 4. NEW ORDER BLOCKS FROM DISPLACEMENT BREAKS OF STRUCTURE
// ─────────────────────────────────────────────────────────────────────────────
if ready and confirmed and not na(lph) and close > lph and bodySz >= dispMult * atr
int k = 1
int lim = math.min(9, bar_index - lphi)
if lim >= 1
for off = 1 to lim
if close[off] < open[off]
k := off
break
float bot = low[k]
float bTop = math.max(open[k], close[k])
float top = (bTop - bot) >= 0.25 * atr ? bTop : high[k]
top := math.min(top, bot + 1.4 * atr)
if top > bot
obs.push(OB.new(1, top, bot, bar_index, time[k], false, 0.0, 0.5))
lph := na
if ready and confirmed and not na(lpl) and close < lpl and bodySz >= dispMult * atr
int k = 1
int lim = math.min(9, bar_index - lpli)
if lim >= 1
for off = 1 to lim
if close[off] > open[off]
k := off
break
float top = high[k]
float bBot = math.min(open[k], close[k])
float bot = (top - bBot) >= 0.25 * atr ? bBot : low[k]
bot := math.max(bot, top - 1.4 * atr)
if top > bot
obs.push(OB.new(-1, top, bot, bar_index, time[k], false, 0.0, 0.5))
lpl := na
// ─────────────────────────────────────────────────────────────────────────────
// PINE SCREENER / DATA WINDOW (first plots)
// ─────────────────────────────────────────────────────────────────────────────
float nearDef = na
float nearVr = na
float nearDist = 1e12
if obs.size() > 0
for ob in obs
float d = math.abs(close - 0.5 * (ob.top + ob.bot))
if d < nearDist
nearDist := d
nearDef := ob.defp * 100
nearVr := ob.vr * 100
plot(longSig ? 1 : shortSig ? -1 : 0, "Signal Direction", display = display.data_window + display.pine_screener)
plot(tDir, "Active Trade", display = display.data_window + display.pine_screener)
plot(nearDef, "Nearest OB Defense %", display = display.data_window + display.pine_screener)
plot(nearVr, "Nearest OB Volume vs POC %", display = display.data_window + display.pine_screener)
plot(na(pocPx) ? na : (close - pocPx) / atr, "Distance to POC (ATR)", display = display.data_window + display.pine_screener)
plot(nTr > 0 ? 100.0 * nWin / nTr : na, "Win Rate %", display = display.data_window + display.pine_screener)
// Invisible price anchors keep the Y-axis locked to price
plot(high, "Anchor High", color = color.new(color.white, 100), display = display.pane, editable = false)
plot(low, "Anchor Low", color = color.new(color.white, 100), display = display.pane, editable = false)
// ─────────────────────────────────────────────────────────────────────────────
// LAST-BAR VISUALS: PROFILE, POC, ACTIVE ORDER BLOCKS, DASHBOARD
// ─────────────────────────────────────────────────────────────────────────────
var array<box> profBoxes = array.new<box>()
var array<box> obBoxes = array.new<box>()
var line pocLine = na
var label pocLabel = na
if barstate.islast and ready
// clear previous last-bar objects
if profBoxes.size() > 0
for bx in profBoxes
bx.delete()
profBoxes.clear()
if obBoxes.size() > 0
for bx in obBoxes
bx.delete()
obBoxes.clear()
pocLine.delete()
pocLabel.delete()
if showProf
int x0 = bar_index + 3
for b = 0 to BINS - 1
float tot = binTot.get(b)
if tot > 0
float frac = tot / pocVol
float bfrac = binBuy.get(b) / tot
int wTot = math.max(1, math.round(frac * profWidth))
int wBuy = math.round(wTot * bfrac)
float yLo = wL + b * stepP
float yHi = yLo + stepP * 0.86
int tr = frac >= 0.70 ? 15 : frac >= 0.25 ? 45 : 72
if wBuy > 0
profBoxes.push(box.new(x0, yHi, x0 + wBuy, yLo, xloc = xloc.bar_index, border_color = color.new(bullCol, 100), bgcolor = color.new(bullCol, tr)))
if wTot - wBuy > 0
profBoxes.push(box.new(x0 + wBuy, yHi, x0 + wTot, yLo, xloc = xloc.bar_index, border_color = color.new(bearCol, 100), bgcolor = color.new(bearCol, tr)))
pocLine := line.new(bar_index - profLen + 1, pocPx, x0 + profWidth, pocPx, xloc = xloc.bar_index, color = pocCol, width = 2)
pocLabel := label.new(x0 + profWidth, pocPx, "POC", xloc = xloc.bar_index, yloc = yloc.price, style = label.style_label_left, color = color.new(color.black, 100), textcolor = pocCol, size = size.small)
// nearest untested order blocks
if obs.size() > 0 and maxOBShow > 0
array<float> dists = array.new<float>()
for ob in obs
dists.push(math.abs(close - 0.5 * (ob.top + ob.bot)))
array<int> order = dists.sort_indices(order.ascending)
int shown = 0
for idx in order
if shown >= maxOBShow
break
OB ob = obs.get(idx)
color oc = ob.dir == 1 ? bullCol : bearCol
string tg = (ob.dir == 1 ? "BULL OB · " : "BEAR OB · ") + str.tostring(math.round(ob.defp * 100)) + "%"
obBoxes.push(box.new(ob.tOrig, ob.top, time, ob.bot, xloc = xloc.bar_time, border_color = color.new(oc, 35), bgcolor = color.new(oc, 85), text = tg, text_color = oc, text_size = size.small, text_halign = text.align_right, text_valign = ob.dir == 1 ? text.align_bottom : text.align_top))
shown += 1
// Dashboard
string tSize = switch tblSizeIn
TblSize.tiny => size.tiny
TblSize.small => size.small
TblSize.normal => size.normal
string tPos = switch tblPosIn
TblPos.br => position.bottom_right
TblPos.bl => position.bottom_left
TblPos.tl => position.top_left
var table dash = table.new(tPos, 2, 6, bgcolor = color.new(chart.bg_color, 10), border_color = color.new(chart.fg_color, 80), border_width = 1, frame_color = color.new(chart.fg_color, 70), frame_width = 1)
if barstate.islast
string sigTxt = tDir == 1 ? "LONG ACTIVE" : tDir == -1 ? "SHORT ACTIVE" : "WAITING"
color sigCol = tDir == 1 ? bullCol : tDir == -1 ? bearCol : color.new(txtCol, 30)
string modeTxt = presetIn == FilterPreset.strict ? "Strict" : presetIn == FilterPreset.balanced ? "Balanced" : "Aggressive"
table.cell(dash, 0, 0, "SMOBVP", text_color = txtCol, text_size = tSize, text_formatting = text.format_bold, text_halign = text.align_left)
table.cell(dash, 1, 0, modeTxt, text_color = color.new(txtCol, 30), text_size = tSize, text_halign = text.align_right)
table.cell(dash, 0, 1, "Signal", text_color = color.new(txtCol, 30), text_size = tSize, text_halign = text.align_left)
table.cell(dash, 1, 1, sigTxt, text_color = sigCol, text_size = tSize, text_formatting = text.format_bold, text_halign = text.align_right)
table.cell(dash, 0, 2, "Nearest OB Defense", text_color = color.new(txtCol, 30), text_size = tSize, text_halign = text.align_left)
table.cell(dash, 1, 2, na(nearDef) ? "none" : str.tostring(math.round(nearDef)) + "%", text_color = txtCol, text_size = tSize, text_halign = text.align_right)
table.cell(dash, 0, 3, "POC", text_color = color.new(txtCol, 30), text_size = tSize, text_halign = text.align_left)
table.cell(dash, 1, 3, na(pocPx) ? "n/a" : str.tostring(pocPx, format.mintick), text_color = pocCol, text_size = tSize, text_halign = text.align_right)
table.cell(dash, 0, 4, "Data", text_color = color.new(txtCol, 30), text_size = tSize, text_halign = text.align_left)
table.cell(dash, 1, 4, anyVol ? "REAL VOL" : "PRICE PROXY", text_color = txtCol, text_size = tSize, text_halign = text.align_right)
table.cell(dash, 0, 5, "Trades / Win / Net", text_color = color.new(txtCol, 30), text_size = tSize, text_halign = text.align_left)
table.cell(dash, 1, 5, str.tostring(nTr) + " / " + (nTr > 0 ? str.tostring(math.round(100.0 * nWin / nTr)) + "%" : "-") + " / " + (netR >= 0 ? "+" : "") + str.tostring(netR, "#.#") + "R", text_color = netR >= 0 ? bullCol : bearCol, text_size = tSize, text_halign = text.align_right)
// ─────────────────────────────────────────────────────────────────────────────
// ALERTS
// ─────────────────────────────────────────────────────────────────────────────
alertcondition(longSig, "SMOBVP Bullish Order Block Signal", "SMOBVP: BUY at a volume-backed bullish order block on {{ticker}} {{interval}}")
alertcondition(shortSig, "SMOBVP Bearish Order Block Signal", "SMOBVP: SELL at a volume-backed bearish order block on {{ticker}} {{interval}}")
alertcondition(longSig or shortSig, "SMOBVP Any Order Block Signal", "SMOBVP: new order block signal on {{ticker}} {{interval}}")