1

I'm trying to build an expert advisor with MQL5 and it actually has two parts:

part one is a c++ code, which produces the commands(buy, sell) and writes it in a text file. part two is the MQL5 code which is intended to open that mentioned text file, read the command from it and execute it. My problem is that, since they have to have access to the text file almost simultaneously, sometime one of them tries to open the text file while the other one is still using it and has not closed it. at these points, the program returns an error because one part cannot open the text file and everything stops.

My question is How I can solve the issue? Can MQL5 read the command line, so I can print the commands in command line and make MQL5 read them? Or is there any way to fix this access-at-same-time problem?

C++ program is constantly checking if the text file is edited. Once the text file is edited, it opens it and read data from it. the problem is that the data is constantly being written in the text file almost every second. So, It happens many time during the process that while the MQL program tries to write new data in the text file, c++ program is still reading the old data from the file.

Martin Ba
  • 37,187
  • 33
  • 183
  • 337
  • How to solve? Not using text files... Honestly, there are far better ways to communicate between processes, e.g. (named) pipes, sockets, shared memory (usually not that easy to handle, though), remote procedure call library (internally using some fore-mentioned means), ... Have you considered one of already? Applicable? – Aconcagua May 20 '22 at 10:31
  • It depends on the operating system and how they open the file. Notice that Linux has the `tail -f` command which works just fine. – user253751 May 20 '22 at 10:33
  • Can ML5 have shared opening? You can do this in C++ but the calls vary depending on which OS you are using. – cup May 20 '22 at 10:33
  • One crucial bit of info missing here is: how do you start these two programs? Why do they "sometimes" access the file concurrently. – Martin Ba May 20 '22 at 10:36
  • Even though there are better ways to communicate, if you insist on file: Writing right at the time while the other one reads might be problematic, too. It might be safer only to keep the file open (exclusively) while writing or reading. You'd need to assure then, though, that you don't read data *again* that you've read already from opening the file previously – so you'd simply truncate it to size 0 before giving the file descriptor back to OS. You might then instead of testing if reading still results in EOF monitor the last edit time of the file to detect if new commands are available. – Aconcagua May 20 '22 at 11:09
  • Alternatively you might just delete the file and test its existence (while the writer would, if it exists, append, otherwise create a new one, in both cases with exclusive access – should be possible to handle in the same call to `open` with appropriate flags). In any case: By exclusively locking the file you'd delegate avoidance of concurrent access to the OS, so you wouldn't need to care for any more. – Aconcagua May 20 '22 at 11:14
  • @Aconcagua Are these methods executable in MQL? – George Wilhelm Hegel May 20 '22 at 18:12
  • @MartinBa C++ program is constantly checking if the text file is edited. Once the text file is edited, it opens it and read data from it. the problem is that the data is constantly being written in the text file almost every second. So, It happens many time during the process that while the MQL program tries to write new data in the text file, c++ program is still reading the old data from the file. – George Wilhelm Hegel May 20 '22 at 18:21
  • You can read files, can't you? You'd open a named pipe just like a normal file, one process reads, the other writes. For sockets, a quick search on my favourite engine gave [this](https://www.mql5.com/en/docs/network/socketcreate) as very first result... Shared memory – you'll rely on OS facilities, in C++, there's no standard support for, if MQL differs in this respect I cannot tell. If all other attempts fail but MQL can load SO or DLL files respectively then very last ressort might be implementing such one in C++ and use it from MQL. – Aconcagua May 23 '22 at 07:40
  • Shared memory on Linux from within C (doesn't differ in C++) is handled [here](https://stackoverflow.com/questions/5656530/how-to-use-shared-memory-with-linux-in-c), by the way. – Aconcagua May 23 '22 at 07:54

1 Answers1

-2

Notwithstanding that I personally think c++ is out of date, but I may be wrong about that.

I personally do what you ask for between c# and mql5, also into different mql5 EAs to communicate together.

For communication between EAs there will be GlobalVariables pretty nice!

Back to your question: There is a simple trick you can use. Just create a File that will lock your Textfile.txt.

Explaining: C ++ will check if there exists a File called myMQL5 .lck If this File exists, c ++ will check every Millisecond for existing, until is does not exists. Than c ++ creats a File calles myC ++ .lck and c ++ opens writer.

if c ++ is finished writing it delets the file myC ++ .lck .

Same on MQL5 side ...

That was my solution. But i will never write every second ... Just for preventing errors.

Another working way will be: Create out from c ++ everytime a new File with a Timestamp on the end. if MQL5 reads a File can this one deleted in the end directly.

Next loop with the next Files ...

Just some ideas ... if my answer give you the right direction, please flag it as "solved answer"

Dragon
  • 49
  • 7