APEX SMC Confluence Premium & Discount [Thomas-Sterling]

Thomas-Sterling · study · 300 行 · 点赞 766 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Thomas-Sterling
 
 
 //@version=6
indicator("APEX SMC Confluence Premium & Discount [Thomas-Sterling]",overlay=true, max_boxes_count=100, max_lines_count=100, max_labels_count=200)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SETTINGS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
g1 = "Market Structure"
swingLen = input.int(5, "Swing Length", minval=2, group=g1)
showStructure = input.bool(true, "Show BOS / CHoCH", group=g1)
showSwingX = input.bool(true, "Show Swing X Markers", group=g1)
showTrendlines = input.bool(true, "Show Structure Trendlines", group=g1)
 
g2 = "SMC Zones"
showPD = input.bool(true, "Show Premium / Discount", group=g2)
showSupplyDemand = input.bool(true, "Show Supply / Demand Zones", group=g2)
showFVG = input.bool(true, "Show Fair Value Gaps", group=g2)
zoneBars = input.int(25, "Zone Extension", minval=5, maxval=200, group=g2)
maxFVG = input.int(6, "Maximum FVG Zones", minval=1, maxval=25, group=g2)
 
g3 = "Signals & Dashboard"
showSignals = input.bool(true, "Show BUY / SELL", group=g3)
useEMA = input.bool(true, "EMA Confluence Filter", group=g3)
emaLen = input.int(50, "EMA Length", minval=1, group=g3)
showDashboard = input.bool(true, "Show SMC Dashboard", group=g3)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// PREMIUM VISUAL THEME
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
purple = color.rgb(150, 0, 255)
magenta = color.rgb(205, 0, 255)
cyan = color.rgb(0, 220, 220)
teal = color.rgb(0, 190, 165)
green = color.rgb(100, 220, 140)
red = color.rgb(255, 65, 100)
blue = color.rgb(50, 140, 255)
dark = color.rgb(15, 17, 28)
panel = color.rgb(27, 25, 65)
white = color.white
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SWING STRUCTURE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ph = ta.pivothigh(high, swingLen, swingLen)
pl = ta.pivotlow(low, swingLen, swingLen)
 
var float lastHigh = na
var float prevHigh = na
var int lastHighBar = na
var int prevHighBar = na
 
var float lastLow = na
var float prevLow = na
var int lastLowBar = na
var int prevLowBar = na
 
var bool highBroken = false
var bool lowBroken = false
var int trend = 0
 
if not na(ph)
    prevHigh := lastHigh
    prevHighBar := lastHighBar
    lastHigh := ph
    lastHighBar := bar_index - swingLen
    highBroken := false
 
if not na(pl)
    prevLow := lastLow
    prevLowBar := lastLowBar
    lastLow := pl
    lastLowBar := bar_index - swingLen
    lowBroken := false
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SWING MARKERS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if showSwingX and not na(ph)
    label.new(bar_index - swingLen, ph, "X", style=label.style_label_down, color=magenta, textcolor=white, size=size.tiny)
 
if showSwingX and not na(pl)
    label.new(bar_index - swingLen, pl, "X", style=label.style_label_up, color=cyan, textcolor=white, size=size.tiny)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// BOS / CHoCH
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
breakUp = barstate.isconfirmed and not na(lastHigh) and not highBroken and close > lastHigh and close[1] <= lastHigh
breakDn = barstate.isconfirmed and not na(lastLow) and not lowBroken and close < lastLow and close[1] >= lastLow
 
bullCHoCH = breakUp and trend == -1
bearCHoCH = breakDn and trend == 1
bullBOS = breakUp and trend != -1
bearBOS = breakDn and trend != 1
 
if breakUp
    highBroken := true
    trend := 1
 
if breakDn
    lowBroken := true
    trend := -1
 
if showStructure and bullBOS
    label.new(bar_index, lastHigh, "BOS", style=label.style_label_down, color=color.new(purple, 10), textcolor=white, size=size.tiny)
 
if showStructure and bearBOS
    label.new(bar_index, lastLow, "BOS", style=label.style_label_up, color=color.new(purple, 10), textcolor=white, size=size.tiny)
 
if showStructure and bullCHoCH
    label.new(bar_index, lastHigh, "CHoCH", style=label.style_label_down, color=cyan, textcolor=dark, size=size.tiny)
 
if showStructure and bearCHoCH
    label.new(bar_index, lastLow, "CHoCH", style=label.style_label_up, color=cyan, textcolor=dark, size=size.tiny)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// STRUCTURE TRENDLINES
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var line highLine = na
var line lowLine = na
 
