Pine Script V5 Trend Analysis Free / Open Source

Multi-Timeframe Trend

Overview

A setup that looks clean on a 5-minute chart can be directly counter to what the daily chart is doing — and lower-timeframe structure loses far more often than it wins when it's fighting the direction the higher timeframe is actually flowing. The core institutional discipline isn't a better lower-timeframe entry; it's refusing to take that entry when it runs against the macro flow.

Multi-Timeframe Trend integrates directional data from your current chart interval with a macro timeframe of your choosing — fetched directly via request.security — and only displays a directional bullish or bearish state when both agree. Any disagreement between the two is treated as noise and shown as neutral, stripping out the lower-timeframe signals that would otherwise pull you into a countertrend position.

Features of This Indicator

How to Use

  1. Copy the script into TradingView's Pine Editor and click Add to Chart.
  2. Set the Macro Timeframe input to the higher timeframe you want confluence against — Daily for an intraday chart, Weekly for a daily chart, and so on.
  3. Read the background shading and EMA color: green means both timeframes are aligned bullish, red means aligned bearish, and no shading means the timeframes disagree — treat that as a no-trade signal.
  4. Check the small macro-state label in the corner for a direct readout of the higher timeframe's independent trend, separate from the aligned signal.
Pro Tip: Even a clean lower-timeframe setup carries materially lower odds when the macro timeframe disagrees with it. Treat unaligned (neutral) readings as a cue to reduce size or stand aside entirely — not as a puzzle to solve by finding a lower-timeframe reason to override the higher-timeframe trend.

The Pine Script V5 Code

multi-timeframe-trend.pine
//@version=5
indicator("Multi-Timeframe Trend — Crypto Indicator Pro", shorttitle="MTF Trend [CIP]", overlay=true)

// ============================================================================
// MULTI-TIMEFRAME TREND
// Combines a fast/slow EMA trend read on the current chart timeframe with the
// same read computed on a higher, macro timeframe via request.security.
// Trend is only displayed as directional when both timeframes agree,
// filtering out lower-timeframe noise that runs counter to the macro flow.
// A persistence filter additionally requires several consecutive bars of
// agreement before the displayed trend state flips, further reducing
// whipsaw caused by single-bar EMA crosses.
// ============================================================================

// ---------------------------- INPUTS ----------------------------
macroTimeframe = input.timeframe("D", "Macro Timeframe", group="Multi-Timeframe")
fastLength     = input.int(21, "Fast EMA Length", minval=2, group="Trend Calculation")
slowLength     = input.int(50, "Slow EMA Length", minval=5, group="Trend Calculation")
confirmBars    = input.int(3, "Confirmation Bars (Noise Filter)", minval=1, maxval=20, group="Trend Calculation",
     tooltip="Number of consecutive agreeing bars required before the displayed trend state is allowed to flip.")
bullBg         = input.color(color.new(color.teal, 85), "Bullish Background", group="Display")
bearBg         = input.color(color.new(color.red, 85), "Bearish Background", group="Display")

// ---------------------------- SHARED TREND FUNCTION (used on both timeframes) ----------------------------
f_trendData() =>
    emaFast = ta.ema(close, fastLength)
    emaSlow = ta.ema(close, slowLength)
    trend = emaFast > emaSlow ? 1 : emaFast < emaSlow ? -1 : 0
    [emaFast, emaSlow, trend]

// ---------------------------- CURRENT TIMEFRAME TREND ----------------------------
[emaFastPlot, emaSlowPlot, currentTrend] = f_trendData()

// ---------------------------- MACRO TIMEFRAME TREND (via request.security) ----------------------------
// gaps_off + lookahead_off ensure no future-bar lookahead and no repainting of the current bar.
[macroFast, macroSlow, macroTrend] = request.security(syminfo.tickerid, macroTimeframe, f_trendData(),
     gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)

// ---------------------------- MULTI-TIMEFRAME ALIGNMENT ----------------------------
// Directional only when both the current and macro timeframe agree; otherwise neutral (noise/conflict).
rawTrend = (currentTrend == macroTrend) ? currentTrend : 0

// ---------------------------- PERSISTENCE / NOISE FILTER ----------------------------
var int displayedTrend = 0
var int pendingTrend   = 0
var int consecCount    = 0

if rawTrend != 0 and rawTrend == pendingTrend
    consecCount += 1
else
    pendingTrend := rawTrend
    consecCount := 1

if consecCount >= confirmBars and pendingTrend != 0
    displayedTrend := pendingTrend

// ---------------------------- VISUALIZATION ----------------------------
plot(emaFastPlot, "Fast EMA", color = displayedTrend == 1 ? color.teal : displayedTrend == -1 ? color.red : color.gray, linewidth=2)
plot(emaSlowPlot, "Slow EMA", color = color.new(color.gray, 40), linewidth=1)

bgcolor(displayedTrend == 1 ? bullBg : displayedTrend == -1 ? bearBg : na)

// Macro-timeframe state label, refreshed on the last bar for a quick independent readout
var label macroLabel = na
if barstate.islast
    if not na(macroLabel)
        label.delete(macroLabel)
    macroText = macroTrend == 1 ? "Macro: Bullish" : macroTrend == -1 ? "Macro: Bearish" : "Macro: Neutral"
    macroLabel := label.new(bar_index, high, macroText,
         style=label.style_label_down,
         color=macroTrend == 1 ? color.teal : macroTrend == -1 ? color.red : color.gray,
         textcolor=color.white, size=size.small)

// ---------------------------- ALERTS ----------------------------
alertcondition(displayedTrend == 1 and displayedTrend[1] != 1, title="Trend Turned Bullish", message="Multi-timeframe trend aligned bullish.")
alertcondition(displayedTrend == -1 and displayedTrend[1] != -1, title="Trend Turned Bearish", message="Multi-timeframe trend aligned bearish.")
Risk Notice: This script is provided for educational and research purposes only and does not constitute financial advice. Always backtest on a demo account before any live deployment. See our full Institutional Risk Disclaimer for details.

03 Related Modules

VOLUME PROFILE

Session Volume Profile

Maps real-time Point of Control (POC), Value Area High (VAH), and high-density volume nodes.

View Module
PINE SCRIPT V5

Automated Order Blocks

Calculates institutional order accumulation zones with real-time market structure verification.

View Module
ALGORITHMIC FLOW

Liquidity Grab Engine

Isolates structural stop-hunts, internal sweeps, and retail liquidity pools by session.

View Module

04 Related Reading

The Best Multi-Timeframe Combination for Crypto Bias (Stop Mixing Random Charts)

Category: Technical Guides
Read Guide →

How to Find Your Trading Bias in Under 10 Seconds Before Every Session

Category: Technical Guides
Read Guide →

Why Your Multi-Timeframe Analysis Keeps Contradicting Itself

Category: Technical Guides
Read Guide →

Trading Against the Higher Timeframe Trend: Why It's Costing You Money

Category: Risk Protocols
Read Guide →