Friday, 5 July 2024

 python c:/Users/ds1020254/Desktop/dante.py







import tkinter as tk

from tkinter import messagebox

import MetaTrader5 as mt5

import matplotlib.pyplot as plt

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

from scipy.signal import find_peaks

def main():

    # Create the main window

    root = tk.Tk()

    root.title("MT4MYM-A")

    # Set the window size

    root.geometry("800x600")

    # Create a frame for the login UI

    login_frame = tk.Frame(root, width=400, height=600, bg="lightgrey")

    login_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

    # Title for the login UI

    login_title = tk.Label(login_frame, text="🏧MYM-A", font=("Helvetica", 20), bg="lightgrey")

    login_title.pack(pady=20)

    # Login fields

    login_label = tk.Label(login_frame, text="Login:", font=("Helvetica", 14), bg="lightgrey")

    login_label.pack(pady=5)

    login_entry = tk.Entry(login_frame, font=("Helvetica", 14))

    login_entry.pack(pady=5)

    login_entry.insert(0, "5027341082")  # Pre-fill with your login

    password_label = tk.Label(login_frame, text="Password:", font=("Helvetica", 14), bg="lightgrey")

    password_label.pack(pady=5)

    password_entry = tk.Entry(login_frame, show="*", font=("Helvetica", 14))

    password_entry.pack(pady=5)

    password_entry.insert(0, "IxS@Po1b")  # Pre-fill with your password

    server_label = tk.Label(login_frame, text="Server:", font=("Helvetica", 14), bg="lightgrey")

    server_label.pack(pady=5)

    server_entry = tk.Entry(login_frame, font=("Helvetica", 14))

    server_entry.pack(pady=5)

    server_entry.insert(0, "MetaQuotes-Demo")  # Pre-fill with your server

    # Connect button

    connect_button = tk.Button(login_frame, text="Connect", font=("Helvetica", 14), command=lambda: connect_to_mt5(login_entry.get(), password_entry.get(), server_entry.get(), root))

    connect_button.pack(pady=20)

    # Create a frame for the main content

    main_frame = tk.Frame(root, width=400, height=600)

    main_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)

    # Create a label with the text "USD/MX"

    label = tk.Label(main_frame, text="USD/MX", font=("Helvetica", 24))

    label.pack(expand=True)

    # Create a label with the text "Loading..." at the bottom

    loading_label = tk.Label(main_frame, text="Loading...", font=("Helvetica", 14))

    loading_label.pack(side=tk.BOTTOM, pady=10)

    # Start the Tkinter event loop

    root.mainloop()

def connect_to_mt5(login, password, server, root):

    # Initialize MetaTrader 5

    if not mt5.initialize():

        messagebox.showerror("Error", "initialize() failed")

        mt5.shutdown()

        return

    # Log in to the MetaTrader 5 account

    authorized = mt5.login(login=int(login), password=password, server=server)

    if authorized:

        print("Connected to MetaTrader 5")

        display_account_info(root)

        fetch_chart(root)  # Fetch and display the chart after successful login

    else:

        messagebox.showerror("Error", "Failed to connect to MetaTrader 5")

def display_account_info(root):

    # Get account info

    account_info = mt5.account_info()

    if account_info is None:

        messagebox.showerror("Error", "Failed to get account info")

        return

    # Create a new window for account information

    info_window = tk.Toplevel(root)

    info_window.title("Account Information")

    info_window.geometry("400x300")

    # Display account information

    info_labels = [

        f"Account ID: {account_info.login}",

        f"Balance: {account_info.balance}",

        f"Equity: {account_info.equity}",

        f"Margin: {account_info.margin}",

        f"Free Margin: {account_info.margin_free}",

        f"Leverage: {account_info.leverage}"

    ]

    for info in info_labels:

        label = tk.Label(info_window, text=info, font=("Helvetica", 14))

        label.pack(pady=5)

def fetch_chart(root):

    rates = mt5.copy_rates_from_pos("USDMXN", mt5.TIMEFRAME_D1, 0, 100)

    if rates is None or len(rates) == 0:

        messagebox.showerror("Error", "Failed to retrieve rates")

        return

    dates = [x['time'] for x in rates]

    close_prices = [x['close'] for x in rates]

    # Find peaks

    peaks, _ = find_peaks(close_prices)

    # Plotting

    fig, ax = plt.subplots()

    ax.plot(dates, close_prices, label="USD/MXN")

    ax.plot([dates[i] for i in peaks], [close_prices[i] for i in peaks], "ro", markersize=10)  # Red circles at peaks

    ax.set_xlabel("Date")

    ax.set_ylabel("Price")

    ax.set_title("USD/MXN Daily Chart")

    ax.legend()

    chart_frame = tk.Frame(root)

    chart_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)

    canvas = FigureCanvasTkAgg(fig, master=chart_frame)

    canvas.draw()

    canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)

if __name__ == "__main__":

    main()








No comments:

Post a Comment

https://youtu.be/jZ38C-M3tyk?si=ERjuJ8hqsQwV2vAp