11-Engine Confluence Ladder

gogaz · study · 922 行 · 点赞 15 · 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/
// © gogaz
 
 
//@version=6
indicator("11-Engine Confluence Ladder ", "11Engines-Confluence ", overlay=true, max_boxes_count=500, max_lines_count=500, max_labels_count=500, precision=4)
 
// ==========================================
// USER INPUTS
// ==========================================
grp_tf = "Timeframes"
tf1_in = input.timeframe("5",  "TF1 (fastest)", group=grp_tf)
tf2_in = input.timeframe("15", "TF2",           group=grp_tf)
tf3_in = input.timeframe("60", "TF3",           group=grp_tf)
tf4_in = input.timeframe("240","TF4 (slowest)", group=grp_tf)
 
grp_vis = "Display"
showLadder  = input.bool(true, "Enable Engine Ladder", group=grp_vis)
maxBars     = input.int(6, "Max Candles", minval=3, maxval=20, group=grp_vis)
fastTop     = input.bool(true, "Fastest TF on Top", group=grp_vis)
textSizeInp = input.string("Tiny", "Text Size", options=["Tiny","Small","Normal"], group=grp_vis)
panelTransp = input.int(40, "Header Transparency", minval=0, maxval=90, group=grp_vis)
 
// ==========================================
// ENGINE PRESET
// ==========================================
grp_preset = "Engine Preset"
preset = input.string("All 11", "Show Which Engines",
     options=["All 11", "Custom (use toggles below)", "Momentum (RSI/MFI/Stoch/UO)",
              "Trend (MACD/EMA/CCI)", "Volume (MFI/VFI/OBV)",
              "Oscillators (RSI/CCI/Stoch/StRSI)",
              "Core 5 (RSI/MACD/Stoch/EMA/ATR)"],
     group=grp_preset,
     tooltip="'All 11' forces every engine on. 'Custom' respects the individual toggles below.")
 
// ==========================================
// ENGINES (ROWS) — raw toggles (used only when preset = Custom)
// ==========================================
grp_eng = "Engines (Rows)"
bool rsiOn   = input.bool(true,  "1. RSI",        group=grp_eng)
bool cciOn   = input.bool(true,  "2. CCI",        group=grp_eng)
bool mfiOn   = input.bool(true,  "3. MFI",        group=grp_eng)
bool stochOn = input.bool(true,  "4. Stochastic", group=grp_eng)
bool srsiOn  = input.bool(true,  "5. Stoch RSI",  group=grp_eng)
bool macdOn  = input.bool(true,  "6. MACD",       group=grp_eng)
bool vfiOn   = input.bool(true,  "7. VFI",        group=grp_eng)
bool obvOn   = input.bool(true,  "8. OBV",        group=grp_eng)
bool atrOn   = input.bool(true,  "9. ATR",        group=grp_eng)
bool uoOn    = input.bool(true,  "10. UO",        group=grp_eng)
bool emaOn   = input.bool(true,  "11. EMA 200",   group=grp_eng)
qtyTransp = input.int(25, "Row Transparency", minval=0, maxval=90, group=grp_eng)
vPadPct   = input.float(0.12, "Vertical Padding", minval=0.0, maxval=0.4, step=0.01, group=grp_eng)
 
grp_head = "Consensus Header"
showHeader   = input.bool(true, "Show TF Consensus Header Row", group=grp_head)
showHeadText = input.bool(true, "Append Score in Header Text", group=grp_head)
 
grp_fp = "Band & Criticality"
showCrit    = input.bool(true, "Show Criticality Markers", group=grp_fp)
critThresh  = input.float(60, "Criticality Threshold (|norm|)", minval=0, maxval=100, group=grp_fp)
showDomTerm = input.bool(true, "Highlight Dominant Engine", group=grp_fp)
domColor    = input.color(color.yellow, "Dominant Engine Color", group=grp_fp)
showBand    = input.bool(true, "Highlight Agreement Band", group=grp_fp)
bandPct     = input.float(0.30, "Band % of Dominant", minval=0.0, maxval=1.0, step=0.05, group=grp_fp)
bandBorderCol = input.color(color.white, "Band Border Color", group=grp_fp)
bandOverrides = input.bool(false, "Band Border Overrides Consensus Border", group=grp_fp)
 
grp_logic = "Trend & Thresholds"
lookback_length = input.int(3, "Trend Lookback (Bars)", options=[2,3,5,10], group=grp_logic)
alert_thresh    = input.int(8, "Consensus Threshold (out of 11)", minval=6, maxval=11, group=grp_logic)
 
grp_conf = "Multi-TF Confluence"
confMode = input.string("All TFs (4/4)", "Confluence Requirement", options=["All TFs (4/4)","Majority (3/4)","Any TF (1/4)"], group=grp_conf)
confRequireSameDir = input.bool(true, "Require same direction across TFs", group=grp_conf)
 
