Smart Money [OrbitOne]

orbitonetv · study · 378 行 · 点赞 51 · 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/
// © orbitonetv
// v1.1
//@version=6
indicator("Smart Money [OrbitOne]", "Smart Money [OrbitOne]", overlay = true, max_lines_count = 500, max_labels_count = 500, max_boxes_count = 500, max_bars_back = 1000)
 
// ════════ STRUCTURE ════════
gS        = "Market structure"
confirm   = input.bool(true, "Confirm on bar close", group = gS, tooltip = "Structure breaks, order blocks and gaps are only drawn on closed bars, so nothing disappears afterwards.")
showInt   = input.bool(true, "Show internal structure", group = gS)
intLen    = input.int(5, "Internal pivot length", minval = 2, maxval = 20, group = gS)
showSwg   = input.bool(true, "Show swing structure", group = gS)
swgLen    = input.int(50, "Swing pivot length", minval = 10, maxval = 200, group = gS)
intSzI    = input.string("Tiny", "Internal label size", options = ["Tiny", "Small", "Normal"], group = gS)
swgSzI    = input.string("Small", "Swing label size", options = ["Tiny", "Small", "Normal"], group = gS)
maxStruct = input.int(80, "Max structure drawings kept", minval = 10, maxval = 200, group = gS, tooltip = "Older structure lines and labels are removed to keep the chart clean.")
colorBars = input.bool(false, "Color candles by internal trend", group = gS)
 
