SMC Pro - FVG Order Blocks BOS CHoCH

ravi123dan · study · 266 行 · 点赞 46 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("SMC Pro - FVG Order Blocks BOS CHoCH", overlay=true, max_boxes_count=300, max_lines_count=100, max_labels_count=200)
 
// ───── 1. OPERATING MODE ─────
g1 = "1. Operating Mode"
mode = input.string("Confluence Signals", "Mode", options=["Structure Only", "Confluence Signals", "Aggressive Entries"], group=g1)
closeConfirm = input.bool(true, "Confirm breaks on candle close", group=g1)
showRisk = input.bool(true, "Show entry, stop and targets", group=g1)
rr1 = input.float(1.5, "Target 1 (R)", minval=0.5, step=0.5, group=g1)
rr2 = input.float(2.0, "Target 2 (R)", minval=0.5, step=0.5, group=g1)
rr3 = input.float(3.0, "Target 3 (R)", minval=0.5, step=0.5, group=g1)
 
// ───── 2. STRUCTURE ─────
g2 = "2. Market Structure"
swingLen = input.int(5, "Swing length", minval=2, maxval=30, group=g2)
showBOS = input.bool(true, "Show BOS", group=g2)
showCHoCH = input.bool(true, "Show CHoCH", group=g2)
showSR = input.bool(true, "Show swing support/resistance", group=g2)
 
// ───── 3. FVG ─────
g3 = "3. Fair Value Gaps"
showLocal = input.bool(true, "Show chart timeframe FVG", group=g3)
show15 = input.bool(true, "Show 15-minute FVG", group=g3)
show60 = input.bool(true, "Show 1-hour FVG", group=g3)
show240 = input.bool(true, "Show 4-hour FVG", group=g3)
tf15 = input.timeframe("15", "HTF 1", group=g3)
tf60 = input.timeframe("60", "HTF 2", group=g3)
tf240 = input.timeframe("240", "HTF 3", group=g3)
extendBars = input.int(100, "Zone extension", minval=10, maxval=500, group=g3)
removeTouched = input.bool(false, "Remove FVG when touched", group=g3)
 
// ───── 4. ORDER BLOCKS ─────
g4 = "4. Order Blocks"
showOB = input.bool(true, "Show Order Blocks", group=g4)
obLookback = input.int(12, "Opposing candle search", minval=2, maxval=50, group=g4)
removeInvalidOB = input.bool(true, "Remove invalidated blocks", group=g4)
 
// ───── 5. COLOURS ─────
bullCol = input.color(color.new(color.lime, 84), "Bullish FVG", group="Colours")
bearCol = input.color(color.new(color.red, 84), "Bearish FVG", group="Colours")
col15 = input.color(color.new(color.aqua, 82), "15m FVG", group="Colours")
col60 = input.color(color.new(color.blue, 84), "1h FVG", group="Colours")
col240 = input.color(color.new(color.purple, 84), "4h FVG", group="Colours")
 
// ───── 6. ZONE ARRAYS ─────
var box[] localBoxes = array.new_box()
var int[] localDirs = array.new_int()
var box[] boxes15 = array.new_box()
var int[] dirs15 = array.new_int()
var box[] boxes60 = array.new_box()
var int[] dirs60 = array.new_int()
var box[] boxes240 = array.new_box()
var int[] dirs240 = array.new_int()
var box[] obBoxes = array.new_box()
var int[] obDirs = array.new_int()
 
f_add(box[] bx, int[] dr, int left, float top, float bottom, int dir, color col, string txt) =>
    if not na(top) and not na(bottom) and top > bottom
        b = box.new(left=left, top=top, right=bar_index + extendBars, bottom=bottom, xloc=xloc.bar_index, bgcolor=col, border_color=color.new(col, 15), text=txt, text_color=color.white, text_size=size.tiny)
        array.push(bx, b)
        array.push(dr, dir)
        if array.size(bx) > 35
            old = array.shift(bx)
            array.shift(dr)
            box.delete(old)
 
f_update(box[] bx, int[] dr, bool touchRemove, bool invalidRemove) =>
    if array.size(bx) > 0
        for i = array.size(bx) - 1 to 0
            b = array.get(bx, i)
            d = array.get(dr, i)
            box.set_right(b, bar_index + extendBars)
            top = box.get_top(b)
            bot = box.get_bottom(b)
            touched = high >= bot and low <= top
            invalid = invalidRemove and ((d == 1 and close < bot) or (d == -1 and close > top))
            if (touchRemove and touched) or invalid
                box.delete(b)
                array.remove(bx, i)
                array.remove(dr, i)
 
f_inside(box[] bx, int[] dr, int wanted) =>
    bool result = false
    if array.size(bx) > 0
        for i = 0 to array.size(bx) - 1
            b = array.get(bx, i)
            if array.get(dr, i) == wanted and high >= box.get_bottom(b) and low <= box.get_top(b)
                result := true
    result
 