// ==========================================
// RESOLVE PRESET → EFFECTIVE FLAGS
// ==========================================
bool showRSI   = false
bool showCCI   = false
bool showMFI   = false
bool showStoch = false
bool showSRSI  = false
bool showMACD  = false
bool showVFI   = false
bool showOBV   = false
bool showATR   = false
bool showUO    = false
bool showEMA   = false
 
switch preset
    "All 11" =>
        showRSI   := true
        showCCI   := true
        showMFI   := true
        showStoch := true
        showSRSI  := true
        showMACD  := true
        showVFI   := true
        showOBV   := true
        showATR   := true
        showUO    := true
        showEMA   := true
    "Momentum (RSI/MFI/Stoch/UO)" =>
        showRSI   := true
        showMFI   := true
        showStoch := true
        showUO    := true
    "Trend (MACD/EMA/CCI)" =>
        showCCI    := true
        showMACD   := true
        showEMA    := true
    "Volume (MFI/VFI/OBV)" =>
        showMFI := true
        showVFI := true
        showOBV := true
    "Oscillators (RSI/CCI/Stoch/StRSI)" =>
        showRSI   := true
        showCCI   := true
        showStoch := true
        showSRSI  := true
    "Core 5 (RSI/MACD/Stoch/EMA/ATR)" =>
        showRSI   := true
        showMACD  := true
        showStoch := true
        showEMA   := true
        showATR   := true
    =>   // "Custom (use toggles below)"
        showRSI   := rsiOn
        showCCI   := cciOn
        showMFI   := mfiOn
        showStoch := stochOn
        showSRSI  := srsiOn
        showMACD  := macdOn
        showVFI   := vfiOn
        showOBV   := obvOn
        showATR   := atrOn
        showUO    := uoOn
        showEMA   := emaOn
 
// ==========================================
// HELPERS
// ==========================================
f_trunc(n, d) => math.floor(n * math.pow(10, d)) / math.pow(10, d)
getTextSize(string s) => s == "Tiny" ? size.tiny : s == "Small" ? size.small : size.normal
 
// Normalize any engine value to roughly -100..+100 for consistent heatmap
normalize(float raw, int engIdx) =>
    float n = 0.0
    if engIdx == 0 or engIdx == 2 or engIdx == 3 or engIdx == 4 or engIdx == 9  // RSI, MFI, Stoch, StRSI, UO
        n := (raw - 50.0) * 2.0
    else if engIdx == 1  // CCI
        n := math.max(-100.0, math.min(100.0, raw / 2.0))
    else if engIdx == 5  // MACD hist
        n := math.max(-100.0, math.min(100.0, raw * 10.0))
    else if engIdx == 6  // VFI (already z-score like)
        n := math.max(-100.0, math.min(100.0, raw * 20.0))
    else if engIdx == 7  // OBV (relative)
        n := raw
    else if engIdx == 8  // ATR relative
        n := raw
    else if engIdx == 10 // EMA relative position
        n := raw
    else
        n := raw
    math.max(-100.0, math.min(100.0, n))
 