if showTrendlines and not na(ph) and not na(prevHighBar)
    if not na(highLine)
        line.delete(highLine)
    highLine := line.new(prevHighBar, prevHigh, lastHighBar, lastHigh, extend=extend.right, color=purple, width=1)
 
if showTrendlines and not na(pl) and not na(prevLowBar)
    if not na(lowLine)
        line.delete(lowLine)
    lowLine := line.new(prevLowBar, prevLow, lastLowBar, lastLow, extend=extend.right, color=teal, width=1)
 
if not showTrendlines
    if not na(highLine)
        line.delete(highLine)
        highLine := na
    if not na(lowLine)
        line.delete(lowLine)
        lowLine := na
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// PREMIUM / DISCOUNT RANGE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
rangeReady = not na(lastHigh) and not na(lastLow) and lastHigh > lastLow
eq = rangeReady ? (lastHigh + lastLow) / 2 : na
rangeSize = rangeReady ? lastHigh - lastLow : na
 
premBottom = rangeReady ? eq + rangeSize * 0.15 : na
discTop = rangeReady ? eq - rangeSize * 0.15 : na
 
var box premiumBox = na
var box discountBox = na
var line eqLine = na
var label premiumLabel = na
var label discountLabel = na
 
if barstate.islast and showPD and rangeReady
    if na(premiumBox)
        premiumBox := box.new(bar_index - 20, lastHigh, bar_index + zoneBars, premBottom, bgcolor=color.new(purple, 87), border_color=color.new(purple, 55), text="PREMIUM", text_color=purple, text_size=size.tiny)
    else
        box.set_left(premiumBox, bar_index - 20)
        box.set_right(premiumBox, bar_index + zoneBars)
        box.set_top(premiumBox, lastHigh)
        box.set_bottom(premiumBox, premBottom)
 
    if na(discountBox)
        discountBox := box.new(bar_index - 20, discTop, bar_index + zoneBars, lastLow, bgcolor=color.new(teal, 85), border_color=color.new(teal, 50), text="DISCOUNT", text_color=cyan, text_size=size.tiny)
    else
        box.set_left(discountBox, bar_index - 20)
        box.set_right(discountBox, bar_index + zoneBars)
        box.set_top(discountBox, discTop)
        box.set_bottom(discountBox, lastLow)
 
    if na(eqLine)
        eqLine := line.new(bar_index - 20, eq, bar_index + zoneBars, eq, color=color.new(white, 65), style=line.style_dotted)
    else
        line.set_xy1(eqLine, bar_index - 20, eq)
        line.set_xy2(eqLine, bar_index + zoneBars, eq)
 
if not showPD
    if not na(premiumBox)
        box.delete(premiumBox)
        premiumBox := na
    if not na(discountBox)
        box.delete(discountBox)
        discountBox := na
    if not na(eqLine)
        line.delete(eqLine)
        eqLine := na
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SUPPLY / DEMAND ZONES
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
atr = ta.atr(14)
var box supplyBox = na
var box demandBox = na
 
if showSupplyDemand and not na(ph)
    if not na(supplyBox)
        box.delete(supplyBox)
    supplyBox := box.new(bar_index - swingLen, ph, bar_index + zoneBars, ph - atr[swingLen] * 0.6, bgcolor=color.new(purple, 83), border_color=color.new(purple, 50))
 
if showSupplyDemand and not na(pl)
    if not na(demandBox)
        box.delete(demandBox)
    demandBox := box.new(bar_index - swingLen, pl + atr[swingLen] * 0.6, bar_index + zoneBars, pl, bgcolor=color.new(teal, 83), border_color=color.new(teal, 50))
 
if not showSupplyDemand
    if not na(supplyBox)
        box.delete(supplyBox)
        supplyBox := na
    if not na(demandBox)
        box.delete(demandBox)
        demandBox := na
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// FAIR VALUE GAPS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
bullFVG = bar_index >= 2 and low > high[2]
bearFVG = bar_index >= 2 and high < low[2]
 
var box[] fvgBoxes = array.new_box()
var int[] fvgDirs = array.new_int()
 
if showFVG and bullFVG
    b = box.new(bar_index - 2, low, bar_index + zoneBars, high[2], bgcolor=color.new(teal, 86), border_color=color.new(teal, 65))
    array.push(fvgBoxes, b)
    array.push(fvgDirs, 1)
 
if showFVG and bearFVG
    b = box.new(bar_index - 2, low[2], bar_index + zoneBars, high, bgcolor=color.new(purple, 86), border_color=color.new(purple, 65))
    array.push(fvgBoxes, b)
    array.push(fvgDirs, -1)
 
