import MetaTrader5 as mt5 import time # 1. State control from your UI Button trading_active = False # Switched to True when you click "Start" def start_trading(login, password, server): global trading_active # Connect directly using the app inputs if not mt5.initialize(login=int(login), password=password, server=server): print("Connection Failed") return False trading_active = True print("Bot Engine Started.") # 2. Main Auto-Trading Loop while trading_active: # Check if user clicked the UI 'Stop' button during the loop if not trading_active: break # Fetch data & calculate confluences rates = mt5.copy_rates_from_pos("EURUSD", mt5.TIMEFRAME_M15, 0, 100) # Replace with your specific confluence logic if check_my_confluences(rates): # Send the execution signal to MT5 execute_trade() time.sleep(1) # Check market ticks at set intervals def stop_trading(): global trading_active trading_active = False mt5.shutdown() print("Bot Engine Stopped safely.")