// ==========================================
// PER-TF ENGINE FETCH (all params simple via if/else on tf string)
// ==========================================
fetchEngines(string tf) =>
    int rsiL = 14
    if tf == "1"
        rsiL := 5
    else if tf == "2"
        rsiL := 7
    else if tf == "3"
        rsiL := 7
    else if tf == "5"
        rsiL := 10
    else if tf == "15"
        rsiL := 14
    else if tf == "30"
        rsiL := 14
    else if tf == "60"
        rsiL := 14
    else if tf == "240"
        rsiL := 16
    else if tf == "D"
        rsiL := 20
    else if tf == "W"
        rsiL := 24
 
    int cciL = 20
    if tf == "1"
        cciL := 7
    else if tf == "2"
        cciL := 10
    else if tf == "3"
        cciL := 10
    else if tf == "5"
        cciL := 14
    else if tf == "15"
        cciL := 20
    else if tf == "30"
        cciL := 20
    else if tf == "60"
        cciL := 20
    else if tf == "240"
        cciL := 24
    else if tf == "D"
        cciL := 30
    else if tf == "W"
        cciL := 36
 
    int mfiL = 14
    if tf == "1"
        mfiL := 10
    else if tf == "2"
        mfiL := 10
    else if tf == "3"
        mfiL := 10
    else if tf == "5"
        mfiL := 10
    else if tf == "15"
        mfiL := 14
    else if tf == "30"
        mfiL := 14
    else if tf == "60"
        mfiL := 14
    else if tf == "240"
        mfiL := 16
    else if tf == "D"
        mfiL := 20
    else if tf == "W"
        mfiL := 24
 
    int stoL = 14
    if tf == "1"
        stoL := 5
    else if tf == "2"
        stoL := 7
    else if tf == "3"
        stoL := 7
    else if tf == "5"
        stoL := 10
    else if tf == "15"
        stoL := 14
    else if tf == "30"
        stoL := 14
    else if tf == "60"
        stoL := 14
    else if tf == "240"
        stoL := 16
    else if tf == "D"
        stoL := 20
    else if tf == "W"
        stoL := 24
 
    int uo1 = 7
    if tf == "1"
        uo1 := 3
    else if tf == "2"
        uo1 := 4
    else if tf == "3"
        uo1 := 4
    else if tf == "5"
        uo1 := 5
    else if tf == "15"
        uo1 := 7
    else if tf == "30"
        uo1 := 7
    else if tf == "60"
        uo1 := 7
    else if tf == "240"
        uo1 := 8
    else if tf == "D"
        uo1 := 10
    else if tf == "W"
        uo1 := 12
 
    int uo2 = 14
    if tf == "1"
        uo2 := 7
    else if tf == "2"
        uo2 := 8
    else if tf == "3"
        uo2 := 8
    else if tf == "5"
        uo2 := 10
    else if tf == "15"
        uo2 := 14
    else if tf == "30"
        uo2 := 14
    else if tf == "60"
        uo2 := 14
    else if tf == "240"
        uo2 := 16
    else if tf == "D"
        uo2 := 20
    else if tf == "W"
        uo2 := 24
 
    int uo3 = 28
    if tf == "1"
        uo3 := 14
    else if tf == "2"
        uo3 := 16
    else if tf == "3"
        uo3 := 16
    else if tf == "5"
        uo3 := 20
    else if tf == "15"
        uo3 := 28
    else if tf == "30"
        uo3 := 28
    else if tf == "60"
        uo3 := 28
    else if tf == "240"
        uo3 := 32
    else if tf == "D"
        uo3 := 40
    else if tf == "W"
        uo3 := 48
 
    int mf = 12
    if tf == "1"
        mf := 6
    else if tf == "2"
        mf := 7
    else if tf == "3"
        mf := 7
    else if tf == "5"
        mf := 8
    else if tf == "15"
        mf := 12
    else if tf == "30"
        mf := 12
    else if tf == "60"
        mf := 12
    else if tf == "240"
        mf := 14
    else if tf == "D"
        mf := 18
    else if tf == "W"
        mf := 22
 
    int ms = 26
    if tf == "1"
        ms := 16
    else if tf == "2"
        ms := 18
    else if tf == "3"
        ms := 18
    else if tf == "5"
        ms := 20
    else if tf == "15"
        ms := 26
    else if tf == "30"
        ms := 26
    else if tf == "60"
        ms := 26
    else if tf == "240"
        ms := 30
    else if tf == "D"
        ms := 36
    else if tf == "W"
        ms := 44
 
    int msg = 9
    if tf == "1"
        msg := 4
    else if tf == "2"
        msg := 5
    else if tf == "3"
        msg := 5
    else if tf == "5"
        msg := 6
    else if tf == "15"
        msg := 9
    else if tf == "30"
        msg := 9
    else if tf == "60"
        msg := 9
    else if tf == "240"
        msg := 10
    else if tf == "D"
        msg := 12
    else if tf == "W"
        msg := 14
 
    int vfiL = 13
    if tf == "1"
        vfiL := 8
    else if tf == "2"
        vfiL := 10
    else if tf == "3"
        vfiL := 10
    else if tf == "5"
        vfiL := 12
    else if tf == "15"
        vfiL := 13
    else if tf == "30"
        vfiL := 13
    else if tf == "60"
        vfiL := 13
    else if tf == "240"
        vfiL := 16
    else if tf == "D"
        vfiL := 20
    else if tf == "W"
        vfiL := 24
 
    int obvL = 20
    if tf == "1"
        obvL := 10
    else if tf == "2"
        obvL := 14
    else if tf == "3"
        obvL := 14
    else if tf == "5"
        obvL := 18
    else if tf == "15"
        obvL := 20
    else if tf == "30"
        obvL := 20
    else if tf == "60"
        obvL := 20
    else if tf == "240"
        obvL := 24
    else if tf == "D"
        obvL := 30
    else if tf == "W"
        obvL := 36
 
    int atrL = 14
    if tf == "1"
        atrL := 7
    else if tf == "2"
        atrL := 10
    else if tf == "3"
        atrL := 10
    else if tf == "5"
        atrL := 12
    else if tf == "15"
        atrL := 14
    else if tf == "30"
        atrL := 14
    else if tf == "60"
        atrL := 14
    else if tf == "240"
        atrL := 16
    else if tf == "D"
        atrL := 20
    else if tf == "W"
        atrL := 24
 
    float rsiV  = ta.rsi(close, rsiL)
    bool  rsiB  = rsiV > 50
    bool  rsiR  = ta.rising(rsiV, lookback_length)
 
    float cciV  = ta.cci(close, cciL)
    bool  cciB  = cciV > 0
    bool  cciR  = ta.rising(cciV, lookback_length)
 
    float mfiV  = ta.mfi(close, mfiL)
    bool  mfiB  = mfiV > 50
    bool  mfiR  = ta.rising(mfiV, lookback_length)
 
    float stoV  = ta.sma(ta.stoch(close, high, low, stoL), 3)
    bool  stoB  = stoV > 50
    bool  stoR  = ta.rising(stoV, lookback_length)
 
    float srsiV = ta.sma(ta.stoch(rsiV, rsiV, rsiV, rsiL), 3)
    bool  srsiB = srsiV > 50
    bool  srsiR = ta.rising(srsiV, lookback_length)
 
    [macdL, sigL, hist] = ta.macd(close, mf, ms, msg)
    bool  macdB = hist > 0
    bool  macdR = ta.rising(hist, lookback_length)
 
    float vfiF  = math.log(close / close[1]) * volume
    float vfiM  = ta.sma(vfiF, vfiL)
    float vfiS  = ta.stdev(vfiF, vfiL)
    float vfiV  = vfiS != 0 ? vfiM / vfiS : 0.0
    bool  vfiB  = vfiV > 0
    bool  vfiR  = ta.rising(vfiV, lookback_length)
 
    float obvV  = ta.obv
    float obvM  = ta.sma(obvV, obvL)
    bool  obvB  = obvV > obvM
    bool  obvR  = ta.rising(obvV, lookback_length)
    float obvNorm = obvM != 0 ? (obvV - obvM) / math.abs(obvM) * 50.0 : 0.0
 
    float atrV  = ta.atr(atrL)
    float atrM  = ta.sma(atrV, 20)
    bool  atrB  = atrV > atrM and close > close[1]
    bool  atrR  = ta.rising(atrV, lookback_length)
    float atrNorm = atrM != 0 ? (atrV / atrM - 1.0) * 100.0 : 0.0
 
    float bpU   = close - math.min(low, close[1])
    float trU   = math.max(high, close[1]) - math.min(low, close[1])
    float bp1   = math.sum(bpU, uo1), float tr1 = math.sum(trU, uo1)
    float bp2   = math.sum(bpU, uo2), float tr2 = math.sum(trU, uo2)
    float bp3   = math.sum(bpU, uo3), float tr3 = math.sum(trU, uo3)
    float uoV   = (4*bp1 + 2*bp2 + bp3) / (4*tr1 + 2*tr2 + tr3) * 100
    bool  uoB   = uoV > 50
    bool  uoR   = ta.rising(uoV, lookback_length)
 
    float emaV  = ta.ema(close, 200)
    bool  emaB  = close > emaV
    bool  emaR  = ta.rising(emaV, lookback_length)
    float emaNorm = emaV != 0 ? (close - emaV) / emaV * 100.0 : 0.0
 
    [rsiV, rsiB, rsiR,
     cciV, cciB, cciR,
     mfiV, mfiB, mfiR,
     stoV, stoB, stoR,
     srsiV, srsiB, srsiR,
     hist, macdB, macdR,
     vfiV, vfiB, vfiR,
     obvNorm, obvB, obvR,
     atrNorm, atrB, atrR,
     uoV, uoB, uoR,
     emaNorm, emaB, emaR]
 
