| import MetaTrader5 as mt5
|
| import pandas as pd
|
| from datetime import datetime
|
| import json
|
|
|
|
|
| if not mt5.initialize():
|
| print("β MT5 initialize failed. Check terminal is open + logged in.")
|
| quit()
|
|
|
| symbol = "XAUUSDc"
|
| timeframe = mt5.TIMEFRAME_M3
|
|
|
|
|
| date_from = datetime(2025, 1, 1)
|
| date_to = datetime.now()
|
|
|
| print(f"π Fetching {symbol} M3 from {date_from.date()} to {date_to.date()} (UTC)...")
|
|
|
| rates = mt5.copy_rates_range(symbol, timeframe, date_from, date_to)
|
|
|
|
|
| if rates is None:
|
| print("β MT5 returned None. Possible reasons:")
|
| print(" β’ Symbol does NOT exist on your broker")
|
| print(" β’ History not downloaded (open chart + scroll back 1 year)")
|
| print(" β’ Date range has no bars")
|
| mt5.shutdown()
|
| quit()
|
|
|
| if len(rates) == 0:
|
| print("β No bars returned (0 rows).")
|
| print(" Try changing symbol to 'XAUUSD' (without 'c') or download history in MT5.")
|
| mt5.shutdown()
|
| quit()
|
|
|
| print(f"β
Fetched {len(rates)} bars successfully!")
|
|
|
|
|
| df = pd.DataFrame(rates)
|
|
|
|
|
| print(f"Columns returned by MT5: {list(df.columns)}")
|
|
|
|
|
| df['time'] = pd.to_datetime(df['time'], unit='s')
|
|
|
|
|
| df = df[['time', 'open', 'high', 'low', 'close', 'tick_volume', 'spread', 'real_volume']]
|
|
|
|
|
| csv_path = "xauusd_3m_2025_2026.csv"
|
| df.to_csv(csv_path, index=False)
|
| print(f"πΎ Saved {len(df)} bars to β {csv_path}")
|
|
|
|
|
| info = mt5.symbol_info(symbol)
|
| if info is None:
|
| print("β οΈ symbol_info failed. Using fallback values.")
|
| params = {"tick_size": 0.01, "tick_value": 1.0, "volume_min": 0.01, "volume_max": 200.0,
|
| "volume_step": 0.01, "point": 0.01, "trade_calc_mode": 0}
|
| else:
|
| params = {
|
| "tick_size": info.trade_tick_size,
|
| "tick_value": info.trade_tick_value,
|
| "volume_min": info.volume_min,
|
| "volume_max": info.volume_max,
|
| "volume_step": info.volume_step,
|
| "point": info.point,
|
| "trade_calc_mode": info.trade_calc_mode
|
| }
|
|
|
| with open("symbol_params.json", "w") as f:
|
| json.dump(params, f, indent=2)
|
| print("πΎ Saved symbol_params.json")
|
|
|
| mt5.shutdown()
|
| print("\nπ DONE! Now upload BOTH files to Google Colab:")
|
| print(" 1. xauusd_3m_2025_2026.csv")
|
| print(" 2. symbol_params.json") |