Learning To Write EA Part 1
The series is initially based on the MQL4 book which can be found at the mql4website. The book is an excellent source to start writing your traders and also a source of a decent amount of platform and market details. Having a bit of an idea about programming does help in getting more out of the book and it does require more than a couple of reads to make one comfortably started. So here, we will try to summarize certain items, ignore but mention in passing certain non critical parts for beginners and hope to save you time in the process.
The MQL4 language is similar to C++ apart from the object oriented part and the more pressing topics like pointers and structures. It allows you to write simple algorithms to make your ideas into auto traders. The benefits are many. Apart from having an auto trader, your strategy testing, data gathering and evaluation of new indicators become a breeze. Even for firm believers in manual trading, this cuts down time spent in exploring alternate methods and makes you aware of the potential risks of your strategy.
Now for the basics. The whole platform can be broken down into components shown in the picture below –
That is –
- Market – The actual transaction area.
- Dealing Centre – The broker you have your account with.
- Metatrader 4 Client – The screen you see in front of you.
The assumption here is that you are already familiar with the metatrader 4 platform. The Dealing Centre here is your account provider –eg. FXDD, Forex.com, DFT. Traders, indicators and scripts use the same interface i.e. Metatrader4 client to deal with the Dealing Centre. To familiarise yourself, look at the left hand side of the default client window. What you see is the navigator panel listing down all the EAs, scripts and indicators that you can use with your charts.
Dive straight in. Certain EAs like the Moving Average come pre-packaged and is highlighted in the picture above. Right Click on it and choose ‘Modify’. This will open up the MetaEditor. A fully self contained environment for development of the EA.
As shown above, all the code is in Editing Area. The File Explorer area shows you other kind of files available in the directory. The files with extension .mq4 are the code files. Now that you are introduced to the development area, lets do a couple of things to familiarize you more and shift to description of the code.
As shown above, go to the top of the editing area, make a change in the line highlighted making the DecreaseFactor 5 from original 3. Now choose File->Save As and save the modified file as “Moving Average Mod”. Press the ‘Compile’ button on the top. Now shift to the Metatrader 4 client and restart it. Look at the Navigator window again and you should see your modified EA just below ‘Moving Average’ called ‘Moving Average Mod’! Congratulations! you have your first EA. Not quite but now you are familiar with essentially what it takes.
To check the modification that you did. It is a minor one. Attach the EA to the chart by right clicking on it and choosing ‘Attach to a chart’. The window will pop up. Select the inputs tab and this is what you will see –
Look at the default value of DecreaseFactor. It is 5 as you changed. Perform the same on ‘Moving Average’ EA and you will see 3. With this, lets end todays familiarization lesson with creating a fresh blank EA. Go to the Metaeditor. Choose File->New. The following screen pops up –
Choose ‘Expert Advisor’, press ‘Next’. Insert ‘First’ in the name section and press ‘Finish’. The editor area will now contain a default template and is essentially blank. Press compile as before and exit the Metatrader4 client and start it again. You will see your blank EA in the ‘Navigator’ window.
That is it for this part. Next, we will try and learn the ever important ‘data types’ and see what we can do with that in the blank ‘First’ EA we created.