MTF Trend Dashboard

msalh295 · study · 102 行 · 点赞 348 · TradingView 原页

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

Pine Script

//@version=6
indicator("MTF Trend Dashboard", shorttitle = "MTF Trend", overlay = true)
 
// ───────── Inputs ─────────
fastLen     = input.int(20, "Fast EMA", minval = 1, group = "Trend")
slowLen     = input.int(50, "Slow EMA", minval = 1, group = "Trend")
rsiLen      = input.int(14, "RSI Length", minval = 1, group = "Trend")
showSignals = input.bool(true, "Show EMA cross signals", group = "Trend")
 
tf1 = input.timeframe("5",   "Timeframe 1", group = "Dashboard")
tf2 = input.timeframe("15",  "Timeframe 2", group = "Dashboard")
tf3 = input.timeframe("60",  "Timeframe 3", group = "Dashboard")
tf4 = input.timeframe("240", "Timeframe 4", group = "Dashboard")
tf5 = input.timeframe("D",   "Timeframe 5", group = "Dashboard")
tablePos  = input.string("Top Right", "Dashboard position", options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group = "Dashboard")
tableSize = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal"], group = "Dashboard")
 
// ───────── Trend logic ─────────
// 1 = bullish (price > fast EMA > slow EMA and RSI > 50)
// -1 = bearish (price < fast EMA < slow EMA and RSI < 50)
// 0 = neutral
trendState() =>
    f = ta.ema(close, fastLen)
    s = ta.ema(close, slowLen)
    r = ta.rsi(close, rsiLen)
    int st = 0
    if close > f and f > s and r > 50
        st := 1
    else if close < f and f < s and r < 50
        st := -1
    [st, r]
 
[s1, r1] = request.security(syminfo.tickerid, tf1, trendState())
[s2, r2] = request.security(syminfo.tickerid, tf2, trendState())
[s3, r3] = request.security(syminfo.tickerid, tf3, trendState())
[s4, r4] = request.security(syminfo.tickerid, tf4, trendState())
[s5, r5] = request.security(syminfo.tickerid, tf5, trendState())
 
score   = nz(s1) + nz(s2) + nz(s3) + nz(s4) + nz(s5)
allBull = score == 5
allBear = score == -5
 
// ───────── Chart plots ─────────
fastEma    = ta.ema(close, fastLen)
slowEma    = ta.ema(close, slowLen)
[stNow, _] = trendState()
 
trendCol = stNow == 1 ? color.teal : stNow == -1 ? color.red : color.gray
p1 = plot(fastEma, "Fast EMA", trendCol, 2)
p2 = plot(slowEma, "Slow EMA", color.new(trendCol, 40), 1)
fill(p1, p2, color.new(trendCol, 85), title = "EMA cloud")
 
bullCross = ta.crossover(fastEma, slowEma)
bearCross = ta.crossunder(fastEma, slowEma)
plotshape(showSignals and bullCross, "Bullish cross", shape.triangleup,   location.belowbar, color.teal, size = size.small)
plotshape(showSignals and bearCross, "Bearish cross", shape.triangledown, location.abovebar, color.red,  size = size.small)
 
// ───────── Alerts ─────────
alertcondition(bullCross, "Bullish EMA cross", "{{ticker}}: bullish EMA crossover on {{interval}}")
alertcondition(bearCross, "Bearish EMA cross", "{{ticker}}: bearish EMA crossover on {{interval}}")
alertcondition(allBull and not allBull[1], "All timeframes bullish", "{{ticker}}: all dashboard timeframes are bullish")
alertcondition(allBear and not allBear[1], "All timeframes bearish", "{{ticker}}: all dashboard timeframes are bearish")
 
// ───────── Dashboard ─────────
tPos = switch tablePos
    "Top Left"     => position.top_left
    "Bottom Right" => position.bottom_right
    "Bottom Left"  => position.bottom_left
    => position.top_right
 
tSize = switch tableSize
    "Tiny"   => size.tiny
    "Normal" => size.normal
    => size.small
 
var table dash = table.new(tPos, 3, 7, bgcolor = color.new(#0b1220, 10), border_color = color.new(color.gray, 70), border_width = 1)
 
tfName(string tf) =>
    sec = timeframe.in_seconds(tf)
    sec < 3600 ? str.tostring(math.round(sec / 60)) + "m" : sec < 86400 ? str.tostring(math.round(sec / 3600)) + "H" : tf
 
fillRow(int row, string tf, int st, float r) =>
    txt = st == 1 ? "▲ Bull" : st == -1 ? "▼ Bear" : "• Neutral"
    bg  = st == 1 ? color.new(color.teal, 30) : st == -1 ? color.new(color.red, 30) : color.new(color.gray, 60)
    table.cell(dash, 0, row, tfName(tf), text_color = color.white, text_size = tSize)
    table.cell(dash, 1, row, txt, bgcolor = bg, text_color = color.white, text_size = tSize)
    table.cell(dash, 2, row, na(r) ? "-" : str.tostring(r, "#.0"), text_color = color.white, text_size = tSize)
 
if barstate.islast
    table.cell(dash, 0, 0, "TF",    text_color = color.silver, text_size = tSize)
    table.cell(dash, 1, 0, "Trend", text_color = color.silver, text_size = tSize)
    table.cell(dash, 2, 0, "RSI",   text_color = color.silver, text_size = tSize)
    fillRow(1, tf1, s1, r1)
    fillRow(2, tf2, s2, r2)
    fillRow(3, tf3, s3, r3)
    fillRow(4, tf4, s4, r4)
    fillRow(5, tf5, s5, r5)
    bias   = score >= 3 ? "Bullish bias" : score <= -3 ? "Bearish bias" : "Mixed"
    biasBg = score >= 3 ? color.new(color.teal, 20) : score <= -3 ? color.new(color.red, 20) : color.new(color.gray, 50)
    table.cell(dash, 0, 6, "Score " + str.tostring(score) + "/5", text_color = color.white, text_size = tSize)
    table.cell(dash, 1, 6, bias, bgcolor = biasBg, text_color = color.white, text_size = tSize)
    table.merge_cells(dash, 1, 6, 2, 6)

← 返回列表