Now that we’ve covered how to create a simple forex expert advisor using the MQL Wizard, it’s time to take things up a notch by learning how to make tweaks on the code itself. Don’t worry, we’ll take it easy by looking at the basic structure of an EA first. You can’t do surgery without a thorough understanding of human anatomy and how the organs work, right?
For forex robots, our organs are called functions and these are responsible for interpreting and translating market information such as price ticks and values of technical indicators into trade signals or orders, much like how your heart and lungs work together to pump oxygen to your blood stream transmitted all over your body and other organs.
If a function isn’t working properly, it could lead to more errors in other parts of the program so it’s important to understand how these functions relate to one another. Here are the basic parts that you should know about:
The header isn’t really a function but rather an introduction containing general program information, such as the system name, author, and copyright details.
Other input parameters such as the lot size, percentage risk, initial stop loss, or trailing stop are also included in this part of the program. We’ll cover the types of variables and parameters later on in this series.
2. Initialization function
The initialization function is a special function that is executed only at the beginning of the program. You can pinpoint this part when you see a line that says init() on the code:
This function will start to run after you’ve implemented the EA on a particular currency pair and time frame before any other functions are executed. Some say that this is just an optional part of the code, as the rest of the program can still run without it.
In human terms, this is probably equivalent to your morning routine after waking up – some people have a routine while some don’t, but the rest of the day will go on with or without it.
3. Start function
Now here’s where things get exciting. The start function can be considered the brains of the program, as this is where decisions are made. This is generally where the bulk of the code goes since it is executed for every price tick and repeats in a loop for every new tick of data received.
The start function usually contains the conditions that must be satisfied for the EA to generate a trade signal, the exit rules, and other commands that can allow the program to add to open positions, reduce exposure, etc. Other special user-defined external functions can also be called in this main function.
4. Deinitialization function
As its name suggests, the deinitialization function is executed at the end of the program. Just like the init() function, the deinit() function runs only during the program termination and is an optional part of the code.
This function runs when a price chart is closed, when switching between trading accounts, when changing the time period, when changing parameters on the chart, and when the EA is recompiled on the MetaEditor window.
5. User-defined functions
As mentioned earlier, the main start() function of the code can contain lines that access other special functions defined by the user in another part of the program. These can involve specific rules for position sizing, order count, trailing stop, and many more.
Some forex EAs can have very concise start() functions, as the lines have shortcuts to call upon other user-defined functions created outside the main body of the program. This way, debugging is easier as the flow of the program can be seen on the start() function but the nuts and bolts can be adjusted in a different section.
For instance, the start() function says that the robot should clap, nod, then wink every time EUR/USD crosses above the 200 SMA on the daily time frame. The actions involving clapping, nodding, and winking are detailed in a separate part of the program outside the start function. This allows the user to modify the components of the start() function by reversing the order to wink, nod, then clap or by making the robot clap twice without having to rewrite too many lines of code.
So far so good? If the details still seem a little sketchy for now, don’t fret! We’re just getting started! Next week we’ll take a look at the different types of variables and how these can be used in a forex EA.