Monday, March 2, 2009

Programming in MQL 4: Plug-ins and import functions

Further acquaintance with the language MQL4 would like to begin with a general discussion on the effectiveness of labor programmers. Source code programs are usually created not with a "clean sheet", as the basis for virtually all the programs fit the existing developments. Architects programs have long been accustomed to use the entire library of blanks as bricks to create new programs. In this connection, the programming language, which does not allow use of existing works, or requiring great effort to have once performed the work is perceived as inconvenient.

Naturally, the need for constant repetition of the same code, making minimal changes, is a routine task, and may permanently discourage creativity, even in the most patient programmer. No one would want for the umpteenth time to write a block of code, sort the array. Another case where it is possible to arrange frequently used, and once written blocks of code in the form of functions. In the last issue of Forex Magazine we already wrote about the possibilities of working with functions added to the programming language MQL4. Fortunately, the developers of MetaQuotes Software is not limited to these innovations and added to the MQL 4 at least two possibilities for the reuse of code.

The first of them - the opportunity to include the files in its program files. This allows you to build your own functions that perform related actions in the individual modules (files).

Second - this is an opportunity to connect to the program at the MQL 4 functions of libraries written in other programming languages and executed in a Shared libraries (dynamically linking libraries - DLL).

To use one of these opportunities - to include in its program file from another file, in MQL 4 adds the directive "# include". This idea is adopted from the C programming language. The following example illustrates the use of the directive "include" for the file of your program file "MyDefines.mq4"

# include

Suppose that the file "MyDefines.mq4" We keep the functions that extend the functionality of the string language MQL4. For illustrative example in the same way that these functions 20. Without a directive "include", we would have to use one of the following ways:

- Either to compile each of the 20 functions as a separate user-defined function, stored in a separate file, which obviously would be some confusion in the work of the library;

- Or would have to store all the functions in a single file, and at the time of writing the program to insert the correct one in its program by using the copy / paste, more preferably less than the previous version. Since functions can be used in the work of each other, when pasted into the program of a function we would have to carefully trace the links and copy all the functions used by the function of interest to us.

The directive "include" saves us from both described problems. In addition, the compiler language MQL 4 is smart enough to pick from the included file, only those functions that may be caused in the process compiled program. This ensures that the program would not be unreasonable to rise after adding any new functions in the plug-file library, unless, of course, they will not directly or indirectly used in the code.

Consider a second opportunity to reuse code, but in this case, we are no longer limited to their own ideas. A DLL-library with exported functions can become the object of our attention.

To connect to the program for the MQL 4 functions of libraries written in other programming languages in the form of DLL, should take advantage of the directive "# import". An example shows how a standard that is included with Windows, the library user32.dll import function MessageBeep ():

# import "user32.dll"
int MessageBeep (int uType);
# import

I think that many programmers have already understood, and thus an opportunity to apply to almost any function of Win32 API. Also, this feature allows you to independently write any more or less developed its own programming language DLL-library that can be realized, our most ambitious dreams.

Incidentally, the first examined the capacity perfectly complements the second. Of course, we could include in our import program of the library functions as needed, but, for future use, it would probably be easier once and for all "roll" in a separate file import functions from a library and include it in the program directive "include" whenever this is necessary.

Suppose that we are interested in some of the functions stored in a file MyLib.dll, which are often used during the writing of several indicators, which, in turn, used in the experts. Once writing a file, for example, MyLib.mq4, which imported all sorts of functions from MyLib.dll, we will facilitate your work. Now we do not have to import each individual function from the DLL-library. Should be easy to incorporate into our program file MyLib.mq4 hold any of the hidden in MyLib.dll exported functions. File MyLib.mq4 will contain the following strings:

/ / This is the content of the file MyLib.mq4
# import "MyLib.dll"
int Function1 (int uParam1);
int Function2 (int uParam1);
int Function3 (int uParam1);
int Function4 ();
int Function5 (int uParam1,
int uParam2);
# import

Then the program uses a function of Function5 MyLib.dll, will be able to do so:

/ / Connect MyLib.mq4 to the program # include

int init ()
(
int nRet = 0;
/ / Do something ...
nRet = Function5 (10,20);
/ / Do something else ...
return (0);
)

Here are today and everything would like to tell. After the release of Meta Trader 4, we will come back to this interesting topic and will consider specific examples described above.




Alexander Ivanov

No comments: