Wake Up Master

j_sachin1990joshi · study · 534 行 · 点赞 193 · TradingView 原页

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

Pine Script

//@version=6
// Wake Up Master - the board that scripts\wake_up_master.bat draws, as one real-time indicator.
// Same rules as auto\master_board.py, auto\hand_mark.py and auto\spx_leg.py:
//   - liquidity rays (RED = highs, GREEN = lows): previous week / day / 4 hr / 1 hr high + low,
//     unswept daily (2 per side) + weekly (1 per side) highs/lows, Asian session (00:00-07:00 UTC) high/low,
//     current session high/low; levels closer than the tolerance merge into one ray with a combined label
//   - nearest valid 15 minute and 5 minute FVG / Gap (3rd candle closes inside the 2nd; gone once closed through)
//   - manipulation legs (swing of 3 candles each side sweeps the previous swing, then a close beyond the
//     nearest opposite swing before the sweep), legs of up to 4 candles, drawn once price taps one of their SPX zones
//     (low + 1.9 x range above or high - 1.9 x range below) before the next leg forms
// Weekends: with the market closed, the last completed day / week counts as the previous one.
indicator("Wake Up Master", overlay = true, max_lines_count = 500, max_labels_count = 500, max_boxes_count = 500)
// an invisible plot of the price ties the indicator to the candles' price scale; without any plot TradingView gives
// a drawings-only indicator its own scale, and the drawings stay put when the chart is moved or zoomed
// display.pane (an invisible line in the chart area) rather than display.none: a fully hidden plot can be ignored, and
// the phone app then puts the indicator on its own scale, so the lines stay put while the candles move (5 Nov 2026)
plot(close, "Price", color = color.new(color.white, 100), linewidth = 1, display = display.pane, editable = false)
 
// ---------------------------------------------------------------- inputs
grpL = "Liquidity levels"
showPW   = input.bool(true, "Previous week high/low", group = grpL)
showPD   = input.bool(true, "Previous day high/low", group = grpL)
show4H   = input.bool(true, "Previous 4 hr high/low", group = grpL)
show1H   = input.bool(true, "Previous 1 hr high/low", group = grpL)
showUD   = input.bool(true, "Unswept daily highs/lows (2 per side)", group = grpL)
showUW   = input.bool(true, "Unswept weekly highs/lows (1 per side)", group = grpL)
showAsia = input.bool(true, "Asian session high/low (00:00-07:00 UTC)", group = grpL)
showSess = input.bool(true, "Current session high/low", group = grpL)
colHigh  = input.color(color.rgb(242, 54, 69), "Highs", group = grpL)
colLow   = input.color(color.rgb(76, 175, 80), "Lows", group = grpL)
txtSize  = input.int(12, "Label font size", minval = 6, maxval = 40, group = grpL)
// Pine can't read the chart's time-zone setting, so sweep times use this one (user, 5 Nov 2026: their chart shows UTC-6)
sweepTZ  = input.string("UTC-6", "Time zone for sweep times", group = grpL,
                        tooltip = "Match the time zone at the bottom right of your chart, e.g. UTC-6, UTC, UTC+5:30, America/New_York, Asia/Kolkata")
 
grpF = "Fair value gaps"
showF15 = input.bool(true, "Nearest 15 minute FVG / Gap (3 minute on a 1m / 3m chart)", group = grpF)
showF5  = input.bool(true, "Nearest 5 minute FVG / Gap (1 minute on a 1m / 3m chart)", group = grpF)
// on a 1m or 3m chart the two FVGs are the 3 minute and 1 minute ones instead of 15 and 5 minute (user, 26 Oct 2026)
// above 15m the FVG and the order blocks are the chart's own timeframe (one FVG) (user, 26 Oct 2026)
lowTF   = timeframe.in_seconds() <= 180
highTF  = timeframe.in_seconds() > 900
fvgTfA  = highTF ? timeframe.period : lowTF ? "3" : "15"
fvgTfB  = lowTF ? "1" : "5"
obTf    = highTF ? timeframe.period : "5"
showIF  = input.bool(true, "Nearest inverse FVG (IFVG) on the same timeframes", group = grpF)
colIBull = input.color(color.rgb(42, 93, 176), "Bullish IFVG (support)", group = grpF)
colIBear = input.color(color.rgb(217, 138, 0), "Bearish IFVG (resistance)", group = grpF)
tfName(string tf) =>
    sec = timeframe.in_seconds(tf)
    sec < 3600 ? str.tostring(sec / 60) + " minute" : sec < 86400 ? (sec == 3600 ? "1 hour" : str.tostring(sec / 3600) + " hr") : sec == 86400 ? "Daily" : sec == 604800 ? "Weekly" : tf
 
