Help Needed: MT5 EA Unable to Access Existing File

 

Hello, I'm developing an EA that needs to read a signal file generated by a Python script. However, the EA is unable to detect or access this file, even though I've confirmed that the file exists in the correct location (error code 5002). What's more confusing is that when I try to make the EA create a sample signal file in the same directory as the actual signal file, the EA can successfully create a new file.

Attempted Solutions

1.Used absolute paths instead of relative paths.

2.Checked and confirmed that MT5's file system access permissions are enabled.

3.Ran MT5 as administrator.

4.Manually created a simple test file and attempted to access it, which also failed.


The Following code is the piece that  I make the EA read the signal file made by Python.


string ReadPythonSignal()
{
    string signal = "";
    int retry_count = 0;
    int max_retries = 5; // Maximum number of retry attempts
    int retry_delay = 1000; // Retry interval (milliseconds)

    while(retry_count < max_retries)
    {
        Print("Attempting to read signal file: ", signalFilePath, " (Attempt ", retry_count + 1, ")");

        if(!FileIsExist(signalFilePath))
        {
            Print("File does not exist: ", signalFilePath);
            Sleep(retry_delay); // Wait for a while before retrying
            retry_count++;
            continue;
        }

        int file_handle = FileOpen(signalFilePath, FILE_READ|FILE_TXT|FILE_ANSI);
        if(file_handle != INVALID_HANDLE)
        {
            string content = FileReadString(file_handle);
            FileClose(file_handle);
            // Process the read signal content...
            return content;
        }
        else
        {
            int error = GetLastError();
            Print("Unable to open file. Error code: ", error, ", Description: ", CustomErrorDescription(error));
            Sleep(retry_delay); // Wait for a while before retrying
            retry_count++;
        }
    }

    Print("Failed to read signal file, maximum retry attempts reached.");
    return signal;
}
 

Please write in English here.

No code no help - how?

 
Carl Schreiber #:

Please write in English here.

No code no help - how?

Thanks for your recommendation, I adjusted my post.
 
SOFTCHICKEN967: Attempted Solutions

1.Used absolute paths instead of relative paths.

That is your problem.

You can't read (or write) outside the sandbox with normal code.
          File Write Problem (Error: 5002) - Expert Advisors and Automated Trading - MQL5 programming forum #1-2 (2020)

For security reasons, work with files is strictly controlled in the MQL5 language. Files with which file operations are conducted using MQL5 means, cannot be outside the file sandbox.
          FileOpen - File Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

File Write Problem (Error: 5002) - Expert Advisors and Automated Trading - MQL5 programming forum #1-2

 

I have read it but I am just a little messed, I put my file in the "permitted folder", but my EA still can't find it.

    signalFilePath = TerminalInfoString(TERMINAL_COMMONDATA_PATH) + "\\MQL5\\Files\\" + SignalFileName;
string ReadPythonSignal()
{
    string signal = "";
    int retry_count = 0;
    int max_retries = 5; // max tries
    int retry_delay = 1000; // gap(ms)

    while (retry_count < max_retries)
    {
        Print("Attempt to read the signal: ", signalFilePath, " (the ", retry_count + 1, " times attemption)");

        if (!FileIsExist(signalFilePath))
        {
            Print("The file doesn't exist: ", signalFilePath);
            Sleep(retry_delay); // Try after a while
            retry_count++;
            continue;
        }

        int handle = FileOpen(signalFilePath, FILE_READ | FILE_CSV | FILE_ANSI | FILE_COMMON);
        if (handle != INVALID_HANDLE)
        {
            Print("File has been opened sucessfully!");
            string content = FileReadString(handle);
            FileClose(handle);

            // Deal with the content of the file
            return content;
        }
        else
        {
            int error = GetLastError();
            Print("Can't open the file, the code: ", error, ", description: ", CustomErrorDescription(error));
            Sleep(retry_delay); // Try after a while
            retry_count++;
        }
    }

    Print("Failed to read the file, have tried for so many times");
    return signal;
}

My outputs are:

2024.08.16 00:55:21.830 Glass_Model_Assistant (BTCUSD,D1)       Attempt to read the signal: C:\Users\ASUS\AppData\Roaming\MetaQuotes\Terminal\Common\MQL5\Files\signal.csv (the x times attemption)
2024.08.16 00:55:21.830 Glass_Model_Assistant (BTCUSD,D1)       The file doesn't exist: C:\Users\ASUS\AppData\Roaming\MetaQuotes\Terminal\Common\MQL5\Files\signal.csv
 
SOFTCHICKEN967 #: I have read it but I am just a little messed, I put my file in the "permitted folder", but my EA still can't find it.
   signalFilePath = TerminalInfoString(TERMINAL_COMMONDATA_PATH) + "\\MQL5\\Files\\" + SignalFileName;

Apparently you can't read. Did you read the link provided? Remove the path and make sure your file is in the approprate place.

  1. Files are at “«DataFolder»\MQLx\Files”.
  2. In the tester, files are at “«DataFolder»\MQLx\Tester\Files”.
 
William Roeder #:

Apparently you can't read. Did you read the link provided? Remove the path and make sure your file is in the approprate place.

  1. Files are at “«DataFolder»\MQLx\Files”.
  2. In the tester, files are at “«DataFolder»\MQLx\Tester\Files”.

Thank you so much, the problem has been solved!