//+------------------------------------------------------------------+ //| Auto Scripts_v2.mq5 | //| Copyright 2024, Optimal Ventures Group, Inc. | //| www.optimalventuresgroup.com/auto-scripts | //+------------------------------------------------------------------+ #property copyright "Copyright 2024, Optimal Ventures Group, Inc." #property link "www.optimalventuresgroup.com/auto-scripts" #property version "1.01" //+------------------------------------------------------------------+ #include // Input parameters input string alertFilePath = "C:\\Users\\optim\\Desktop\\EA_MT5\\Auto_Scripts_MT5\\alert.txt"; input string triggerFilePath = "C:\\Users\\optim\\Desktop\\EA_MT5\\Auto_Scripts_MT5\\trigger.txt"; // Global variables CTrade trade; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { Print("Auto_Scripts_EA initialized."); EventSetTimer(60); // Set timer to trigger every 60 seconds (1 minute) return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { Print("Auto_Scripts_EA deinitialized."); EventKillTimer(); // Kill the timer when deinitializing } //+------------------------------------------------------------------+ //| Timer function | //+------------------------------------------------------------------+ void OnTimer() { Print("Timer triggered, checking for new alerts."); // Only read the file when there is a request if (CheckForRequest()) { Print("Request found, reading alert file."); ReadAndProcessAlertFile(); ClearTriggerFile(); } else { Print("No request found, not reading alert file."); } } //+------------------------------------------------------------------+ //| Check for request (trigger file implementation) | //+------------------------------------------------------------------+ bool CheckForRequest() { Print("Checking for request."); if (!FileExists(triggerFilePath)) { Print("Trigger file does not exist: ", triggerFilePath); return false; // No request if the file does not exist } int handle = FileOpen(triggerFilePath, FILE_READ | FILE_TXT); if (handle < 0) { Print("Trigger file not found or inaccessible: ", triggerFilePath); return false; // No request if the file cannot be read } string content = FileReadString(handle); FileClose(handle); Print("Trigger file content: ", content); return (content == "1"); } //+------------------------------------------------------------------+ //| Clear the trigger file | //+------------------------------------------------------------------+ void ClearTriggerFile() { Print("Clearing trigger file."); if (!FileExists(triggerFilePath)) { Print("Trigger file does not exist, creating new: ", triggerFilePath); } int handle = FileOpen(triggerFilePath, FILE_WRITE | FILE_TXT); if (handle >= 0) { FileWrite(handle, "0"); FileClose(handle); Print("Trigger file cleared."); } else { Print("Failed to clear trigger file: ", triggerFilePath); } } //+------------------------------------------------------------------+ //| Read and process the alert file | //+------------------------------------------------------------------+ void ReadAndProcessAlertFile() { Print("Attempting to read alert file."); if (!FileExists(alertFilePath)) { Print("Alert file does not exist: ", alertFilePath); return; } int handle = FileOpen(alertFilePath, FILE_READ | FILE_TXT); if (handle < 0) { int error_code = GetLastError(); Print("Failed to open file for reading: ", alertFilePath, ", error code: ", error_code); if (error_code == 5002) { Print("Error 5002: File access denied. Please check permissions and ensure the file is not locked by another application."); } return; } string alertContent = FileReadString(handle); FileClose(handle); Print("File read successfully, content: ", alertContent); ProcessAlert(alertContent); } //+------------------------------------------------------------------+ //| Process alert | //+------------------------------------------------------------------+ void ProcessAlert(string alertContent) { Print("Processing alert: ", alertContent); string symbol = GetJsonValue(alertContent, "symbol"); string action = GetJsonValue(alertContent, "action"); double entry = StringToDouble(GetJsonValue(alertContent, "entry")); double stop = StringToDouble(GetJsonValue(alertContent, "stop")); double profit_target = StringToDouble(GetJsonValue(alertContent, "profit_target")); if (action == "buy") { trade.Buy(1.0, symbol, entry, stop, profit_target); Print("Buy order placed: Symbol=", symbol, " Entry=", entry, " Stop=", stop, " Profit Target=", profit_target); } else if (action == "sell") { trade.Sell(1.0, symbol, entry, stop, profit_target); Print("Sell order placed: Symbol=", symbol, " Entry=", entry, " Stop=", stop, " Profit Target=", profit_target); } else { Print("Invalid action: ", action); } } //+------------------------------------------------------------------+ //| Function to extract JSON values by key | //+------------------------------------------------------------------+ string GetJsonValue(string json, string key) { string result = ""; int keyPos = StringFind(json, "\"" + key + "\""); if (keyPos != -1) { int valueStart = StringFind(json, ":", keyPos) + 1; int valueEnd = StringFind(json, ",", valueStart); if (valueEnd == -1) // Last item, no comma { valueEnd = StringFind(json, "}", valueStart); } result = StringTrim(StringSubstr(json, valueStart, valueEnd - valueStart)); result = StringTrim(result, "\""); } return result; } //+------------------------------------------------------------------+ //| String trimming function | //+------------------------------------------------------------------+ string StringTrim(string str, string chars = " \t\n\r") { int start = 0; int end = StringLen(str) - 1; while (start <= end && StringFind(chars, StringSubstr(str, start, 1)) != -1) { start++; } while (end >= start && StringFind(chars, StringSubstr(str, end, 1)) != -1) { end--; } return StringSubstr(str, start, end - start + 1); } //+------------------------------------------------------------------+ //| Check if file exists | //+------------------------------------------------------------------+ bool FileExists(string filename) { int handle = FileOpen(filename, FILE_READ | FILE_TXT); if (handle < 0) { return false; } FileClose(handle); return true; }