MQL4 ERR_CANNOT_OPEN_FILE

MQL4 error 5004 cannot open file: check the sandbox

MQL4 error 5004 cannot open file maps to ERR_CANNOT_OPEN_FILE: cannot open file. FileOpen returned INVALID_HANDLE because MT4 could not open the requested file under its file sandbox and access flags. Capture GetLastError immediately after the failed call, correct the cause, and retry only when the condition can change.

Open the Coding Agent, then choose MQL4 from the language menu.

FileOpenDiagnostic.mq4.mq4
#property strict

void OnStart()
  {
   string file_name = "pineify/diagnostic.csv";
   ResetLastError();
   int handle = FileOpen(file_name, FILE_READ|FILE_CSV|FILE_ANSI);
   if(handle == INVALID_HANDLE)
     {
      Print("FileOpen failed. error=", GetLastError(),
            " file=", file_name);
      return;
     }

   Print("File opened inside the MQL4 file sandbox.");
   FileClose(handle);
  }
  • Official meaning: cannot open file
  • Read the failed API return value first
  • Capture GetLastError immediately
  • Retry only when the condition can change

What MQL4 error 5004 means

The official MQL4 runtime table names error 5004 ERR_CANNOT_OPEN_FILE and defines it as “cannot open file.” FileOpen returned INVALID_HANDLE because MT4 could not open the requested file under its file sandbox and access flags.

The error number is evidence about one failed call, not a diagnosis of the whole EA. Read the API sentinel first, then call GetLastError immediately because that function returns the current _LastError value and resets it.

Check these conditions before editing the strategy

Start with the inputs and external state that existed at the failure point. A later log entry or a successful call can overwrite the useful context.

  • The path is outside MQL4/Files, Tester/Files, or the shared FILE_COMMON area
  • The subfolder is missing or the name contains an unsupported path
  • Read, write, CSV, text, binary, Unicode, or ANSI flags do not match the job
  • Another process or permission state blocks access

Fix the cause, then choose a retry policy

Retry only after the path, file, flags, or access state changes. A rapid loop cannot repair a missing or blocked file.

Keep the original trading and risk rules intact. A workaround that widens stops, increases volume, changes an account setting, or ignores a failed return value can hide the symptom while changing the system.

CheckAction
The path is outside MQL4/Files, Tester/Files, or the shared FILE_COMMON areaUse a relative path inside the documented MQL4 file sandbox.
The subfolder is missing or the name contains an unsupported pathCreate and deploy the expected subfolder or file in the correct terminal data directory.
Read, write, CSV, text, binary, Unicode, or ANSI flags do not match the jobChoose the minimum FileOpen flags required for the format and access mode.
Another process or permission state blocks accessLog the resolved relative name and error, then close every valid handle.

Review the complete example

The script uses a relative sandbox path, tests INVALID_HANDLE, reads GetLastError immediately, and closes a valid handle.

The example improves validation and logging. It does not prove the operation will be accepted by a broker or behave safely in live trading.

FileOpenDiagnostic.mq4.mq4
#property strict

void OnStart()
  {
   string file_name = "pineify/diagnostic.csv";
   ResetLastError();
   int handle = FileOpen(file_name, FILE_READ|FILE_CSV|FILE_ANSI);
   if(handle == INVALID_HANDLE)
     {
      Print("FileOpen failed. error=", GetLastError(),
            " file=", file_name);
      return;
     }

   Print("File opened inside the MQL4 file sandbox.");
   FileClose(handle);
  }
Where Pineify fits

Move from the rule to editable MQL4 source

Pineify can generate or revise one self-contained .mq4 file with MQL4-specific reference context. Its independent static checker returns MetaEditor diagnostics so you can repair source issues before testing in your own MT4 environment.

Open MQL4 Coding Agent

Static diagnostics do not prove runtime behavior, backtest results, broker compatibility, or live-trading safety.

Pineify MQL4 Coding Agent with MQL4 selected and an editable MetaTrader 4 code artifact

A practical workflow

  1. 1

    Confirm the failed call

    Check the API return value before interpreting an error number from the log.

  2. 2

    Capture the error once

    Call GetLastError immediately and record error 5004, symbol, prices, volume, ticket, and terminal state relevant to the operation.

  3. 3

    Correct the triggering condition

    Use a relative path inside the documented MQL4 file sandbox.

  4. 4

    Retest in the target environment

    Compile the revised source, then test with the same MT4 build, broker, symbol, account permissions, and market state.

Frequently asked questions

What does MQL4 error 5004 cannot open file mean?

ERR_CANNOT_OPEN_FILE means cannot open file. The code should still identify which API returned failure and log the inputs used for that call.

What usually causes MQL4 error 5004?

The path is outside MQL4/Files, Tester/Files, or the shared FILE_COMMON area The subfolder is missing or the name contains an unsupported path Read, write, CSV, text, binary, Unicode, or ANSI flags do not match the job Another process or permission state blocks access

Should an EA retry after error 5004?

Retry only after the path, file, flags, or access state changes. A rapid loop cannot repair a missing or blocked file.

Why should GetLastError be called immediately?

It reports the current _LastError value and resets it. Other operations can change the error state, so a delayed read may describe the wrong call.

Can Pineify fix MQL4 error 5004?

Pineify can revise a self-contained .mq4 file, add validation and error logging, and return static MetaEditor diagnostics. It cannot prove broker acceptance, account permissions, market availability, or live execution.

Last verified against the linked official references on . Compile and test the final source in the MetaTrader 4 build, broker, symbol, and account environment you plan to use.

Continue with one reviewable MQL4 file

Define the requirement, inspect the generated source, repair diagnostics, and test the behavior in your own MT4 environment.

Open Coding Agent