PyMT5 — Getting Started 🚀¶
1) Requirements 🧰¶
- Windows with PowerShell.
- Python 3.13.x (project verified with 3.13.7).
- MetaTrader 5 terminal and valid account (Demo or Live).
- Network access to a gRPC gateway (default
mt5.mrpc.pro:443
).
2) Project checkout 📦¶
cd C:\Users\<YOU>\
# Download/unzip the project into C:\Users\<YOU>\PyMT5
cd C:\Users\<YOU>\PyMT5
main.py
is your entry point (a phase-based PLAYBOOK).
3) Virtual environment 🧪¶
# Create venv (one time)
python -m venv .venv
# Activate (every session)
.\.venv\Scripts\Activate.ps1
# Check version
python --version # expecting Python 3.13.x
4) Credentials & configuration (ENV) 🔐¶
This project does not hardcode credentials. They are read from environment variables (see main.py
):
MT5_LOGIN
(int)MT5_PASSWORD
(string)MT5_SERVER
(e.g.,MetaQuotes-Demo
)GRPC_SERVER
(e.g.,mt5.mrpc.pro:443
)- Optional:
TIMEOUT_SECONDS
,MT5_VERBOSE
A) Set ENV in PowerShell (session-only)¶
$env:MT5_LOGIN = "<YOUR_LOGIN>"
$env:MT5_PASSWORD = "<YOUR_PASSWORD>"
$env:MT5_SERVER = "<YOUR_MT5_SERVER>" # e.g., MetaQuotes-Demo
$env:GRPC_SERVER = "mt5.mrpc.pro:443"
$env:TIMEOUT_SECONDS = "120"
$env:MT5_VERBOSE = "1"
echo "login=$($env:MT5_LOGIN) server=$($env:MT5_SERVER) grpc=$($env:GRPC_SERVER) t=$($env:TIMEOUT_SECONDS)"
B) Use a .env
file (recommended) 📄¶
Create .env
in the project root:
MT5_LOGIN=YOUR_LOGIN
MT5_PASSWORD=YOUR_PASSWORD
MT5_SERVER=MetaQuotes-Demo
GRPC_SERVER=mt5.mrpc.pro:443
TIMEOUT_SECONDS=120
MT5_VERBOSE=1
.env
is loaded viapython-dotenv
inmain.py
. Add.env
to.gitignore
.
5) Run the project ▶️¶
python .\main.py
You should see [connect] ...
logs and phase sections (0–7). A healthy run shows server_time
, account_summary
, symbol ticks, etc.
6) Phases (fine-grained control) 🧭¶
main.py
executes demo phases. You can turn them on/off via ENV without changing code:
- PHASE0 — Connectivity & Account: connection check,
server_time
,account_summary
. - PHASE1 — Symbols & Market info: symbol params, ticks, margin/session data.
- PHASE2 — Opened state snapshot: positions/orders quick snapshot.
- PHASE3 — Calculations & dry‑run:
order_calc_*
sanity checks. - PHASE4 — Charts/Copy & DOM: optional/placeholder in current build.
- PHASE5 — History & lookups: history for
HISTORY_DAYS
, ticket lookups. - PHASE6 — Streaming (compact): short live stream (ticks + open tickets) with throttle.
- TRADE_PRESET — Safe trading preset: demo market/close/pending; can be dry‑run.
- LIVE_TEST — Developer mini‑test: see
app/playground/live_test.py
.
Toggle phases via ENV¶
# Minimal run (PHASE0 only)
$env:RUN_PHASE0 = "1"
$env:RUN_PHASE1 = "0"
$env:RUN_PHASE2 = "0"
$env:RUN_PHASE3 = "0"
$env:RUN_PHASE4 = "0"
$env:RUN_PHASE5 = "0"
$env:RUN_PHASE6 = "0"
# Make sure trading preset is off during learning
$env:RUN_TRADE_PRESET = "0" # or keep TRADE_PRESET=0 in .env
Internally
main.py
maps ENV flags to aRUN[...]
dictionary; defaults are applied if ENV not set.
7) Helpful ENV knobs ⚙️¶
SELECTED_SYMBOL
(defaultEURUSD
) — primary example symbol.ALT_SYMBOL
(defaultGBPUSD
).CRYPTO_SYMBOL
(defaultBTCUSD
).HISTORY_DAYS
(default7
).STREAM_TIMEOUT_SECONDS
(default45
).BASE_CHART_SYMBOL
— fallback symbol for pings/tick requests if your broker uses suffixes (e.g.,.m
).
8) Typical signals & what they mean 🔎¶
AttributeError('terminalInstanceGuid')
duringconnect_by_host_port
→ expected for some builds; code auto‑falls back toConnectEx/Connect
and may enter LITE mode.LITE mode: skip session/terminal handshake (pb2 missing)
→ your protobuf set lacks some methods (Ping/handshake). The project handles this and still works.- Post‑connect ping fails on
symbols_total(False)
→ temporarily skip that call in_try_post_connect_ping
inapp/core/mt5_connect_helper.py
. - Final
KeyboardInterrupt
after cancel → cosmetic on Python 3.13; catchCancelledError/KeyboardInterrupt
in the lowesttry/except
inmain.py
if you want clean logs.
9) Quick start checklist ✅¶
- Activate venv → verify
python --version
. - Provide ENV (login/password/server/GRPC) → increase
TIMEOUT_SECONDS
if needed. - First run with PHASE0 only.
- If okay → enable PHASE1–PHASE6.
- Only then try trading:
TRADE_PRESET=1
andTRADE_DRY_RUN=0
— on demo only.
10) Example .env
for development 🧾¶
MT5_LOGIN=12345678
MT5_PASSWORD=***
MT5_SERVER=MetaQuotes-Demo
GRPC_SERVER=mt5.mrpc.pro:443
TIMEOUT_SECONDS=120
MT5_VERBOSE=1
HISTORY_DAYS=7
STREAM_TIMEOUT_SECONDS=45
SELECTED_SYMBOL=EURUSD
ALT_SYMBOL=GBPUSD
CRYPTO_SYMBOL=BTCUSD
BASE_CHART_SYMBOL=EURUSD
# Phases
RUN_PHASE0=1
RUN_PHASE1=1
RUN_PHASE2=1
RUN_PHASE3=1
RUN_PHASE4=0
RUN_PHASE5=1
RUN_PHASE6=1
# Trading preset
TRADE_PRESET=0
TRADE_DRY_RUN=1
¶
MT5_LOGIN=12345678
MT5_PASSWORD=***
MT5_SERVER=MetaQuotes-Demo
GRPC_SERVER=mt5.mrpc.pro:443
TIMEOUT_SECONDS=120
MT5_VERBOSE=1
HISTORY_DAYS=7
STREAM_TIMEOUT_SECONDS=45
SELECTED_SYMBOL=EURUSD
ALT_SYMBOL=GBPUSD
CRYPTO_SYMBOL=BTCUSD
BASE_CHART_SYMBOL=EURUSD
# Phases
RUN_PHASE0=1
RUN_PHASE1=1
RUN_PHASE2=1
RUN_PHASE3=1
RUN_PHASE4=0
RUN_PHASE5=1
RUN_PHASE6=1
# Trading preset
TRADE_PRESET=0
TRADE_DRY_RUN=1
11) Run after setting .env
▶️¶
.\.venv\Scripts\Activate.ps1
python .\main.py
You should see phase sections like “0) ▶ Connectivity & Account”, “1) 📈 Symbols & Market info”, etc. If logs are clean — you’re ready to go.