// ==========================================
// REQUEST ENGINE DATA PER TF
// ==========================================
[rsi1V, rsi1B, rsi1R, cci1V, cci1B, cci1R, mfi1V, mfi1B, mfi1R, sto1V, sto1B, sto1R,
 srsi1V, srsi1B, srsi1R, macd1V, macd1B, macd1R, vfi1V, vfi1B, vfi1R, obv1V, obv1B, obv1R,
 atr1V, atr1B, atr1R, uo1V, uo1B, uo1R, ema1V, ema1B, ema1R] = request.security(syminfo.tickerid, tf1_in, fetchEngines(tf1_in), lookahead=barmerge.lookahead_off)
 
[rsi2V, rsi2B, rsi2R, cci2V, cci2B, cci2R, mfi2V, mfi2B, mfi2R, sto2V, sto2B, sto2R,
 srsi2V, srsi2B, srsi2R, macd2V, macd2B, macd2R, vfi2V, vfi2B, vfi2R, obv2V, obv2B, obv2R,
 atr2V, atr2B, atr2R, uo2V, uo2B, uo2R, ema2V, ema2B, ema2R] = request.security(syminfo.tickerid, tf2_in, fetchEngines(tf2_in), lookahead=barmerge.lookahead_off)
 
[rsi3V, rsi3B, rsi3R, cci3V, cci3B, cci3R, mfi3V, mfi3B, mfi3R, sto3V, sto3B, sto3R,
 srsi3V, srsi3B, srsi3R, macd3V, macd3B, macd3R, vfi3V, vfi3B, vfi3R, obv3V, obv3B, obv3R,
 atr3V, atr3B, atr3R, uo3V, uo3B, uo3R, ema3V, ema3B, ema3R] = request.security(syminfo.tickerid, tf3_in, fetchEngines(tf3_in), lookahead=barmerge.lookahead_off)
 
