import MetaTrader5 as mt5 import pandas as pd from datetime import datetime import json # ==================== MT5 INITIALIZATION ==================== if not mt5.initialize(): print("āŒ MT5 initialize failed. Check terminal is open + logged in.") quit() symbol = "XAUUSDc" timeframe = mt5.TIMEFRAME_M3 # ==================== DATE RANGE (auto-safe) ==================== date_from = datetime(2025, 1, 1) date_to = datetime.now() # ← changed from fixed 2026-03-20 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) # ==================== CRITICAL SAFETY CHECKS ==================== 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!") # ==================== CREATE DATAFRAME ==================== df = pd.DataFrame(rates) # Debug columns (this will now never crash) print(f"Columns returned by MT5: {list(df.columns)}") # Convert time (MT5 always returns unix seconds in 'time' column) df['time'] = pd.to_datetime(df['time'], unit='s') # Keep only needed columns + spread df = df[['time', 'open', 'high', 'low', 'close', 'tick_volume', 'spread', 'real_volume']] # ==================== SAVE CSV ==================== csv_path = "xauusd_3m_2025_2026.csv" df.to_csv(csv_path, index=False) print(f"šŸ’¾ Saved {len(df)} bars to → {csv_path}") # ==================== SAVE SYMBOL PARAMS (for Colab) ==================== 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")