Expert Advisors in MetaTrader 4

Kirill Eremenko
A free video tutorial from Kirill Eremenko
Data Scientist
4.5 instructor rating • 118 courses • 1,574,580 students

Lecture description

In this tutorial we will introduce a new type of program in MQL4 – expert advisors.

In essence, expert advisors (EA’s) are your Forex robots. There are some key differences between expert advisors and scrips, and we will discuss them in this tutorial.

Also, we will create our first test EA and see how it works in the MetaTrader 4 trading terminal.

Source code below video. Important: remember that this is no longer a script – it’s an expert advisor now, and therefore you need to select the associated option in when using the MetaEditor Wizard.

Learn more from the full course

Algorithmic Trading In Forex: Create Your First Forex Robot!

Trading With Forex Robots: Learn MQL4 Programming By Doing! Create Your First Algorithmic Trading System in MetaTrader 4

06:24:19 of on-demand video • Updated September 2020

  • Learn what forex algorithmic trading is
  • Quickly & simply learn basic MQL4 programming
  • Learn elementary concepts of forex trading system design
  • Send, modify, and close your first market order
  • Learn how to create, test, and optimize algorithmic trading systems (expert advisors)
English [Auto] Hello this is Carol from Fargo dot com and welcome to the 21st tutorial on algorithmic trading for beginners. And this is going to be a very exciting tutorial because we're moving onto a new section which is called putting everything together. And as you can tell from the name of the section we're actually going to be putting all of the knowledge we've accumulated so far in MQ all four we're going to putting it all into action and we will be building our first standalone fully functional for X robot. And I hope you guys are excited as me and this is going to be fun. It's going to be a very interesting thing. This is the second last section after this we will just have one short section for a course summary where we'll wrap up the course. So pay attention this is where all the magic happens. All right. And today we're talking about expert advisors and I'm kill for you so far. If you have noticed we've been working with scripts predominantly and it was a very convenient way to learn the language and to understand how basic trading operations are executed. However when we move on to creating Forex robots or standalone algorithmic trading systems which can function over a long period of time and our new profits we will be working with expert advisors. Well that's that's the name they're given in em kill for and it's arrogance. Find out what the main difference is between them and the scripts that we've been looking at. So let's have a look. This is a script Well this is how a script works. We attach a script to a chart and then the script is executed it has a start of execution and a finish. Basically any program has a start of execution and a finish for the script the way it works is assume you attach it to the chart that On-Star function that within the body of which we've been writing all of our code it gets cold. And as soon as it is it is executed when that function finishes working the script completes its life. And basically that's it. The execution has been completed. Whereas expert advisors are a bit different expert advisors are designed to continue working for a long period of time and the execution part likelike and you program. They have a start and finish because sooner or later you'll probably close them take them off the chart or close a terminal or something. But until that happens this is how it works. First of all the there's three functions three main functions within an expert advisor. And originally the ON IN IT function is called and that allows your program to set up the environment it wants to work and do some in the initializations of variables and so on. So that function gets execute only once when the program is when the expert advisor is launched. Then there's the tick function and that is the main working function that's like the the main body or the heart if you will of the expert adviser. And what it does is it gets executed on every tick so every time a tick comes in to your terminal meaning a new price or change in price in the instrument that exported Wiser's is attached to every time a new tick arrives this function gets executed gets executed and then the expert advice waits for another tick to arrive and then gets executed again and again and again and so on. So that just keeps happening until the expert advisor is taken off the chart and that there may be may be many causes to why it's taken off the chart or why it has been forced to complete its execution. And once that happens when the execution is complete the expert advises still has a few brief moments to finalize its work by actually calling on DNA to function and that function just cleans up or basically any code that you put in there will be executed upon termination of expertize. So that's how they look. Schematically what the different looks like between the scripts and expert advisers might be a bit hard to grasp before actually looking at the code understanding and having a feel for what's happening so why don't we just jump to Mitta editor and have a look there. And so this is meant as sir and here are the scripts that we've been working with. And now we're going to create a new expert analyzer which should appear in this different folders separate folder for experts. It should appear here. So now we're going to look at new years. As usual we'll use them for Wizard. But this time will select expert adviser up here. We will call this expert adviser. My first D.A. and will stick to that name throughout the throughout the section. So just building up on that same file. Don't forget to put your credentials in there next. We don't need to click anything here. Nothing here either. And there we go. Click Finish. And as you can see the template has been set up. And the expert advice that has appeared here my first E-A. And as you can see there's three functions on it on d it and on tick. So this function gets executed. Initially this function gets executed when the expert visor is terminated and this function gets execute on every take. So for further ado let's populate some text or some test function some test code into these functions and see how they'll work. So when we attach the expert adviser to the chart why don't we say tell the trade of that it has been attached. Launched There we go then on tick every time a new tech arrives Usually it would have some analysis here or some sort of rule based decision making which we'll talk about in the next tutorial. But for now we'll just also have an alert. We'll just put an alert in here saying that maybe we'll tell the trade of what the new bid price is the price is. And then as we remember we need to concatenate it. So put a string in here and we'll look for the bid price which is the in-built bid variable. I hope by now that you're relatively comfortable with this type of statement. And finally on Dean at functionals say Alert. Expert adviser terminated about that. All right let's compile that no errors or warnings. Go to the terminal and see what happens here. So there's your adult chart there that's our count and now we usually we looked for scripts in the scripts tab or folder. Now we're going to look in the expert as well. Here is my first E.A.. So if we dragged that over right away we all see this Pop-Pop because expert advisors normally have external variables but ours doesn't at the moment. So I'll just let let it play. As you can see expert wise has been launched. Now the program is waiting for a new tech to arrive for it to do something about it. So let's just hold on hold on patiently. See what happens when it does arrive. There we go. And as you can see Tich has arrived and the price has changed. Now we'll wait for another take the. There you go. As you can see quite a few tricks have arrived. Every time the expert advises telling us what the new Price is all right now we will terminate the expert adviser and see if we get a message. So we right click on the chart and we look at expert advisors and we will remove our expert advice and expert advice removed. And as you can see we have a message here expert adviser terminated. So there you go. That's what expert advisers are in a nutshell and how they're structured internally. And we will be building up on that structure going forward. And in this is coming towards will create our very first standalone trading robot. And thank you very much for watching and have a play around with that. I hope to see you on the next tutorial. Until then happy coding.