Sei sulla pagina 1di 17

1|Page

brought to you by maziro

Automated Trading Software

Want to Learn MQL4 in the Shortest Possible Time? <<CLICK HERE>>

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.

Want to Learn MQL4 in the Shortest Possible Time? <<CLICK HERE>>

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

Want to Learn MQL4 in the Shortest Possible Time? <<CLICK HERE>>

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

Want to Learn MQL4 in the Shortest Possible Time? <<CLICK HERE>>

5|Page

An Overview of What Were Doing


A brief outline of our objectives
No matter how long youve been trading Forex you will have had occasion to adjust your stop or tighten it up to lock in profits as price moves in your favor. Depending on your chosen timeframe this can be something you do often, as in every few minutes or hours, or not very much at all if you are trading the daily timeframe. If you find yourself moving your stoploss often then the EA Im going to show you how to build will most certainly be of use to you. It will do away with the need for you to constantly monitor your trade, set alert triggers and manually move your stoploss. This EA will do it all for you! Well be using a moving average (MA) that will trail a specified distance behind price and move your stoploss accordingly. The EA will be intelligent enough to know if your orders is a buy or sell, will display an informative message on the screen telling your if the EA is active or not and will also feature a safety precaution too. I included the safety check because quite often you will switch timeframes during the course of an open trade. By setting the EA to only operate on a specific timeframe we avoid the problems associated with changing timeframes and therefore changing the value of the MA that the EA uses. If you dont want this safety then just set exTimeFrame to 0 and enjoy the wild ride!

Want to Learn MQL4 in the Shortest Possible Time? <<CLICK HERE>>

6|Page

Elements Common to All Expert Advisors


