Expert Advisor Programming by Andrew R. Young
Expert Advisor Programming by Andrew R. Young
Expert Advisor Programming by Andrew R. Young
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Working with Functions<br />
Libraries are stored in the \experts\libraries folder. The source code files have an .mq4<br />
extension, and the executables have an .ex4 extension. Libraries do not have a start(), init() or<br />
deinit() function. To declare a file as a library, you must place the #property library<br />
preprocessor directive at the beginning of the file.<br />
The advantage of libraries is that they are compiled, so if you need to distribute a function library,<br />
you can do so without exposing your intellectual property as you would if you distributed an include<br />
file. You can also make bug fixes to a library without having to recompile your expert advisors – as<br />
long as you do not make any changes to the function declarations, such as adding and removing<br />
arguments or functions.<br />
There are a few disadvantages to libraries as well. Since they are already compiled, it is not possible<br />
for the compiler to check if the parameters are correct. You cannot specify a default value for a<br />
parameter in a library function, which means you will need to specify a value for every argument in a<br />
function call. You cannot use external variables in a library, or create globally scoped variables that<br />
your expert advisor can access.<br />
You'll need to use the #import directive to import library functions into your expert advisor. If the<br />
library contains numerous functions, it may be best to create an include file with the #import<br />
statements. This increases the number of files you'll need to work with. Unless you have a very good<br />
reason to use libraries, it is suggested that you stick with include files for storing your functions.<br />
You can also import functions from Windows DLLs using #import directives. The WinUser32.mqh<br />
include file in \experts\includes has numerous examples that are used for the MessageBox()<br />
function. (We'll discuss the MessageBox() function in chapter 8). Using DLL functions is an advanced<br />
usage that we will not cover here. There are articles on the MQL4 website on using DLLs for those<br />
who are interested.<br />
A Simple <strong>Expert</strong> <strong>Advisor</strong> (with Functions)<br />
Here is our expert advisor source code, as it appears in the source code file. We'll assume that the<br />
functions we've created in this chapter are declared in the include file IncludeExample.mqh, the<br />
contents of which are listed in Appendix D.<br />
// Preprocessor<br />
#include <br />
// External Variables<br />
extern bool DynamicLotSize = true;<br />
extern double EquityPercent = 2;<br />
extern double FixedLotSize = 0.1;<br />
75