[rsi4V, rsi4B, rsi4R, cci4V, cci4B, cci4R, mfi4V, mfi4B, mfi4R, sto4V, sto4B, sto4R,
 srsi4V, srsi4B, srsi4R, macd4V, macd4B, macd4R, vfi4V, vfi4B, vfi4R, obv4V, obv4B, obv4R,
 atr4V, atr4B, atr4R, uo4V, uo4B, uo4R, ema4V, ema4B, ema4R] = request.security(syminfo.tickerid, tf4_in, fetchEngines(tf4_in), lookahead=barmerge.lookahead_off)
 
// ==========================================
// PER-TF CONSENSUS COUNTS (based on enabled engines only)
// ==========================================
int enabledCount = (showRSI ? 1 : 0) + (showCCI ? 1 : 0) + (showMFI ? 1 : 0) + (showStoch ? 1 : 0) + (showSRSI ? 1 : 0) + (showMACD ? 1 : 0) + (showVFI ? 1 : 0) + (showOBV ? 1 : 0) + (showATR ? 1 : 0) + (showUO ? 1 : 0) + (showEMA ? 1 : 0)
 
bull1 = (showRSI   and rsi1B  ? 1 : 0) + (showCCI   and cci1B  ? 1 : 0) + (showMFI   and mfi1B  ? 1 : 0) + (showStoch and sto1B  ? 1 : 0) + (showSRSI  and srsi1B ? 1 : 0) + (showMACD  and macd1B ? 1 : 0) + (showVFI   and vfi1B  ? 1 : 0) + (showOBV   and obv1B  ? 1 : 0) + (showATR   and atr1B  ? 1 : 0) + (showUO    and uo1B   ? 1 : 0) + (showEMA   and ema1B  ? 1 : 0)
bull2 = (showRSI   and rsi2B  ? 1 : 0) + (showCCI   and cci2B  ? 1 : 0) + (showMFI   and mfi2B  ? 1 : 0) + (showStoch and sto2B  ? 1 : 0) + (showSRSI  and srsi2B ? 1 : 0) + (showMACD  and macd2B ? 1 : 0) + (showVFI   and vfi2B  ? 1 : 0) + (showOBV   and obv2B  ? 1 : 0) + (showATR   and atr2B  ? 1 : 0) + (showUO    and uo2B   ? 1 : 0) + (showEMA   and ema2B  ? 1 : 0)
bull3 = (showRSI   and rsi3B  ? 1 : 0) + (showCCI   and cci3B  ? 1 : 0) + (showMFI   and mfi3B  ? 1 : 0) + (showStoch and sto3B  ? 1 : 0) + (showSRSI  and srsi3B ? 1 : 0) + (showMACD  and macd3B ? 1 : 0) + (showVFI   and vfi3B  ? 1 : 0) + (showOBV   and obv3B  ? 1 : 0) + (showATR   and atr3B  ? 1 : 0) + (showUO    and uo3B   ? 1 : 0) + (showEMA   and ema3B  ? 1 : 0)
bull4 = (showRSI   and rsi4B  ? 1 : 0) + (showCCI   and cci4B  ? 1 : 0) + (showMFI   and mfi4B  ? 1 : 0) + (showStoch and sto4B  ? 1 : 0) + (showSRSI  and srsi4B ? 1 : 0) + (showMACD  and macd4B ? 1 : 0) + (showVFI   and vfi4B  ? 1 : 0) + (showOBV   and obv4B  ? 1 : 0) + (showATR   and atr4B  ? 1 : 0) + (showUO    and uo4B   ? 1 : 0) + (showEMA   and ema4B  ? 1 : 0)
 
bear1 = enabledCount - bull1
bear2 = enabledCount - bull2
bear3 = enabledCount - bull3
bear4 = enabledCount - bull4
 
// Effective threshold (can't exceed enabled count)
int effThresh = math.min(alert_thresh, enabledCount)
 
consensusColor(int b, int br) =>
    b >= effThresh ? color.green : br >= effThresh ? color.red : color.gray
 
// ==========================================
// CONFLUENCE
// ==========================================
bool isBull1 = bull1 >= effThresh
bool isBull2 = bull2 >= effThresh
bool isBull3 = bull3 >= effThresh
bool isBull4 = bull4 >= effThresh
 
bool isBear1 = bear1 >= effThresh
bool isBear2 = bear2 >= effThresh
bool isBear3 = bear3 >= effThresh
bool isBear4 = bear4 >= effThresh
 
