Bubble scanner Board

FabioJimenez · study · 624 行 · 点赞 60 · TradingView 原页

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

Pine Script

//@version=6
// Arruda Signal Board
// A table that shows the last A, C, D, R and oscillator-divergence (Ⓓ) signal on up to 6 markets,
// calculated on the chart timeframe. Put it on a 5m chart to watch the 5m signals of every market.
//
// • D, R and Ⓓ use exactly the same maths as Arruda Bubbles (price only).
// • A and C are an APPROXIMATION: the volume bubbles and the delta / CVD need 1-minute volume of
//   each market, which is too heavy to request for 6 markets at once. Here A is scored out of 6
//   (bias, EMA 89, structure, pullback to the BOS level / EMA 89, counter-trend candle, Hull) and C
//   out of 3 (bias, structure, pullback). The EQH / EQL zones are not used and A has no re-entry.
//   An A / C on the board can therefore differ from the one on that market's own chart.
// • The board only refreshes when a candle closes on the chart it sits on.
indicator("Bubble scanner Board", overlay = true, max_bars_back = 5000)
 
// ═════════════════════════════ MARKETS ═════════════════════════════
grpM = "Markets"
s1On = input.bool(true, "", group = grpM, inline = "m1")
s1   = input.symbol("OANDA:XAUUSD", "", group = grpM, inline = "m1")
n1   = input.string("XAUUSD", "Name", group = grpM, inline = "m1")
s2On = input.bool(true, "", group = grpM, inline = "m2")
s2   = input.symbol("OANDA:NAS100USD", "", group = grpM, inline = "m2")
n2   = input.string("NAS100", "Name", group = grpM, inline = "m2")
s3On = input.bool(true, "", group = grpM, inline = "m3")
s3   = input.symbol("OANDA:US30USD", "", group = grpM, inline = "m3")
n3   = input.string("US30", "Name", group = grpM, inline = "m3")
s4On = input.bool(true, "", group = grpM, inline = "m4")
s4   = input.symbol("OANDA:DE30EUR", "", group = grpM, inline = "m4")
n4   = input.string("GER40", "Name", group = grpM, inline = "m4")
s5On = input.bool(true, "", group = grpM, inline = "m5")
s5   = input.symbol("OANDA:EURUSD", "", group = grpM, inline = "m5")
n5   = input.string("EURUSD", "Name", group = grpM, inline = "m5")
s6On = input.bool(true, "", group = grpM, inline = "m6")
s6   = input.symbol("COINBASE:BTCUSD", "", group = grpM, inline = "m6")
n6   = input.string("BTCUSD", "Name", group = grpM, inline = "m6")
 
// ═════════════════════════════ SIGNALS ═════════════════════════════
grpG  = "Signals (same defaults as Arruda Bubbles)"
showA = input.bool(true, "A (approximate)", group = grpG, inline = "sh")
showC = input.bool(true, "C (approximate)", group = grpG, inline = "sh")
showD = input.bool(true, "D", group = grpG, inline = "sh")
showR = input.bool(true, "R", group = grpG, inline = "sh")
showV = input.bool(true, "Ⓓ", group = grpG, inline = "sh")
aMin  = input.int(4, "A: minimum score (of 6)", minval = 1, maxval = 6, group = grpG, tooltip = "Arruda Bubbles scores A out of 8; the bubble and the delta / CVD points are not available here, so the board scores out of 6. 4 of 6 ≈ 5 of 8.")
qBias = input.int(4, "Bias needed (−6 … +6)", minval = 1, maxval = 6, group = grpG)
qLook = input.int(10, "A: look back for the pullback (candles)", minval = 2, maxval = 50, group = grpG)
qTolA = input.float(0.3, "Touch tolerance (ATR)", minval = 0, step = 0.05, group = grpG)
rgOn  = input.bool(true, "A: block in sideways markets (range filter)", group = grpG)
cLook = input.int(6, "C: pullback within (candles)", minval = 1, maxval = 30, group = grpG)
cGap  = input.int(10, "C: minimum candles between C signals", minval = 1, maxval = 100, group = grpG)
cMin  = input.int(0, "C: minimum score (of 3)", minval = 0, maxval = 3, group = grpG)
dLen  = input.int(55, "D: breakout of the last N candles", minval = 5, group = grpG)
dK    = input.float(3.0, "D: stop (× ATR)", minval = 0.5, step = 0.5, group = grpG)
dR    = input.float(3.0, "D: target (R)", minval = 0.5, step = 0.5, group = grpG)
dMax  = input.int(96, "D: close after N candles (0 = off)", minval = 0, group = grpG)
rMin  = input.int(0, "R: minimum score (+N, 0 = all)", minval = 0, maxval = 12, group = grpG)
 