grpO = "Order blocks"
showOB  = input.bool(true, "Order blocks (5 minute; the chart timeframe above 15m)", group = grpO)
obSide  = input.int(3, "Nearest fresh order blocks per side", minval = 1, maxval = 5, group = grpO)
 
grpG = "Manipulation legs"
showLegs   = input.bool(true, "Manipulation legs", group = grpG)
maxCandles = input.int(4, "Max candles per leg", minval = 1, group = grpG)
tappedOnly = input.bool(false, "Only legs whose SPX zones price tapped before the next leg", group = grpG)
pivN       = input.int(2, "Swing strength (candles each side)", minval = 1, maxval = 10, group = grpG)
legNearest = input.bool(true, "Leg starts at the nearest swing before the sweep", group = grpG, tooltip = "Off: the highest swing high (bullish) / lowest swing low (bearish) between the two swept swings")
colBear    = input.color(color.rgb(242, 54, 69), "Bearish leg", group = grpG)
colBull    = input.color(color.rgb(76, 175, 80), "Bullish leg", group = grpG)
 
// ---------------------------------------------------------------- higher-timeframe candles
[wH1, wL1, wH0, wL0] = request.security(syminfo.tickerid, "W", [high[1], low[1], high, low], lookahead = barmerge.lookahead_on)
[dH1, dL1, dH0, dL0] = request.security(syminfo.tickerid, "D", [high[1], low[1], high, low], lookahead = barmerge.lookahead_on)
[fH1, fL1, fH0, fL0] = request.security(syminfo.tickerid, "240", [high[1], low[1], high, low], lookahead = barmerge.lookahead_on)
[hH1, hL1, hH0, hL0] = request.security(syminfo.tickerid, "60", [high[1], low[1], high, low], lookahead = barmerge.lookahead_on)
atr1h = request.security(syminfo.tickerid, "60", ta.sma(ta.tr(true), 14))
 
// market closed (weekend / daily break): the newest candle closed more than 30 min ago
closed = timenow - time_close > 30 * 60 * 1000
 
// ---------------------------------------------------------------- unswept daily / weekly highs and lows (built from the chart)
type Pool
    float price
    int   t
 
var array<Pool> udH = array.new<Pool>()
var array<Pool> udL = array.new<Pool>()
var array<Pool> uwH = array.new<Pool>()
var array<Pool> uwL = array.new<Pool>()
var float cdH = na
var float cdL = na
var int   cdT = na
var float cwH = na
var float cwL = na
var int   cwT = na
var int   lastDayT = na
var int   lastWeekT = na
 
sweep(array<Pool> a, bool isHigh) =>
    if a.size() > 0
        for i = a.size() - 1 to 0
            p = a.get(i)
            if isHigh ? high > p.price : low < p.price
                a.remove(i)
 
if timeframe.change("D") and not na(cdH)
    udH.push(Pool.new(cdH, cdT)), udL.push(Pool.new(cdL, cdT)), lastDayT := cdT
    if udH.size() > 30
        udH.shift()
    if udL.size() > 30
        udL.shift()
    cdH := na, cdL := na
if timeframe.change("W") and not na(cwH)
    uwH.push(Pool.new(cwH, cwT)), uwL.push(Pool.new(cwL, cwT)), lastWeekT := cwT
    if uwH.size() > 20
        uwH.shift()
    if uwL.size() > 20
        uwL.shift()
    cwH := na, cwL := na
