calling and execution other programs

 

from an MQL4-program can I

write data to a file,

call another program written in another language

that reads the data from the file,

analyzes it,

writes the result to another file

returnls to the calling MQL4 program

open the result-file from MQL4 and read the data

continue within MQL4 to find the trading decision and access the quotes

 
uevst3e:

from an MQL4-program can I

write data to a file,

call another program written in another language

that reads the data from the file,

analyzes it,

writes the result to another file

returnls to the calling MQL4 program

open the result-file from MQL4 and read the data

continue within MQL4 to find the trading decision and access the quotes


Yes, you can do this.

Also:

Write a dll and call the dll from MetaTrader (Dont need to write/read Files maybe)

Write values to registry and read registry from other program

and so on....

 
EADeveloper:


Yes, you can do this.

Also:

Write a dll and call the dll from MetaTrader (Dont need to write/read Files maybe)

Write values to registry and read registry from other program

and so on....


good, thanks.

I don't know what dll is and what's in the registry.

I'm writing programs in C under DOS which compile to executables (.exe).

In C we have the command

system("program.exe");

is there something similar in MQL4 ?

this would let me program my indicatoranalysis in C, while collecting the data and getting the result

and translating it into broker orders in MQL4.

This would be much better and easier for me.

any link or just google-keyword to find more info about this

is appreciated.

 
uevst3e:


good, thanks.

I don't know what dll is and what's in the registry.

I'm writing programs in C under DOS which compile to executables (.exe).

In C we have the command

system("program.exe");

is there something similar in MQL4 ?

this would let me program my indicatoranalysis in C, while collecting the data and getting the result

and translating it into broker orders in MQL4.

This would be much better and easier for me.

any link or just google-keyword to find more info about this

is appreciated.


here is al about file functions (open,close,read,write)

https://docs.mql4.com/files

i guess in your case will be most easy your program is running all time and waiting for a file written by mql.

Open file, analyce, delete file, write result file.

Mql read result file, execute orders or else and delete result file.

 
EADeveloper:


here is al about file functions (open,close,read,write)

https://docs.mql4.com/files

i guess in your case will be most easy your program is running all time and waiting for a file written by mql.

Open file, analyce, delete file, write result file.

Mql read result file, execute orders or else and delete result file.


OK, I gues that means no, there is no "system(program.exe)" or "call(program.exe)" - like command

in MLQ4. It could be presumably done by interrupts - giving control periodically to the other analysis program ?!

... maybe ask in the C-forum...

 

You can start an external program by using the windows API (and I believe there exist working examples for this already here in the forum or in the code base) but I would rather do it in one of the following ways:


1. (pragmatic but ugly) permanently poll for existence of file. It involves 4 different files.

a) the mql EA will write its data to a file (call it "data.txt")

b) when finished writing it will create a second empty file (call it "start.txt") to signal the other process that it is now waiting for answer

c) the mql4 EA is now entering a loop to wait for the appearance of a file called "done.txt"

meanwhile in the other process the following happens

d) the other process is permanently in an infinite loop polling for the appearance of "start.txt"

e) once this file exists it will open and read "data.txt", then it will process the data and write the results into the file "answer.txt"

f) then it will delete "start.txt" and "data.txt"

g) now it will create an empty file "done.txt"

h) now it will go back to (d) to wait for the next workload.

now in the mql4 EA which has been waiting for "done.txt" all the time the following happens

i) it will detect the existence of "done.txt" and read "answer.txt" and trade accordingly.

j) it will delete "done.txt" and "answer.txt"

k) it is finished and everything can start again in the next tick or next bar with (a)

I have done this already, it works but it is quite ugly. I am using 4 files instead of only 2 because it can happen that the external process starts reading the data file before the EA is fully done writing it, I have had this problem in a real world application although it only wrote 2 short lines of text into the file, so I decided to use two additional empty files to synchronize it properly and avoid such race conditions. I have used this approach because my external program was a python program, I needed to do calculations in Python and in R and it also had to maintain its own state across multiple calls and and this was before I had finally written my mql4-Python binding and my mql4-R binding which finally solved this problem in a much more elegant manner (now i can directly call Python functions and R functions seamlessly from within mql4).


2. make the external program a function in a dll that you can call. Depending on how huge your data is you can either pass arrays containing the data or a string containing the same kind of data you would have written to the file or write it to a file and then call the function. Once the function returns the EA can continue. The result of the calculation can be passed by return value if it is only a trade signal or written into an array if it is more complex.

This solution is much more elegant and also much faster. There are plenty of tutorials about writing dlls for metatrader with examples using a few different popular programming languages that compile to native machine code such as C, C++, Delphi or Free Pascal.


3. use the mql4-python bindings instead of the windows API to start the external process or to communicate with an external process or even write it entirely in Python instead of C, this is much easier than messing around with the naked windows API and CreateProcess() and friends. If you need complicated statistical calculations then consider using the mql4-R bindings (google: mt4R.dll) and write your external code in R.

 

thanks. Uglyness is no problem.

Best would be if I could use the broker functions directly in C, no metatrader needed.

Or just send the program to the broker and they run it for me.

 

is that program/interface available ?

If I pay $$ ? Or if I chose that broker then, who provides it ?

so I could basically write my EA in C or Basic or whatever.

I make an executable that reads the data and order-status from a file, analyses the data,

and writes orders to another file.

MetaTrader must keep track of the files, update quotes and does the communication with the broker.

But for the analysis I want my own math-software.

if I do it myself ... learn MQ4 etc. ... how many hours,days,weeks would it take ?

Someone must have done it already.

It's the natural/canonical way.

 

The following post should help you with this: https://www.mql5.com/en/forum/137456, it uses anonymous pipes to communicate with the dos command.

 

I have developed trading algorithm in Python and I try to implement 7bit trick to pass information between Python and mql4 using data files. But mql4 only allows me to save files in expert/files folder and Python cannot access that folder (get error message: IOError: [Errno 22] invalid mode ('r') or filename: 'C:\\Program Files (x86)\\OANDA - MetaTrader\\experts\x0ciles\\XYZ.txt'. ) Any suggestions would be appreciated. Thanks.

 
VincentSo:

I have developed trading algorithm in Python and I try to implement 7bit trick to pass information between Python and mql4 using data files. But mql4 only allows me to save files in expert/files folder and Python cannot access that folder (get error message: IOError: [Errno 22] invalid mode ('r') or filename: 'C:\\Program Files (x86)\\OANDA - MetaTrader\\experts\x0ciles\\XYZ.txt'. ) Any suggestions would be appreciated. Thanks.

Don't install MT4 in Program Files, instead use something like C:\MT4Installs\BrokerName\
Reason: