MTF Trend Stack - Multi-Timeframe Resonance Table

jimlai1005 · study · 171 行 · 点赞 9 · TradingView 原页

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

Pine Script

//@version=6
// ============================================================================
//  MTF Trend Stack
//  Six-timeframe trend alignment in one table:
//    - per-timeframe state: BULL / BEAR / FLAT
//    - resonance score: bulls minus bears across the six timeframes
//    - a one-line verdict, plus alerts when all six timeframes align
//
//  The trend engine is one function (f_trendState). Replace the EMA logic with
//  Supertrend, ADX or anything else that returns 1 / -1 / 0 and the rest of
//  the script keeps working.
//
//  Repainting note: higher-timeframe states are requested with lookahead_off,
//  so historical bars never see future data. The state of a higher-timeframe
//  bar that is still open updates in real time until that bar closes.
// ============================================================================
indicator("MTF Trend Stack", shorttitle = "MTF Stack", overlay = true)
 
// ─────────────────────────────────────────────────────────────────────────
//  1. Inputs
// ─────────────────────────────────────────────────────────────────────────
string GRP_ENGINE = "Trend Engine"
int    emaFastLen = input.int(11, "EMA Fast", minval = 1, group = GRP_ENGINE)
int    emaSlowLen = input.int(29, "EMA Slow", minval = 1, group = GRP_ENGINE)
bool   useConfirm = input.bool(true, "Confirm: price vs slow EMA", group = GRP_ENGINE,
     tooltip = "On: bull needs fast EMA > slow EMA AND close > slow EMA (bear is the mirror).\n開啟:要『快EMA>慢EMA』且『收盤>慢EMA』才算多頭,反之空頭。")
float  flatPct    = input.float(0.0, "Neutral band (% of slow EMA)", minval = 0, step = 0.05, group = GRP_ENGINE,
     tooltip = "An EMA gap smaller than this % counts as FLAT (ranging). 0 = off.\n快慢EMA距離 < 此%時視為 FLAT(盤整)。設 0 = 不啟用中性帶。")
 
string GRP_TF = "Timeframes"
string tf1 = input.timeframe("5",   "TF 1", group = GRP_TF)
string tf2 = input.timeframe("15",  "TF 2", group = GRP_TF)
string tf3 = input.timeframe("60",  "TF 3", group = GRP_TF)
string tf4 = input.timeframe("240", "TF 4", group = GRP_TF)
string tf5 = input.timeframe("D",   "TF 5", group = GRP_TF)
string tf6 = input.timeframe("W",   "TF 6", group = GRP_TF)
 