sweep(udH, true), sweep(udL, false), sweep(uwH, true), sweep(uwL, false)
cdT := na(cdH) ? time_tradingday : cdT          // trading date (gold's day opens the evening before)
cwT := na(cwH) ? time_tradingday : cwT
cdH := na(cdH) ? high : math.max(cdH, high)
cdL := na(cdL) ? low : math.min(cdL, low)
cwH := na(cwH) ? high : math.max(cwH, high)
cwL := na(cwL) ? low : math.min(cwL, low)
 
// ---------------------------------------------------------------- Asian session + current session (UTC)
hUTC = hour(time, "UTC") + minute(time, "UTC") / 60.0
sessName = hUTC < 7 ? "Asia" : hUTC < 12 ? "London" : hUTC >= 12.5 and hUTC < 17 ? "NY" : "Off-hrs"
var float aH = na
var float aL = na
var bool  inA = false
if sessName == "Asia"
    if not inA
        aH := high, aL := low
    else
        aH := math.max(aH, high), aL := math.min(aL, low)
    inA := true
else
    inA := false
var string sNow = ""
var float  sH = na
var float  sL = na
if sessName != "Off-hrs"                       // in the Off-hrs gap the last real session stays on the chart
    if sessName != sNow or timeframe.change("D")
        sNow := sessName, sH := high, sL := low
    else
        sH := math.max(sH, high), sL := math.min(sL, low)
 
// ---------------------------------------------------------------- FVG / Gap + inverse FVG (evaluated on its own timeframe)
// FVG: 3rd candle closes inside the 2nd; it lives until a candle closes back through it.
// IFVG (the old board engine's rule): the FIRST candle to tap a gap decides - if it closes through the far edge the gap
// flips into an inverse FVG of the opposite direction; a tap that rejects means it can never invert. An IFVG is gone once
// a candle closes back through it.
fvgNearest() =>
    // closed candles only (26 Oct 2026, user): everything below sees the last CLOSED candle as "now", so nothing is
    // marked from a candle that is still open
    cS = close[1]
    oS = open[1]
    hS = high[1]
    lS = low[1]
    tS = time[1]
    var array<float> fLo = array.new<float>()
    var array<float> fHi = array.new<float>()
    var array<int>   fT  = array.new<int>()
    var array<int>   fD  = array.new<int>()
    var array<bool>  fTap = array.new<bool>()     // already tapped once (and rejected)
    var array<float> iLo = array.new<float>()
    var array<float> iHi = array.new<float>()
    var array<int>   iT  = array.new<int>()
    var array<int>   iD  = array.new<int>()      // IFVG direction after the flip: 1 support, -1 resistance
    if iLo.size() > 0                          // an IFVG dies on a close back through it
        for i = iLo.size() - 1 to 0
            d = iD.get(i)
            if (d == -1 and cS > iHi.get(i)) or (d == 1 and cS < iLo.get(i))
                iLo.remove(i), iHi.remove(i), iT.remove(i), iD.remove(i)
    if fLo.size() > 0
        for i = fLo.size() - 1 to 0
            d = fD.get(i)
            lo = fLo.get(i)
            hi = fHi.get(i)
            through = (d == 1 and cS < lo) or (d == -1 and cS > hi)
            taps = d == 1 ? lS < hi : hS > lo
            if taps and not fTap.get(i) and through                 // first tap closes through -> inverse FVG
                iLo.push(lo), iHi.push(hi), iT.push(fT.get(i)), iD.push(-d)
            if through                                               // a gap dies once a candle closes back through it
                fLo.remove(i), fHi.remove(i), fT.remove(i), fD.remove(i), fTap.remove(i)
            else if taps
                fTap.set(i, true)
    if bar_index >= 2 and cS >= lS[1] and cS <= hS[1]   // valid: 3rd candle closes inside the 2nd
        if hS[2] < lS
            fLo.push(hS[2]), fHi.push(lS), fT.push(tS[2]), fD.push(1), fTap.push(false)
        else if lS[2] > hS
            fLo.push(hS), fHi.push(lS[2]), fT.push(tS[2]), fD.push(-1), fTap.push(false)
    if fLo.size() > 100
        fLo.shift(), fHi.shift(), fT.shift(), fD.shift(), fTap.shift()
    if iLo.size() > 100
        iLo.shift(), iHi.shift(), iT.shift(), iD.shift()
    int best = -1
    float bd = na
    for i = 0 to fLo.size() - 1
        if fLo.size() > 0
            dist = math.abs((fLo.get(i) + fHi.get(i)) / 2 - cS)
            if na(bd) or dist < bd
                bd := dist, best := i
    int ib = -1
    float ibd = na
    for i = 0 to iLo.size() - 1
        if iLo.size() > 0
            dist = math.abs((iLo.get(i) + iHi.get(i)) / 2 - cS)
            if na(ibd) or dist < ibd
                ibd := dist, ib := i
    int   rd = 0
    float rlo = na
    float rhi = na
    int   rt = na
    if best >= 0
        rd := fD.get(best), rlo := fLo.get(best), rhi := fHi.get(best), rt := fT.get(best)
    int   xd = 0
    float xlo = na
    float xhi = na
    int   xt = na
    if ib >= 0
        xd := iD.get(ib), xlo := iLo.get(ib), xhi := iHi.get(ib), xt := iT.get(ib)
    [rd, rlo, rhi, rt, xd, xlo, xhi, xt]
 
