| """ |
| Quick MT5 diagnostic -- run this to see exactly what the Python API can access. |
| """ |
| import MetaTrader5 as mt5 |
| from datetime import datetime, timedelta, timezone |
|
|
| print(f"MT5 package version: {mt5.__version__}") |
|
|
| |
| if not mt5.initialize(): |
| print(f"FAIL: initialize() -> {mt5.last_error()}") |
| quit() |
| print(f"OK: initialize()") |
|
|
| |
| info = mt5.terminal_info() |
| if info: |
| print(f"Terminal: {info.name}, Build {info.build}, Path: {info.path}") |
| print(f"Connected: {info.connected}, Trade allowed: {info.trade_allowed}") |
|
|
| |
| acc = mt5.account_info() |
| if acc: |
| print(f"Account: {acc.login}, Server: {acc.server}, Balance: {acc.balance}") |
|
|
| |
| sym = "XAUUSDc" |
| si = mt5.symbol_info(sym) |
| if si is None: |
| print(f"FAIL: symbol_info('{sym}') returned None -> {mt5.last_error()}") |
| |
| all_s = mt5.symbols_get() |
| if all_s: |
| gold = [s.name for s in all_s if "XAU" in s.name.upper() or "GOLD" in s.name.upper()] |
| print(f"Gold-related symbols found: {gold}") |
| mt5.shutdown() |
| quit() |
|
|
| print(f"OK: symbol_info('{sym}') -> visible={si.visible}, spread={si.spread}, bid={si.bid}") |
|
|
| |
| mt5.symbol_select(sym, True) |
|
|
| |
| utc_to = datetime(2026, 1, 1, tzinfo=timezone.utc) |
| utc_from = datetime(2025, 1, 1, tzinfo=timezone.utc) |
|
|
| print(f"\nFetching {sym} from {utc_from} to {utc_to}...") |
|
|
| |
| for tf_name, tf_val in [("M1", mt5.TIMEFRAME_M1), ("M3", mt5.TIMEFRAME_M3), ("M5", mt5.TIMEFRAME_M5)]: |
| rates = mt5.copy_rates_range(sym, tf_val, utc_from, utc_to) |
| if rates is not None and len(rates) > 0: |
| print(f" copy_rates_range {tf_name}: {len(rates):,} bars") |
| else: |
| print(f" copy_rates_range {tf_name}: NONE -> {mt5.last_error()}") |
|
|
| |
| for tf_name, tf_val in [("M1", mt5.TIMEFRAME_M1), ("M3", mt5.TIMEFRAME_M3)]: |
| rates = mt5.copy_rates_from_pos(sym, tf_val, 0, 100) |
| if rates is not None and len(rates) > 0: |
| print(f" copy_rates_from_pos {tf_name} (last 100): {len(rates)} bars") |
| else: |
| print(f" copy_rates_from_pos {tf_name}: NONE -> {mt5.last_error()}") |
|
|
| mt5.shutdown() |
| print("\nDone.") |
|
|