string GRP_UI = "Display"
string posInp  = input.string("Top Right", "Table position",
     options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left", "Middle Right"], group = GRP_UI)
string sizeInp = input.string("Normal", "Text size",
     options = ["Tiny", "Small", "Normal", "Large"], group = GRP_UI)
bool   plotEmas = input.bool(true, "Plot current-TF EMAs (sanity check)", group = GRP_UI)
 
// ─────────────────────────────────────────────────────────────────────────
//  2. Trend engine. Returns 1 = bull, -1 = bear, 0 = flat.
//     To use another trend definition, change this function only.
// ─────────────────────────────────────────────────────────────────────────
f_trendState() =>
    emaFast = ta.ema(close, emaFastLen)
    emaSlow = ta.ema(close, emaSlowLen)
    gapPct  = math.abs(emaFast - emaSlow) / emaSlow * 100.0
    isFlat  = flatPct > 0.0 and gapPct < flatPct
    bullRaw = emaFast > emaSlow and (not useConfirm or close > emaSlow)
    bearRaw = emaFast < emaSlow and (not useConfirm or close < emaSlow)
    isFlat ? 0 : bullRaw ? 1 : bearRaw ? -1 : 0
 
f_state(simple string tf) =>
    request.security(syminfo.tickerid, tf, f_trendState(), lookahead = barmerge.lookahead_off)
 
int s1 = f_state(tf1)
int s2 = f_state(tf2)
int s3 = f_state(tf3)
int s4 = f_state(tf4)
int s5 = f_state(tf5)
int s6 = f_state(tf6)
 
// ─────────────────────────────────────────────────────────────────────────
//  3. Resonance score
// ─────────────────────────────────────────────────────────────────────────
states = array.from(s1, s2, s3, s4, s5, s6)
tfs    = array.from(tf1, tf2, tf3, tf4, tf5, tf6)
 
int bulls = 0
int bears = 0
for i = 0 to array.size(states) - 1
    st = array.get(states, i)
    bulls := bulls + (st ==  1 ? 1 : 0)
    bears := bears + (st == -1 ? 1 : 0)
int net = bulls - bears
bool fullBull = bulls == 6
bool fullBear = bears == 6
 
// ─────────────────────────────────────────────────────────────────────────
//  4. Helpers
// ─────────────────────────────────────────────────────────────────────────
f_tfLabel(string tf) =>
    switch tf
        ""    => "Chart"
        "1"   => "1m"
        "3"   => "3m"
        "5"   => "5m"
        "15"  => "15m"
        "30"  => "30m"
        "45"  => "45m"
        "60"  => "1h"
        "120" => "2h"
        "180" => "3h"
        "240" => "4h"
        "D"   => "1D"
        "W"   => "1W"
        "M"   => "1M"
        => tf
 
f_stateTxt(int s) => s ==  1 ? "BULL ▲" : s == -1 ? "BEAR ▼" : "FLAT –"
f_stateCol(int s) => s ==  1 ? color.new(color.teal, 0) : s == -1 ? color.new(color.red, 0) : color.new(color.gray, 30)
 
tblPos = switch posInp
    "Top Right"    => position.top_right
    "Top Left"     => position.top_left
    "Bottom Right" => position.bottom_right
    "Bottom Left"  => position.bottom_left
    "Middle Right" => position.middle_right
    => position.top_right
 
tblSize = switch sizeInp
    "Tiny"   => size.tiny
    "Small"  => size.small
    "Normal" => size.normal
    "Large"  => size.large
    => size.normal
 
color COL_HEAD = color.new(color.blue, 0)
color COL_DARK = color.new(color.gray, 40)
color COL_ROW  = color.new(color.gray, 70)
 
// ─────────────────────────────────────────────────────────────────────────
//  5. Table
// ─────────────────────────────────────────────────────────────────────────
var table dash = table.new(tblPos, 2, 9, border_width = 1, border_color = color.new(color.gray, 50))
 
if barstate.islast
    string verdict = fullBull ? "FULL BULL STACK 🟢" :
                     fullBear ? "FULL BEAR STACK 🔴" :
                     bulls >= 5 ? "Strong Bull"      :
                     bears >= 5 ? "Strong Bear"      :
                     bulls == bears ? "No Resonance" :
                     net > 0 ? "Lean Bull" : "Lean Bear"
    color verdictCol = bulls >= 5 ? color.new(color.teal, 0) :
                       bears >= 5 ? color.new(color.red, 0)  :
                       color.new(color.gray, 20)
    string netTxt = (net > 0 ? "+" : "") + str.tostring(net)
 
    // header
    table.cell(dash, 0, 0, syminfo.ticker, text_color = color.white, text_size = tblSize, bgcolor = COL_HEAD)
    table.cell(dash, 1, 0, "Trend",        text_color = color.white, text_size = tblSize, bgcolor = COL_HEAD)
 
    // timeframe rows
    for i = 0 to array.size(states) - 1
        st = array.get(states, i)
        tf = array.get(tfs, i)
        table.cell(dash, 0, i + 1, f_tfLabel(tf),  text_color = color.white, text_size = tblSize, bgcolor = COL_ROW)
        table.cell(dash, 1, i + 1, f_stateTxt(st), text_color = color.white, text_size = tblSize, bgcolor = f_stateCol(st))
 
    // resonance summary
    table.cell(dash, 0, 7, str.format("{0}↑ / {1}↓", bulls, bears), text_color = color.white, text_size = tblSize, bgcolor = COL_DARK)
    table.cell(dash, 1, 7, netTxt,    text_color = color.white, text_size = tblSize, bgcolor = COL_DARK)
    table.cell(dash, 0, 8, "Verdict", text_color = color.white, text_size = tblSize, bgcolor = verdictCol)
    table.cell(dash, 1, 8, verdict,   text_color = color.white, text_size = tblSize, bgcolor = verdictCol)
 
// ─────────────────────────────────────────────────────────────────────────
//  6. Optional: plot the two EMAs of the chart timeframe
// ─────────────────────────────────────────────────────────────────────────
plot(plotEmas ? ta.ema(close, emaFastLen) : na, "EMA Fast", color = color.new(color.aqua,   0))
plot(plotEmas ? ta.ema(close, emaSlowLen) : na, "EMA Slow", color = color.new(color.orange, 0))
 
// ─────────────────────────────────────────────────────────────────────────
//  7. Alerts
// ─────────────────────────────────────────────────────────────────────────
alertcondition(fullBull and not fullBull[1], "Full Bull Stack", "{{ticker}} FULL BULL STACK: all six timeframes bullish / 六時區全多頭共振")
alertcondition(fullBear and not fullBear[1], "Full Bear Stack", "{{ticker}} FULL BEAR STACK: all six timeframes bearish / 六時區全空頭共振")

← 返回列表