[g15d, g15lo, g15hi, g15t, i15d, i15lo, i15hi, i15t] = request.security(syminfo.tickerid, fvgTfA, fvgNearest())
[g5d, g5lo, g5hi, g5t, i5d, i5lo, i5hi, i5t]         = request.security(syminfo.tickerid, fvgTfB, fvgNearest())
 
// ---------------------------------------------------------------- order blocks (rules.json order_block_rules, = auto/ob_5m.py)
// OB = the last opposite candle before a 1-3 candle displacement leg (one body >= 1.5 x ATR(14) or >= 0.40) that closes
// beyond the last confirmed swing (BOS) or whose start swept the opposite swing; "OB+FVG" when the leg leaves a gap.
// Fresh until the first touch; gone on a body close beyond the far edge, on the first touch, or after 120 candles.
// Returns the nearest fresh ones, packed 5 numbers each: kind (1 bull / -1 bear), low, high, OB candle time, FVG flag.
obScan(int nSide) =>
    // closed candles only (26 Oct 2026, user): everything below sees the last CLOSED candle as "now", so nothing is
    // marked from a candle that is still open
    cS = close[1]
    oS = open[1]
    hS = high[1]
    lS = low[1]
    tS = time[1]
    var array<float> oK = array.new<float>()
    var array<float> oLo = array.new<float>()
    var array<float> oHi = array.new<float>()
    var array<float> oT = array.new<float>()
    var array<float> oF = array.new<float>()
    var array<int>   oB = array.new<int>()        // bar the OB became active (for the 120-candle expiry)
    atrS = ta.sma(ta.tr(true), 14)[1]
    var float lastSH = na
    var float lastSL = na
    if bar_index >= 4 and hS[2] > hS[1] and hS[2] > hS[0] and hS[2] > hS[3] and hS[2] > hS[4]
        lastSH := hS[2]
    if bar_index >= 4 and lS[2] < lS[1] and lS[2] < lS[0] and lS[2] < lS[3] and lS[2] < lS[4]
        lastSL := lS[2]
    shS = lastSH
    slS = lastSL
    // a leg of L candles (bars 1..L back) is complete on this bar when this candle breaks the colour run (or L = 3)
    if bar_index >= 20
        for L = 1 to 3
            for kind = -1 to 1 by 2
                upLeg = kind == 1
                bool run = true
                float maxBody = 0.0
                bool gap = false
                for k = 1 to L
                    same = upLeg ? cS[k] > oS[k] : cS[k] < oS[k]
                    run := run and same
                    maxBody := math.max(maxBody, math.abs(cS[k] - oS[k]))
                    gap := gap or (upLeg ? lS[k - 1] > hS[k + 1] : hS[k - 1] < lS[k + 1])
                breaks = L == 3 or not (upLeg ? cS > oS : cS < oS)
                opp = upLeg ? cS[L + 1] < oS[L + 1] : cS[L + 1] > oS[L + 1]
                a = atrS[L + 1]
                sh = shS[L + 1]
                sl = slS[L + 1]
                if run and breaks and opp and not na(a) and not na(sh) and not na(sl) and maxBody >= math.max(1.5 * a, 0.40)
                    bos = upLeg ? (cS[1] > sh or math.min(lS[L + 1], lS[L]) < sl) : (cS[1] < sl or math.max(hS[L + 1], hS[L]) > sh)
                    if bos
                        oK.push(kind), oLo.push(lS[L + 1]), oHi.push(hS[L + 1]), oT.push(tS[L + 1]), oF.push(gap ? 1 : 0), oB.push(bar_index)
    // fate on every candle from the one after the leg: body beyond the far edge = invalid, touch = mitigated, 120 = expired
    if oK.size() > 0
        for q = oK.size() - 1 to 0
            k = oK.get(q)
            dead = bar_index - oB.get(q) >= 120
            if k == 1
                dead := dead or math.min(oS, cS) < oLo.get(q) or lS <= oHi.get(q)
            else
                dead := dead or math.max(oS, cS) > oHi.get(q) or hS >= oLo.get(q)
            if dead
                oK.remove(q), oLo.remove(q), oHi.remove(q), oT.remove(q), oF.remove(q), oB.remove(q)
    while oK.size() > 100
        oK.shift(), oLo.shift(), oHi.shift(), oT.shift(), oF.shift(), oB.shift()
    // nearest nSide per side to the current price
    array<float> out = array.new<float>()
    for kind = -1 to 1 by 2
        array<float> dist = array.new<float>()
        array<int>   idx = array.new<int>()
        for q = 0 to oK.size() - 1
            if oK.size() > 0 and oK.get(q) == kind
                dist.push(math.abs((oLo.get(q) + oHi.get(q)) / 2 - cS)), idx.push(q)
        ord = dist.sort_indices(order.ascending)
        for m = 0 to math.min(nSide, ord.size()) - 1
            if ord.size() > 0
                q = idx.get(ord.get(m))
                out.push(oK.get(q)), out.push(oLo.get(q)), out.push(oHi.get(q)), out.push(oT.get(q)), out.push(oF.get(q))
    out
 
