Smart Money Concepts [Xalgo]

SahilTradeSchool · study · 258 行 · 点赞 173 · TradingView 原页

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

Pine Script

//@version=6
indicator("Smart Money Concepts ", shorttitle="Smart Money Concepts", overlay=true, max_lines_count=500, max_labels_count=500, max_boxes_count=500, max_bars_back=5000)
 
// ═════════════ INPUTS ═════════════
// Market structure
showStruct = input.bool(true, "Show BOS / CHoCH", group="Market Structure")
swingLen   = input.int(10, "Swing Length", 2, group="Market Structure", tooltip="Bars on each side used to confirm a swing high/low. Higher = bigger structure.")
colBull    = input.color(#089981, "Bullish Color", group="Market Structure")
colBear    = input.color(#f23645, "Bearish Color", group="Market Structure")
 
// Order blocks
showOB = input.bool(true, "Show Order Blocks", group="Order Blocks")
obMax  = input.int(5, "Max Order Blocks per side shown (total kept)", 1, 30, group="Order Blocks")
 
// Fair value gaps
showFVG = input.bool(true, "Show Fair Value Gaps", group="Fair Value Gaps")
fvgMin  = input.float(0.1, "Min gap size (x ATR)", 0.0, 3.0, step=0.05, group="Fair Value Gaps")
fvgMax  = input.int(10, "Max FVGs kept", 1, 50, group="Fair Value Gaps")
 
// Liquidity
showEQ = input.bool(true, "Show Equal Highs / Lows", group="Liquidity")
eqLen  = input.int(3, "EQH/EQL Pivot Length", 1, 10, group="Liquidity")
eqTol  = input.float(0.1, "EQH/EQL Tolerance (x ATR)", 0.01, 1.0, step=0.01, group="Liquidity")
 
// Auto support & resistance
showSR      = input.bool(true, "Show Auto Support & Resistance", group="Support & Resistance")
colorSup    = input.color(#09ff00, "Support Color", group="Support & Resistance")
colorRes    = input.color(#fd0202, "Resistance Color", group="Support & Resistance")
strengthSR  = input.int(2, "S/R Strength (min pivots touching level)", 1, group="Support & Resistance")
clusterPerc = input.float(10, "Cluster Width % of 300-bar range", 0.5, 30, step=0.5, group="Support & Resistance")
maxLevels   = input.int(10, "Max S/R levels shown", 1, 20, group="Support & Resistance")
lineStyle   = input.string("Dotted", "Line Style", ["Solid", "Dotted", "Dashed"], group="Support & Resistance")
lineWidth   = input.int(2, "S/R Line Width", 1, group="Support & Resistance")
expandSR    = input.bool(true, "Extend lines both sides", group="Support & Resistance")
 
// ═════════════ HELPERS ═════════════
atrV = ta.atr(50)
 
// Offset (bars back from current bar) of the order block candle:
// lowest low for a bullish OB, highest high for a bearish OB
f_ob(int startBar, bool bull) =>
    int off = 0
    float best = bull ? 1e10 : -1e10
    int n = math.max(math.min(bar_index - startBar, 400), 0)
    for i = 0 to n
        if bull
            if low[i] < best
                best := low[i]
                off := i
        else
            if high[i] > best
                best := high[i]
                off := i
    off
 
// ═════════════ MARKET STRUCTURE (BOS / CHoCH) ═════════════
var float swHigh    = na
var int   swHighBar = na
var float swLow     = na
var int   swLowBar  = na
var bool  highCrossed = false
var bool  lowCrossed  = false
var int   trend = 0
 
float sph = ta.pivothigh(high, swingLen, swingLen)
float spl = ta.pivotlow(low, swingLen, swingLen)
 
if not na(sph)
    swHigh      := sph
    swHighBar   := bar_index - swingLen
    highCrossed := false
if not na(spl)
    swLow      := spl
    swLowBar   := bar_index - swingLen
    lowCrossed := false
 
bullBreak = not na(swHigh) and not highCrossed and close > swHigh
bearBreak = not na(swLow) and not lowCrossed and close < swLow
bullChoch = bullBreak and trend == -1
bullBos   = bullBreak and trend != -1
bearChoch = bearBreak and trend == 1
bearBos   = bearBreak and trend != 1
 
var obBoxes = array.new<box>()
var obBull  = array.new<bool>()
 
if bullBreak
    if showStruct
        line.new(swHighBar, swHigh, bar_index, swHigh, color=colBull, style=line.style_dashed, width=1)
        label.new(int(math.avg(swHighBar, bar_index)), swHigh, bullChoch ? "CHoCH" : "BOS", color=color.new(colBull, 100), style=label.style_label_down, textcolor=colBull, size=size.small)
    if showOB
        bo = f_ob(swHighBar, true)
        array.push(obBoxes, box.new(bar_index - bo, high[bo], bar_index, low[bo], border_color=colBull, bgcolor=color.new(colBull, 85), extend=extend.right))
        array.push(obBull, true)
    highCrossed := true
    trend := 1
 
if bearBreak
    if showStruct
        line.new(swLowBar, swLow, bar_index, swLow, color=colBear, style=line.style_dashed, width=1)
        label.new(int(math.avg(swLowBar, bar_index)), swLow, bearChoch ? "CHoCH" : "BOS", color=color.new(colBear, 100), style=label.style_label_up, textcolor=colBear, size=size.small)
    if showOB
        so = f_ob(swLowBar, false)
        array.push(obBoxes, box.new(bar_index - so, high[so], bar_index, low[so], border_color=colBear, bgcolor=color.new(colBear, 85), extend=extend.right))
        array.push(obBull, false)
    lowCrossed := true
    trend := -1
 
// Keep the number of order blocks under the limit
while array.size(obBoxes) > obMax * 2
    box.delete(array.shift(obBoxes))
    array.shift(obBull)
 
// Remove mitigated order blocks (price closed through them)
if array.size(obBoxes) > 0
    for i = array.size(obBoxes) - 1 to 0
        b = array.get(obBoxes, i)
        isBull = array.get(obBull, i)
        if (isBull and close < box.get_bottom(b)) or (not isBull and close > box.get_top(b))
            box.delete(b)
            array.remove(obBoxes, i)
            array.remove(obBull, i)
 
// ═════════════ FAIR VALUE GAPS ═════════════
var fvgBoxes = array.new<box>()
var fvgBull  = array.new<bool>()
 
if showFVG and bar_index > 2
    bullFVG = low > high[2] and close[1] > high[2] and (low - high[2]) > atrV * fvgMin
    bearFVG = high < low[2] and close[1] < low[2] and (low[2] - high) > atrV * fvgMin
    if bullFVG
        array.push(fvgBoxes, box.new(bar_index - 2, low, bar_index, high[2], border_color=na, bgcolor=color.new(colBull, 80), extend=extend.right))
        array.push(fvgBull, true)
    if bearFVG
        array.push(fvgBoxes, box.new(bar_index - 2, low[2], bar_index, high, border_color=na, bgcolor=color.new(colBear, 80), extend=extend.right))
        array.push(fvgBull, false)
 
while array.size(fvgBoxes) > fvgMax
    box.delete(array.shift(fvgBoxes))
    array.shift(fvgBull)
 
// Remove filled FVGs
if array.size(fvgBoxes) > 0
    for i = array.size(fvgBoxes) - 1 to 0
        fb = array.get(fvgBoxes, i)
        fBull = array.get(fvgBull, i)
        if (fBull and close < box.get_bottom(fb)) or (not fBull and close > box.get_top(fb))
            box.delete(fb)
            array.remove(fvgBoxes, i)
            array.remove(fvgBull, i)
 
// ═════════════ LIQUIDITY: EQUAL HIGHS / LOWS ═════════════
float eph = ta.pivothigh(high, eqLen, eqLen)
float epl = ta.pivotlow(low, eqLen, eqLen)
var float lastEph    = na
var int   lastEphBar = na
var float lastEpl    = na
var int   lastEplBar = na
 
if showEQ
    if not na(eph)
        if not na(lastEph) and math.abs(eph - lastEph) < atrV * eqTol
            line.new(lastEphBar, lastEph, bar_index - eqLen, eph, color=colBear, style=line.style_dotted, width=1)
            label.new(int(math.avg(lastEphBar, bar_index - eqLen)), math.max(eph, lastEph), "EQH", color=color.new(colBear, 100), style=label.style_label_down, textcolor=colBear, size=size.tiny)
        lastEph    := eph
        lastEphBar := bar_index - eqLen
    if not na(epl)
        if not na(lastEpl) and math.abs(epl - lastEpl) < atrV * eqTol
            line.new(lastEplBar, lastEpl, bar_index - eqLen, epl, color=colBull, style=line.style_dotted, width=1)
            label.new(int(math.avg(lastEplBar, bar_index - eqLen)), math.min(epl, lastEpl), "EQL", color=color.new(colBull, 100), style=label.style_label_up, textcolor=colBull, size=size.tiny)
        lastEpl    := epl
        lastEplBar := bar_index - eqLen
 
// ═════════════ AUTO SUPPORT & RESISTANCE ═════════════
rb  = 10   // pivot left/right bars
prd = 284  // lookback for scanning pivots
 
lnStyle = lineStyle == "Solid" ? line.style_solid : lineStyle == "Dotted" ? line.style_dotted : line.style_dashed
extd    = expandSR ? extend.both : extend.right
 
float srPh = ta.pivothigh(high, rb, rb)
float srPl = ta.pivotlow(low, rb, rb)
 
prdhighest = ta.highest(300)
prdlowest  = ta.lowest(300)
cwidth     = (prdhighest - prdlowest) * clusterPerc / 100
 
var sr_levs = array.new_float(21, na)
 
// Recalculate levels only when a new pivot is confirmed
if showSR and (not na(srPh) or not na(srPl))
    sr_levels = array.new_float(21, na)
    aas       = array.new_bool(41, true)
    countpp   = 0
    for x = 0 to prd
        if na(close[x])
            break
        if not na(srPh[x]) or not na(srPl[x])
            countpp += 1
            if countpp > 40
                break
            if array.get(aas, countpp)
                upl = (not na(srPh[x]) ? high[x + rb] : low[x + rb]) + cwidth
                dnl = (not na(srPh[x]) ? high[x + rb] : low[x + rb]) - cwidth
                tmp    = array.new_bool(41, true)
                cnt    = 0
                tpoint = 0
                for xx = 0 to prd
                    if na(close[xx])
                        break
                    if not na(srPh[xx]) or not na(srPl[xx])
                        chg = false
                        cnt += 1
                        if cnt > 40
                            break
                        if array.get(aas, cnt)
                            if not na(srPh[xx])
                                if high[xx + rb] <= upl and high[xx + rb] >= dnl
                                    tpoint += 1
                                    chg := true
                            if not na(srPl[xx])
                                if low[xx + rb] <= upl and low[xx + rb] >= dnl
                                    tpoint += 1
                                    chg := true
                        if chg and cnt < 41
                            array.set(tmp, cnt, false)
                if tpoint >= strengthSR
                    for g = 0 to 40
                        if not array.get(tmp, g)
                            array.set(aas, g, false)
                    if not na(srPh[x]) and countpp < 21
                        array.set(sr_levels, countpp, high[x + rb])
                    if not na(srPl[x]) and countpp < 21
                        array.set(sr_levels, countpp, low[x + rb])
    for x = 0 to 20
        array.set(sr_levs, x, array.get(sr_levels, x))
 
// Draw S/R lines on the last bar only
var sr_lines = array.new<line>(21, na)
 
if barstate.islast
    for x = 0 to 20
        line.delete(array.get(sr_lines, x))
    if showSR
        x1    = math.max(bar_index - 355, 0)
        drawn = 0
        for x = 0 to 20
            lvl = array.get(sr_levs, x)
            if not na(lvl) and drawn < maxLevels
                drawn += 1
                line_col = close >= lvl ? colorSup : colorRes
                array.set(sr_lines, x, line.new(x1, lvl, bar_index, lvl, xloc.bar_index, extd, line_col, lnStyle, lineWidth))
 
// ═════════════ ALERTS ═════════════
alertcondition(bullBos,   "Bullish BOS",   "Bullish Break of Structure")
alertcondition(bearBos,   "Bearish BOS",   "Bearish Break of Structure")
alertcondition(bullChoch, "Bullish CHoCH", "Bullish Change of Character")
alertcondition(bearChoch, "Bearish CHoCH", "Bearish Change of Character")

← 返回列表