00:00
I'm sorry.
00:10
Hello and welcome to the Toolkit for Traders YouTube channel where we give you the
tools for trading success. And in this lesson, we are doing the third episode in the MQL5
programming tutorial series.
00:26
So in this lesson, we're looking at the building blocks of our code, the sort of basic Lego
pieces, if you like, that help us to put together our expert advisors or indicators, if that's
what you're doing. And these apply to pretty much any program language you come
across. So pay attention to this one, because this is a fundamental part. And hopefully,
you can understand using the analogies I'll be using.
00:53
what we're talking about and I'll try and do it in a really, really clear way.
00:59
So today we're looking at lines in code and how they are like sentences. There's nothing
to be scared of what a line of code is. We're looking at variables, how to make them,
what they are. And we're also looking at comments, which is an underused but really
important part of coding.
01:19
So if you like this video, like the content on this YouTube channel, please like and
subscribe. I'm trying to build something really cool here with lots of free content about
how you can automate your trading to give you more consistency and better results in
the markets.
01:36
So quote for this lesson, you can practice shooting eight hours a day, but if your
technique is wrong, then all you become is very good at shooting the wrong way. Get the
fundamentals down and the level of everything you do will rise. And that's Michael
Jordan, who most of you will know, famous basketball player, but it's important to get
the basics right when you're starting out, because if you start out doing the basics
wrong, can lead to things you didn't want later down the line. So let's get the basics right
and that's what we're gonna look at today.
02:06
So the first thing we're looking at is how lines of code are like sentences, and I'll show
you that here. So we're gonna use an analogy here of making a cup of tea. I'm from the
UK, we like a lot of tea. And if you wanna make a cup of tea, you have to go through
steps. And the first thing you do is put the cold water into the kettle.
02:29
And the next thing you do is switch the kettle on.
02:33
And the next thing you do is you get a cut from the cupboard. Now, what we've got here
is a bullet pointed list, numbered list. We've got the line numbers on the left hand side,
number one, two, three, with each step. And we've got the main instruction, get a cut
from the cupboard in this case. And we also use a full stop at the end of our sentences
in English, in most languages. And that's to add grammar and make it easily
understandable by the person reading it.
03:01
When we're writing for computers, we have to put things in a step-by-step format. You
know, most of us would know how to make a cup of tea, a cup of coffee. If we were
asked to do it, I could tell you, you know, Joe Bloggs, go make a cup of tea and you would
intuitively know all the steps to do that without needing all these instructions in
between. But with a computer, we have to go through every single step that we want it to
do as it often will not do anything that it's not told to do.
03:31
So we really need to break down our thought processes into really, really short and
simple steps in order to make them understood by computer and then executed by the
computer.
03:44
So a full set of instructions, you know, pour the milk in the cup. Once the kettle's boiled,
if the kettle's boiled, pour the water into the teapot, add a teabag to the teapot, wait for
two minutes for it to brew, pour tea into milk, drink and enjoy. So full set of instructions.
We've had to break it down. We could even break it down even further, you know, lift the
teapot to 45 degrees, et cetera, et cetera, which, you know, if you were asking a robot to
do.
04:11
a physical robot you would have to do. And it's the same for coding in EA. So let's have a
look at how this is similar to when we're writing code. So when we're writing code, we'll
have line numbers as we saw before on the left-hand side. And with those lines, we have
instructions on each. So on line 20, we have an instruction which is to create a variable,
which we'll talk about in a moment. And in this case, it's a double type variable.
04:40
We're gonna call that variable spread and we're gonna set that variable to ask minus the
bid, which are two factory setting variables within MQL5. And then we can print that,
which is a function, which we'll cover again in another lesson. We can say that the
current spread is, and then we can insert the spread we've just calculated into that
function. And notice here as well, that we have the semicolon like the full stop.
05:10
at the end of our sentences here and that just tells the compiler that that's the end of
our line, that's the end of that specific instruction. So whereas in a normal sentence we
use a full stop, in this language and in many other languages we use a semicolon.
05:30
So again, just to recap, we've got the line number on the left-hand side. We have the
main instruction like we did before, and we have the semicolon at the end to end that.
So the next thing we're gonna look at, which was on line 20 before, was variables. What
are variables and how do you make them? We've got a little picture of a baby in a
computer and I'll show you why I think variables are a bit like babies.
05:59
So variables are like storage boxes for data. You create a variable to store a bit of
information or data that is useful to you.
06:11
This makes it easier to read your code if you use good naming on your variables and you
can copy bits of data and reuse that information. There are also pre-made variables in
MQL5 for things like bid and ask that you can use for your convenience. So in this case,
you see we've got a box here and we're popping in the number 1.21546.
06:36
So how do we make a variable? Let's explain that. So when a baby is born, the first thing
the parent tells us is what kind of baby or what type of baby it is. So think about the baby
boy or girl being the type of baby that it is. We also have to know what type of variable it
is. Now there's not just boy and girl. Variables are very modern. There's many, many
types of variables which we can set the star. So.
07:03
When we make a variable, we need to tell the computer what type of variable we want,
whether that's text, number, true or false, or another kind. Once we know what the baby
is, when we're thinking about babies, once we know what the baby is, boy or girl, we
usually ask for his or her name. And as encoding, we need to name our variables as
well. So in this example, we have the type, sort of the boy or girl is an integer.
07:31
which is shortened to int, so we can just type int. And then we can name our variable. In
this case, we've called it max pips, which is not a good name for a baby, but a good
name for a variable because it is understandable. And if someone else was to read your
code, they understand that you're trying to set something around the theme of the
maximum number of pips. So here are the main types of variables that you'll use. There
are many more, but these ones are the key ones. So the first type is double, probably the
most common one that you will use.
08:01
And a double is any number that includes a decimal place like 3.14159 or 12202021.1.
It's useful for storing prices because prices usually have a decimal place after them.
Indicator values and calculations also often use double because they will have a
decimal place and numbers after the decimal place. For instance, here we've set a
variable up.
08:31
of the double type. We've called it pi, and we've set it to 3.14159. And then we've set the
semicolon at the end. So we've got the type. We've given it a name, and we've also given
it a value for the computer to store in that box, if you remember the box. We put the
number 3.145 in a box called pi, and the computer knows that the kind of thing in that
box is going to be a number with a decimal place.
08:59
Second type of variable we look at is an int, which is any number that does not include a
decimal place like zero, one, two, one, five, four, five or 12. It's useful for counting. So if
you're counting up, maybe the number of trades, you would use an integer. You store
ticket numbers when you get that returned from the MetaTrader, that will come as an
integer. And you can also use it for enumerating lists. So if you have a list of, let's say
names,
09:27
Classically, you might have long state or short state. They might be enumerated to zero
and one, which makes it easier for the computer to check whether a state is true or
false. So in this case, we have type again, which is int for integer. We've given it name
ticket, which is understandable. And it is important to give your names, your variables,
understandable names. And we've set that to one, two, three, four, five, nine.
09:56
with a semicolon at the end. Another variable type you'll come across is a string type.
This is to store readable alphanumeric text. For example, error sending by order to tell
you something, for instance, in that case. It's useful for chart symbols because they are
strings in MetaTrader.
10:23
storing error messages and showing words on the chart or creating user interfaces. So
here again, we set the type to string, we give it a name, my favorite symbol, and we set
that to Euro USD in these speech marks here. So in between the speech marks, we've
got our string and that's so that the computer knows where the start and end of our text
is. And that separates it from this text over here. And you see it goes green.
10:53
in the editor when you do that, semicolon at the end, and you've made a string variable.
The final main type of variable that we have is bool. It's a variable that will only store true
or false, and it's useful for checking if conditions are met before taking a trade. Checking
an order has been opened as expected, and you'll often see functions return a true or
false variable, which we'll look at further down the line.
11:21
a ball. So here we've set up a type ball. We've given it the name trade open and we've set
that to true and a semi colon. So we can use that set that to true. You could set that to
false and then you can check that true or false later down the line to see if you've got an
open trade. So the other types of variables you may come across are color and we can
use color for things like setting line colors on the chart or box button colors that kind of
thing.
11:50
We can set the color using a standard variable here, color red, which is in MetaTrader 5
as a standard one. We can give it an RGB code, starting with a capital C, a single speech
mark, and then three numbers separated by commas. So 25500, which gives you the
same red color as color red. We can also use a hex system if you're familiar with that,
starting with a 0x and then we can use FF.
12:18
which is equivalent of 255 and 0000.
12:25
We also have date time, which is useful for storing time and date information. So again,
like the color, we've got D with the single speech mark. We can set the year first, four
numbers for the year 2024, adopt the month, adopt the date, and then we can use two
further numbers separated by a colon for the time.
12:50
We also have structures and enums, but we'll not cover these just for now as they're not
vital to what we're gonna do, but we will come across them later down the line. And
hopefully as you get to practice these, they will become clearer.
13:05
So the last thing we're gonna cover in this lesson is comments and why comments are
important.
13:15
So what are comments? Comments are text for the coder only. So comments are not
read by the computer when it's compiled. The computer will do nothing with the
comments. It's just for you to write down what you're thinking when you're coding,
because if you code, for instance, for a week and then you leave your code for two
weeks because you're busy, you're doing something, and then you come back to it, quite
often you will find that you can't really remember what you were doing.
13:44
So it's really useful to put comments in because it helps to explain what each line of
code does and it makes it easier to come back to or if you're working as part of a team or
you're gonna share your code with other people. Comments are really, really useful for
that because it helps other people understand what your code is doing and why it's
doing it in that way. So there's two main ways to do comments. The first way is to just do
a double slash and we have a double slash here and that says that this single line here
is a comment.
14:14
So then we can write this as a, this is one way to comment code using two slashes. So
this will not be read by the compiler. It's just there for the human who's reading that
code to understand. Second way we can use the slash and then the star to start a multi-
line comment. We can write our comment over multiple lines and then we can close
that like a closed bracket with a star then a slash.
14:38
Comments vary under use, but don't make that mistake. Comment as you go along, use
comments to remind yourself of what each part of your code does, and you'll be
pleased with yourself later when you've done that, if you take a break from your coding
especially. A well commented piece of code is easy to improve and edit, especially in a
team. So keep your comments thorough and throughout your code. And also you can
use it to break up your code to make it more readable.
15:08
later down the line, because if your code gets longer and there's lots, lots of lines of
code, you can use the comments to give your eyes somewhere to go when you're trying
to read through different sections of it. So we'll go to the meta editor now and we'll have
a quick look on there about what we can do with what we've already learned.
15:28
So we're here in our MetaTrader and we're gonna go to the MetaEditor, which we can go
to by clicking on the IDE button here. You can also go to Tools and you can go to
MetaQuotes Language Editor and there is a keyboard shortcut for F4. So we can just
press F4 and we can bring up our MetaEditor. So we're gonna create an expert advisor.
So we'll go to New.
15:57
We'll go to expert advisor template. No, and we'll give this one a MQL five lesson three
name. We can set the author and we can set a website if we have one. And we'll go
there. We'll not click on any of these. We'll just go straight through to there. And we've
got a very basic expert advisor.
16:25
the framework for which we can work with. So what are we gonna do in this lesson?
Well, we're just gonna do a simple expert advisor. That's just gonna do something when
the expert advisor is loaded up. It's not going to do any trading or anything. We're just
going to put some writing on the screen. And what we're gonna do is we're gonna
16:55
use variables to do that, which we've been learning about today. So we've got here at the
top, we've got our three property information, which will leave as is. And we've got here
our init function. So this is what we're going to use today. The on init function runs when
the expert advisor is loaded onto your meta trader. So it runs once and it runs when
that's loaded on.
17:22
So that's what we're gonna use today. And what we're gonna do is we're gonna write a
comment on the screen. So we can use the function comment. We see here when you
type it in, it comes up with the helper. There's a function called comment and we'll see
how that can be used in a moment. So what we wanna do is we want to give the, we
want to put in here something like my name. So let's say Joe Blogs.
17:52
and we wanna put in my number on a new line, for instance. So we'll use a string that
tells the comment to put a new line in, to separate onto new nine. We're gonna put in a
number here, one, two, three, four, five, six. We're gonna put in pi, 3.14159 as a number
as well.
18:20
and we're going to put in a new line again. So we're just kind of writing some nonsense
on the screen, but we're going to use some variables. The first thing we're going to do is
we're going to try and get this to run, and then we're going to set a variable called is true.
In fact, we'll just say true. And we'll put a semicolon at the end of that. We'll press save,
which is Control S. So we've saved that.
18:49
So what we've got here is we've got a comment, we're gonna write some things in here.
We've got a string, a new line, we've got an integer, we've got a double here, we've got a
new line again, and we've got the word true. And in fact, we will put another new line in
here between the integer and the double. So we'll do our backslash N in the double
speech marks.
19:19
and the computer will do that. So we'll click save and then we'll click compile. We're
happy with it. The code is generated without any warnings, which is lovely. So we go over
to our meta editor, meta trader now, and we look for the expert advisor we just made. So
we come down here into the navigator panel, expert advisors, and we're looking for MQL
lesson three, which is the one we just made, we'll come and drop this on the chart. I'll
untick allow.
19:49
go trading, it's not gonna do anything at the moment, but just to be safe. And you can
see we've got our right up here in the corner, we've got Joe blogs, 123456. We've got
3.14159, which is PI, and we've also got true. So that's pretty good. We've got some
things to write on the screen, but we wanted to use variables in this lesson. So what
we're gonna do is we're gonna change these to be variables. So what we're gonna do is
we're gonna create some input variables for our expert advisor.
20:18
So we're gonna use the phrase input, and then we're gonna set the type. So input sets
the variable to be an input variable for your expert advisor. So if you were using, let's say
a simple moving average, you might have an input of the period for that moving average
as an integer, and you would set that to say 200. But when you load up the expert
advisor into MetaTrader, you'll be able to change that without having to come back in the
code. We'll see that in a moment.
20:48
just bear with me. So first thing is we're going to have a variable, we've given it a type of
string. I'm going to call this my name. I'm going to change that to Joe. Blogs.
20:58
That's the first one. The second one we wanted was an integer. So we'll give it an input
again. We'll have an in and we'll give this one my number as a name. And we'll give this
one, not using double speech marks. We're using one, two, three, four, five, six. And
we'll use the semicolon to end that line. Next, we have an input for a double and we'll
use the name pi.
21:27
And we'll give this a value of, and I say we give this a value, we've not used the equal sign
on any of these. You need to use equals to set the value of those. So we'll give this a
value of 3.14115926535, which is as far as I know, pi off by heart. And then we'll go to
input bool. So we'll use a true or false and we'll say is true.
21:56
is the name of that variable, and then we'll set this to true. So we have those, but if we
now look at our code and we go to our expert list, we can see the properties. Nothing's
changed on here. We've got no input variables. So we're just going to remove that now.
We'll put it back on.
22:21
and they're not showing.
22:27
So we need to go over here and we'll change this to my name. So now we're, instead of
writing it directly in the comment, we're going to use my name is Joe Bloggs there. We're
going to use my number.
22:45
We're going to use pi and we're going to use is true as our variable names. So now we've
got in here our variable names and there, we also need to set another function here
because we've got a second one, a D in it function here. And this is run when the expert
advisor is removed from the MetaTrader. So this is important because we've set our
comment to be the line of text we just had.
23:14
But if we remove the expert advisor, that comment will remain there forever, which can
be really annoying. So we'll just set the comment to blank, which is two speech marks
with nothing in it. And that will remove that when we remove that on there. So we
compile the expert advisor, which we didn't do before, which is why it wasn't showing
up, but there we go. So now we've put our variables, we've used the variables we've
made here, we've transferred them into our function. So now when we load up this
expert advisor,
23:44
it will use the variables we set up here. So it will say comment and then look for my
name. Now it will go to the my name box. It will pull out the word Joe blogs and it will
then put it into the comment function for that to use. So that's the process by which that
works. So we'll go over to MetaTrader now and we look at the, we have the same
numbers here. So we'll just do the, we'll just remove that for now.
24:11
And you can see the comments disappeared because we put the D in it function in.
We'll now load that back in. And you can see now we've got inputs. So we can click on
this inputs tab here and we've got the name Joe blogs and all the values we set press.
Okay. And you can see they come back up. Now, if we go back into the expert advisor
and we change the name from Joe blogs to John Smith, and we do that.
24:39
The expert advisor is reinitialized and we have the name John Smith and we can use that
and we can change all of the variables in here. So we can change the number to 67890.
We can change the double to one point for blah, blah, blah, blah. And we can change
the value to false. So we change all of those, we press that and we've got different
values showing there. Last thing we can do is we can say reset here.
25:06
And that will return to the default values that we originally set, um, in the expert advisor
in the code. So we can do that and we're back to square one with that. So that's what
we're going to cover today. We're going to expand on that a bit more in the next lesson,
but, uh, I hope that's been useful to see what variables are, how you make them, and
how you can use them in MetaTrader. So in the next video, we'll be getting our code on
again. We'll
25:33
and do a recap on variables. And we'd also look at functions and what functions are and
how they're like factories, where we have an input, we churn through those inputs, and
we have a different output. It's where we start to do something clever with our code
rather than just putting names or numbers on the chart. And we can start to do things
with those variables that we've created. So if you have liked today's video, please like
and subscribe or drop a comment in below saying what you thought was good, what
could be better.
26:01
and ask any questions and I'll try to answer as many as I can. Getting back into the
YouTube business again, making videos. So I'll be much more active in the future as I've
had a bit of a time out, but getting back into it now. So if you do have any comments or
questions, we'll be looking at those. So please leave them below. Thank you very much.
All the best.