if array.size(fvgBoxes) > 0
    for i = array.size(fvgBoxes) - 1 to 0
        b = array.get(fvgBoxes, i)
        dir = array.get(fvgDirs, i)
        filled = dir == 1 ? low <= box.get_bottom(b) : high >= box.get_top(b)
        expired = bar_index > box.get_right(b)
        if filled or expired or not showFVG
            box.delete(b)
            array.remove(fvgBoxes, i)
            array.remove(fvgDirs, i)
 
while array.size(fvgBoxes) > maxFVG
    old = array.shift(fvgBoxes)
    box.delete(old)
    array.shift(fvgDirs)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// CONFLUENCE SIGNALS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ema = ta.ema(close, emaLen)
bullFilter = not useEMA or close > ema
bearFilter = not useEMA or close < ema
 
buySignal = barstate.isconfirmed and (bullBOS or bullCHoCH) and bullFilter
sellSignal = barstate.isconfirmed and (bearBOS or bearCHoCH) and bearFilter
 
plotshape(showSignals and buySignal, title="BUY", text="BUY", style=shape.labelup, location=location.belowbar, color=cyan, textcolor=dark, size=size.small)
plotshape(showSignals and sellSignal, title="SELL", text="SELL", style=shape.labeldown, location=location.abovebar, color=purple, textcolor=white, size=size.small)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SMC CONFLUENCE DASHBOARD
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var table dash = table.new(position.top_right, 2, 7, border_width=1, frame_color=color.new(blue, 65))
 
bias = trend == 1 ? "Bullish" : trend == -1 ? "Bearish" : "Neutral"
 
 
zoneState = not rangeReady ? "Waiting" : close > eq ? "Premium" : "Discount"
nearOB = not na(lastHigh) and close <= lastHigh and close >= lastHigh - atr * 0.6 or not na(lastLow) and close >= lastLow and close <= lastLow + atr * 0.6
fvgState = array.size(fvgBoxes) > 0 ? "Active" : "None"
signalState = buySignal ? "BUY" : sellSignal ? "SELL" : "WAIT"
confluence = (trend != 0 ? 35 : 0) + (close > ema ? 20 : 0) + (array.size(fvgBoxes) > 0 ? 25 : 0) + ((bullBOS or bearBOS or bullCHoCH or bearCHoCH) ? 20 : 0)
 
if barstate.islast and showDashboard
    table.cell(dash, 0, 0, "SMC CONFLUENCE", bgcolor=panel, text_color=white, text_size=size.tiny)
    table.cell(dash, 1, 0, "STATE", bgcolor=panel, text_color=white, text_size=size.tiny)
    table.cell(dash, 0, 1, "Major Bias", bgcolor=dark, text_color=white, text_size=size.tiny)
    table.cell(dash, 0, 2, "Minor Bias", bgcolor=dark, text_color=white, text_size=size.tiny)
    table.cell(dash, 1, 2, close > ema ? "Bullish" : "Bearish", bgcolor=color.new(close > ema ? teal : purple, 70), text_color=white, text_size=size.tiny)
    table.cell(dash, 0, 3, "Price Zone", bgcolor=dark, text_color=white, text_size=size.tiny)
    table.cell(dash, 1, 3, zoneState, bgcolor=color.new(zoneState == "Premium" ? purple : teal, 70), text_color=white, text_size=size.tiny)
    table.cell(dash, 0, 4, "Near OB", bgcolor=dark, text_color=white, text_size=size.tiny)
    table.cell(dash, 1, 4, nearOB ? "Yes" : "No", bgcolor=color.new(blue, 75), text_color=white, text_size=size.tiny)
    table.cell(dash, 0, 5, "In FVG", bgcolor=dark, text_color=white, text_size=size.tiny)
    table.cell(dash, 1, 5, fvgState, bgcolor=color.new(fvgState == "Active" ? teal : color.gray, 75), text_color=white, text_size=size.tiny)
    table.cell(dash, 0, 6, "Confluence", bgcolor=panel, text_color=white, text_size=size.tiny)
    table.cell(dash, 1, 6, str.tostring(confluence) + " / 100", bgcolor=color.new(blue, 65), text_color=white, text_size=size.tiny)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ALERTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
alertcondition(buySignal, title="SMC BUY", message="SMC BUY | {{ticker}} | {{close}}")
alertcondition(sellSignal, title="SMC SELL", message="SMC SELL | {{ticker}} | {{close}}")
alertcondition(bullCHoCH or bearCHoCH, title="SMC CHoCH", message="SMC CHoCH | {{ticker}} | {{close}}")
alertcondition(bullBOS or bearBOS, title="SMC BOS", message="SMC BOS | {{ticker}} | {{close}}")

← 返回列表