obPack = request.security(syminfo.tickerid, obTf, obScan(obSide))
 
// ---------------------------------------------------------------- manipulation legs
var array<int>   shI = array.new<int>()
var array<float> shP = array.new<float>()
var array<int>   slI = array.new<int>()
var array<float> slP = array.new<float>()
// pending legs: swept, waiting for the structure-shift close
var array<int>   pK = array.new<int>()      // 1 bullish, -1 bearish
var array<float> pLo = array.new<float>()
var array<float> pHi = array.new<float>()
var array<int>   pS = array.new<int>()
var array<int>   pE = array.new<int>()
// the current (latest) short leg
var int   cK = 0
var float cLo = na
var float cHi = na
var int   cS = na
var int   cE = na
var int   cKnown = na
var bool  cDrawn = true
 
drawLeg(int k, float lo, float hi, int s, int e) =>
    col = k == -1 ? colBear : colBull
    box.new(math.min(s, e), hi, math.max(s, e) + 1, lo, border_color = col, bgcolor = color.new(col, 65))
 
// swings are confirmed pivN candles later; equal highs / lows count, like the Python swings
hiWin = ta.highest(high, 2 * pivN + 1)
loWin = ta.lowest(low, 2 * pivN + 1)
isSH = bar_index >= 2 * pivN and high[pivN] == hiWin
isSL = bar_index >= 2 * pivN and low[pivN] == loWin
 