// ───── 7. MARKET STRUCTURE ─────
pivotH = ta.pivothigh(high, swingLen, swingLen)
pivotL = ta.pivotlow(low, swingLen, swingLen)
 
var float lastH = na
var float lastL = na
var bool brokenH = false
var bool brokenL = false
var int trend = 0
 
if not na(pivotH)
    lastH := pivotH
    brokenH := false
 
if not na(pivotL)
    lastL := pivotL
    brokenL := false
 
upBreak = not na(lastH) and not brokenH and (closeConfirm ? close > lastH : high > lastH)
dnBreak = not na(lastL) and not brokenL and (closeConfirm ? close < lastL : low < lastL)
 
bullCH = upBreak and trend == -1
bearCH = dnBreak and trend == 1
bullBOS = upBreak and trend != -1
bearBOS = dnBreak and trend != 1
 
if upBreak
    brokenH := true
    trend := 1
 
if dnBreak
    brokenL := true
    trend := -1
 
plot(showSR ? lastH : na, "Swing Resistance", color=color.new(color.red, 15), style=plot.style_stepline)
plot(showSR ? lastL : na, "Swing Support", color=color.new(color.lime, 15), style=plot.style_stepline)
 
plotshape(showBOS and bullBOS, title="Bull BOS", style=shape.labelup, location=location.belowbar, color=color.green, text="BOS", textcolor=color.white, size=size.tiny)
plotshape(showBOS and bearBOS, title="Bear BOS", style=shape.labeldown, location=location.abovebar, color=color.red, text="BOS", textcolor=color.white, size=size.tiny)
plotshape(showCHoCH and bullCH, title="Bull CHoCH", style=shape.labelup, location=location.belowbar, color=color.aqua, text="CHoCH", textcolor=color.black, size=size.tiny)
plotshape(showCHoCH and bearCH, title="Bear CHoCH", style=shape.labeldown, location=location.abovebar, color=color.orange, text="CHoCH", textcolor=color.black, size=size.tiny)
 
// ───── 8. LOCAL FVG ─────
bullGap = low > high[2]
bearGap = high < low[2]
 
if showLocal and bullGap
    f_add(localBoxes, localDirs, bar_index - 2, low, high[2], 1, bullCol, "Bull FVG")
 
if showLocal and bearGap
    f_add(localBoxes, localDirs, bar_index - 2, low[2], high, -1, bearCol, "Bear FVG")
 
// ───── 9. NON-LOOKAHEAD HTF FVG DATA ─────
f_htf() =>
    bg = low > high[2]
    sg = high < low[2]
    bt = low
    bb = high[2]
    st = low[2]
    sb = high
    [bg[1], bt[1], bb[1], sg[1], st[1], sb[1], time[1]]
 
[b15, bt15, bb15, s15, st15, sb15, tm15] = request.security(syminfo.tickerid, tf15, f_htf(), lookahead=barmerge.lookahead_on)
[b60, bt60, bb60, s60, st60, sb60, tm60] = request.security(syminfo.tickerid, tf60, f_htf(), lookahead=barmerge.lookahead_on)
[b240, bt240, bb240, s240, st240, sb240, tm240] = request.security(syminfo.tickerid, tf240, f_htf(), lookahead=barmerge.lookahead_on)
 
new15 = ta.change(tm15) != 0
new60 = ta.change(tm60) != 0
new240 = ta.change(tm240) != 0
 
if show15 and new15
    if b15
        f_add(boxes15, dirs15, bar_index, bt15, bb15, 1, col15, "15m Bull FVG")
    if s15
        f_add(boxes15, dirs15, bar_index, st15, sb15, -1, col15, "15m Bear FVG")
 
if show60 and new60
    if b60
        f_add(boxes60, dirs60, bar_index, bt60, bb60, 1, col60, "1h Bull FVG")
    if s60
        f_add(boxes60, dirs60, bar_index, st60, sb60, -1, col60, "1h Bear FVG")
 
if show240 and new240
    if b240
        f_add(boxes240, dirs240, bar_index, bt240, bb240, 1, col240, "4h Bull FVG")
    if s240
        f_add(boxes240, dirs240, bar_index, st240, sb240, -1, col240, "4h Bear FVG")
 
f_update(localBoxes, localDirs, removeTouched, false)
f_update(boxes15, dirs15, removeTouched, false)
f_update(boxes60, dirs60, removeTouched, false)
f_update(boxes240, dirs240, removeTouched, false)
 
// ───── 10. ORDER BLOCKS ─────
if showOB and upBreak
    int bullIdx = na
    for j = 1 to obLookback
        if close[j] < open[j] and na(bullIdx)
            bullIdx := j
    if not na(bullIdx)
        f_add(obBoxes, obDirs, bar_index - bullIdx, high[bullIdx], low[bullIdx], 1, color.new(color.green, 82), "Bull OB")
 
