Okay, this is a big one for MT5 traders. Let's do it properly.
In Python, the "Merge Tool" usually means 2 things:
1. *Merge MT5 data from different symbols / timeframes into one DataFrame*
2. *Plot it as a clean graph*
Here is a complete working setup:
*Step 1: Install*
pip install MetaTrader5 pandas matplotlib mplfinance
*Step 2: Full Code - Get MT5 Data + Merge + Plot*
import MetaTrader5 as mt5
import pandas as pd
import mplfinance as mpf
from datetime import datetime
# 1. CONNECT TO MT5
if not mt5.initialize():
print("MT5 failed, open MT5 terminal first")
quit()
# 2. FUNCTION TO GET DATA
def get_data(symbol, timeframe, bars=500):
rates = mt5.copy_rates_from_pos(symbol, timeframe, 0, bars)
df = pd.DataFrame(rates)
df['time'] = pd.to_datetime(df['time'], unit='s')
df.set_index('time', inplace=True)
df.rename(columns={'tick_volume': 'volume'}, inplace=True)
return df
# 3. GET 2 DIFFERENT DATA SETS
# Example: XAUUSD M1 + XAUUSD H1
df_m1 = get_data("XAUUSD", mt5.TIMEFRAME_M1, 500)
df_h1 = get_data("XAUUSD", mt5.TIMEFRAME_H1, 100)
# 4. MERGE TOOL - Merge M1 and H1 on time
# We merge the H1 close as a trend filter onto M1 chart
df_h1_trend = df_h1[['close']].rename(columns={'close': 'h1_close'})
df_merged = pd.merge_asof(
df_m1.sort_index(),
df_h1_trend.sort_index(),
left_index=True,
right_index=True,
direction='backward'
)
# Add indicators
df_merged['EMA_20'] = df_merged['close'].ewm(span=20).mean()
df_merged['EMA_50'] = df_merged['close'].ewm(span=50).mean()
print(df_merged.tail())
mt5.shutdown()
# 5. PLOT GRAPH
# Create extra plots for EMAs
ap = [
mpf.make_addplot(df_merged['EMA_20'], color='blue', width=1),
mpf.make_addplot(df_merged['EMA_50'], color='orange', width=1),
mpf.make_addplot(df_merged['h1_close'], color='purple', width=1, linestyle='--')
]
mpf.plot(
df_merged.tail(200), # plot last 200 candles
type='candle',
style='charles',
title='XAUUSD M1 Merged with H1 Trend',
ylabel='Price',
volume=True,
addplot=ap,
figsize=(14, 7),
savefig='merged_chart.png'
)
print("Chart saved as merged_chart.png")
*What this does:*
- Pulls live data straight from your MT5 terminal
- Merges the H1 close price onto the M1 chart, so you can see the higher timeframe trend while scalping
- Plots candles + EMA 20 / EMA 50 + H1 trend line
*If you want to merge 2 SYMBOLS instead (e.g. XAUUSD + EURUSD):*
# Get both
gold = get_data("XAUUSD", mt5.TIMEFRAME_H1, 500)[['close']].rename(columns={'close': 'GOLD'})
eur = get_data("EURUSD", mt5.TIMEFRAME_H1, 500)[['close']].rename(columns={'close': 'EURUSD'})
# Merge on time
merged_symbols = pd.merge(gold, eur, left_index=True, right_index=True, how='inner')
# Plot correlation
merged_symbols.plot(figsize=(14,6), title='GOLD vs EURUSD - Merged')
Which merge do you want to build?
1. Same symbol, different timeframes (M1 + H1) - for trend filter
2. Different symbols together - for correlation
3. Merge MT5 data from a CSV file + live MT5
Tell me which one and your symbol, I'll give you the exact code for it.