if showLegs and barstate.isconfirmed                  // closed candles only
    // 1) the current leg is drawn once price taps one of its zones (from the candle after it was confirmed)
    if not cDrawn and bar_index > cKnown
        R = cHi - cLo
        if high >= cLo + 1.9 * R or low <= cHi - 1.9 * R
            drawLeg(cK, cLo, cHi, cS, cE), cDrawn := true
    // 2) new swings: a swing that sweeps the previous one arms a leg (or confirms it if the shift already closed)
    i = bar_index - pivN
    int   bK = 0
    float bLo = na
    float bHi = na
    int   bS = na
    int   bE = -1
    if isSH
        if shI.size() > 0 and high[pivN] > shP.last()
            p = shI.last()
            int j = -1
            float jp = na
            for q = 0 to slI.size() - 1
                if slI.size() > 0 and slI.get(q) > p and slI.get(q) < i and (na(jp) or (legNearest ? slI.get(q) > j : slP.get(q) < jp))
                    j := slI.get(q), jp := slP.get(q)
            if j >= 0
                bool mss = false
                for k = 0 to pivN - 1
                    mss := mss or close[k] < jp
                if mss
                    if math.abs(i - j) + 1 <= maxCandles and i > bE
                        bK := -1, bLo := jp, bHi := high[pivN], bS := j, bE := i
                else
                    pK.push(-1), pLo.push(jp), pHi.push(high[pivN]), pS.push(j), pE.push(i)
        shI.push(i), shP.push(high[pivN])
    if isSL
        if slI.size() > 0 and low[pivN] < slP.last()
            p = slI.last()
            int j = -1
            float jp = na
            for q = 0 to shI.size() - 1
                if shI.size() > 0 and shI.get(q) > p and shI.get(q) < i and (na(jp) or (legNearest ? shI.get(q) > j : shP.get(q) > jp))
                    j := shI.get(q), jp := shP.get(q)
            if j >= 0
                bool mss = false
                for k = 0 to pivN - 1
                    mss := mss or close[k] > jp
                if mss
                    if math.abs(i - j) + 1 <= maxCandles and i > bE
                        bK := 1, bLo := low[pivN], bHi := jp, bS := j, bE := i
                else
                    pK.push(1), pLo.push(low[pivN]), pHi.push(jp), pS.push(j), pE.push(i)
        slI.push(i), slP.push(low[pivN])
    while shI.size() > 300
        shI.shift(), shP.shift()
    while slI.size() > 300
        slI.shift(), slP.shift()
    // 3) pending legs whose structure shift closes on this candle
    if pK.size() > 0
        for q = pK.size() - 1 to 0
            k = pK.get(q)
            if (k == -1 and close < pLo.get(q)) or (k == 1 and close > pHi.get(q))
                if math.abs(pE.get(q) - pS.get(q)) + 1 <= maxCandles and pE.get(q) > bE
                    bK := k, bLo := pLo.get(q), bHi := pHi.get(q), bS := pS.get(q), bE := pE.get(q)
                pK.remove(q), pLo.remove(q), pHi.remove(q), pS.remove(q), pE.remove(q)
    while pK.size() > 100
        pK.shift(), pLo.shift(), pHi.shift(), pS.shift(), pE.shift()
    // 4) a new short leg replaces the current one (an untapped one is never drawn)
    if bK != 0
        cK := bK, cLo := bLo, cHi := bHi, cS := bS, cE := bE, cKnown := bar_index, cDrawn := false
        if not tappedOnly
            drawLeg(cK, cLo, cHi, cS, cE), cDrawn := true
 
// ---------------------------------------------------------------- live sweeps (rules.json swept_liquidity)
// A level counts as swept the moment a WICK trades through it - no waiting for the 1 hr / 4 hr / day / week candle to
// close. It then turns dotted, is tagged "SWEPT hh:mm" (in sweepTZ) and is removed 60 candles later. Each call keeps its
// own state; it resets when the level itself changes (a new higher-timeframe candle, a new Asian session).
sweptAt(float lvl, bool isHigh) =>
    var int   t = na
    var float ref = na
    if na(ref) or lvl != ref                   // (na != x is never true in Pine, so test na first)
        t := na, ref := lvl
    if na(t) and not na(lvl) and (isHigh ? high > lvl : low < lvl)
        t := time
    t
