Expert Advisor Creation
Expert Advisors are programs that allow automation of the analytical and trading processes in the MT4 platform. To create an Expert Advisor (or “Expert”), the expert editing program – MetaEditor – has to be opened from within the MT4 platform. To open the editor (see Figure 1):
- In the Navigator window, right-click on Expert Advisors and select “Create”; or
- In the Main Menu > Tools > MetaQuotes Language Editor; or
- Click on the MetaEditor icon in the Standard Toolbar: or
- Press F4 on the computer keyboard.
|Figure 1 – There are several ways to open the MetaEditor.|
Any of these actions will open the Expert Creation Wizard. The Wizard can be used to create Expert Advisors, Custom Indicators, Scripts and DLLs. To create an Expert Advisor, select “Expert Advisor” and click “Next” to continue, as shown in Figure 2.
|Figure 2 – MT4\’s Expert Advisor Wizard is used to create Expert Advisors, Custom Indicators, Scripts and Libraries (DLLs).|
The “General Properties of the Expert Advisor” window appears. Here, traders must specify the:
- Name – A user-created name for the Expert.
- Developer – The developer’s name.
- Link – To the developer’s website, if applicable.
- Inputs – the list of Expert inputs
To add a new parameter to the Inputs field, press the “Add” button. For each Parameter, the trader must specify the Name, Type and Initial Value, as shown in Figure 3. To delete a parameter, highlight the parameter and press “Delete.” These become the Input Variables within the Expert. Once all the inputs have been listed, click “Finish” to continue.
|Figure 3 – Create the input variables by identifying Name, Type and Initial Value.|
A new window appears in the programming environment. The Expert’s name appears at the top of the window, and the previously entered input parameters are listed near the top of the code, as shown in Figure 4.
|Figure 4 -The Expert name and inputs appear in the code window.|
From here, the Expert code can be entered into the window using the MQL4 programming language and syntax (see Figure 5).
Note: Specifics regarding programming are outside the scope of this tutorial; understanding programming logic and learning a specific language require significant effort. Traders can learn more about programming in the MQL4 environment by reading the MT4 Help Guides and participating in the active MQL4 community forums.
MQL4, like other proprietary languages, has a list of Reserved Words and Standard Constants that are used during programming. Examples of constants for trade operations, along with their descriptions, include:
- OP_BUY – Buying position
- OP_SELL – Selling position
- OP_BUYLIMIT – Buy limit pending position
- OP_SELLLIMIT – Sell limit pending position
- OP_BUYSTOP – Buy stop pending position
- OP_SELLSTOP – Sell stop pending position
|Figure 5 – part of the code for an Expert Advisor. Certain words have predefined uses; here, OP_SELL instructs the computer to sell if other criteria are met (if statements).|
Traders can find a MQL4 Reference in the Help tab of the Toolbox in the MetaEditor window. This Reference includes information that is helpful to beginner and experienced programmers including:
- Standard Constants
- Predefined Variables
- Program Run
- Account Information
- Array Functions
- Common Functions
- Conversion Functions
- Custom Indicators
- Date & Time Functions
- File Functions
- Object Functions
- Trading Functions
Expert Advisor Compiling
After the Expert development has been completed, it must be compiled in order to ensure that the code has been written in the proper format needed to run the Expert. To compile the Expert:
- Select File > Compile (see Figure 6); or
- Click the Compile button on the toolbar; or
- Press F5 on the computer keyboard.
Once compiling has been initiated, an update appears in the Toolbox beneath the code in the MetaEditor window, as shown in Figure 6. An errors or warnings will be listed.
|Figure 6 – Successful compiling with zero errors and zero warnings.|
After successful compilation, the new Expert will appear in the Navigator – Expert Advisors window, as shown in Figure 7. If the Expert did not compile successfully, it will still appear but its icon will be gray and the Expert cannot be used.
|Figure 7 – The new Expert now appears in the Navigator-Expert Advisors window.|
Expert Advisor Setup
Before the Expert can be used, its parameters must be defined in the Terminal Settings window. To open the window:
- In the Main Menu > Tools > Options; or
- Pressing CTRL + O on the computer keyboard.
- Either action will open the “Options” window. Select the “Expert Advisors” tab, as shown in Figure 8.
|Figure 8 – Select the “Expert Advisors” tab in the Options window to define an Expert\’s parameters.|
The following settings are available in the “Expert Advisors” tab:
- Enable Expert Advisors – this option allows the user to enable (check) or disable (uncheck) the use of all Experts.
- Disable experts when the account has been changed – this option disables the Expert if the account has been changes, such as from a demo to a live account.
- Disable experts when the profile has been changed – this option prevents Experts from launching if the profile has changed.
- Allow live trading – to enable Experts in real-time mode (rather than testing an Expert on historical data).
- Ask manual confirmation – to send trade confirmation prior to submitting the order.
- Allow DLL imports -to use DLLs to enhance Expert functionality.
- Confirm DLL function calls – to allow control over the execution of each called function.
- Allow external experts imports – to allow the Expert to access functions from other Experts or MQL4 libraries.
Once the selections have been made, click “OK” to close the window.
Expert Advisor Launch
- After the Expert has been created and setup, it is ready to be launched. To launch an Expert:
- Right-click on the Expert in the Navigator – Expert Advisors window and select “Attach to a chart”; or
- Double-click on the Expert in the Navigator – Expert Advisors window; or
- Drag-and-drop the Expert to the desired chart.
A window appears with Common and Inputs tabs, as shown in Figure 9. Review the settings in each tab and make any necessary changes, and then click “OK” to attach the Expert to the active price chart.
|Figure 9 – Make any changes to the Common and Inputs tabs before attaching the Expert to the active price chart.|
The Expert will now be attached to the price chart. Its name will appear in the upper right-hand corner of the chart. The Expert’s name will be followed by a smiley face, as shown in Figure 10, if live trading is enabled. Otherwise, the Expert’s name will appear with a frowny face, a dagger after the name indicates that all experts are disabled.
|Figure 10 – An Expert with a smiley face indicates that live trading has been enabled.|
The Expert is now ready to begin analytical and trading functions.
Expert Advisor Shutdown
To shut down an Expert, it has to be removed from the chart. To remove an Expert, right-click on the active price chart, select “Expert Advisors” and then “Remove,” as shown in Figure 11.
|Figure 11 – To remove an Expert, right-click the active price chart, select “Expert Advisors” from the drop-down menu, and then select “Remove.”|
Notes About Expert Advisors
- All Experts are shutdown if the Terminal is closed.
- If a chart is closed, the Expert attached to the chart will shut down as well.
- Adding another Expert to a chart will remove the previous one (a confirmation appears).
- Deleting the Expert from the Navigator window does not shut down an Expert of the same name on an active price chart.