本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("smc order block + bos/choch model with sl/tp", overlay=true, max_boxes_count=200, max_lines_count=500, max_labels_count=500, max_bars_back=500)
// ==========================================
// 1. INPUTS
// ==========================================
var string G_STR = "market structure (bos / choch)"
swingLen = input.int(5, "swing length", minval=1, group=G_STR)
showStructure = input.bool(true, "show structure lines + labels", group=G_STR)
showBOS = input.bool(true, "show bos", group=G_STR)
showCHoCH = input.bool(true, "show choch", group=G_STR)
var string G_OB = "order blocks"
showOB = input.bool(true, "show order blocks", group=G_OB)
chochOnly = input.bool(false, "create ob only on choch (reversal)", group=G_OB)
trendFilter = input.bool(true, "only trade in direction of structure", group=G_OB)
maxOB = input.int(4, "max active ob per side", minval=1, maxval=20, group=G_OB)
removeMit = input.bool(true, "remove ob when mitigated", group=G_OB)
var string G_SIG = "signals"
showArrows = input.bool(true, "show entry arrows", group=G_SIG)
var string G_SLTP = "sl / tp"
showSLTP = input.bool(true, "show sl/tp with arrows", group=G_SLTP)
atrLen = input.int(14, "atr length", minval=1, group=G_SLTP)
slBuffer = input.float(0.2, "sl buffer (x atr)", minval=0.0, step=0.1, group=G_SLTP)
tp1RR = input.float(1.0, "tp1 risk:reward", minval=0.1, step=0.1, group=G_SLTP)
tp2RR = input.float(2.0, "tp2 risk:reward", minval=0.1, step=0.1, group=G_SLTP)
showTP2 = input.bool(true, "show tp2", group=G_SLTP)
tradeLen = input.int(20, "sl/tp line length (bars)", minval=3, group=G_SLTP)
maxTrades = input.int(5, "max trades kept on chart", minval=1, maxval=50, group=G_SLTP)
showPrices = input.bool(true, "show price in labels", group=G_SLTP)
var string G_COL = "colors"
bullOBCol = input.color(color.new(color.green, 85), "bullish ob fill", group=G_COL)
bullOBBorder = input.color(color.new(color.green, 50), "bullish ob border", group=G_COL)
bearOBCol = input.color(color.new(color.red, 85), "bearish ob fill", group=G_COL)
bearOBBorder = input.color(color.new(color.red, 50), "bearish ob border", group=G_COL)
mitCol = input.color(color.new(color.gray, 88), "mitigated ob (if kept)", group=G_COL)
bullStructCol = input.color(color.teal, "bullish bos/choch", group=G_COL)
bearStructCol = input.color(color.orange, "bearish bos/choch", group=G_COL)
bullArrowCol = input.color(color.lime, "buy arrow", group=G_COL)
bearArrowCol = input.color(color.fuchsia, "sell arrow", group=G_COL)
entryCol = input.color(color.gray, "entry line", group=G_COL)
slCol = input.color(color.red, "sl line", group=G_COL)
tp1Col = input.color(color.green, "tp1 line", group=G_COL)
tp2Col = input.color(color.aqua, "tp2 line", group=G_COL)
// ==========================================
// 2. TYPES & STORAGE
// ==========================================
type OB
box b
float top
float bottom
bool isBull
int created
bool used
type Trade
line entryL
line slL
line tp1L
line tp2L
label entryLb
label slLb
label tp1Lb
label tp2Lb
var OB[] bullOBs = array.new<OB>()
var OB[] bearOBs = array.new<OB>()
var Trade[] trades = array.new<Trade>()
// ==========================================
// 3. HELPERS
// ==========================================
priceTxt(string name, float p) =>
showPrices ? name + " " + str.tostring(p, format.mintick) : name
drawTrade(bool isLong, float entry, float sl) =>
float risk = math.abs(entry - sl)
float tp1 = isLong ? entry + risk * tp1RR : entry - risk * tp1RR
float tp2 = isLong ? entry + risk * tp2RR : entry - risk * tp2RR
int x2 = bar_index + tradeLen
line eL = line.new(bar_index, entry, x2, entry, color=entryCol, style=line.style_dotted, width=1)
line sL = line.new(bar_index, sl, x2, sl, color=slCol, style=line.style_solid, width=1)
line t1L = line.new(bar_index, tp1, x2, tp1, color=tp1Col, style=line.style_solid, width=1)
line t2L = showTP2 ? line.new(bar_index, tp2, x2, tp2, color=tp2Col, style=line.style_solid, width=1) : na
label eLb = label.new(x2, entry, priceTxt("entry", entry), color=color.new(color.white, 100), textcolor=entryCol, style=label.style_label_left, size=size.small)
label sLb = label.new(x2, sl, priceTxt("sl", sl), color=color.new(color.white, 100), textcolor=slCol, style=label.style_label_left, size=size.small)
label t1Lb = label.new(x2, tp1, priceTxt("tp1", tp1), color=color.new(color.white, 100), textcolor=tp1Col, style=label.style_label_left, size=size.small)
label t2Lb = showTP2 ? label.new(x2, tp2, priceTxt("tp2", tp2), color=color.new(color.white, 100), textcolor=tp2Col, style=label.style_label_left, size=size.small) : na
array.push(trades, Trade.new(eL, sL, t1L, t2L, eLb, sLb, t1Lb, t2Lb))
if array.size(trades) > maxTrades
Trade old = array.shift(trades)
line.delete(old.entryL)
line.delete(old.slL)
line.delete(old.tp1L)
line.delete(old.tp2L)
label.delete(old.entryLb)
label.delete(old.slLb)
label.delete(old.tp1Lb)
label.delete(old.tp2Lb)
array.size(trades)
// ==========================================
// 4. CALCULATIONS
// ==========================================
pHi = ta.pivothigh(high, swingLen, swingLen)
pLo = ta.pivotlow(low, swingLen, swingLen)
atrV = ta.atr(atrLen)
var float lastSH = na
var int lastSHidx = na
var bool shBroken = true
var float lastSL = na
var int lastSLidx = na
var bool slBroken = true
var int trend = 0
bool longSig = false
bool shortSig = false
bool bullCHoCH = false
bool bearCHoCH = false
if barstate.isconfirmed
// ---- track swings ----
if not na(pHi)
lastSH := pHi
lastSHidx := bar_index - swingLen
shBroken := false
if not na(pLo)
lastSL := pLo
lastSLidx := bar_index - swingLen
slBroken := false
// ---- bullish break (close above last swing high) ----
if not shBroken and not na(lastSH) and close > lastSH
bool isCh = trend == -1
float lvl = lastSH
int startX = math.max(lastSHidx, bar_index - 400)
shBroken := true
trend := 1
if isCh
bullCHoCH := true
if showStructure and ((isCh and showCHoCH) or (not isCh and showBOS))
line.new(startX, lvl, bar_index, lvl, color=bullStructCol, style=isCh ? line.style_solid : line.style_dotted, width=1)
label.new(math.round(math.avg(startX, bar_index)), lvl, isCh ? "CHoCH" : "BOS", color=color.new(color.white, 100), textcolor=bullStructCol, style=label.style_label_down, size=size.small)
// bullish ob = candle with lowest low in the leg since last swing low
if showOB and (isCh or not chochOnly)
int refIdx = math.max(math.max(nz(lastSLidx, bar_index - 20), bar_index - 100), 0)
int off = bar_index - refIdx
float minL = low
int minI = 0
for i = 1 to off
if low[i] < minL
minL := low[i]
minI := i
float oTop = high[minI]
float oBot = low[minI]
box bx = box.new(bar_index - minI, oTop, bar_index, oBot, bgcolor=bullOBCol, border_color=bullOBBorder)
array.push(bullOBs, OB.new(bx, oTop, oBot, true, bar_index, false))
if array.size(bullOBs) > maxOB
OB oldOB = array.shift(bullOBs)
box.delete(oldOB.b)
// ---- bearish break (close below last swing low) ----
if not slBroken and not na(lastSL) and close < lastSL
bool isCh = trend == 1
float lvl = lastSL
int startX = math.max(lastSLidx, bar_index - 400)
slBroken := true
trend := -1
if isCh
bearCHoCH := true
if showStructure and ((isCh and showCHoCH) or (not isCh and showBOS))
line.new(startX, lvl, bar_index, lvl, color=bearStructCol, style=isCh ? line.style_solid : line.style_dotted, width=1)
label.new(math.round(math.avg(startX, bar_index)), lvl, isCh ? "CHoCH" : "BOS", color=color.new(color.white, 100), textcolor=bearStructCol, style=label.style_label_up, size=size.small)
// bearish ob = candle with highest high in the leg since last swing high
if showOB and (isCh or not chochOnly)
int refIdx = math.max(math.max(nz(lastSHidx, bar_index - 20), bar_index - 100), 0)
int off = bar_index - refIdx
float maxH = high
int maxI = 0
for i = 1 to off
if high[i] > maxH
maxH := high[i]
maxI := i
float oTop = high[maxI]
float oBot = low[maxI]
box bx = box.new(bar_index - maxI, oTop, bar_index, oBot, bgcolor=bearOBCol, border_color=bearOBBorder)
array.push(bearOBs, OB.new(bx, oTop, oBot, false, bar_index, false))
if array.size(bearOBs) > maxOB
OB oldOB = array.shift(bearOBs)
box.delete(oldOB.b)
// ---- process bullish OBs: mitigation + retest entry ----
if array.size(bullOBs) > 0
for i = array.size(bullOBs) - 1 to 0
OB o = array.get(bullOBs, i)
if close < o.bottom
if removeMit
box.delete(o.b)
else
box.set_right(o.b, bar_index)
box.set_bgcolor(o.b, mitCol)
box.set_border_color(o.b, mitCol)
array.remove(bullOBs, i)
else
box.set_right(o.b, bar_index)
if not o.used and not longSig and bar_index > o.created and low <= o.top and close > open and (not trendFilter or trend == 1)
o.used := true
longSig := true
if showSLTP and not na(atrV)
drawTrade(true, close, math.min(o.bottom, low) - atrV * slBuffer)
// ---- process bearish OBs: mitigation + retest entry ----
if array.size(bearOBs) > 0
for i = array.size(bearOBs) - 1 to 0
OB o = array.get(bearOBs, i)
if close > o.top
if removeMit
box.delete(o.b)
else
box.set_right(o.b, bar_index)
box.set_bgcolor(o.b, mitCol)
box.set_border_color(o.b, mitCol)
array.remove(bearOBs, i)
else
box.set_right(o.b, bar_index)
if not o.used and not shortSig and bar_index > o.created and high >= o.bottom and close < open and (not trendFilter or trend == -1)
o.used := true
shortSig := true
if showSLTP and not na(atrV)
drawTrade(false, close, math.max(o.top, high) + atrV * slBuffer)
// ==========================================
// 5. ARROWS
// ==========================================
plotshape(showArrows and longSig, title="buy arrow", style=shape.arrowup, location=location.belowbar, color=bullArrowCol, size=size.small)
plotshape(showArrows and shortSig, title="sell arrow", style=shape.arrowdown, location=location.abovebar, color=bearArrowCol, size=size.small)
// ==========================================
// 6. ALERTS
// ==========================================
alertcondition(longSig, "buy: bullish ob retest", "BUY (bullish OB retest) on {{ticker}} {{interval}} @ {{close}}")
alertcondition(shortSig, "sell: bearish ob retest", "SELL (bearish OB retest) on {{ticker}} {{interval}} @ {{close}}")
alertcondition(bullCHoCH, "bullish choch", "Bullish CHoCH on {{ticker}} {{interval}} @ {{close}}")
alertcondition(bearCHoCH, "bearish choch", "Bearish CHoCH on {{ticker}} {{interval}} @ {{close}}")