Algorithmic Trading & Indicators Intermediate

Multi-Timeframe Trend Confluence Matrix (5m, 15m, 1h, Daily) in Python

Build an automated multi-timeframe trend alignment matrix in Python Pandas confirming 200 EMA direction across 5m, 15m, 1h, and Daily charts.

#pandas#numpy

Problem Statement

Trading against higher timeframe trends results in frequent stop-outs. This script merges 5-minute, 15-minute, 1-hour, and Daily EMAs into a single confluence matrix.

Formula / Core Logic

Confluence Score = Sum(Trend_5m + Trend_15m + Trend_1h + Trend_Daily) ranging from -4 to +4.

Installation

pip install pandas numpy
multi-timeframe-confluence-matrix-python.py
import pandas as pd
import numpy as np
\.def multi_timeframe_matrix(df_5m):
    df_15m = df_5m.resample('15min').agg({'Close': 'last'})
    df_1h = df_5m.resample('1h').agg({'Close': 'last'})
    
    df_5m['EMA_200'] = df_5m['Close'].ewm(span=200).mean()
    df_15m['EMA_50'] = df_15m['Close'].ewm(span=50).mean()
    df_1h['EMA_20'] = df_1h['Close'].ewm(span=20).mean()
    
    # Align back to 5m
    res = df_5m.copy()
    res['Trend_5m'] = np.where(res['Close'] > res['EMA_200'], 1, -1)
    res['Trend_15m'] = np.where(res['Close'] > res['EMA_50'].reindex(res.index, method='ffill'), 1, -1)
    res['Trend_1h'] = np.where(res['Close'] > res['EMA_20'].reindex(res.index, method='ffill'), 1, -1)
    res['Total_Confluence'] = res['Trend_5m'] + res['Trend_15m'] + res['Trend_1h']
    return res

Get the Complete 100+ Python Quant & Automation Bundle

Instant lifetime access to all production-ready scripts, valuation models, and trading bots ($19).

Buy Full Bundle on Gumroad ($19) →

Frequently Asked Questions

Q: Why is higher timeframe alignment critical for intraday traders?

Higher timeframes represent institutional order flow; trading in harmony with 1h and Daily trends significantly increases win rates.