MetaTrader5

raw JSON →
5.0.5735 verified Mon Apr 27 auth: no python

Python interface for connecting to and trading with MetaTrader 5 (MT5) terminal. Current version 5.0.5735; actively maintained with frequent updates mirroring MT5 build changes.

pip install metatrader5
error ModuleNotFoundError: No module named 'metatrader5'
cause Package not installed or installed with wrong Python version (MT5 requires 32-bit Python on Windows).
fix
Install with: pip install metatrader5. Ensure you are using a 32-bit Python interpreter on Windows (check with python -c 'import struct; print(struct.calcsize("P") * 8)')
error mt5.initialize() returns False
cause MT5 terminal not running, not logged in, or wrong terminal build.
fix
Launch MetaTrader 5 terminal, log in to your broker, and ensure the terminal is not minimized (keep it open). Check path with mt5.initialize(path='C:/Program Files/MetaTrader 5/terminal64.exe')
error AttributeError: module 'MetaTrader5' has no attribute 'copy_rates_from'
cause Using old method name with capital letters (e.g., CopyRates) or outdated version.
fix
Use lowercase methods: mt5.copy_rates_from_pos, mt5.copy_rates_from_range. Update with: pip install --upgrade metatrader5
breaking In version 5.0.5735, the return type of mt5.copy_rates_from_pos changed from tuple to numpy array. Code expecting list or tuple may break.
fix Ensure your code handles numpy arrays, or convert: list(mt5.copy_rates_from_pos(...))
gotcha mt5.initialize() requires the MT5 terminal to be already running and logged in. Fails silently if terminal is not open.
fix Always check the return value and print error with mt5.last_error() if available.
deprecated The function mt5.CopyRates (capital C) was deprecated in favor of mt5.copy_rates_from_pos and mt5.copy_rates_from_range.
fix Use mt5.copy_rates_from_pos or mt5.copy_rates_from_range with lowercase.

Initialize connection to MT5 terminal (must be running on local machine).

import MetaTrader5 as mt5
if not mt5.initialize():
    print("initialize() failed")
    mt5.shutdown()
else:
    print("MetaTrader5 version:", mt5.__version__)
    mt5.shutdown()