Expert Advisors in MetaTrader 4

A free video tutorial from Kirill Eremenko
Data Scientist
Rating: 4.5 out of 5Instructor rating
58 courses
2,206,874 students
Expert Advisors in MetaTrader 4

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:23:57 of on-demand video • Updated September 2022

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 Forex 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 MKR for we're going to be putting it all into action and we will be building our first standalone, fully functional forex robot. And I hope you guys are excited as me. And this is going to be fun. It's going to be very interesting. This is the second last section in the 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 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 earn new profits, we will be working with expert advisors. Well, that's that's the name they're given in Q4. And today we're going to 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 the script to a chart and then the script gets executed. It has a star of execution and a finished basically, any program has a start of execution and a finish for the script. The way it works is as soon as you attach it to the chart that on start function, that within the body of which we've been writing all of our code, it gets cold and as soon as it hits, 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, like like any program, they have a start and finish because sooner or later you'll probably close them, take them off the chart or close the 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 an IT function is called. And that allows your program to set up the environment. It wants to work and do something initialization of variables and so on. So that function gets executed only once. When the program is when the expert advisor is launched, then, um, there's the UNTEACH function and that is the main working function. That's like the the the main the body or the heart, if you will, of the expert advisor. And what it does is it it gets executed on every tick. So every time a tick comes in to your terminal, meaning a new price or a change in price in the instrument that the expert advisor is attached to, every time a new tick arrives, this function gets executed, gets executed, and then the expert advisor waits for another tick to arrive. And then it gets to execute it again and again and again and so on. So that just keeps happening until the expert advisor is taken off the chart and that that there may be maybe 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 advisors still has a few brief moments to finalize its work by actually calling the on D and its function. And that function just cleans up or basically any code that you put in there will be executed upon termination of the expert advice. So that's how they look schematically. How what the different looks like between the scripts and expert advisors might be a bit hard to. Ross profound, actually, looking at the code, understanding and having a feel for what's happening, so why don't we just jump to Medda Ed and have a look there? So this is Matt Ed, and here are the scripts that we've been working with. And now we're going to create a new expert advisor, which should appear in this different folders, separate folder for experts. It should appear here. So now we're going to click new use the as usual, we'll use them for Wizard, but this time will select expert advisor up here. We will call this expert advisor my first day and we'll stick to that name throughout throughout the S. soldiers building upon 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 advisor has appeared here, my first EHA. And as you can see, there's three functions on in it, on Dehnart and on TEQ. So this function gets executed initially. This function gets executed when the expert advisor is terminated and this function gets executed on every take. So without further ado, let's populate some text or some test function as some test code into these functions and see how they'll work. And so when we attach the expert advisor to the chart one and we say tell the trader that it has been attached. Being launched. There we go then on tick, every time a new tick arrives, usually would have some analysis here, 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 trader what the new bid prices bid price is. And then, as we remember, we need to concatenate. So put a string in here and we'll look for the bid price, which is the inbuilt bid variable. I hope by now that you're relatively comfortable with this type of alert statement. And finally, in their own dehnart function will say alert expert advisor terminated. How about that? All right, let's compile that no errors or warnings. Let's go to the terminal. And see what happens here. So there's our euro dollar chart. That's our account. And now we usually we looked for scripts in the scripts tab or folder. Now we're going to look in the expert wiser's folder. Here it is my first day. So if we drag that over right away, we will see this pop up because expert advisers normally have external variables, but ours doesn't at the moment. So we'll just let let it play. As you can see, Expert Wise has been launched. Now the program's waiting for a new tick to arrive for it to do something about it. So let's just hold hold on patiently, see what happens when it does arrive. There we go. As you can see, Tick has arrived and the price has changed now. We'll wait for another take. There you go, as you can see, quite a few ticks have arrived, and 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're right. Click on the chart and we look at expert advisers and we will remove our expert adviser expert was removed. And as you can see, we have a message here. Expert advisor terminated. Um, so there you go. That's what expert advisers are in a nutshell and how they're structured internally. And we will be building upon that structure going forward. And in this in these coming to terms will create our very first stand alone 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.