Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
2|Page
FREE! You Now Own Resell and Giveaway Rights to This Report
Greetings! By owning resell and giveaway rights, you may freely distribute this report to anyone you wish, resell it for any price and keep 100% of the profits, or just give it to your friends as a great gift. The choice is yours. The only restriction is that you cannot modify this document in any way without permission from the author. Enjoy!
Hot Tip: If you would like to learn how to earn easy money from this report take a look at the last page for full details.
3|Page
Table of Contents
Introduction .............................................................................................................................................. 4 An Overview of What Were Doing........................................................................................................... 5 Basic Building Blocks ................................................................................................................................. 6 Lets Get Started ........................................................................................................................................ 7 Setting Our External Parameters .............................................................................................................. 8 Setup Internal Variables ............................................................................................................................ 9 Initialize Our Expert Advisor ................................................................................................................... 10 The Start Function ................................................................................................................................... 11 Custom Functions.................................................................................................................................... 12 Dont Forget to Clean Up! ....................................................................................................................... 15 Enjoy the Fruits of Your Labor................................................................................................................. 16 Pay it forward and Make Some Money along the Way! ......................................................................... 17
4|Page
What is my motivation?
Following my last eBook Newbies Guide to MQL4 Objects I decided to write another short eBook that I thought might be useful to new programmers. In this book Ill show you how you can build your own Expert Advisor (EA) that will trail your open orders automatically. We will use a moving average (MA) for the price we wish to move the stop loss on each new bar. As with all my efforts to educate and help people this book will be aimed at complete novices to programming so there will be no complicated explanations or long words that basically dont really mean much anyway. I want EVERYBODY to be able to grasp the simplicity and power of programming in MQL4 so that they might then go on and start creating their own expert advisors and indicators for their own personal strategies. If I cant explain it to you then Ive not done my job properly and that is not something I intend to let happen. Once you get some more experience and have built a few EAs yourself you can then branch out and start finding clients and coding their strategies too. You can actually make this a business if you just apply yourself and allow yourself to become successful. For a little bit of motivation be sure to read this interesting article about what you need to be an MQL4 programmer. Its not as much as you might think! Do You Have the Right Mindset to Become an MQL4 Programmer? My only wish is that you will learn something and be able to apply it in your live Forex trading and start making some money. Good luck and enjoy the journey! maziro
Introduction
5|Page
6|Page
7|Page
Youll then be presented with a wizard that will walk you through the process of creating a blank template for our expert. On the first screen select the Expert Advisor button.
Then click Next. Give your expert a unique name. It can be anything you want but for this project we will call it Move_MA.
You can fill out the other fields or leave them as their defaults it doesnt matter. Click Finish. Your EA template has now been created and were ready to move on to the next step.
8|Page
It looks like a lot but its actually not. The words on each line after the // are just comments which you dont really need to type but I just left them here so you can see what each parameter is intended for. The only variable that may not be obvious is the exBuffer variable. This will allow the user to specify a buffer in pips from the trailing MA. For example it is set to 0 then our stoploss will move EXACTLY along with the MA. If we set it to 10 then our stoploss will trail at the price shown by the MA + or -, depending on the direction of the trade, 10 pips.
9|Page
Now the variable gdPoint can be used anywhere in the code. Again its name can be anything but just like functions we try to name it something useful. In this case I can see that it is a global variable (g), as in can be seen from anywhere, and that its type is double (d). We wont get into types here but you can learn more about variables in the sample video found here.
10 | P a g e
We also add a call to a custom function that we will write later on. The function will simply place a label on the screen that will inform the user whether the EA is active or not. Again you can ignore the comments (the lines that start with the double slashes) as they are only there for your information. You can type them if you want and its good practice to always comment your code but for this exercise you dont have to.
11 | P a g e
Here you can see that the start function is kept relatively short and has little code. That is by design. We dont want to clutter and confuse the code by sticking everything in one function. Instead its much cleaner and again good practice to put everything in functions that will perform one specific task. Here you can see the CountOrders, iMA and the TrailOrders functions. The iMA is whats called a built-in function as it pre-exists within the metaeditor environment along with many others that you can use. The other two are custom functions which means youll have to write them. This provides a great deal of flexibility and is what makes programming such a powerful thing.
12 | P a g e
Custom Functions
As mentioned above we need to write three (including the one we called in our init function) custom functions so lets get that done right now. The first will return the count of all currently open orders. If there are no orders then theres nothing to trail. You can type your functions anywhere but its good practice to put them below the start function. Functions are declared with return type of the function (if no return then the type void is used), the unique name you choose for the function and then any parameters your function expects. Then the body of the function is written within two curly braces as you can see below.
I wont go into detail about the code as it will just distract you but if you want to know more you can use the help system. You can see that the return type for this function is int which basically means a whole number. The function has empty braces after the name which means it doesnt expect any parameters to be passed to it.
13 | P a g e
Our next function is quite long and it will handle the actual trailing of our orders. Type this below the function you just wrote.
14 | P a g e
We must also create the function that is called from our init function which, if you recall, will display a helpful message on screen for the user.
15 | P a g e
This will simply delete the object we created in the init function when the user removes the EA from the chart.
16 | P a g e
Once youve got all that code typed in the only thing left to do is compile it and start using your new expert advisor. Hit F5 and the code will compile. If youve done everything correctly you should see the following message in the Errors tab at the bottom of the MetaEditor screen.
Congratulations! Youve created your very first expert advisor in MQL4. You should be really proud of yourself. To use the EA just go back to MetaTrader and drag it onto your chart. If you want to see the MA that the expert is using youll have to place that on manually as this EA, like most others, wont draw the indicators it uses on your chart. Just make sure you use the same settings for the MA as you have in the EA or it will look as if the EA is moving the stoploss incorrectly when its not. Well I hope you enjoyed that brief introduction to writing a useful EA in MQL4 and I wish you luck on your journey to becoming a competent MQL4 programmer. Good luck! maziro PS: If you would like to have your Forex strategy coded by a professional MQL4 programmer with over 10 years of coding experience then please contact Steve Fleming at steve@automatedtradingsoftware.com for a quote. Want to Learn MQL4 in the Shortest Possible Time? <<CLICK HERE>>
17 | P a g e