本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script(R) code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// (c) Shaakuni
//@version=6
indicator("Shaakuni - Market Structure (BOS/CHoCH) & Order Blocks", shorttitle="Shaakuni Structure & OB", overlay=true, max_bars_back=500, max_lines_count=500, max_labels_count=500, max_boxes_count=500)
// ==========================================
// Shaakuni - Market Structure (BOS/CHoCH) & Order Blocks
// 1) Market Structure: BOS / CHoCH (swing break confirmed on candle close)
// 2) Order Blocks (OB / POI): lookback break + ATR impulse rule, mitigation tracking
// Everything changes state on closed bars only. Swing levels and Order Blocks are drawn on an earlier
// candle after they are confirmed, so they are placed in the past. The two parts are calculated
// independently of each other. No buy/sell signals.
// ==========================================
// ==========================================
// 1. INPUTS
// ==========================================
grp_ms = "1. Market Structure"
struct_len = input.int(5, "Structure Swing Length", minval=2, group=grp_ms)
show_bull = input.bool(true, "Bullish Structure (BOS / CHoCH)", inline="bull", group=grp_ms)
col_bull = input.color(#089981, "", inline="bull", group=grp_ms)
show_bear = input.bool(true, "Bearish Structure (BOS / CHoCH)", inline="bear", group=grp_ms)
col_bear = input.color(#f23645, "", inline="bear", group=grp_ms)
grp_ob = "2. Order Blocks"
showOB = input.bool(true, "Show Order Blocks (OB / POI)", group=grp_ob)
showFresh = input.bool(true, "Show Unmitigated", group=grp_ob)
showMit = input.bool(false, "Show Mitigated", group=grp_ob)
lookbackMode = input.string("Auto", "Zone Lookback", options=["Auto", "Manual"], group=grp_ob)
lookbackMan = input.int(500, "Manual Lookback (bars)", minval=30, maxval=2000, group=grp_ob)
obSwingLen = input.int(10, "OB Structure Lookback (bars)", minval=3, maxval=50, group=grp_ob)
impMult = input.float(0.7, "OB Impulse Candle (x ATR)", minval=0.1, maxval=5.0, step=0.1, group=grp_ob)
colBullOB = input.color(color.new(color.rgb(41, 121, 255), 82), "Bull OB / POI", group=grp_ob)
colBearOB = input.color(color.new(color.rgb(255, 112, 67), 82), "Bear OB / POI", group=grp_ob)
// ==========================================
// 2. COMMON
// ==========================================
OB_MIN_HOFF = 3
BOX_EXTEND = 5
atrVal = ta.atr(14)
int tfms = timeframe.in_seconds() * 1000
string obTxt = timeframe.in_seconds(timeframe.period) >= 3600 ? "POI" : "OB"
autoLookback() =>
s = timeframe.in_seconds(timeframe.period)
s <= 60 ? 2000 : s <= 300 ? 1500 : s <= 900 ? 1000 : s <= 1800 ? 700 : s <= 3600 ? 500 : s <= 14400 ? 250 : 150
zoneLB = lookbackMode == "Auto" ? autoLookback() : lookbackMan
color C_BOB = colBullOB
color C_SOB = colBearOB
color C_BOB_B = color.new(colBullOB, 30)
color C_SOB_B = color.new(colBearOB, 30)
color C_HIDE = color.new(color.black, 100)
// ==========================================
// 3. MARKET STRUCTURE (BOS / CHoCH)
// A close above the latest unbroken swing high = bullish break; a close below the latest unbroken swing low = bearish break.
// If the previous structure was the opposite direction it is a CHoCH, otherwise (same direction or no prior structure) a BOS.
// ==========================================
sph = ta.pivothigh(high, struct_len, struct_len)
spl = ta.pivotlow(low, struct_len, struct_len)
var float upVal = na
var int upTime = na
var bool upCrossed = true
var float dnVal = na
var int dnTime = na
var bool dnCrossed = true
var int os = 0 // 1 = bullish structure, -1 = bearish structure
bool bullBos = false
bool bullChoch = false
bool bearBos = false
bool bearChoch = false
if barstate.isconfirmed
if not na(sph)
upVal := sph
upTime := time[struct_len]
upCrossed := false
if not na(spl)
dnVal := spl
dnTime := time[struct_len]
dnCrossed := false
if not upCrossed and not na(upVal) and close > upVal
bool isChoch = os == -1
upCrossed := true
os := 1
bullBos := not isChoch
bullChoch := isChoch
if show_bull
line.new(upTime, upVal, time, upVal, xloc=xloc.bar_time, color=col_bull, width=1)
label.new(int(math.avg(upTime, time)), upVal, isChoch ? "CHoCH" : "BOS", xloc=xloc.bar_time, style=label.style_label_down, color=color.new(color.black, 100), textcolor=col_bull, size=size.tiny)
if not dnCrossed and not na(dnVal) and close < dnVal
bool isChoch = os == 1
dnCrossed := true
os := -1
bearBos := not isChoch
bearChoch := isChoch
if show_bear
line.new(dnTime, dnVal, time, dnVal, xloc=xloc.bar_time, color=col_bear, width=1)
label.new(int(math.avg(dnTime, time)), dnVal, isChoch ? "CHoCH" : "BOS", xloc=xloc.bar_time, style=label.style_label_up, color=color.new(color.black, 100), textcolor=col_bear, size=size.tiny)
// ==========================================
// 4. ORDER BLOCKS / POI
// Bullish OB: the last bearish candle before the impulse that broke the recent highest high.
// Bearish OB: the last bullish candle before the impulse that broke the recent lowest low.
// A zone stays fresh until price trades back into it (mitigation).
// ==========================================
type OB
bool isBull
float top
float bot
int createdBar
int state = 0 // 0 fresh, 2 mitigated
int mitTime = na
box bx
var array<OB> obs = array.new<OB>()
var int lastBullObBar = -1
var int lastBearObBar = -1
bool newBullOB = false
bool newBearOB = false
if barstate.isconfirmed and not na(atrVal) and bar_index > obSwingLen + 2 and showOB
// ---- Bullish OB ----
float hh = high[1]
int hOff = 1
for i = 2 to obSwingLen
if high[i] > hh
hh := high[i]
hOff := i
if close > hh and hOff >= OB_MIN_HOFF
float mnLow = low[1]
int mnOff = 1
for i = 2 to hOff - 1
if low[i] < mnLow
mnLow := low[i]
mnOff := i
int dOff = -1
for k = 0 to mnOff - 1
int o = mnOff - 1 - k
if close[o] > open[o] and (close[o] - open[o]) >= atrVal * impMult
dOff := o
break
if dOff >= 0
int obOff = mnOff
for j = dOff + 1 to mnOff
if close[j] < open[j]
obOff := j
break
bool fresh = true
if obOff >= 2
for k = 0 to obOff - 2
if low[k] <= high[obOff]
fresh := false
break
int obBar = bar_index - obOff
if fresh and obBar != lastBullObBar
lastBullObBar := obBar
newBullOB := true
box nb = box.new(time[obOff], high[obOff], time + BOX_EXTEND * tfms, low[obOff], xloc=xloc.bar_time, bgcolor=C_BOB, border_color=C_BOB_B, border_width=1, text=obTxt, text_size=size.tiny, text_color=color.new(color.white, 15), text_halign=text.align_center, text_valign=text.align_center)
array.push(obs, OB.new(isBull=true, top=high[obOff], bot=low[obOff], createdBar=bar_index, bx=nb))
// ---- Bearish OB ----
float ll = low[1]
int lOff = 1
for i = 2 to obSwingLen
if low[i] < ll
ll := low[i]
lOff := i
if close < ll and lOff >= OB_MIN_HOFF
float mxHigh = high[1]
int mxOff = 1
for i = 2 to lOff - 1
if high[i] > mxHigh
mxHigh := high[i]
mxOff := i
int dOff = -1
for k = 0 to mxOff - 1
int o = mxOff - 1 - k
if close[o] < open[o] and (open[o] - close[o]) >= atrVal * impMult
dOff := o
break
if dOff >= 0
int obOff = mxOff
for j = dOff + 1 to mxOff
if close[j] > open[j]
obOff := j
break
bool fresh = true
if obOff >= 2
for k = 0 to obOff - 2
if high[k] >= low[obOff]
fresh := false
break
int obBar = bar_index - obOff
if fresh and obBar != lastBearObBar
lastBearObBar := obBar
newBearOB := true
box nb = box.new(time[obOff], high[obOff], time + BOX_EXTEND * tfms, low[obOff], xloc=xloc.bar_time, bgcolor=C_SOB, border_color=C_SOB_B, border_width=1, text=obTxt, text_size=size.tiny, text_color=color.new(color.white, 15), text_halign=text.align_center, text_valign=text.align_center)
array.push(obs, OB.new(isBull=false, top=high[obOff], bot=low[obOff], createdBar=bar_index, bx=nb))
// ---- mitigation, lookback cleanup, box update ----
if barstate.isconfirmed
int idx = array.size(obs) - 1
while idx >= 0
OB z = array.get(obs, idx)
if z.createdBar < bar_index - zoneLB
box.delete(z.bx)
array.remove(obs, idx)
else
if z.state == 0 and bar_index > z.createdBar and high >= z.bot and low <= z.top
z.state := 2
z.mitTime := time
if z.state == 0
box.set_right(z.bx, time + BOX_EXTEND * tfms)
if not showOB or not showFresh
box.set_bgcolor(z.bx, C_HIDE)
box.set_border_color(z.bx, C_HIDE)
box.set_text(z.bx, "")
else
box.set_bgcolor(z.bx, z.isBull ? C_BOB : C_SOB)
box.set_border_color(z.bx, z.isBull ? C_BOB_B : C_SOB_B)
else
box.set_right(z.bx, z.mitTime + 2 * tfms)
if not showOB or not showMit
box.set_bgcolor(z.bx, C_HIDE)
box.set_border_color(z.bx, C_HIDE)
box.set_text(z.bx, "")
else
box.set_bgcolor(z.bx, color.new(color.black, 75))
box.set_border_color(z.bx, C_HIDE)
idx := idx - 1
// ==========================================
// 5. ALERTS (choose "Once per bar close" when creating the alert)
// ==========================================
alertcondition(bullBos, "Bullish BOS", "Bullish BOS: bar closed above the latest swing high")
alertcondition(bullChoch, "Bullish CHoCH", "Bullish CHoCH: bar closed above the latest swing high after bearish structure")
alertcondition(bearBos, "Bearish BOS", "Bearish BOS: bar closed below the latest swing low")
alertcondition(bearChoch, "Bearish CHoCH", "Bearish CHoCH: bar closed below the latest swing low after bullish structure")
alertcondition(newBullOB, "New Bullish OB", "A new bullish Order Block was created")
alertcondition(newBearOB, "New Bearish OB", "A new bearish Order Block was created")