// ════════ ORDER BLOCKS ════════
gO        = "Order blocks"
showIntOB = input.bool(true, "Show internal order blocks", group = gO)
intOBn    = input.int(5, "Internal order blocks kept", minval = 1, maxval = 20, group = gO)
showSwgOB = input.bool(false, "Show swing order blocks", group = gO)
swgOBn    = input.int(5, "Swing order blocks kept", minval = 1, maxval = 20, group = gO)
mitSrc    = input.string("Close", "Mitigation price", options = ["Close", "High/Low"], group = gO, tooltip = "An order block is removed when price closes (or wicks) through it.")
obBull    = input.color(#3179F5, "Bullish order block", group = gO)
obBear    = input.color(#F7525F, "Bearish order block", group = gO)
 
// ════════ FAIR VALUE GAPS ════════
gF       = "Fair value gaps"
showFVG  = input.bool(false, "Show fair value gaps", group = gF)
fvgAuto  = input.bool(true, "Auto size threshold", group = gF, tooltip = "Ignores tiny gaps: a gap must be larger than twice the average bar body in percent.")
fvgMax   = input.int(10, "Gaps kept", minval = 1, maxval = 50, group = gF)
fvgExt   = input.int(10, "Extend gaps (bars)", minval = 1, maxval = 100, group = gF)
fvgBull  = input.color(color.new(#00C853, 75), "Bullish gap", group = gF)
fvgBear  = input.color(color.new(#FF1744, 75), "Bearish gap", group = gF)
 
// ════════ LIQUIDITY ════════
gL        = "Liquidity"
showEQ    = input.bool(true, "Show equal highs / lows", group = gL)
eqLen     = input.int(3, "Equal highs / lows pivot length", minval = 1, maxval = 10, group = gL)
eqThr     = input.float(0.1, "Equal threshold (ATR)", minval = 0.01, maxval = 1, step = 0.01, group = gL)
showSweep = input.bool(false, "Mark liquidity sweeps", group = gL, tooltip = "A wick takes out an unbroken pivot high/low but the bar closes back inside: a possible stop hunt.")
sweepSrc  = input.string("Internal", "Sweep pivots", options = ["Internal", "Swing"], group = gL)
 
// ════════ ZONES & LEVELS ════════
gZ      = "Zones & levels"
showHL  = input.bool(true, "Show strong / weak high & low", group = gZ)
showPD  = input.bool(false, "Show premium / discount zones", group = gZ)
showPDL = input.bool(false, "Show previous day high / low", group = gZ)
showPWL = input.bool(false, "Show previous week high / low", group = gZ)
 
// ════════ STYLE & DASHBOARD ════════
gG      = "Style"
bullCol = input.color(#089981, "Bullish structure", group = gG)
bearCol = input.color(#F23645, "Bearish structure", group = gG)
showDb  = input.bool(false, "Show dashboard", group = gG)
dbPos   = input.string("Bottom right", "Dashboard position", options = ["Top right", "Bottom right", "Top left", "Bottom left"], group = gG)
 
// ════════ TYPES ════════
type Pivot
    float price   = na
    int   bar     = na
    bool  crossed = true
    bool  swept   = true
 
type Struct
    Pivot hi
    Pivot lo
    int   trend = 0
 
type Zone
    box   bx
    float top
    float btm
    int   dir
    bool  touched = false
 
// ════════ STATE ════════
var Struct sInt = Struct.new(Pivot.new(), Pivot.new(), 0)
var Struct sSwg = Struct.new(Pivot.new(), Pivot.new(), 0)
var array<Zone>  obInt = array.new<Zone>()
var array<Zone>  obSwg = array.new<Zone>()
var array<Zone>  fvgs  = array.new<Zone>()
var array<line>  stL   = array.new<line>()
var array<label> stB   = array.new<label>()
var string lastEv = "—"
 
okBar = not confirm or barstate.isconfirmed
atrF  = ta.atr(200)
 
f_size(string s) =>
    s == "Tiny" ? size.tiny : s == "Small" ? size.small : size.normal
 
f_keep(line ln, label lb) =>
    stL.push(ln)
    stB.push(lb)
    while stL.size() > maxStruct
        line.delete(stL.shift())
    while stB.size() > maxStruct
        label.delete(stB.shift())
 
f_draw(int x1, float y, string txt, color c, bool isSwing, bool up) =>
    ln = line.new(x1, y, bar_index, y, color = c, style = isSwing ? line.style_solid : line.style_dashed, width = isSwing ? 2 : 1)
    lb = label.new(math.round((x1 + bar_index) / 2), y, txt, style = up ? label.style_label_down : label.style_label_up, color = color.new(color.white, 100), textcolor = c, size = f_size(isSwing ? swgSzI : intSzI))
    f_keep(ln, lb)
 
// order block: the last opposite-side candle between the broken pivot and the break
f_ob(int pivBar, int dir, bool isSwing) =>
    if (isSwing and showSwgOB) or (not isSwing and showIntOB)
        int n = math.min(bar_index - pivBar, 300)
        int bi = na
        float best = na
        if n >= 1
            for i = 1 to n
                bool calm = (high[i] - low[i]) < 2 * atrF[i]
                if calm
                    if dir == 1 and (na(best) or low[i] < best)
                        best := low[i]
                        bi := i
                    if dir == -1 and (na(best) or high[i] > best)
                        best := high[i]
                        bi := i
        if not na(bi)
            float top = high[bi]
            float btm = low[bi]
            color c = dir == 1 ? obBull : obBear
            bx = box.new(bar_index - bi, top, bar_index + 5, btm, border_color = color.new(c, 100), bgcolor = color.new(c, isSwing ? 75 : 80))
            arr = isSwing ? obSwg : obInt
            arr.unshift(Zone.new(bx, top, btm, dir, false))
            lim = isSwing ? swgOBn : intOBn
            while arr.size() > lim
                Zone z = arr.pop()
                box.delete(z.bx)
 
// structure break: returns 1 bull BOS, 2 bull CHoCH, -1 bear BOS, -2 bear CHoCH, 0 none
f_break(Struct s, bool isSwing, bool show) =>
    int ev = 0
    Pivot h = s.hi
    Pivot l = s.lo
    if okBar and not na(h.price) and not h.crossed and close > h.price
        ev := s.trend == -1 ? 2 : 1
        h.crossed := true
        s.trend := 1
        if show
            f_draw(h.bar, h.price, ev == 2 ? "CHoCH" : "BOS", bullCol, isSwing, true)
        f_ob(h.bar, 1, isSwing)
    else if okBar and not na(l.price) and not l.crossed and close < l.price
        ev := s.trend == 1 ? -2 : -1
        l.crossed := true
        s.trend := -1
        if show
            f_draw(l.bar, l.price, ev == -2 ? "CHoCH" : "BOS", bearCol, isSwing, false)
        f_ob(l.bar, -1, isSwing)
    ev
 
// keep zones alive until price trades through them; report first touch
f_mitig(array<Zone> arr, bool isGap) =>
    bool tBull = false
    bool tBear = false
    if arr.size() > 0
        for i = arr.size() - 1 to 0
            Zone z = arr.get(i)
            float dnPx = isGap ? low : mitSrc == "Close" ? close : low
            float upPx = isGap ? high : mitSrc == "Close" ? close : high
            bool gone = z.dir == 1 ? dnPx < z.btm : upPx > z.top
            if gone
                box.delete(z.bx)
                arr.remove(i)
            else
                box.set_right(z.bx, bar_index + (isGap ? fvgExt : 5))
                if not z.touched and z.dir == 1 and low <= z.top
                    z.touched := true
                    tBull := true
                if not z.touched and z.dir == -1 and high >= z.btm
                    z.touched := true
                    tBear := true
    [tBull, tBear]
 
// ════════ PIVOTS ════════
phI = ta.pivothigh(high, intLen, intLen)
plI = ta.pivotlow(low, intLen, intLen)
phS = ta.pivothigh(high, swgLen, swgLen)
plS = ta.pivotlow(low, swgLen, swgLen)
 
f_setPivot(Pivot p, float v, int len) =>
    p.price   := v
    p.bar     := bar_index - len
    p.crossed := false
    p.swept   := false
 
if not na(phI)
    f_setPivot(sInt.hi, phI, intLen)
if not na(plI)
    f_setPivot(sInt.lo, plI, intLen)
if not na(phS)
    f_setPivot(sSwg.hi, phS, swgLen)
if not na(plS)
    f_setPivot(sSwg.lo, plS, swgLen)
 
// ════════ ZONE UPKEEP (before new zones are added) ════════
[tbI, tsI] = f_mitig(obInt, false)
[tbS, tsS] = f_mitig(obSwg, false)
[gb, gs]   = f_mitig(fvgs, true)
obTouchBull = tbI or tbS
obTouchBear = tsI or tsS
 
// ════════ STRUCTURE BREAKS ════════
evI = f_break(sInt, false, showInt)
evS = f_break(sSwg, true, showSwg)
 
if evS != 0
    lastEv := "Swing " + (math.abs(evS) == 2 ? "CHoCH" : "BOS") + (evS > 0 ? " ▲" : " ▼")
else if evI != 0
    lastEv := "Internal " + (math.abs(evI) == 2 ? "CHoCH" : "BOS") + (evI > 0 ? " ▲" : " ▼")
 
// ════════ FAIR VALUE GAPS ════════
bodyPct = nz(math.abs(close[1] - open[1]) / open[1] * 100)
thr     = fvgAuto ? ta.cum(bodyPct) / (bar_index + 1) * 2 : 0.0
bullGap = low > high[2] and close[1] > high[2] and (low - high[2]) / high[2] * 100 > thr
bearGap = high < low[2] and close[1] < low[2] and (low[2] - high) / high * 100 > thr
newBullGap = showFVG and okBar and bullGap
newBearGap = showFVG and okBar and bearGap
 
if newBullGap
    fvgs.unshift(Zone.new(box.new(bar_index - 2, low, bar_index + fvgExt, high[2], border_color = color.new(fvgBull, 100), bgcolor = fvgBull, text = ""), low, high[2], 1, false))
if newBearGap
    fvgs.unshift(Zone.new(box.new(bar_index - 2, low[2], bar_index + fvgExt, high, border_color = color.new(fvgBear, 100), bgcolor = fvgBear, text = ""), low[2], high, -1, false))
while fvgs.size() > fvgMax
    Zone z = fvgs.pop()
    box.delete(z.bx)
 
// ════════ EQUAL HIGHS / LOWS ════════
phE = ta.pivothigh(high, eqLen, eqLen)
plE = ta.pivotlow(low, eqLen, eqLen)
var float eqH  = na
var int   eqHb = na
var float eqL  = na
var int   eqLb = na
isEQH = false
isEQL = false
if not na(phE)
    if not na(eqH) and math.abs(phE - eqH) < eqThr * atrF
        isEQH := true
        if showEQ
            f_keep(line.new(eqHb, eqH, bar_index - eqLen, phE, color = bearCol, style = line.style_dotted, width = 1), label.new(math.round((eqHb + bar_index - eqLen) / 2), math.max(eqH, phE), "EQH", style = label.style_label_down, color = color.new(color.white, 100), textcolor = bearCol, size = size.tiny))
    eqH  := phE
    eqHb := bar_index - eqLen
if not na(plE)
    if not na(eqL) and math.abs(plE - eqL) < eqThr * atrF
        isEQL := true
        if showEQ
            f_keep(line.new(eqLb, eqL, bar_index - eqLen, plE, color = bullCol, style = line.style_dotted, width = 1), label.new(math.round((eqLb + bar_index - eqLen) / 2), math.min(eqL, plE), "EQL", style = label.style_label_up, color = color.new(color.white, 100), textcolor = bullCol, size = size.tiny))
    eqL  := plE
    eqLb := bar_index - eqLen
 
// ════════ LIQUIDITY SWEEPS ════════
Struct sw = sweepSrc == "Swing" ? sSwg : sInt
Pivot swH = sw.hi
Pivot swL = sw.lo
sweepHi = false
sweepLo = false
if okBar and not na(swH.price) and not swH.crossed and not swH.swept and high > swH.price and close < swH.price
    swH.swept := true
    sweepHi := true
if okBar and not na(swL.price) and not swL.crossed and not swL.swept and low < swL.price and close > swL.price
    swL.swept := true
    sweepLo := true
plotshape(showSweep and sweepHi, "Sweep of highs", shape.xcross, location.abovebar, bearCol, text = "Sweep", textcolor = bearCol, size = size.tiny, display = display.all - display.status_line)
plotshape(showSweep and sweepLo, "Sweep of lows", shape.xcross, location.belowbar, bullCol, text = "Sweep", textcolor = bullCol, size = size.tiny, display = display.all - display.status_line)
 
// ════════ TRAILING EXTREMES ════════
var float tTop  = na
var int   tTopB = na
var float tBot  = na
var int   tBotB = na
if not na(phS)
    tTop  := phS
    tTopB := bar_index - swgLen
if not na(plS)
    tBot  := plS
    tBotB := bar_index - swgLen
if not na(tTop) and high > tTop
    tTop  := high
    tTopB := bar_index
if not na(tBot) and low < tBot
    tBot  := low
    tBotB := bar_index
 
// ════════ PREVIOUS DAY / WEEK ════════
[pdh, pdl] = request.security(syminfo.tickerid, "D", [high[1], low[1]], lookahead = barmerge.lookahead_on)
[pwh, pwl] = request.security(syminfo.tickerid, "W", [high[1], low[1]], lookahead = barmerge.lookahead_on)
var int dStart = 0
var int wStart = 0
if timeframe.change("D")
    dStart := bar_index
if timeframe.change("W")
    wStart := bar_index
 
// ════════ LAST-BAR DRAWINGS ════════
var array<line>  lastL = array.new<line>()
var array<label> lastB = array.new<label>()
var array<box>   lastX = array.new<box>()
 
f_lvl(int x1, float y, string txt, color c, string st) =>
    lastL.push(line.new(x1, y, bar_index + 20, y, color = c, style = st, width = 1))
    lastB.push(label.new(bar_index + 20, y, txt, style = label.style_label_left, color = color.new(color.white, 100), textcolor = c, size = size.tiny))
 
if barstate.islast
    while lastL.size() > 0
        line.delete(lastL.pop())
    while lastB.size() > 0
        label.delete(lastB.pop())
    while lastX.size() > 0
        box.delete(lastX.pop())
    if showHL and not na(tTop) and not na(tBot)
        f_lvl(tTopB, tTop, sSwg.trend == -1 ? "Strong High" : "Weak High", bearCol, line.style_solid)
        f_lvl(tBotB, tBot, sSwg.trend == 1 ? "Strong Low" : "Weak Low", bullCol, line.style_solid)
    if showPD and not na(tTop) and not na(tBot)
        int x1 = math.min(tTopB, tBotB)
        float rg = tTop - tBot
        lastX.push(box.new(x1, tTop, bar_index + 20, tTop - rg * 0.05, border_color = na, bgcolor = color.new(bearCol, 85), text = "Premium", text_size = size.tiny, text_color = bearCol, text_halign = text.align_right))
        lastX.push(box.new(x1, tBot + rg * 0.525, bar_index + 20, tBot + rg * 0.475, border_color = na, bgcolor = color.new(color.gray, 85), text = "Equilibrium", text_size = size.tiny, text_color = color.gray, text_halign = text.align_right))
        lastX.push(box.new(x1, tBot + rg * 0.05, bar_index + 20, tBot, border_color = na, bgcolor = color.new(bullCol, 85), text = "Discount", text_size = size.tiny, text_color = bullCol, text_halign = text.align_right))
    if showPDL and not na(pdh)
        f_lvl(dStart, pdh, "PDH", color.gray, line.style_dashed)
        f_lvl(dStart, pdl, "PDL", color.gray, line.style_dashed)
    if showPWL and not na(pwh)
        f_lvl(wStart, pwh, "PWH", color.silver, line.style_dotted)
        f_lvl(wStart, pwl, "PWL", color.silver, line.style_dotted)
 
barcolor(colorBars ? (sInt.trend == 1 ? bullCol : sInt.trend == -1 ? bearCol : na) : na)
 
// ════════ DASHBOARD ════════
var table db = table.new(dbPos == "Top right" ? position.top_right : dbPos == "Bottom right" ? position.bottom_right : dbPos == "Top left" ? position.top_left : position.bottom_left, 2, 6, border_width = 1)
 
f_row(int r, string k, string v, color c) =>
    table.cell(db, 0, r, k, bgcolor = color.new(color.gray, 35), text_color = color.white, text_size = size.tiny)
    table.cell(db, 1, r, v, bgcolor = c, text_color = color.white, text_size = size.tiny)
 
if showDb and barstate.isfirst
    table.merge_cells(db, 0, 0, 1, 0)
 
if showDb and barstate.islast
    table.cell(db, 0, 0, "Smart Money", bgcolor = color.new(color.black, 30), text_color = color.white, text_size = size.tiny)
    f_row(1, "Swing trend", sSwg.trend == 1 ? "▲ Bullish" : sSwg.trend == -1 ? "▼ Bearish" : "—", sSwg.trend == 1 ? bullCol : sSwg.trend == -1 ? bearCol : color.gray)
    f_row(2, "Internal trend", sInt.trend == 1 ? "▲ Bullish" : sInt.trend == -1 ? "▼ Bearish" : "—", sInt.trend == 1 ? bullCol : sInt.trend == -1 ? bearCol : color.gray)
    f_row(3, "Last event", lastEv, color.new(color.gray, 20))
    float pos = not na(tTop) and not na(tBot) and tTop > tBot ? (close - tBot) / (tTop - tBot) : na
    string zn = na(pos) ? "—" : pos > 0.525 ? "Premium" : pos < 0.475 ? "Discount" : "Equilibrium"
    f_row(4, "Price zone", zn, zn == "Premium" ? bearCol : zn == "Discount" ? bullCol : color.gray)
    int nb = 0
    int ns = 0
    for z in obInt
        nb += z.dir == 1 ? 1 : 0
        ns += z.dir == -1 ? 1 : 0
    for z in obSwg
        nb += z.dir == 1 ? 1 : 0
        ns += z.dir == -1 ? 1 : 0
    f_row(5, "Active OBs", str.tostring(nb) + " bull / " + str.tostring(ns) + " bear", color.new(color.gray, 20))
 
// ════════ ALERTS ════════
alertcondition(evI == 1, "Internal bullish BOS", "Internal bullish BOS | {{ticker}} | {{interval}} | {{close}}")
alertcondition(evI == -1, "Internal bearish BOS", "Internal bearish BOS | {{ticker}} | {{interval}} | {{close}}")
alertcondition(evI == 2, "Internal bullish CHoCH", "Internal bullish CHoCH | {{ticker}} | {{interval}} | {{close}}")
alertcondition(evI == -2, "Internal bearish CHoCH", "Internal bearish CHoCH | {{ticker}} | {{interval}} | {{close}}")
alertcondition(evS == 1, "Swing bullish BOS", "Swing bullish BOS | {{ticker}} | {{interval}} | {{close}}")
alertcondition(evS == -1, "Swing bearish BOS", "Swing bearish BOS | {{ticker}} | {{interval}} | {{close}}")
alertcondition(evS == 2, "Swing bullish CHoCH", "Swing bullish CHoCH | {{ticker}} | {{interval}} | {{close}}")
alertcondition(evS == -2, "Swing bearish CHoCH", "Swing bearish CHoCH | {{ticker}} | {{interval}} | {{close}}")
alertcondition(obTouchBull, "Price entered bullish order block", "Price entered a bullish order block | {{ticker}} | {{interval}} | {{close}}")
alertcondition(obTouchBear, "Price entered bearish order block", "Price entered a bearish order block | {{ticker}} | {{interval}} | {{close}}")
alertcondition(newBullGap, "Bullish FVG", "Bullish fair value gap | {{ticker}} | {{interval}} | {{close}}")
alertcondition(newBearGap, "Bearish FVG", "Bearish fair value gap | {{ticker}} | {{interval}} | {{close}}")
alertcondition(isEQH, "Equal highs", "Equal highs | {{ticker}} | {{interval}} | {{close}}")
alertcondition(isEQL, "Equal lows", "Equal lows | {{ticker}} | {{interval}} | {{close}}")
alertcondition(sweepHi, "Liquidity sweep of highs", "Liquidity sweep of highs | {{ticker}} | {{interval}} | {{close}}")
alertcondition(sweepLo, "Liquidity sweep of lows", "Liquidity sweep of lows | {{ticker}} | {{interval}} | {{close}}")

← 返回列表