int bullTfCount = (isBull1 ? 1 : 0) + (isBull2 ? 1 : 0) + (isBull3 ? 1 : 0) + (isBull4 ? 1 : 0)
int bearTfCount = (isBear1 ? 1 : 0) + (isBear2 ? 1 : 0) + (isBear3 ? 1 : 0) + (isBear4 ? 1 : 0)
 
int requiredTfs = switch confMode
    "All TFs (4/4)"  => 4
    "Majority (3/4)" => 3
    => 1
 
bool confluenceBull = bullTfCount >= requiredTfs
bool confluenceBear = bearTfCount >= requiredTfs
 
bool noMixedBull = not confRequireSameDir or bearTfCount == 0
bool noMixedBear = not confRequireSameDir or bullTfCount == 0
 
bool longConfluence  = confluenceBull and noMixedBull
bool shortConfluence = confluenceBear and noMixedBear
 
longConfluenceSignal  = longConfluence  and not longConfluence[1]
shortConfluenceSignal = shortConfluence and not shortConfluence[1]
longConfluenceStrong  = longConfluence  and longConfluence[1]  and not longConfluence[2]
shortConfluenceStrong = shortConfluence and shortConfluence[1] and not shortConfluence[2]
 
// ==========================================
// LADDER STRUCTURE
// ==========================================
type ladderRow
    int    tfIdx
    int    engIdx
    float  value
    float  norm
    bool   rising
    string lbl
 
getEnabledEngines() =>
    array<int> res = array.new_int()
    if showRSI
        array.push(res, 0)
    if showCCI
        array.push(res, 1)
    if showMFI
        array.push(res, 2)
    if showStoch
        array.push(res, 3)
    if showSRSI
        array.push(res, 4)
    if showMACD
        array.push(res, 5)
    if showVFI
        array.push(res, 6)
    if showOBV
        array.push(res, 7)
    if showATR
        array.push(res, 8)
    if showUO
        array.push(res, 9)
    if showEMA
        array.push(res, 10)
    res
 
engineLabel(int idx) =>
    switch idx
        0  => "RSI"
        1  => "CCI"
        2  => "MFI"
        3  => "Stoch"
        4  => "StRSI"
        5  => "MACD"
        6  => "VFI"
        7  => "OBV"
        8  => "ATR"
        9  => "UO"
        10 => "EMA"
        =>   ""
 
tfLabel(int tfIdx) => "TF" + str.tostring(tfIdx + 1)
getBull(int tfIdx) => tfIdx == 0 ? bull1 : tfIdx == 1 ? bull2 : tfIdx == 2 ? bull3 : bull4
getBear(int tfIdx) => tfIdx == 0 ? bear1 : tfIdx == 1 ? bear2 : tfIdx == 2 ? bear3 : bear4
getBullFlag(int tfIdx) => tfIdx == 0 ? isBull1 : tfIdx == 1 ? isBull2 : tfIdx == 2 ? isBull3 : isBull4
 
getEngineTF(int tfIdx, int engIdx) =>
    float v = 0.0
    bool r = false
    if tfIdx == 0
        if engIdx == 0
            v := rsi1V, r := rsi1R
        else if engIdx == 1
            v := cci1V, r := cci1R
        else if engIdx == 2
            v := mfi1V, r := mfi1R
        else if engIdx == 3
            v := sto1V, r := sto1R
        else if engIdx == 4
            v := srsi1V, r := srsi1R
        else if engIdx == 5
            v := macd1V, r := macd1R
        else if engIdx == 6
            v := vfi1V, r := vfi1R
        else if engIdx == 7
            v := obv1V, r := obv1R
        else if engIdx == 8
            v := atr1V, r := atr1R
        else if engIdx == 9
            v := uo1V, r := uo1R
        else if engIdx == 10
            v := ema1V, r := ema1R
    else if tfIdx == 1
        if engIdx == 0
            v := rsi2V, r := rsi2R
        else if engIdx == 1
            v := cci2V, r := cci2R
        else if engIdx == 2
            v := mfi2V, r := mfi2R
        else if engIdx == 3
            v := sto2V, r := sto2R
        else if engIdx == 4
            v := srsi2V, r := srsi2R
        else if engIdx == 5
            v := macd2V, r := macd2R
        else if engIdx == 6
            v := vfi2V, r := vfi2R
        else if engIdx == 7
            v := obv2V, r := obv2R
        else if engIdx == 8
            v := atr2V, r := atr2R
        else if engIdx == 9
            v := uo2V, r := uo2R
        else if engIdx == 10
            v := ema2V, r := ema2R
    else if tfIdx == 2
        if engIdx == 0
            v := rsi3V, r := rsi3R
        else if engIdx == 1
            v := cci3V, r := cci3R
        else if engIdx == 2
            v := mfi3V, r := mfi3R
        else if engIdx == 3
            v := sto3V, r := sto3R
        else if engIdx == 4
            v := srsi3V, r := srsi3R
        else if engIdx == 5
            v := macd3V, r := macd3R
        else if engIdx == 6
            v := vfi3V, r := vfi3R
        else if engIdx == 7
            v := obv3V, r := obv3R
        else if engIdx == 8
            v := atr3V, r := atr3R
        else if engIdx == 9
            v := uo3V, r := uo3R
        else if engIdx == 10
            v := ema3V, r := ema3R
    else if tfIdx == 3
        if engIdx == 0
            v := rsi4V, r := rsi4R
        else if engIdx == 1
            v := cci4V, r := cci4R
        else if engIdx == 2
            v := mfi4V, r := mfi4R
        else if engIdx == 3
            v := sto4V, r := sto4R
        else if engIdx == 4
            v := srsi4V, r := srsi4R
        else if engIdx == 5
            v := macd4V, r := macd4R
        else if engIdx == 6
            v := vfi4V, r := vfi4R
        else if engIdx == 7
            v := obv4V, r := obv4R
        else if engIdx == 8
            v := atr4V, r := atr4R
        else if engIdx == 9
            v := uo4V, r := uo4R
        else if engIdx == 10
            v := ema4V, r := ema4R
    [v, r]
 
