Download MetaTrader 5

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

To add comments, please log in or register
Find out how social trading can improve your profits
MetaQuotes Software Corp.
MetaQuotes Software Corp. 2009.07.13 12:00 

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

MQL4 Comments
MQL4 Comments 2009.08.20 22:33  
In .m file, Fc is not initialized. Would you correct it? Thanks.
MQL4 Comments
MQL4 Comments 2013.02.10 19:28  
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
MQL4 Comments
MQL4 Comments 2014.10.05 00:36  


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?


leo1987 2015.09.11 17:24  

Hi guys!

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

Thank you very much! 

To add comments, please log in or register