swWH = sweptAt(wH1, true)
swWL = sweptAt(wL1, false)
swDH = sweptAt(dH1, true)
swDL = sweptAt(dL1, false)
sw4H = sweptAt(fH1, true)
sw4L = sweptAt(fL1, false)
sw1H = sweptAt(hH1, true)
sw1L = sweptAt(hL1, false)
swAH = sweptAt(aH, true)                       // the running Asian high can't be swept until the session is over
swAL = sweptAt(aL, false)
 
// ---------------------------------------------------------------- draw the rays + FVG boxes on the newest candle
var array<line>  rays = array.new<line>()
var array<label> tags = array.new<label>()
var array<box>   gaps = array.new<box>()
var array<box>   obBoxes = array.new<box>()
 
addLvl(array<float> P, array<bool> H, array<string> T, float p, bool isHigh, string txt, int swept = na) =>
    keep = na(swept) or time - swept <= 60 * timeframe.in_seconds() * 1000
    if not na(p) and keep
        P.push(p), H.push(isHigh), T.push(na(swept) ? txt : txt + " SWEPT " + str.format_time(swept, "HH:mm", sweepTZ))
 
nearest(array<Pool> a, bool above, int skipT, int n, array<float> P, array<bool> H, array<string> T, string name) =>
    array<float> pr = array.new<float>()
    array<int>   tt = array.new<int>()
    for x in a
        if x.t != skipT and (above ? x.price > close : x.price < close)
            pr.push(x.price), tt.push(x.t)
    idx = pr.sort_indices(above ? order.ascending : order.descending)
    for k = 0 to math.min(n, idx.size()) - 1
        if idx.size() > 0
            q = idx.get(k)
            addLvl(P, H, T, pr.get(q), above, name + (above ? " high " : " low ") + str.format_time(tt.get(q), "MM-dd", "UTC"))
 
drawCluster(array<float> P, array<string> T, array<int> ord, bool isHigh, float tol, bool swept = false) =>
    float ref = na
    float best = na
    string txt = ""
    for k = 0 to ord.size()
        q = k < ord.size() ? ord.get(k) : -1
        if q >= 0 and not na(ref) and math.abs(P.get(q) - ref) <= tol
            best := isHigh ? math.max(best, P.get(q)) : math.min(best, P.get(q)), txt += " / " + T.get(q)
        else
            if not na(ref)
                col = isHigh ? colHigh : colLow
                rays.push(line.new(bar_index - 1, best, bar_index, best, extend = extend.both, color = col, width = 1, style = swept ? line.style_dotted : line.style_solid))
                tags.push(label.new(bar_index + 2, best, txt, style = label.style_label_lower_left, color = color.new(color.black, 100), textcolor = col, size = txtSize))
            if q >= 0
                ref := P.get(q), best := P.get(q), txt := T.get(q)
 
