MT5 error 5004: File opening error
Open files inside MQL5\Files.
Updated 24 October 2026. Runs in your browser; nothing you type is uploaded.
The number after retcode= or "error" in MT5's Experts or Journal tab. 296 codes, return codes and runtime errors, as MQL5's reference words them.
Share this fix
What MQL5 says
5004ERR_CANNOT_OPEN_FILE- File opening error
From MQL5's table of runtime errors, read 24 October 2026. An Expert Advisor reads it with GetLastError() after a call fails; the Experts tab prints what the EA logs.
What causes it
FileOpen() couldn't open the file. MQL5's FileOpen page keeps files in a sandbox: MQL5\Files of the terminal (the testing agent's MQL5\Files in the tester), or the folder shared by all terminals with FILE_COMMON. A full path outside it can't be opened.
The fix in MQL5
Pass a name relative to MQL5\Files, add FILE_COMMON for a file several terminals share, and check for INVALID_HANDLE, which FileOpen() returns when it fails.
void SaveLog()
{
int f = FileOpen("C:\\Trading\\log.csv", FILE_WRITE | FILE_CSV); // outside the file sandbox
FileWrite(f, TimeCurrent(), _Symbol);
FileClose(f);
}void SaveLog()
{
// relative to MQL5\Files of this terminal; add FILE_COMMON for the folder all terminals share
int f = FileOpen("log.csv", FILE_WRITE | FILE_CSV);
if(f == INVALID_HANDLE)
{
Print("FileOpen failed: ", GetLastError());
return;
}
FileWrite(f, TimeCurrent(), _Symbol);
FileClose(f);
}Both compile in MetaEditor 5 with no errors or warnings. Adapt them to your EA; they were not run against a trade server.
The same error in MT4
MT4 reports it as an error code instead: MT4 error 5004: Cannot open file.
Other MT5 errors
- MT5 error 4014: Function is not allowed for call
- MT5 error 4753: Position not found
- MT5 error 4756: Trade request sending failed
- MT5 error 4802: Indicator cannot be created
- MT5 error 4806: Requested data not found
- MT5 error 10004: Requote
- MT5 error 10006: Request rejected
- MT5 retcode 10009: Request completed
- MT5 error 10013: Invalid request
- MT5 error 10014: Invalid volume in the request
- MT5 error 10015: Invalid price in the request
- MT5 error 10016: Invalid stops in the request
- MT5 error 10017: Trade is disabled
- MT5 error 10018: Market is closed
- MT5 error 10019: There is not enough money to complete the request
- MT5 error 10021: There are no quotes to process the request
- MT5 error 10022: Invalid order expiration date in the request
- MT5 error 10026: Autotrading disabled by server
- MT5 error 10027: Autotrading disabled by client terminal
- MT5 error 10030: Invalid order filling type
- MT5 error 10031: No connection with the trade server
- MT5 error 10044: The request is rejected, because the "Only position closing is allowed" rule is set for the symbol
- MT5 error 10046: The request is rejected, because the "Opposite positions on a single symbol are disabled" rule is set for the trading account.
Read a whole MT5 EA's source
- MTR FullSystemEA (MQL5 source) The full MQL5 source of a production MT5 EA for XAUUSD M5 (hedge-grid and recovery): 21 files, 16,923 lines. $49 once.