buildLadder() =>
    int[] eIdx = getEnabledEngines()
    int nE = array.size(eIdx)
    rowArr = array.new<ladderRow>()
 
    tfOrder = array.new<int>()
    if fastTop
        array.push(tfOrder, 0), array.push(tfOrder, 1), array.push(tfOrder, 2), array.push(tfOrder, 3)
    else
        array.push(tfOrder, 3), array.push(tfOrder, 2), array.push(tfOrder, 1), array.push(tfOrder, 0)
 
    for t in tfOrder
        if showHeader
            int bc = getBull(t)
            int br = getBear(t)
            string marker = getBullFlag(t) ? "  ●" : br >= effThresh ? "  ○" : ""
            string hLbl = tfLabel(t) + "  " + str.tostring(bc) + "/" + str.tostring(enabledCount) + marker
            float hv = enabledCount > 0 ? (bc - enabledCount / 2.0) / (enabledCount / 2.0) * 100.0 : 0.0
            array.push(rowArr, ladderRow.new(t, -1, hv, hv, bc >= effThresh, hLbl))
        for j = 0 to nE - 1
            ei = array.get(eIdx, j)
            [mV, mR] = getEngineTF(t, ei)
            float nrm = normalize(mV, ei)
            array.push(rowArr, ladderRow.new(t, ei, mV, nrm, mR, engineLabel(ei)))
    rowArr
 
f_dominant(rows) =>
    float mx = 0.0
    int idx = 0
    for i = 0 to array.size(rows) - 1
        r = array.get(rows, i)
        if r.engIdx != -1
            v = math.abs(r.norm)
            if v > mx
                mx := v
                idx := i
    [mx, idx]
 
// ==========================================
// ==========================================
// ==========================================
// DRAW LADDER — history trail (one ladder per bar)
// ==========================================
var box[] barBoxes = array.new_box()
 
drawLadder() =>
    n  = bar_index
    ts = getTextSize(textSizeInp)
    rows = buildLadder()
    nR = array.size(rows)
 
    if nR > 0
        // New bar → forget previous bar's boxes (they remain visible on chart;
        // we just release our references so max_boxes_count can recycle them).
        if barstate.isnew
            array.clear(barBoxes)
 
        // Real-time tick on the current bar → delete its old boxes, redraw fresh.
        if barstate.islast and array.size(barBoxes) > 0
            for b in barBoxes
                box.delete(b)
            array.clear(barBoxes)
 
        // --- draw rows ---
        r    = math.max(high - low, syminfo.mintick * 8)
        rowH = r / nR
        vPad = rowH * vPadPct
        [domVal, domIdx] = f_dominant(rows)
 
        bandThresh = domVal * bandPct
        int bandCount = 0
        for i = 0 to nR - 1
            rr = array.get(rows, i)
            if rr.engIdx != -1 and math.abs(rr.norm) >= bandThresh and bandThresh > 0
                bandCount += 1
        int bandShown = 0
 
        for i = 0 to nR - 1
            row = array.get(rows, i)
            top = high - i * rowH
            btm = top - rowH + (rowH * 0.06)
            yT  = top - vPad
            yB  = btm + vPad
 
            isHeader = row.engIdx == -1
            baseCol  = row.rising ? color.green : color.red
            color cellCol = na
            if isHeader
                int bc = getBull(row.tfIdx)
                int br = getBear(row.tfIdx)
                cellCol := color.new(consensusColor(bc, br), panelTransp)
            else
                mag = domVal > 0 ? math.min(math.abs(row.norm) / domVal, 1.0) : 0.5
                cellCol := color.from_gradient(mag, 0.0, 1.0, color.new(baseCol, 80), color.new(baseCol, qtyTransp))
 
            isDom = showDomTerm and not isHeader and i == domIdx and domVal > 0
            if isDom
                cellCol := color.new(domColor, 30)
 
            inBand = showBand and not isHeader and not isDom and bandShown < bandCount and math.abs(row.norm) >= bandThresh and bandThresh > 0
            if inBand
                bandShown += 1
 
            string critMark = ""
            if showCrit and not isHeader and math.abs(row.norm) >= critThresh
                critMark := row.rising ? "  ▲" : "  ▼"
 
            int bc = getBull(row.tfIdx)
            int br = getBear(row.tfIdx)
            color borderCol = consensusColor(bc, br)
            int borderW = isHeader ? 2 : 1
            if inBand and bandOverrides
                borderCol := bandBorderCol
                borderW := 2
 
            string rowTxt = row.lbl
            if not isHeader
                rowTxt := row.lbl + " " + str.tostring(f_trunc(row.value, 1)) + (row.rising ? " ↑" : " ↓") + critMark
 
            b = box.new(n, yT, n + 1, yB,
                 text         = rowTxt,
                 text_color   = isDom ? color.black : color.white,
                 text_size    = ts,
                 bgcolor      = cellCol,
                 border_color = borderCol,
                 border_width = borderW)
            array.push(barBoxes, b)
 
