Search code examples
filealgorithmic-tradingmetatrader4mt4

MT4 cannot open file error 4103 when running an EA in Strategy Tester


I wrote an EA using an indicator which reads in a configuration file. This configuration file is put in the folder MQL4\Files.

When I run the EA with the live chart, it works well. But when I tried to test this EA using the strategy tester, it returns an 4103 error: can not open file.

Any idea why this happens?

And how to address this problem?


Solution

  • A thing that happens is a feature of the MQL4 ecosystem:

    For security reasons, work with files is strictly controlled in the MQL4 language. Files with which file operations are conducted using MQL4 means, cannot be outside the file sandbox.

    The file is opened in the folder of the client terminal in the subfolder MQL4\files ( or Tester\Files in case of testing ). If FILE_COMMON is specified among flags, the file is opened in a shared folder for all MetaTrader 4 client terminals.

    How to address this?

    Simply, take care of using just the "relative" specification of the file-path ( and keep in mind, which of the three possible root-directory cases is being used in which modus-operandi ).

    aFH = FileOpen( "aFileInsideDefaultDirectoryRoot.txt", ... );
    // 
    // USED:
    // in Strategy Tester---+
    //    plain EA-mode-----|------------+
    //    FILE_COMMON-mode--|------------|----------------+
    //                      |            |                |
    // RESULTS:             v            v                v
    // in a file in { tester\files | MQL4\Files | <shared_common_directory> }
    

    For the case of reading the file, you might enforce to have a copy of the same configuration file in both of the locations for the non-FILE_COMMON use-cases, or keep a single instance in <shared_common_directory>