File size: 2,847 Bytes
13c3b17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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")