Thought I'd start a basic MT4 EA 101 thread. Feel free to add some comments/examples
To create a very basic EA, we need to do the following:
In MetaTrader, select Tools/MetaQuotes Language Editor to bring up the Editor used to write EA's.
Then in MetaEditor, select New or File/New to bring up the MQL Wizard
And as shown below, Select Expert Advisor (template) and click Next.
Now perform the actions shown in the below left diagram and click Next.
As shown in below right diagram, make sure everything is unchecked and click Next.
There is a third dialog that involves Tester event handlers which is not shown here. Make sure all of these are unchecked and click Finish.
After unchecking all checkboxes in Tester event handlers dialog and clicking finish, the MetaEditor window displays the template code used to build the EA as shown in below left diagram. Notice the computer generated code for variable InitCount which we created earlier. The copyright and link has also been copied.
Now click Compile to generate the object code used to run the EA. In MetaEditor, the bottom Toolbox pane (not shown here) shows the source file name and if there are any errors/warnings. The diagram on the right shows the files created for this EA in Windows Explorer. In Windows 7 64 bit, it's usually C:\Users\User\AppData\Roaming.....\MQL4\Experts\ as seen in below right diagram.
If you can't find where your files are stored, then when you first open MetaTrader, click on the Journal tab in the bottom Terminal pane and it will display a message like:
Data Folder: C:\Users\User\........
I find it quite useful to create a desktop shortcut to the MQL4 directory as it saves time from clicking on 20 folders.
Now in MetaTrader, you should be able to see your EA name in the Experts Advisors branch of the Navigator Pane/Tree ("EA101_01_01a") as seen in below left diagram (if not, restart MetaTrader). Open up a chart, then right click on EA101_01_01a and select Attach to a chart. A properties dialog box is displayed as shown on the below right diagrams.
Notice some of the settings that you inputted earlier are displayed. There is even a version number "1.00" which is also in the computer generated code posted earlier. The inputs tab is an important one because this is where you would define important global variables such as Lot Size, Stop Loss and Profit Target values. At the moment, we just use InitCount, which is used in posts following.
Click Ok to attach EA to chart.
Now as you can see in the red circle on the chart of the below diagram, your EA is attached to the chart, but we do not have a smiley face here. This is because in the Common tab (details not shown) in the above bottom right diagram (properties dialog), we did not check checkbox for allowing live trading and also we have allow automated trading disabled in Tools/Options/Expert Advisors. We do not need to worry about this for now as we are not placing any trades in this EA and near future posts.
Note: If EA is attached, you can bring up the properties dialog by right clicking on the chart and selecting Expert Advisors/Properties (or by pressing F7).
Also notice here that any Expert Advisor messages are displayed in the Experts tab of the bottom Terminal pane. This will be referred to in posts following. To remove EA from chart, you can either close the chart or keep chart open and right click on chart and select Expert Advisors/Remove. And there you have it, your first EA. So basically how this EA executes is as follows:
Execute all code in OnInit() first.
Every time a tick occurs, we call OnTick() provided OnTick() has finished executing.
And when the EA is removed, we call OnDeinit().
To create a very basic EA, we need to do the following:
In MetaTrader, select Tools/MetaQuotes Language Editor to bring up the Editor used to write EA's.
Then in MetaEditor, select New or File/New to bring up the MQL Wizard
And as shown below, Select Expert Advisor (template) and click Next.
Now perform the actions shown in the below left diagram and click Next.
As shown in below right diagram, make sure everything is unchecked and click Next.
There is a third dialog that involves Tester event handlers which is not shown here. Make sure all of these are unchecked and click Finish.
After unchecking all checkboxes in Tester event handlers dialog and clicking finish, the MetaEditor window displays the template code used to build the EA as shown in below left diagram. Notice the computer generated code for variable InitCount which we created earlier. The copyright and link has also been copied.
Now click Compile to generate the object code used to run the EA. In MetaEditor, the bottom Toolbox pane (not shown here) shows the source file name and if there are any errors/warnings. The diagram on the right shows the files created for this EA in Windows Explorer. In Windows 7 64 bit, it's usually C:\Users\User\AppData\Roaming.....\MQL4\Experts\ as seen in below right diagram.
If you can't find where your files are stored, then when you first open MetaTrader, click on the Journal tab in the bottom Terminal pane and it will display a message like:
Data Folder: C:\Users\User\........
I find it quite useful to create a desktop shortcut to the MQL4 directory as it saves time from clicking on 20 folders.
Now in MetaTrader, you should be able to see your EA name in the Experts Advisors branch of the Navigator Pane/Tree ("EA101_01_01a") as seen in below left diagram (if not, restart MetaTrader). Open up a chart, then right click on EA101_01_01a and select Attach to a chart. A properties dialog box is displayed as shown on the below right diagrams.
Notice some of the settings that you inputted earlier are displayed. There is even a version number "1.00" which is also in the computer generated code posted earlier. The inputs tab is an important one because this is where you would define important global variables such as Lot Size, Stop Loss and Profit Target values. At the moment, we just use InitCount, which is used in posts following.
Click Ok to attach EA to chart.
Now as you can see in the red circle on the chart of the below diagram, your EA is attached to the chart, but we do not have a smiley face here. This is because in the Common tab (details not shown) in the above bottom right diagram (properties dialog), we did not check checkbox for allowing live trading and also we have allow automated trading disabled in Tools/Options/Expert Advisors. We do not need to worry about this for now as we are not placing any trades in this EA and near future posts.
Note: If EA is attached, you can bring up the properties dialog by right clicking on the chart and selecting Expert Advisors/Properties (or by pressing F7).
Also notice here that any Expert Advisor messages are displayed in the Experts tab of the bottom Terminal pane. This will be referred to in posts following. To remove EA from chart, you can either close the chart or keep chart open and right click on chart and select Expert Advisors/Remove. And there you have it, your first EA. So basically how this EA executes is as follows:
Execute all code in OnInit() first.
Every time a tick occurs, we call OnTick() provided OnTick() has finished executing.
And when the EA is removed, we call OnDeinit().