Monday, August 17, 2009

Interaction between MеtaTrader 4 and MATLAB Engine

http://articles.mql4.com/833
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!).