Error Code 5002 When Opening File for Exchange Rate and Trade Calculation in MQL4

 

Hi MQL5 Community,

I'm working on a script that involves reading an exchange rate from a file to perform risk calculations before opening trades. Unfortunately, I'm encountering an issue with opening the file and getting the following error:

"Failed to open exchange_rate.txt file, Error code = 5002"

This error is causing problems in my trade execution because I need the exchange rate to calculate the capital at risk in local currency.

Here is the relevant part of my code:

string InpFileName = "exchange_rate.txt"; 
double ReadExchangeRateFromFile() {
    ResetLastError();
    string directory = TerminalInfoString(TERMINAL_DATA_PATH) + "\\MQL4\\Files\\";
    
    int file_handle = FileOpen(directory + InpFileName, FILE_READ | FILE_TXT); // fileName passed in parameter
    
    if (file_handle != INVALID_HANDLE) {
        PrintFormat("%s file is available for reading", InpFileName);
        PrintFormat("File path: %s", directory);
        
        string exchangeRateStr = "";
        double exchangeRate = 0.0;
        
        while (!FileIsEnding(file_handle)) {
            exchangeRateStr = FileReadString(file_handle);
            Print(exchangeRateStr);
        }
        
        FileClose(file_handle);
        exchangeRate = StringToDouble(exchangeRateStr);
        return exchangeRate;
    } else {
        PrintFormat("Failed to open %s file, Error code = %d", InpFileName, GetLastError());
        return 0.0;
    }
}

I also use the exchange rate in my trade calculations as shown below:

void HandleTradeResult(int resultTicket, string orderType, double price, double tradeLot, string dateTime) {
    if (resultTicket <= 0) {
        SendMyMessage(dateTime + " " + "Error sending " + orderType + " order. Error code: " + IntegerToString(GetLastError()), true);
    } else {
        SendMyMessage(dateTime + " " + orderType + " order executed successfully at " + DoubleToString(price, Digits) + " with lot size: " + DoubleToString(tradeLot, Digits));
        
        // Use the exchange rate directly
        double exchangeRate = ReadExchangeRateFromFile();
        
        if (exchangeRate == 0.0) {
            SendMyMessage("Invalid exchange rate value. Calculation aborted.", true);
            return;
        }

        // Calculate and send risk in local currency
        double AccountRisk = AccountBalance() * MaxRiskPerTrade / 100;
        double CapitalAtRisk = AccountRisk * exchangeRate;
        SendMyMessage("Capital At Risk: " + DoubleToString(CapitalAtRisk, Digits));
    }
}

Error Details:

  • Error Code: 5002
  • File Name: exchange_rate.txt
  • Expected File Path: TerminalDataPath\MQL4\Files\

Things I Have Checked:

  1. The file " exchange_rate.txt"  is located in the MQL4\Files directory.
  2. The file is not open or being used by another program.
  3. File permissions seem correct, and it can be opened normally outside of the platform.

Could someone please help me understand what might be causing this error and how to resolve it?

Any guidance on resolving this file opening issue or any suggestions for improving the overall approach would be greatly appreciated.

credit of partial code built upon and obtained from codebase docs.mql4.com

Thanks in advance for your help!

Kind Regards,
Benjamin

 

MT4/MQL4 issue on MQL4 section please !

I moved your topic.

 
Alain Verleyen #:

MT4/MQL4 issue on MQL4 section please !

I moved your topic.

Thanks