There are a few things that are common to all EAs. They are the basic building blocks and youll need to have a general understanding of them before moving on. Theyre not complicated but they may seem strange at first. Allow yourself to accept the new ideas youre being offered and it will be much easier to progress. MQL4, like most other programming languages, has variables and functions. You will see me mention them quite often in this lesson so Ill give you a brief introduction. Variables are simply containers that can be referred to anywhere in your program and their contents can vary, hence the name variables. Think back to your days in school and the dreaded algebra classes. Where the teacher might write on the board something like x+y=z. Well x, y and z are variables just like the ones we use in programming. The simple math will always be the same, the only thing that changes are the values of x,y and z. Simple right? So now you understand what variables are how about functions. Well you can think of functions as a collection of variables and other code that achieves a specific task. Lets imagine you are trying to mimic the process of placing an order in Forex. Part of the process may require you to count all the currently open orders. Wouldnt it be great to just write a single line of code that would output that count? If you or somebody else where to write a function called OrderCount (the name can be anything but its good practice to make it something meaningful. The function would contain all the necessary details to extract the correct count of orders, it may be a few lines or it may be hundreds, the point is you dont need to know or even care. Functions are really useful in that they hide all of the complexity from us and we can simply use the functionality they provide just by referring to them. Youll see a few examples of functions and variables in the EA well be writing.

Basic Building Blocks

Want to Learn MQL4 in the Shortest Possible Time? <<CLICK HERE>>

7|Page

Lets Get Started


This step should be simple for you if youve ever looked at MetaEditor but just in case youve never looked at it heres how to do it. From MT4 hit the F4 key and youll see MetaEditor. Then click File -> New

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.

Want to Learn MQL4 in the Shortest Possible Time? <<CLICK HERE>>

8|Page

Setting Our External Parameters


Changing Settings Easily
Youve no doubt used many EAs and indicators and whenever you want to change their settings you just call up the relevant Settings box and change a few numbers right? Well thats exactly what were going to do here. We are going to add code that will allow the user of the EA to change settings easily. I wont be explaining in too much detail the actual code because I dont want to confuse things and you really dont need to know that right now. If youre interested you can always check out the help file on a particular constant (word) or function. At the top of your template code type in the following

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.

Want to Learn MQL4 in the Shortest Possible Time? <<CLICK HERE>>

9|Page

Setup Internal Variables


Weve declared our external variables in the step above but for this EA we are going to use an internal variable too. We may refer to this variable in a few different places though out the EA so we declare it outside of any particular function. That way its accessible to all the code. Itll make more sense as we go along so for now just accept the idea. Just before the init function type the following:

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.

Want to Learn MQL4 in the Shortest Possible Time? <<CLICK HERE>>

10 | P a g e

Initialize Our Expert Advisor


Getting the Expert Setup
The init function is called once when the ea or indicator is placed on the chart. It will also be called if you change time frames. Its where we put code that we only need to run once and its usually where we put code that will basically setup the indicator or EA. Next were going to type some code that will set our global variable to the correct value depending on whether we have a four or five digit broker.

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.

Want to Learn MQL4 in the Shortest Possible Time? <<CLICK HERE>>

11 | P a g e

The Start Function


Where it all happens
The start function is called on every single tick that is received from your broker. The best way to think of it is like a switchboard. Every time a tick is received the start function will execute and any code it contains will likewise be executed. Therefore its where youll place the heart of your code. Think of the switchboard operator in one of those old movies. A call comes in and she figures out where to route it. From here any other code is executed via function calls. Again the details are beyond the scope of this eBook but theres plenty of information available if youre interested to learn more.

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.

Want to Learn MQL4 in the Shortest Possible Time? <<CLICK HERE>>

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.

Want to Learn MQL4 in the Shortest Possible Time? <<CLICK HERE>>

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.

Want to Learn MQL4 in the Shortest Possible Time? <<CLICK HERE>>

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.

Want to Learn MQL4 in the Shortest Possible Time? <<CLICK HERE>>

15 | P a g e

Dont Forget to Clean Up!


Being environmentally friendly
Now the last thing to do is to make sure your code cleans itself after the user removes it from the screen. Many so-called programmers forget this simple but necessary step; make sure youre not one of them! Seriously though its such a simple thing to do I dont know why they dont do it. I guess its just lack of consideration or laziness. Try and get into the habit early of always cleaning up after yourself, its good coding practice. Clean up code goes in the deinit function. Similar to the init function this is called only once at the end of the indicators run just before it is permanently deleted from the chart. When a user decides to remove an indicator or EA this is the code that is ran. Type the following

This will simply delete the object we created in the init function when the user removes the EA from the chart.

Want to Learn MQL4 in the Shortest Possible Time? <<CLICK HERE>>

16 | P a g e

Enjoy the Fruits of Your Labor

Use your brand new EA for the 1st time

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

Pay it forward and Make Some Money along the Way!


I hope you enjoyed this free report and if youre feeling inspired and want to learn all the ins and outs of MQL4 then pick up your copy of my MQL4 Training Video Package Before you rush off to share this report with your Facebook friends and Twitter followers, let me ask you a question... Would you like to get paid a commission when the people you share this report with also buy MQL4 Training Video Package If so, then all you have to do is register as an affiliate of mine and then rebrand this special report with your affiliate ID before sharing it with others. That's it. You can then post it to your blog, share it on Facebook and Twitter, or even use it to build your list. And here's the best part... Your readers will also receive giveaway rights to this report. Which means, if they give it away without rebranding it (and many will) you'll get paid commissions on ALL of their referred sales as well. Example: Let's say Stephanie downloads your report, but does not rebrand it with her ID. She then shares YOUR branded version with Bill who buys my MQL4 Training Video Package. guess what? YOU just got paid... Cha-ching!

Here's what to do next...


Step 1: Download FREE rebranding software from this link Step 2: Using the software follow the instructions to rebrand the file. Step 3: Start sharing your new report with others and GET PAID! Want to Learn MQL4 in the Shortest Possible Time? <<CLICK HERE>>

Potrebbero piacerti anche