if barstate.islast
    for x in rays
        x.delete()
    for x in tags
        x.delete()
    for x in gaps
        x.delete()
    for x in obBoxes
        x.delete()
    rays.clear(), tags.clear(), gaps.clear(), obBoxes.clear()
    array<float>  P = array.new<float>()
    array<bool>   H = array.new<bool>()
    array<string> T = array.new<string>()
    if showPW
        addLvl(P, H, T, closed ? wH0 : wH1, true, "Prev week high", closed ? na : swWH), addLvl(P, H, T, closed ? wL0 : wL1, false, "Prev week low", closed ? na : swWL)
    if showPD
        addLvl(P, H, T, closed ? dH0 : dH1, true, "Prev days high", closed ? na : swDH), addLvl(P, H, T, closed ? dL0 : dL1, false, "Prev days low", closed ? na : swDL)
    if show4H
        addLvl(P, H, T, closed ? fH0 : fH1, true, "4 hr high", closed ? na : sw4H), addLvl(P, H, T, closed ? fL0 : fL1, false, "4 hr low", closed ? na : sw4L)
    if show1H
        addLvl(P, H, T, closed ? hH0 : hH1, true, "1 hr high", closed ? na : sw1H), addLvl(P, H, T, closed ? hL0 : hL1, false, "1 hr low", closed ? na : sw1L)
    // unswept pools skip the candle already drawn as the previous day / week (none to skip when closed)
    if showUD
        nearest(udH, true, closed ? -1 : lastDayT, 2, P, H, T, "Daily"), nearest(udL, false, closed ? -1 : lastDayT, 2, P, H, T, "Daily")
    if showUW
        nearest(uwH, true, closed ? -1 : lastWeekT, 1, P, H, T, "Weekly"), nearest(uwL, false, closed ? -1 : lastWeekT, 1, P, H, T, "Weekly")
    if showAsia
        addLvl(P, H, T, aH, true, "Asia high", swAH), addLvl(P, H, T, aL, false, "Asia low", swAL)
    if showSess and sNow != ""
        addLvl(P, H, T, sH, true, sNow + " high"), addLvl(P, H, T, sL, false, sNow + " low")
    tol = math.max(nz(atr1h) * 0.10, close * 0.0004)
    for sw = 0 to 1
        array<float> hp = array.new<float>()
        array<float> lp = array.new<float>()
        array<string> ht = array.new<string>()
        array<string> lt = array.new<string>()
        for k = 0 to P.size() - 1
            if P.size() > 0 and (str.contains(T.get(k), " SWEPT ") == (sw == 1))
                if H.get(k)
                    hp.push(P.get(k)), ht.push(T.get(k))
                else
                    lp.push(P.get(k)), lt.push(T.get(k))
        drawCluster(hp, ht, hp.sort_indices(order.ascending), true, tol, sw == 1)
        drawCluster(lp, lt, lp.sort_indices(order.ascending), false, tol, sw == 1)
    if showF15 and g15d != 0
        c = g15d == 1 ? colLow : colHigh
        gaps.push(box.new(g15t, g15hi, time, g15lo, xloc = xloc.bar_time, extend = extend.right, border_color = c, bgcolor = color.new(c, 80),
                  text = (g15d == 1 ? "FVG " : "Gap ") + tfName(fvgTfA), text_color = c, text_size = txtSize, text_halign = text.align_left))
    if showF5 and not highTF and g5d != 0
        c = g5d == 1 ? colLow : colHigh
        gaps.push(box.new(g5t, g5hi, time, g5lo, xloc = xloc.bar_time, extend = extend.right, border_color = c, bgcolor = color.new(c, 80),
                  text = (g5d == 1 ? "FVG " : "Gap ") + tfName(fvgTfB), text_color = c, text_size = txtSize, text_halign = text.align_left))
    if showIF and showF15 and i15d != 0
        c = i15d == 1 ? colIBull : colIBear
        gaps.push(box.new(i15t, i15hi, time, i15lo, xloc = xloc.bar_time, extend = extend.right, border_color = c, bgcolor = color.new(c, 80),
                  text = "IFVG " + tfName(fvgTfA), text_color = c, text_size = txtSize, text_halign = text.align_left))
    if showIF and showF5 and not highTF and i5d != 0
        c = i5d == 1 ? colIBull : colIBear
        gaps.push(box.new(i5t, i5hi, time, i5lo, xloc = xloc.bar_time, extend = extend.right, border_color = c, bgcolor = color.new(c, 80),
                  text = "IFVG " + tfName(fvgTfB), text_color = c, text_size = txtSize, text_halign = text.align_left))
    if showOB and not na(obPack)
        for n = 0 to obPack.size() / 5 - 1
            if obPack.size() >= 5
                k = obPack.get(n * 5)
                c = k == 1 ? colLow : colHigh
                obBoxes.push(box.new(int(obPack.get(n * 5 + 3)), obPack.get(n * 5 + 2), time, obPack.get(n * 5 + 1), xloc = xloc.bar_time, extend = extend.right,
                          border_color = c, border_style = line.style_dashed, bgcolor = color.new(c, 86),
                          text = (obPack.get(n * 5 + 4) == 1 ? "OB+FVG " : "OB ") + tfName(obTf), text_color = c, text_size = txtSize, text_halign = text.align_left))

← 返回列表