#include #import "libmysql.dll" // Define the MySQL connection parameters string mysql_host = "localhost"; string mysql_user = "root"; string mysql_password = "root"; string mysql_database = "tutorials"; int mysql_port = 3306; // Define the MySQL query to retrieve the signal records string mysql_query = "SELECT TRADE_SYMBOL, TRADE_VOLUME, TRADE_TYPE, TAKE_PROFIT, STOP_LOSS, TRADE_STATUS FROM signal WHERE TRADE_STATUS = 'PENDING'"; // Define the time interval for checking the MySQL table (5 minutes) ulong check_interval = 5 * 60 * 1000; // Define the trade execution function void execute_trade(string symbol, double volume, int type, double take_profit, double stop_loss) { // Execute the trade in MT5 ulong ticket = OrderSend(symbol, type, volume, Bid, 0, stop_loss, take_profit, "MySQL trade", 0, 0, Green); if (ticket == 0) { Print("Failed to execute trade for ", symbol, "! Error: ", GetLastError()); } else { Print("Executed trade for ", symbol, " with ticket ", ticket); } } void onStart() { // Initialize the MySQL connection CMySQL mysql; if (!mysql.Connect(mysql_host, mysql_user, mysql_password, mysql_database, mysql_port)) { Print("Failed to connect to MySQL database! Error: ", mysql.Error()); return; } // Continuously loop through and execute trades every 5 minutes while (true) { // Execute the MySQL query CMySQLResult mysql_result = mysql.Query(mysql_query); if (mysql_result.IsEmpty()) { Print("No pending trades found."); } else { // Loop through each pending signal record and execute the trade in MT5 while (!mysql_result.IsEmpty()) { CMySQLRow mysql_row = mysql_result.FetchRow(); string symbol = mysql_row.String(0); double volume = mysql_row.Double(1); int type = mysql_row.String(2) == "buy" ? ORDER_TYPE_BUY : ORDER_TYPE_SELL; double take_profit = mysql_row.Double(3); double stop_loss = mysql_row.Double(4); string trade_status = mysql_row.String(5); execute_trade(symbol, volume, type, take_profit, stop_loss); // Update the trade status to "DONE" in the MySQL table string update_query = "UPDATE signal SET TRADE_STATUS = 'DONE' WHERE TRADE_SYMBOL = '" + symbol + "'"; if (!mysql.Query(update_query)) { Print("Failed to update trade status for ", symbol, "! Error: ", mysql.Error()); } } // Free the MySQL result mysql_result.Free(); } // Wait for 5 minutes before checking the MySQL table again Sleep(check_interval); } // Close the MySQL connection mysql.Disconnect(); }