// ═════════════════════════════ TABLE ═════════════════════════════
grpT   = "Table"
tPosI  = input.string("Top Right", "Position", options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left", "Middle Right"], group = grpT)
tSzI   = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal"], group = grpT)
maxAge = input.int(30, "Show a signal for up to (candles)", minval = 1, maxval = 500, group = grpT, tooltip = "Older signals are shown as –.")
freshN = input.int(3, "Highlight signals from the last (candles)", minval = 0, maxval = 50, group = grpT)
alA    = input.bool(true, "Alert on A", group = "Alerts ('Any alert() function call')", inline = "al")
alC    = input.bool(true, "C", group = "Alerts ('Any alert() function call')", inline = "al")
alD    = input.bool(true, "D", group = "Alerts ('Any alert() function call')", inline = "al")
alR    = input.bool(true, "R", group = "Alerts ('Any alert() function call')", inline = "al")
alV    = input.bool(true, "Ⓓ", group = "Alerts ('Any alert() function call')", inline = "al")
 
// ═════════════════════════════ HELPERS ═════════════════════════════
pt(bool b) => b ? 1 : 0
 
// EMA 5 / 13 cloud of the higher timeframe, CLOSED candle only (no repaint)
f_trendC() =>
    r = ta.ema(close, 5) >= ta.ema(close, 13) ? 1 : -1
    r[1]
 
// Kalman (same as the Suite)
f_suKalman(float source, int length) =>
    var float est  = na
    var float errE = na
    var float gn   = na
    if na(errE)
        errE := 1.0
    if na(gn)
        gn := 0.0
    if na(est) and not na(source[1])
        est := source[1]
    float prevE = est
    if not na(prevE)
        gn   := errE / (errE + 0.01 * length)
        est  := prevE + gn * (source - prevE)
        errE := (1.0 - gn) * errE + 0.1 / length
    est
f_suMean(array<float> a) =>
    int n = array.size(a)
    float s = 0.0
    if n > 0
        for i = 0 to n - 1
            s += array.get(a, i)
    n > 0 ? s / n : na
f_suStd(array<float> a, float mean) =>
    int n = array.size(a)
    float ss = 0.0
    if n > 1
        for i = 0 to n - 1
            float dv = array.get(a, i) - mean
            ss += dv * dv
    n > 1 ? math.sqrt(ss / (n - 1)) : na
f_suKdeBuild(array<float> samples) =>
    array<float> cdf = array.new_float()
    int n = array.size(samples)
    float m = na
    float sd = na
    if n > 0
        m := f_suMean(samples)
        sd := f_suStd(samples, m)
        float den = na(sd) or sd == 0 ? 1.0 : sd
        array<float> zs = array.new_float()
        for i = 0 to n - 1
            array.push(zs, (array.get(samples, i) - m) / den)
        array<float> dens = array.new_float(100, 0.0)
        float total = 0.0
        for bi = 0 to 99
            float x = -3.0 + bi * 0.06
            float dsum = 0.0
            for j = 0 to n - 1
                dsum += 1.0 / math.sqrt(2.0 * math.pi) * math.exp(-0.5 * math.pow((x - array.get(zs, j)) / 0.1, 2.0))
            array.set(dens, bi, dsum)
            total += dsum
        if total > 0
            float acc = 0.0
            for bi = 0 to 99
                acc += array.get(dens, bi) / total
                array.push(cdf, acc)
    [cdf, m, sd]
f_suKdeLook(array<float> cdf, float x, float m, float sd) =>
    float r = na
    if array.size(cdf) > 0 and not na(x) and not na(sd) and sd != 0
        int k = int(math.round(((x - m) / sd + 3.0) / 0.06))
        r := array.get(cdf, math.max(0, math.min(99, k)))
    r
f_suRsi(float s, int n) =>
    float ch = s - s[1]
    float up = ch > 0.0 ? ch : 0.0
    float dn = ch < 0.0 ? -ch : 0.0
    float ag = ta.rma(up, n)
    float al = ta.rma(dn, n)
    al == 0.0 ? 100.0 : 100.0 - 100.0 / (1.0 + ag / al)
f_suPctMean(float s, int n) =>
    float mean = ta.sma(s, n)
    float total = 0.0
    if not na(mean) and mean != 0.0
        for i = 0 to n - 1
            total += math.abs((s[i] - mean) / mean * 100.0)
    total / n
f_suEma(float s, int n) =>
    float alpha = 2.0 / (n + 1.0)
    var float e = na
    e := na(e[1]) ? s : alpha * s + (1.0 - alpha) * e[1]
    e
 
// ═════════════════════════════ ALL SIGNALS OF ONE MARKET ═════════════════════════════
// Runs inside request.security, in the context of each market. Returns, for A, C, D, R and Ⓓ:
// direction of the last signal (1 buy, −1 sell), its time and its close.
f_sig() =>
    // --- EMAs, cloud, Hull
    eF  = ta.ema(close, 5)
    eS  = ta.ema(close, 13)
    e89 = ta.ema(close, 89)
    cloudUp = ta.crossover(eF, eS)
    cloudDn = ta.crossunder(eF, eS)
    trendUp = eF >= eS
    hullMA  = ta.wma(2 * ta.wma(close, 7) - ta.wma(close, 14), 4)
    fastUp  = hullMA >= hullMA[1]
    atr     = ta.atr(14)
    qTol    = qTolA * atr
 
    // --- Bias: this timeframe + 15m, 1H, 4H, D, W (closed candles)
    t15 = request.security(syminfo.tickerid, "15",  f_trendC(), lookahead = barmerge.lookahead_on)
    t60 = request.security(syminfo.tickerid, "60",  f_trendC(), lookahead = barmerge.lookahead_on)
    t4h = request.security(syminfo.tickerid, "240", f_trendC(), lookahead = barmerge.lookahead_on)
    tD  = request.security(syminfo.tickerid, "D",   f_trendC(), lookahead = barmerge.lookahead_on)
    tW  = request.security(syminfo.tickerid, "W",   f_trendC(), lookahead = barmerge.lookahead_on)
    bias = (trendUp ? 1 : -1) + nz(t15) + nz(t60) + nz(t4h) + nz(tD) + nz(tW)
 
    // --- Market structure (swing 5, break on close)
    sPH = ta.pivothigh(high, 5, 5)
    sPL = ta.pivotlow(low, 5, 5)
    var float lastSH = na
    var bool  shUsed = true
    var float lastSL = na
    var bool  slUsed = true
    var int   msTrend = 0
    var float brkUp = na
    var float brkDn = na
    if not na(sPH)
        lastSH := sPH
        shUsed := false
    if not na(sPL)
        lastSL := sPL
        slUsed := false
    if not shUsed and not na(lastSH) and close > lastSH
        shUsed := true
        brkUp := lastSH
        msTrend := 1
    if not slUsed and not na(lastSL) and close < lastSL
        slUsed := true
        brkDn := lastSL
        msTrend := -1
 
    // --- A (approximate, score of 6)
    touchB = (not na(brkUp) and low <= brkUp + qTol and high >= brkUp - qTol) or (low <= e89 + qTol and high >= e89 - qTol)
    touchS = (not na(brkDn) and high >= brkDn - qTol and low <= brkDn + qTol) or (high >= e89 - qTol and low <= e89 + qTol)
    a4B = nz(ta.barssince(touchB), 9999) <= qLook
    a4S = nz(ta.barssince(touchS), 9999) <= qLook
    a6B = nz(ta.barssince(not trendUp and close > open), 9999) <= qLook
    a6S = nz(ta.barssince(trendUp and close < open), 9999) <= qLook
    scAB = pt(bias >= qBias) + pt(close > e89) + pt(msTrend == 1) + pt(a4B) + pt(a6B) + pt(fastUp)
    scAS = pt(bias <= -qBias) + pt(close < e89) + pt(msTrend == -1) + pt(a4S) + pt(a6S) + pt(not fastUp)
    rgHi = ta.highest(high, 30)[1]
    rgLo = ta.lowest(low, 30)[1]
    rgFl = math.sum((cloudUp or cloudDn) ? 1 : 0, 30)
    side = (rgHi - rgLo) <= 4.0 * atr or rgFl >= 4
    okB  = not rgOn or not side or nz(ta.barssince(side[1] and close > rgHi), 9999) <= 10
    okS  = not rgOn or not side or nz(ta.barssince(side[1] and close < rgLo), 9999) <= 10
    sigAB = cloudUp and scAB >= aMin and okB
    sigAS = cloudDn and scAS >= aMin and okS
 
    // --- C (approximate, score of 3)
    cPullB = nz(ta.barssince(low <= eF), 9999) <= cLook
    cPullS = nz(ta.barssince(high >= eF), 9999) <= cLook
    c3B = nz(ta.barssince(touchB), 9999) <= cLook
    c3S = nz(ta.barssince(touchS), 9999) <= cLook
    scCB = pt(bias >= qBias) + pt(msTrend == 1) + pt(c3B)
    scCS = pt(bias <= -qBias) + pt(msTrend == -1) + pt(c3S)
    var int lastCB = na
    var int lastCS = na
    sigCB = not cloudUp and trendUp and close > e89 and cPullB and fastUp and not fastUp[1] and close > open and close > eF and (na(lastCB) or bar_index - lastCB >= cGap) and not sigAB and scCB >= cMin
    sigCS = not cloudDn and not trendUp and close < e89 and cPullS and not fastUp and fastUp[1] and close < open and close < eF and (na(lastCS) or bar_index - lastCS >= cGap) and not sigAS and scCS >= cMin
    if sigCB
        lastCB := bar_index
    if sigCS
        lastCS := bar_index
 
    // --- D (exact)
    dHi = ta.highest(high, dLen)[1]
    dLo = ta.lowest(low, dLen)[1]
    dRawB = close > dHi and t60 == 1 and t4h == 1 and close > e89
    dRawS = close < dLo and t60 == -1 and t4h == -1 and close < e89
    var int   dDir = 0
    var float dStp = na
    var float dTgt = na
    var int   dBar = na
    bool dExit = false
    if dDir != 0 and bar_index > dBar
        if (dDir == 1 ? low <= dStp : high >= dStp) or (dDir == 1 ? high >= dTgt : low <= dTgt) or (dMax > 0 and bar_index - dBar >= dMax)
            dDir := 0
            dExit := true
    sigDB = dRawB and dDir == 0 and not dExit
    sigDS = dRawS and dDir == 0 and not dExit and not sigDB
    if sigDB
        dDir := 1
        dStp := close - dK * atr
        dTgt := close + dR * dK * atr
        dBar := bar_index
    if sigDS
        dDir := -1
        dStp := close + dK * atr
        dTgt := close - dR * dK * atr
        dBar := bar_index
 
    // --- R (exact port of the Suite signal)
    suKs = f_suKalman(close, 5)
    suKl = f_suKalman(close, 13)
    suBullCloud = suKs > suKl
    suArrowBull = suBullCloud and suKs > suKs[1]
    suArrowBear = not suArrowBull
    int suState = suBullCloud and suKs > suKs[2] ? 0 : not suBullCloud and suKs < suKs[2] ? 1 : 2
    [suSt, suStDir] = ta.supertrend(1.0, 2)
    suBullBrk = ta.crossover(close, suSt)
    suBearBrk = ta.crossunder(close, suSt)
    suRng   = high - low
    suBuyV  = suRng != 0 ? volume * (close - low) / suRng : 0.0
    suSellV = suRng != 0 ? volume * (high - close) / suRng : 0.0
    suBS = ta.ema(suBuyV, 25)
    suBF = ta.ema(suBuyV, 10)
    suSS = ta.ema(suSellV, 25)
    suSF = ta.ema(suSellV, 10)
    suBullRel = suBS != 0 ? suBF / suBS : na
    suBearRel = suSS != 0 ? suSF / suSS : na
    var array<float> suBullSmp = array.new_float()
    var array<float> suBearSmp = array.new_float()
    var array<float> suBullCdf = array.new_float()
    var array<float> suBearCdf = array.new_float()
    var float suBullM = na
    var float suBullSd = na
    var float suBearM = na
    var float suBearSd = na
    if suBullBrk and not na(suBullRel)
        if array.size(suBullSmp) >= 100
            array.shift(suBullSmp)
        array.push(suBullSmp, suBullRel)
        [c1_, m1_, s1_] = f_suKdeBuild(suBullSmp)
        suBullCdf := c1_
        suBullM := m1_
        suBullSd := s1_
    if suBearBrk and not na(suBearRel)
        if array.size(suBearSmp) >= 100
            array.shift(suBearSmp)
        array.push(suBearSmp, suBearRel)
        [c2_, m2_, s2_] = f_suKdeBuild(suBearSmp)
        suBearCdf := c2_
        suBearM := m2_
        suBearSd := s2_
    float suVolProb = na
    if suStDir < 0 and array.size(suBullSmp) > 0
        suVolProb := f_suKdeLook(suBullCdf, suBullRel, suBullM, suBullSd)
    if suStDir > 0 and array.size(suBearSmp) > 0
        suVolProb := f_suKdeLook(suBearCdf, suBearRel, suBearM, suBearSd)
    suPassKde = not na(suVolProb) and suVolProb >= 0.33
    suKdeBull = suBullBrk and suPassKde
    suKdeBear = suBearBrk and suPassKde
 
    suRaw = f_suRsi(hlc3, 5)
    var float suOsc = na
    suOsc := 0.1 * suRaw + 0.9 * nz(suOsc[1], suRaw)
    suPct  = f_suPctMean(suOsc, 200)
    suLrc  = ta.linreg(suPct, 5, 0)
    suEmaO = f_suEma(suOsc, 200)
    suOP   = suEmaO[1]
    suU2  = suOP * (suLrc * 2.0 / 100.0 + 1.0)
    suL2  = suOP * (1.0 - suLrc * 2.0 / 100.0)
    suU1  = suOP * (suLrc * 1.0 / 100.0 + 1.0)
    suL1  = suOP * (1.0 - suLrc * 1.0 / 100.0)
    suU15 = suOP * (suLrc * 1.5 / 100.0 + 1.0)
    suL15 = suOP * (1.0 - suLrc * 1.5 / 100.0)
    suOscUp = suOsc > suOsc[1]
    suOscDn = suOsc < suOsc[1]
    suXDnFix = ta.crossunder(suOsc, 65.5) or ta.crossunder(suOsc, 80.5) or ta.crossunder(suOsc, 75.5)
    suXUpFix = ta.crossover(suOsc, 34.5) or ta.crossover(suOsc, 19.5) or ta.crossover(suOsc, 24.5)
    suXDnBnd = ta.crossunder(suOsc, suU2) or ta.crossunder(suOsc, suU1) or ta.crossunder(suOsc, suU15)
    suXUpBnd = ta.crossover(suOsc, suL2) or ta.crossover(suOsc, suL1) or ta.crossover(suOsc, suL15)
 
    [suStT, suStTDir] = ta.supertrend(3.0, 10)
    suDonF = (ta.highest(high, 5) + ta.lowest(low, 5)) / 2.0
    suDonB = (ta.highest(high, 26) + ta.lowest(low, 26)) / 2.0
    suSar  = ta.sar(0.02, 0.02, 0.2)
    suE20  = ta.ema(close, 20)
    suHiOff = math.abs(ta.highestbars(high, 2))
    suLoOff = math.abs(ta.lowestbars(low, 2))
    suRecH  = high[suHiOff]
    suRecL  = low[suLoOff]
    suSmaH  = ta.sma(high, 2)
    suSmaL  = ta.sma(low, 2)
    var int   suHtT = 0
    var int   suHtN = 0
    var float suMaxL = na
    var float suMinH = na
    var float suHtUp = na
    var float suHtDn = na
    suMaxL := na(suMaxL) ? nz(low[1], low) : suMaxL
    suMinH := na(suMinH) ? nz(high[1], high) : suMinH
    if suHtN == 1
        suMaxL := math.max(suRecL, suMaxL)
        if suSmaH < suMaxL and close < nz(low[1], low)
            suHtT := 1
            suHtN := 0
            suMinH := suRecH
    else
        suMinH := math.min(suRecH, suMinH)
        if suSmaL > suMinH and close > nz(high[1], high)
            suHtT := 0
            suHtN := 1
            suMaxL := suRecL
    if suHtT == 0
        if not na(suHtT[1]) and suHtT[1] != 0
            suHtUp := nz(suHtDn[1], suMaxL)
        else
            suHtUp := na(suHtUp[1]) ? suMaxL : math.max(suMaxL, suHtUp[1])
    else
        if not na(suHtT[1]) and suHtT[1] != 1
            suHtDn := nz(suHtUp[1], suMinH)
        else
            suHtDn := na(suHtDn[1]) ? suMinH : math.min(suMinH, suHtDn[1])
    suHalf = suHtT == 0 ? suHtUp : suHtDn
    suBullVotes = (close > suE20 ? 1 : 0) + (close > suStT ? 1 : 0) + (close > suHalf ? 1 : 0) + (((close > suDonF and close > suDonB) or suDonF > suDonB) ? 1 : 0) + (close > suSar ? 1 : 0)
    suBearVotes = (close < suE20 ? 1 : 0) + (close < suStT ? 1 : 0) + (close < suHalf ? 1 : 0) + (((close < suDonF and close < suDonB) or suDonF < suDonB) ? 1 : 0) + (close < suSar ? 1 : 0)
    suBullCons = suBullVotes >= 3
    suBearCons = suBearVotes >= 3
 
    suPHs = ta.pivothigh(suOsc, 3, 3)
    suPLs = ta.pivotlow(suOsc, 3, 3)
    suPHl = ta.pivothigh(suOsc, 3, 1)
    suPLl = ta.pivotlow(suOsc, 3, 1)
    suLBP = ta.valuewhen(not na(suPHs), high[3], 0)
    suLBO = ta.valuewhen(not na(suPHs), suOsc[3], 0)
    suLLP = ta.valuewhen(not na(suPLs), low[3], 0)
    suLLO = ta.valuewhen(not na(suPLs), suOsc[3], 0)
    suBearDiv = not na(suPHl) and not na(suLBP) and high[1] > suLBP and suOsc[1] < suLBO and suLBO >= 65 and suOsc[1] > 50
    suBullDiv = not na(suPLl) and not na(suLLP) and low[1] < suLLP and suOsc[1] > suLLO and suLLO <= 35 and suOsc[1] < 50
 
    suSd  = ta.stdev(hlc3, 55)
    suMid = ta.wma(hlc3, 55)
    suDev = ta.wma(1.5 * suSd, 55)
    suUbb = suMid + suDev
    suLbb = suMid - suDev
    suTk  = syminfo.ticker
    suTkU = str.upper(suTk)
    suGold = str.contains(suTk, "XAU") or suTkU == "GOLD"
    suSilv = str.contains(suTk, "XAG") or suTkU == "SILVER"
    suOil  = str.contains(suTkU, "USOIL") or str.contains(suTkU, "UKOIL") or str.contains(suTkU, "WTI") or str.contains(suTkU, "BRENT") or str.contains(suTkU, "CRUDE")
    suJpy  = str.contains(suTk, "JPY")
    suCryp = syminfo.type == "crypto"
    suIdx  = syminfo.type == "index"
    suUnit = suGold ? 0.01 : suSilv ? 0.0001 : suOil ? 0.01 : suIdx ? 1.0 : suJpy ? 0.01 : suCryp ? 1.0 : 0.0001
    suBody  = math.abs(close - open)
    suBodyP = suRng != 0 ? suBody / suRng * 100.0 : 0.0
    suValid = suBodyP >= 39 and (suUnit != 0 ? suBody / suUnit : 0.0) >= 5
    suTouchL = ta.lowest(low, 3) <= suLbb
    suTouchU = ta.highest(high, 3) >= suUbb
    suGrayB = suState == 2 and open < suKs and open < suKl and close > suKs and close > suKl
    suGrayS = suState == 2 and open > suKs and open > suKl and close < suKs and close < suKl
    suEngB  = close > open and close[1] < open[1] and close > high[1]
    suEngS  = close < open and close[1] > open[1] and close < low[1]
    suRDivB = suBullDiv or suBullDiv[1] or suBullDiv[2]
    suRDivS = suBearDiv or suBearDiv[1] or suBearDiv[2]
    suRChB  = suXUpBnd or suXUpBnd[1] or suXUpBnd[2] or suXUpFix or suXUpFix[1] or suXUpFix[2]
    suRChS  = suXDnBnd or suXDnBnd[1] or suXDnBnd[2] or suXDnFix or suXDnFix[1] or suXDnFix[2]
    suBullBase = suTouchL and suValid and suOscUp and close > open
    suBearBase = suTouchU and suValid and suOscDn and close < open
    suBullConf = (suArrowBull ? 1 : 0) + (suKdeBull ? 1 : 0) + (suState == 0 ? 1 : 0) + (suEngB ? 1 : 0) + (suRDivB ? 1 : 0) + (suGrayB ? 1 : 0) + (suRChB ? 1 : 0) + (suBullCons ? 2 : 0) + (suOscUp ? 1 : 0)
    suBearConf = (suArrowBear ? 1 : 0) + (suKdeBear ? 1 : 0) + (suState == 1 ? 1 : 0) + (suEngS ? 1 : 0) + (suRDivS ? 1 : 0) + (suGrayS ? 1 : 0) + (suRChS ? 1 : 0) + (suBearCons ? 2 : 0) + (suOscDn ? 1 : 0)
    suBullSc = suBullBase and suBullConf >= 2 ? 1 + suBullConf : 0
    suBearSc = suBearBase and suBearConf >= 2 ? 1 + suBearConf : 0
    var int suLastB = -100000
    var int suLastS = -100000
    sigRB = suBullSc > 0 and suBullSc >= rMin and bar_index - suLastB > 3
    sigRS = suBearSc > 0 and suBearSc >= rMin and bar_index - suLastS > 3
    if sigRB
        suLastB := bar_index
    if sigRS
        suLastS := bar_index
 
    // --- Ⓓ oscillator divergence (same rule as the panel: compared with the last 10 oscillator pivots)
    var array<float> phP = array.new_float()
    var array<float> phO = array.new_float()
    var array<float> plP = array.new_float()
    var array<float> plO = array.new_float()
    if not na(suPHs)
        array.unshift(phP, high[3])
        array.unshift(phO, suOsc[3])
        if array.size(phP) > 10
            array.pop(phP)
            array.pop(phO)
    if not na(suPLs)
        array.unshift(plP, low[3])
        array.unshift(plO, suOsc[3])
        if array.size(plP) > 10
            array.pop(plP)
            array.pop(plO)
    bool sigVS = false
    bool sigVB = false
    float o1 = suOsc[1]
    float h1 = high[1]
    float l1 = low[1]
    if not na(suPHl) and array.size(phP) > 0
        for i = 0 to array.size(phP) - 1
            float oR = array.get(phO, i)
            if h1 > array.get(phP, i) and o1 < oR and oR >= 65 and o1 > 50
                sigVS := true
                break
    if not na(suPLl) and array.size(plP) > 0
        for i = 0 to array.size(plP) - 1
            float oR = array.get(plO, i)
            if l1 < array.get(plP, i) and o1 > oR and oR <= 35 and o1 < 50
                sigVB := true
                break
 
    // --- remember the last signal of each type: direction, time, close, score
    var int   aD = 0
    var int   aB = na
    var int   aT = na
    var float aP = na
    var int   aS = na
    var int   cD = 0
    var int   cB = na
    var int   cT = na
    var float cP = na
    var int   cS = na
    var int   dD = 0
    var int   dB = na
    var int   dT = na
    var float dP = na
    var int   rD = 0
    var int   rB = na
    var int   rT = na
    var float rP = na
    var int   rS = na
    var int   vD = 0
    var int   vB = na
    var int   vT = na
    var float vP = na
    if sigAB or sigAS
        aD := sigAB ? 1 : -1
        aT := time
        aB := bar_index
        aP := close
        aS := sigAB ? scAB : scAS
    if sigCB or sigCS
        cD := sigCB ? 1 : -1
        cT := time
        cB := bar_index
        cP := close
        cS := sigCB ? scCB : scCS
    if sigDB or sigDS
        dD := sigDB ? 1 : -1
        dT := time
        dB := bar_index
        dP := close
    if sigRB or sigRS
        rD := sigRB ? 1 : -1
        rT := time
        rB := bar_index
        rP := close
        rS := sigRB ? suBullSc : suBearSc
    if sigVB or sigVS
        vD := sigVB ? 1 : -1
        vT := time
        vB := bar_index
        vP := close
    [aD, aT, aP, aS, aB, cD, cT, cP, cS, cB, dD, dT, dP, dB, rD, rT, rP, rS, rB, vD, vT, vP, vB, bar_index]
 
// ═════════════════════════════ REQUESTS ═════════════════════════════
[a1D, a1T, a1P, a1S, a1B, c1D, c1T, c1P, c1S, c1B, d1D, d1T, d1P, d1B, r1D, r1T, r1P, r1S, r1B, v1D, v1T, v1P, v1B, z1] = request.security(s1, timeframe.period, f_sig())
[a2D, a2T, a2P, a2S, a2B, c2D, c2T, c2P, c2S, c2B, d2D, d2T, d2P, d2B, r2D, r2T, r2P, r2S, r2B, v2D, v2T, v2P, v2B, z2] = request.security(s2, timeframe.period, f_sig())
[a3D, a3T, a3P, a3S, a3B, c3D, c3T, c3P, c3S, c3B, d3D, d3T, d3P, d3B, r3D, r3T, r3P, r3S, r3B, v3D, v3T, v3P, v3B, z3] = request.security(s3, timeframe.period, f_sig())
[a4D, a4T, a4P, a4S, a4B, c4D, c4T, c4P, c4S, c4B, d4D, d4T, d4P, d4B, r4D, r4T, r4P, r4S, r4B, v4D, v4T, v4P, v4B, z4] = request.security(s4, timeframe.period, f_sig())
[a5D, a5T, a5P, a5S, a5B, c5D, c5T, c5P, c5S, c5B, d5D, d5T, d5P, d5B, r5D, r5T, r5P, r5S, r5B, v5D, v5T, v5P, v5B, z5] = request.security(s5, timeframe.period, f_sig())
[a6D, a6T, a6P, a6S, a6B, c6D, c6T, c6P, c6S, c6B, d6D, d6T, d6P, d6B, r6D, r6T, r6P, r6S, r6B, v6D, v6T, v6P, v6B, z6] = request.security(s6, timeframe.period, f_sig())
 
// ═════════════════════════════ TABLE ═════════════════════════════
tPos = tPosI == "Top Left" ? position.top_left : tPosI == "Bottom Right" ? position.bottom_right : tPosI == "Bottom Left" ? position.bottom_left : tPosI == "Middle Right" ? position.middle_right : position.top_right
tSz  = tSzI == "Tiny" ? size.tiny : tSzI == "Normal" ? size.normal : size.small
var table brd = table.new(tPos, 6, 7, bgcolor = color.new(#131722, 10), border_color = color.new(#787B86, 75), border_width = 1, frame_color = color.new(#787B86, 50), frame_width = 1)
 
// One cell: arrow + candles ago (bright when fresh, faded when old, – when too old or none)
f_cell(int col, int row, bool on, int now, int dir, int t, float px, int sc, int b, string what, string nm) =>
    if not on
        table.cell(brd, col, row, "", bgcolor = color.new(#131722, 100), text_size = tSz)
    else
        int ago = na(b) or na(now) ? na : now - b
        bool has = dir != 0 and not na(ago) and ago <= maxAge
        bool fresh = has and ago <= freshN
        color base = dir == 1 ? #00C853 : #FF1744
        string txt = not has ? "–" : (dir == 1 ? "▲ " : "▼ ") + (ago == 0 ? "now" : str.tostring(ago))
        string tip = not has ? (dir == 0 ? "No " + what + " yet" : "Last " + what + " is older than " + str.tostring(maxAge) + " candles") : nm + " · " + what + (dir == 1 ? " BUY" : " SELL") + (na(sc) ? "" : " · score " + str.tostring(sc)) + "\n" + str.format_time(t, "dd/MM HH:mm") + " · close " + str.tostring(px) + "\n" + (ago == 0 ? "on the last closed candle" : str.tostring(ago) + " candle(s) ago")
        table.cell(brd, col, row, txt, text_color = not has ? color.new(#787B86, 30) : fresh ? color.white : base, bgcolor = fresh ? color.new(base, 25) : color.new(#131722, 100), text_size = tSz, tooltip = tip)
 
f_row(int row, bool on, string nm, int now, int aD, int aT, float aP, int aS, int aB, int cD, int cT, float cP, int cS, int cB, int dD, int dT, float dP, int dB, int rD, int rT, float rP, int rS, int rB, int vD, int vT, float vP, int vB) =>
    if on
        table.cell(brd, 0, row, nm, text_color = color.white, text_halign = text.align_left, text_size = tSz)
        f_cell(1, row, showA, now, aD, aT, aP, aS, aB, "A", nm)
        f_cell(2, row, showC, now, cD, cT, cP, cS, cB, "C", nm)
        f_cell(3, row, showD, now, dD, dT, dP, na, dB, "D", nm)
        f_cell(4, row, showR, now, rD, rT, rP, rS, rB, "R", nm)
        f_cell(5, row, showV, now, vD, vT, vP, na, vB, "Ⓓ divergence", nm)
    else
        table.clear(brd, 0, row, 5, row)
 
if barstate.islast
    hdr = color.new(#2A2E39, 0)
    table.cell(brd, 0, 0, timeframe.isminutes ? (timeframe.multiplier >= 60 ? str.tostring(timeframe.multiplier / 60) + "H" : timeframe.period + "m") : timeframe.period, text_color = #B2B5BE, bgcolor = hdr, text_size = tSz, tooltip = "Signals on this chart's timeframe. Numbers = candles since the signal.")
    table.cell(brd, 1, 0, showA ? "A≈" : "", text_color = color.white, bgcolor = hdr, text_size = tSz, tooltip = "A · approximate (no bubbles / delta / EQ zones; score of 6)")
    table.cell(brd, 2, 0, showC ? "C≈" : "", text_color = color.white, bgcolor = hdr, text_size = tSz, tooltip = "C · approximate (Hull turn trigger; score of 3)")
    table.cell(brd, 3, 0, showD ? "D" : "", text_color = color.white, bgcolor = hdr, text_size = tSz, tooltip = "D · Donchian breakout with the 1H / 4H trend")
    table.cell(brd, 4, 0, showR ? "R" : "", text_color = color.white, bgcolor = hdr, text_size = tSz, tooltip = "R · reversal at the band")
    table.cell(brd, 5, 0, showV ? "Ⓓ" : "", text_color = color.rgb(255, 215, 0), bgcolor = hdr, text_size = tSz, tooltip = "Ⓓ · oscillator divergence")
    f_row(1, s1On, n1, z1, a1D, a1T, a1P, a1S, a1B, c1D, c1T, c1P, c1S, c1B, d1D, d1T, d1P, d1B, r1D, r1T, r1P, r1S, r1B, v1D, v1T, v1P, v1B)
    f_row(2, s2On, n2, z2, a2D, a2T, a2P, a2S, a2B, c2D, c2T, c2P, c2S, c2B, d2D, d2T, d2P, d2B, r2D, r2T, r2P, r2S, r2B, v2D, v2T, v2P, v2B)
    f_row(3, s3On, n3, z3, a3D, a3T, a3P, a3S, a3B, c3D, c3T, c3P, c3S, c3B, d3D, d3T, d3P, d3B, r3D, r3T, r3P, r3S, r3B, v3D, v3T, v3P, v3B)
    f_row(4, s4On, n4, z4, a4D, a4T, a4P, a4S, a4B, c4D, c4T, c4P, c4S, c4B, d4D, d4T, d4P, d4B, r4D, r4T, r4P, r4S, r4B, v4D, v4T, v4P, v4B)
    f_row(5, s5On, n5, z5, a5D, a5T, a5P, a5S, a5B, c5D, c5T, c5P, c5S, c5B, d5D, d5T, d5P, d5B, r5D, r5T, r5P, r5S, r5B, v5D, v5T, v5P, v5B)
    f_row(6, s6On, n6, z6, a6D, a6T, a6P, a6S, a6B, c6D, c6T, c6P, c6S, c6B, d6D, d6T, d6P, d6B, r6D, r6T, r6P, r6S, r6B, v6D, v6T, v6P, v6B)
 
// ═════════════════════════════ ALERTS ═════════════════════════════
// A new signal = its time changed since the previous chart candle (so a closed market does not repeat it).
f_new(int t) => not na(t) and t != t[1] and not na(t[1])
f_msg(string nm, string what, int dir, float px) =>
    "Arruda Signal Board: " + what + (dir == 1 ? " BUY " : " SELL ") + nm + " (" + timeframe.period + ") @ " + str.tostring(px)
f_alertRow(bool on, string nm, int aD, int aT, float aP, int cD, int cT, float cP, int dD, int dT, float dP, int rD, int rT, float rP, int vD, int vT, float vP) =>
    bool hit = false
    string m = ""
    nA = f_new(aT)
    nC = f_new(cT)
    nD = f_new(dT)
    nR = f_new(rT)
    nV = f_new(vT)
    if on
        if showA and alA and nA
            m += f_msg(nm, "A", aD, aP) + "\n"
            hit := true
        if showC and alC and nC
            m += f_msg(nm, "C", cD, cP) + "\n"
            hit := true
        if showD and alD and nD
            m += f_msg(nm, "D", dD, dP) + "\n"
            hit := true
        if showR and alR and nR
            m += f_msg(nm, "R", rD, rP) + "\n"
            hit := true
        if showV and alV and nV
            m += f_msg(nm, "Ⓓ divergence", vD, vP) + "\n"
            hit := true
    [hit, m]
[k1, m1] = f_alertRow(s1On, n1, a1D, a1T, a1P, c1D, c1T, c1P, d1D, d1T, d1P, r1D, r1T, r1P, v1D, v1T, v1P)
[k2, m2] = f_alertRow(s2On, n2, a2D, a2T, a2P, c2D, c2T, c2P, d2D, d2T, d2P, r2D, r2T, r2P, v2D, v2T, v2P)
[k3, m3] = f_alertRow(s3On, n3, a3D, a3T, a3P, c3D, c3T, c3P, d3D, d3T, d3P, r3D, r3T, r3P, v3D, v3T, v3P)
[k4, m4] = f_alertRow(s4On, n4, a4D, a4T, a4P, c4D, c4T, c4P, d4D, d4T, d4P, r4D, r4T, r4P, v4D, v4T, v4P)
[k5, m5] = f_alertRow(s5On, n5, a5D, a5T, a5P, c5D, c5T, c5P, d5D, d5T, d5P, r5D, r5T, r5P, v5D, v5T, v5P)
[k6, m6] = f_alertRow(s6On, n6, a6D, a6T, a6P, c6D, c6T, c6P, d6D, d6T, d6P, r6D, r6T, r6P, v6D, v6T, v6P)
anyNew = k1 or k2 or k3 or k4 or k5 or k6
if barstate.isconfirmed and anyNew
    alert(m1 + m2 + m3 + m4 + m5 + m6, alert.freq_once_per_bar_close)
alertcondition(anyNew, "New signal on any market", "Arruda Signal Board: new signal on one of the markets ({{interval}}) - check the board")

← 返回列表