# # import MetaTrader5 as mt5 # # mt5.initialize() # # symbols = mt5.symbols_get() # # for s in symbols: # # if "100" in s.name: # # print(s.name) # # mt5.shutdown() # import MetaTrader5 as mt5 # import smtplib # from email.mime.text import MIMEText # from email.mime.multipart import MIMEMultipart # import json # import os # from datetime import datetime # # ---------------------------- # # Load MT5 config # # ---------------------------- # def load_config(filepath="config.json"): # if not os.path.exists(filepath): # raise FileNotFoundError(f"Config file not found: {filepath}") # with open(filepath, "r") as f: # return json.load(f) # # ---------------------------- # # Send email function # # ---------------------------- # def send_email(subject, body, smtp_config): # msg = MIMEMultipart() # msg["From"] = smtp_config["sender_email"] # msg["To"] = smtp_config["recipient_email"] # msg["Subject"] = subject # msg.attach(MIMEText(body, "plain")) # try: # with smtplib.SMTP(smtp_config["smtp_server"], smtp_config["smtp_port"]) as server: # server.starttls() # server.login(smtp_config["sender_email"], smtp_config["sender_password"]) # server.send_message(msg) # print(f"[EMAIL] Sent to {smtp_config['recipient_email']}") # except Exception as e: # print(f"[EMAIL ERROR] {e}") # # ---------------------------- # # Get daily candle # # ---------------------------- # def get_us100_daily_candle(): # symbol = "USTEC100" # NASDAQ 100 CFD symbol (check broker name may differ e.g., NAS100) # rates = mt5.copy_rates_from_pos(symbol, mt5.TIMEFRAME_D1, 0, 1) # if rates is None or len(rates) == 0: # return None # candle = rates[0] # candle_time = datetime.fromtimestamp(candle['time']).strftime('%Y-%m-%d') # return { # "date": candle_time, # "open": candle['open'], # "high": candle['high'], # "low": candle['low'], # "close": candle['close'], # "volume": candle['tick_volume'] # } # # ---------------------------- # # Main function # # ---------------------------- # def main(): # config = load_config() # # Email credentials # smtp_config = { # "smtp_server": "smtp.gmail.com", # "smtp_port": 587, # "sender_email": "ifairvaluegap@gmail.com", # Replace with your Gmail # "sender_password": "pyxv pjnd jftb klnz", # Gmail App Password # "recipient_email": "griffinskimutai@gmail.com" # } # # Initialize MT5 # if not mt5.initialize( # path=config["mt5Pathway"], # login=int(config["username"]), # password=config["password"], # server=config["server"] # ): # error_msg = f"[MT5] Connection failed: {mt5.last_error()}" # print(error_msg) # send_email("MT5 Connection Failed", error_msg, smtp_config) # return # print("[MT5] Connected successfully") # # Get US100 daily candle # candle = get_us100_daily_candle() # if candle: # body = ( # f"US100 Daily Candle ({candle['date']}):\n\n" # f"Open: {candle['open']}\n" # f"High: {candle['high']}\n" # f"Low: {candle['low']}\n" # f"Close: {candle['close']}\n" # f"Volume: {candle['volume']}" # ) # send_email(f"US100 Daily Candle - {candle['date']}", body, smtp_config) # else: # send_email("US100 Data Fetch Failed", "No data was returned for US100.", smtp_config) # mt5.shutdown() # # ---------------------------- # # Run script # # ---------------------------- # if __name__ == "__main__": # main() import MetaTrader5 as mt5 import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import json import os from datetime import datetime # ---------------------------- # Load MT5 config # ---------------------------- def load_config(filepath="config.json"): if not os.path.exists(filepath): raise FileNotFoundError(f"Config file not found: {filepath}") with open(filepath, "r") as f: return json.load(f) # ---------------------------- # Send email function # ---------------------------- def send_email(subject, body, smtp_config): msg = MIMEMultipart() msg["From"] = smtp_config["sender_email"] msg["To"] = smtp_config["recipient_email"] msg["Subject"] = subject msg.attach(MIMEText(body, "plain")) try: with smtplib.SMTP(smtp_config["smtp_server"], smtp_config["smtp_port"]) as server: server.starttls() server.login(smtp_config["sender_email"], smtp_config["sender_password"]) server.send_message(msg) print(f"[EMAIL] Sent to {smtp_config['recipient_email']}") except Exception as e: print(f"[EMAIL ERROR] {e}") # ---------------------------- # Get daily candle for XAUUSD # ---------------------------- def get_xauusd_daily_candle(): symbol = "XAUUSD" # Some brokers may use "GOLD", "XAU/USD", etc. rates = mt5.copy_rates_from_pos(symbol, mt5.TIMEFRAME_D1, 0, 1) if rates is None or len(rates) == 0: return None candle = rates[0] candle_time = datetime.fromtimestamp(candle['time']).strftime('%Y-%m-%d') return { "date": candle_time, "open": candle['open'], "high": candle['high'], "low": candle['low'], "close": candle['close'], "volume": candle['tick_volume'] } # ---------------------------- # Main # ---------------------------- def main(): config = load_config() # Email settings smtp_config = { "smtp_server": "smtp.gmail.com", "smtp_port": 587, "sender_email": "ifairvaluegap@gmail.com", # Replace with your Gmail "sender_password": "pyxv pjnd jftb klnz", # Gmail app password "recipient_email": "griffinskimutai@gmail.com" # Replace with recipient email } # Initialize MT5 if not mt5.initialize( path=config["mt5Pathway"], login=int(config["username"]), password=config["password"], server=config["server"] ): error_msg = f"[MT5] Connection failed: {mt5.last_error()}" print(error_msg) send_email("MT5 Connection Failed", error_msg, smtp_config) return print("[MT5] Connected successfully") # Fetch candle candle = get_xauusd_daily_candle() if candle: body = ( f"XAUUSD Daily Candle ({candle['date']}):\n\n" f"Open: {candle['open']}\n" f"High: {candle['high']}\n" f"Low: {candle['low']}\n" f"Close: {candle['close']}\n" f"Volume: {candle['volume']}" ) send_email(f"XAUUSD Daily Candle - {candle['date']}", body, smtp_config) else: send_email("XAUUSD Data Fetch Failed", "No data returned for XAUUSD.", smtp_config) mt5.shutdown() # ---------------------------- # Run # ---------------------------- if __name__ == "__main__": main()