MT5 terminal (logged in)
↓
MetaTrader5 Python API
↓
Python Agent (Market Watcher)
↓
Trend / Volatility / Levels / Candle Logic
- MT5 does NOT push data outward
- Python pulls data from MT5
- This is stable, fast, and widely used in algo trading
https://www.python.org/downloads/windows/
Download Python version 3.10.11. 64 bit. Other versions don't work. MT5 is way behind technology.
Code: Select all
https://www.python.org/ftp/python/3.10.11/python-3.10.11-amd64.exe- Version: Python 3.9 – 3.11
Check “Add Python to PATH”
Code: Select all
python --versionCode: Select all
pip install MetaTrader5 pandas numpyIn MT5:
- Open MT5
- Login to your trading account
- Go to:
Code: Select all
Tools → Options → Expert Advisors - Enable:
Allow algorithmic trading
Allow WebRequest (optional for later)
- Keep MT5 OPEN (Python connects to the running terminal)
Minimal Connection Test
Code: Select all
import MetaTrader5 as mt5
# Initialize MT5 connection
if not mt5.initialize():
print("MT5 initialize failed")
mt5.shutdown()
else:
print("MT5 connected successfully")
# Check account info
account = mt5.account_info()
print(account)
mt5.shutdown()Fetch Candles (OHLC)
i
Code: Select all
mport MetaTrader5 as mt5
import pandas as pd
mt5.initialize()
SYMBOL = "XAUUSD"
TIMEFRAME = mt5.TIMEFRAME_M5 # 5-minute candles
BARS = 300
rates = mt5.copy_rates_from_pos(SYMBOL, TIMEFRAME, 0, BARS)
df = pd.DataFrame(rates)
df['time'] = pd.to_datetime(df['time'], unit='s')
print(df.tail())
mt5.shutdown()
- Open
- High
- Low
- Close
- Volume
- Time