if showOB and dnBreak
    int bearIdx = na
    for j = 1 to obLookback
        if close[j] > open[j] and na(bearIdx)
            bearIdx := j
    if not na(bearIdx)
        f_add(obBoxes, obDirs, bar_index - bearIdx, high[bearIdx], low[bearIdx], -1, color.new(color.maroon, 82), "Bear OB")
 
f_update(obBoxes, obDirs, false, removeInvalidOB)
 
// ───── 11. THREE SIGNAL MODES ─────
bullHTF = (show15 and f_inside(boxes15, dirs15, 1)) or (show60 and f_inside(boxes60, dirs60, 1)) or (show240 and f_inside(boxes240, dirs240, 1))
bearHTF = (show15 and f_inside(boxes15, dirs15, -1)) or (show60 and f_inside(boxes60, dirs60, -1)) or (show240 and f_inside(boxes240, dirs240, -1))
bullOB = showOB and f_inside(obBoxes, obDirs, 1)
bearOB = showOB and f_inside(obBoxes, obDirs, -1)
 
bullContext = bullHTF or bullOB
bearContext = bearHTF or bearOB
 
structureOnly = mode == "Structure Only"
confluence = mode == "Confluence Signals"
aggressive = mode == "Aggressive Entries"
 
buyTrigger = aggressive ? (bullCH or bullBOS) : bullBOS
sellTrigger = aggressive ? (bearCH or bearBOS) : bearBOS
 
buySignal = not structureOnly and (confluence ? buyTrigger and bullContext : buyTrigger)
sellSignal = not structureOnly and (confluence ? sellTrigger and bearContext : sellTrigger)
 
plotshape(buySignal, title="SMC BUY", style=shape.labelup, location=location.belowbar, color=color.lime, text="BUY", textcolor=color.black, size=size.small)
plotshape(sellSignal, title="SMC SELL", style=shape.labeldown, location=location.abovebar, color=color.red, text="SELL", textcolor=color.white, size=size.small)
 
// ───── 12. ENTRY, SL AND TARGETS ─────
var line entryLine = na
var line slLine = na
var line t1Line = na
var line t2Line = na
var line t3Line = na
var label tradeLabel = na
 
if showRisk and (buySignal or sellSignal)
    if not na(entryLine)
        line.delete(entryLine)
        line.delete(slLine)
        line.delete(t1Line)
        line.delete(t2Line)
        line.delete(t3Line)
        label.delete(tradeLabel)
 
    dir = buySignal ? 1 : -1
    entry = close
    candidateSL = dir == 1 ? lastL : lastH
    stop = na(candidateSL) ? (dir == 1 ? low : high) : candidateSL
    risk = math.max(math.abs(entry - stop), syminfo.mintick)
    target1 = entry + dir * risk * rr1
    target2 = entry + dir * risk * rr2
    target3 = entry + dir * risk * rr3
 
    entryLine := line.new(bar_index, entry, bar_index + extendBars, entry, color=color.white)
    slLine := line.new(bar_index, stop, bar_index + extendBars, stop, color=color.red, style=line.style_dashed)
    t1Line := line.new(bar_index, target1, bar_index + extendBars, target1, color=color.lime, style=line.style_dotted)
    t2Line := line.new(bar_index, target2, bar_index + extendBars, target2, color=color.lime, style=line.style_dotted)
    t3Line := line.new(bar_index, target3, bar_index + extendBars, target3, color=color.lime, style=line.style_dotted)
 
    txt = (dir == 1 ? "BUY" : "SELL") + "\nEntry: " + str.tostring(entry, format.mintick) + "\nSL: " + str.tostring(stop, format.mintick) + "\nT1: " + str.tostring(target1, format.mintick) + "\nT2: " + str.tostring(target2, format.mintick) + "\nT3: " + str.tostring(target3, format.mintick)
    tradeLabel := label.new(bar_index, entry, txt, style=dir == 1 ? label.style_label_up : label.style_label_down, color=dir == 1 ? color.green : color.red, textcolor=color.white)
 
// ───── 13. ALERTS ─────
alertcondition(bullBOS, title="Bullish BOS", message="Bullish BOS on {{ticker}} {{interval}}")
alertcondition(bearBOS, title="Bearish BOS", message="Bearish BOS on {{ticker}} {{interval}}")
alertcondition(bullCH, title="Bullish CHoCH", message="Bullish CHoCH on {{ticker}} {{interval}}")
alertcondition(bearCH, title="Bearish CHoCH", message="Bearish CHoCH on {{ticker}} {{interval}}")
alertcondition(buySignal, title="SMC BUY", message="SMC BUY on {{ticker}} {{interval}}")
alertcondition(sellSignal, title="SMC SELL", message="SMC SELL on {{ticker}} {{interval}}")

← 返回列表