// History trail: draw on every confirmed bar + on the live bar (redraws on each tick)
if showLadder and barstate.isconfirmed //or barstate.islast
    drawLadder()
// ==========================================
// ALERTS
// ==========================================
buy  = isBull1
sell = isBear1
long_       = buy  and not buy[1]
stronglong  = buy  and buy[1] and not buy[2]
short_      = sell and not sell[1]
strongshort = sell and sell[1] and not sell[2]
 
alertcondition(buy,         title="TF1 Bullish Alignment", message="11E-ISF-MTF: TF1 Bullish Consensus")
alertcondition(sell,        title="TF1 Bearish Alignment", message="11E-ISF-MTF: TF1 Bearish Consensus")
alertcondition(long_,       title="long",                  message="11E-ISF-MTF: Long (TF1)")
alertcondition(stronglong,  title="stronglong",            message="11E-ISF-MTF: Strong Long (TF1)")
alertcondition(short_,      title="short",                 message="11E-ISF-MTF: Short (TF1)")
alertcondition(strongshort, title="strongshort",           message="11E-ISF-MTF: Strong Short (TF1)")
 
alertcondition(longConfluence,        title="Confluence Bullish",     message="11E-ISF-MTF: Multi-TF Bullish Confluence")
alertcondition(shortConfluence,       title="Confluence Bearish",     message="11E-ISF-MTF: Multi-TF Bearish Confluence")
alertcondition(longConfluenceSignal,  title="Confluence Long",        message="11E-ISF-MTF: Multi-TF Long Signal")
alertcondition(longConfluenceStrong,  title="Confluence Strong Long", message="11E-ISF-MTF: Multi-TF Strong Long")
alertcondition(shortConfluenceSignal, title="Confluence Short",       message="11E-ISF-MTF: Multi-TF Short Signal")
alertcondition(shortConfluenceStrong, title="Confluence Strong Short",message="11E-ISF-MTF: Multi-TF Strong Short")
 
if longConfluence
    alert("🟢 CONFLUENCE BULL (" + timeframe.period + "): " + str.tostring(bullTfCount) + "/4 TFs aligned bullish! Price: " + str.tostring(close, "#.##"), alert.freq_once_per_bar_close)
if shortConfluence
    alert("🔴 CONFLUENCE BEAR (" + timeframe.period + "): " + str.tostring(bearTfCount) + "/4 TFs aligned bearish! Price: " + str.tostring(close, "#.##"), alert.freq_once_per_bar_close)
if longConfluenceSignal
    alert("🟢🟢 CONFLUENCE LONG ENTRY (" + timeframe.period + "): First bar of multi-TF bullish agreement. Entry: " + str.tostring(close, "#.##"), alert.freq_once_per_bar_close)
if longConfluenceStrong
    alert("🟢🟢🟢 CONFLUENCE STRONG LONG (" + timeframe.period + "): Confirmed 2-bar multi-TF bullish. Entry: " + str.tostring(close, "#.##"), alert.freq_once_per_bar_close)
if shortConfluenceSignal
    alert("🔴🔴 CONFLUENCE SHORT ENTRY (" + timeframe.period + "): First bar of multi-TF bearish agreement. Entry: " + str.tostring(close, "#.##"), alert.freq_once_per_bar_close)
if shortConfluenceStrong
    alert("🔴🔴🔴 CONFLUENCE STRONG SHORT (" + timeframe.period + "): Confirmed 2-bar multi-TF bearish. Entry: " + str.tostring(close, "#.##"), alert.freq_once_per_bar_close)

← 返回列表