Sharing information between two MetaTrader 5 (MT5) terminals can be accomplished in several ways, depending on your specific needs and the type of information you want to share. Here are some common methods:
1. Using a File
You can write data to a file from one terminal and read it from another. This is a straightforward method for sharing information.
Example: Writing to a File
In the first terminal, you can write data to a file:
void WriteToFile(string filename, string data)
{
int fileHandle = FileOpen(filename, FILE_WRITE | FILE_TXT);
if (fileHandle != INVALID_HANDLE)
{
FileWrite(fileHandle, data);
FileClose(fileHandle);
}
else
{
Print("Error opening file for writing: ", GetLastError());
}
}
When you use the FileOpen function with the FILE_WRITE flag in MQL5, it opens the specified file for writing. If the file already exists, opening it in this mode will overwrite the existing content. This means that any previous data in the file will be replaced with the new data you write.
Error opening file for writing: 5002
The error code 5002 in MQL5 comes when youtry to access a file outside of the allowed directories for the MetaTrader 5 terminal. By default, MetaTrader 5 restricts file access to certain directories for security reasons.
//--- Folder that stores the terminal data
string terminal_data_path=TerminalInfoString(TERMINAL_DATA_PATH);
//--- Common folder for all client terminals
string common_data_path=TerminalInfoString(TERMINAL_COMMONDATA_PATH);
To avoid the 5002 error, you should place your files in one of the allowed directories.
Use the Files Directory: You can create a file in the Files directory, which is accessible from your MQL5 code. The path to this directory can be accessed using the TerminalInfoString(TERMINAL_DATA_PATH) function.
Sample code to access file from the commom data path
int filehandle=FileOpen("filename.txt",FILE_WRITE|FILE_TXT|FILE_COMMON,",");