Examples: Interaction between MеtaTrader 4 and MATLAB Engine (Virtual MATLAB Machine)

 

New article Interaction between MеtaTrader 4 and MATLAB Engine (Virtual MATLAB Machine) has been published:

The article contains considerations regarding creation of a DLL library - wrapper that will enable the interaction of MetaTrader 4 and the MATLAB mathematical desktop package. It describes "pitfalls" and ways to overcome them. The article is intended for prepared C/C++ programmers that use the Borland C++ Builder 6 compiler.

MetaTrader 4 and the MATLAB mathematical package have gained high popularity among users due to their positive characteristics, including "flexibility" in creation of complex calculation systems. There are three main ways of MATLAB connection with external applications, but only one of them is recommended - use of the virtual desktop MATLAB Engine. This method guarantees the full compatibility with the whole MATLAB package. Many programmers avoid this method for below reasons:

  • Many users find it slow. This is true, if compared with the direct function call from DLL libraries of MATLAB. The main delay takes place at the beginning of operation, when the virtual machine is called because of the call of numerous libraries that are uploaded into the virtual space of the calling process (in our case MetaTrader 4).
  • Transferability of the project. True, when transferring a project to another computer all MATLAB DLL libraries must also be transferred, though as well as when direct call is used, to know the "relations" of the latter ones, i.e. start queue!
  • Obligatory knowledge of C++ or Fortran. Well, if you know MQL4, you can easily learn C++ and vice versa.

Why I recommend this method:

  1. This is the most reliable and independent from the MATLAB version method of connection with external programs. You can change MATLAB version and your indicators or Expert Advisors won't notice it. This is the most important advantage.
  2. It has a relatively quick development method. It doesn't require debuggers, and it will make no difficulties to write the DLL wrapper.
  3. "Common desktop" for several indicators and/or Expert Advisors. I consider this method useful when we need to make a decision based on data of several indicators or in the implementation of a pyramid trade.

This article describes the way of connecting MetaTrader 4 and MATLAB ver. 7.4.0 (R2007a) via a "DLL-wrapper" written in Borland C++ Builder 6. Programmers who prefer Microsoft products will have to adapt examples to their compiler (good luck to you in this complicated matter!).

Author: Andrey Emelyanov

 
In .m file, Fc is not initialized. Would you correct it? Thanks.
 
This usage of DLL works only in testing mode, not in trading. Because MT in trading calls DLL from multiple threads, it's necessary to start Matlab in a separate thread. For better explanation, see my comment in forum
 

INITIALIZING MATLAB.


How do you initialize the MCR engine? I don't use Broland so I can't tell how you control the starting and stopping of the Matlab Runtime?

What Matlab Includes are you using as well?


Thanks

 

Hi guys!

Do you know if this procedure still works after several MATLAB/MT4 updates?

Thank you very much!