本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// =============================================================================
// Multi-Timeframe Trend Confluence — Portfolio Sample
// -----------------------------------------------------------------------------
// Overlays the trend state (bull / bear / neutral) from three higher timeframes
// onto any chart. Useful as a confluence filter: only take trades in the
// direction all three HTFs agree on.
//
// Non-repainting (uses request.security with lookahead_off and bar close).
// Works on free TradingView accounts. Alert-ready.
//
// Author: LG
// =============================================================================
indicator(
title = "MTF Trend Confluence",
shorttitle = "MTF Trend",
overlay = true,
max_labels_count = 50)
// =============================================================================
// Inputs
// =============================================================================
grpTF = "Timeframes"
tf1 = input.timeframe("60", "Timeframe 1", group = grpTF, tooltip = "First higher timeframe to check trend on, e.g. 60 for 1-hour")
tf2 = input.timeframe("240", "Timeframe 2", group = grpTF, tooltip = "Second HTF, e.g. 240 for 4-hour")
tf3 = input.timeframe("D", "Timeframe 3", group = grpTF, tooltip = "Third HTF, e.g. D for daily")
grpSig = "Trend signal"
emaLen = input.int(50, "EMA length", minval = 2, group = grpSig, tooltip = "A bar is 'bullish' on an HTF when its close is above this EMA and the EMA is rising")
grpViz = "Visuals"
showTable = input.bool(true, "Show status table", group = grpViz)
showBg = input.bool(true, "Shade background", group = grpViz, tooltip = "Green when all three HTFs agree bullish; red when all three agree bearish")
labelOnFlip = input.bool(false, "Label on confluence flip", group = grpViz)
grpAlerts = "Alerts"
alertOnFlip = input.bool(true, "Fire alert on confluence flip", group = grpAlerts)
// =============================================================================
// Non-repainting HTF trend check
// =============================================================================
f_trend(tf) =>
[c, e] = request.security(
syminfo.tickerid, tf,
[close, ta.ema(close, emaLen)],
lookahead = barmerge.lookahead_off)
priorE = ta.valuewhen(not na(e), e, 1)
emaRising = na(priorE) ? false : e > priorE
emaFalling = na(priorE) ? false : e < priorE
bull = c > e and emaRising
bear = c < e and emaFalling
bull ? 1 : (bear ? -1 : 0)
t1 = f_trend(tf1)
t2 = f_trend(tf2)
t3 = f_trend(tf3)
allBull = t1 == 1 and t2 == 1 and t3 == 1
allBear = t1 == -1 and t2 == -1 and t3 == -1
// Confluence flip detection (bar-close only, no repaint)
flipBull = allBull and not allBull[1]
flipBear = allBear and not allBear[1]
// =============================================================================
// Visuals
// =============================================================================
bgColor = showBg ? (allBull ? color.new(color.green, 90) : allBear ? color.new(color.red, 90) : na) : na
bgcolor(bgColor)
if labelOnFlip and barstate.isconfirmed
if flipBull
label.new(bar_index, low, "🟢 BULL", style = label.style_label_up,
color = color.new(color.green, 20), textcolor = color.white, size = size.small)
if flipBear
label.new(bar_index, high, "🔴 BEAR", style = label.style_label_down,
color = color.new(color.red, 20), textcolor = color.white, size = size.small)
// Status table (top-right)
f_cell(tbl, col, row, txt, bg) =>
table.cell(tbl, col, row, txt, text_color = color.white, bgcolor = bg, text_size = size.small)
f_tfLabel(trend) =>
trend == 1 ? "▲ bull" : trend == -1 ? "▼ bear" : "— flat"
f_tfColor(trend) =>
trend == 1 ? color.new(color.green, 20) : trend == -1 ? color.new(color.red, 20) : color.new(color.gray, 20)
var table tbl = na
if showTable and barstate.islast
if na(tbl)
tbl := table.new(position.top_right, 2, 4, border_width = 1)
f_cell(tbl, 0, 0, "Timeframe", color.new(color.navy, 20))
f_cell(tbl, 1, 0, "Trend", color.new(color.navy, 20))
f_cell(tbl, 0, 1, tf1, color.new(color.gray, 50))
f_cell(tbl, 1, 1, f_tfLabel(t1), f_tfColor(t1))
f_cell(tbl, 0, 2, tf2, color.new(color.gray, 50))
f_cell(tbl, 1, 2, f_tfLabel(t2), f_tfColor(t2))
f_cell(tbl, 0, 3, tf3, color.new(color.gray, 50))
f_cell(tbl, 1, 3, f_tfLabel(t3), f_tfColor(t3))
// =============================================================================
// Alerts
// =============================================================================
if alertOnFlip and barstate.isconfirmed
if flipBull
alert("MTF Trend Confluence: all " + tf1 + "/" + tf2 + "/" + tf3 + " bull on " + syminfo.ticker, alert.freq_once_per_bar_close)
if flipBear
alert("MTF Trend Confluence: all " + tf1 + "/" + tf2 + "/" + tf3 + " bear on " + syminfo.ticker, alert.freq_once_per_bar_close)
alertcondition(flipBull, title = "All-TF Bull flip", message = "{{ticker}}: MTF trend flipped bull")
alertcondition(flipBear, title = "All-TF Bear flip", message = "{{ticker}}: MTF trend flipped bear")