Hey this re in this video you will learn how to write code for the metatrader 5 trading platform even if you are an absolute beginner and even if you never touch code before also when I teach something I always want you to understand how things work so you will not only copy my code but I will explain everything in detail and this is super crucial to really understand how coding work so you will be able to write your Own programs in the future so so make sure to watch the whole video from the beginning to
the end do not skip around do not jump around in the video if you need more time and I know this video has become a little bit longer if you need more time pause the video come back at another day for example but do not just jump around it will not help you also I will put time stamps so you can always come back to this video to watch the parts again if you need some assistance With your own coding so save this video all already if you stick around and watch the whole video you
will learn all the essential Basics like variables data types control structures you can create your own functions etc etc you will really know how the metat 5 framework Works what you need to do exactly to write your own programs this is a skill that is so so so important if you want to become a successful and in best in the best case automated Trader I was not Able to trade manually there was just too much mental stress negative factors Etc that kept me from being profitable but this changed with automating my trading strategies and I
heard this from many of my students already I taught hundreds of people at this point how to code successfully for the metat Trader 5 and many of my students read their own programs some of them even provide coding services for others so I think I know one or two things about teaching How to code at this point and this is what you will hopefully experience in this video so have fun watching this video it's really a great chance for you to learn how to use the metatrader 5 so let's start right away with the coding
so to demonstrate you the strategy to explain quickly what we will create in this specific programming tutorial it is a really simple strategy which um follows some kind of dony and general logic if you heard of this indicator Before so we always take the highest point of the last X bars usually something like 20 bars for example and then we wait for the price to rise above the high of the last X bars so for example here at this point it happened and in this situation then if this trigger is here we open a by
position then we try to follow a long trend of course we do have a TP uh or a stop loss in place and we can also use a TP in this strategy which you will learn later In the programming part but one most important concept here is the trailing stop loss so this first trade was not successful it was closed in the stop loss but here you can see the second trade is already going a lot better and you can see that as soon as there are more candles going into the right direction the long
Direction we will Trail the stop loss at the last uh at the low of the last bar plus a specific buffer percentage so you Can see the stop loss if there is a long Trend will be trailed along until the trade is closed at the stop loss so this is what the program always does so it's a really simple strategy but I think it can be really effective effective or it can be a good basis for Effective strategies in markets like the us30 the US 100 or NASDAQ or any index pretty much or any Market
that has longlasting long trends for example also stock cfds so these were just some example trades So you know how the strategy performs and now we will have a look at actually coding this beauty so let us now start writing the code for this specific strategy so the first thing I want you to do is open the metat trader 5 probably know this platform if you are a Trader already so in the metat trader 5 we have a integrated development envirment or in short IDE we can find this in the metatrader 5 toolbar up here
or if you do not find it there you can Go to tools and then click on meta quotes language editor this will open the IDE for you and this is the environment where we can write the code for the meter Trader 5 and more important we can compile the code for the metat Trader 5 which means that we take the code that we can read as a human being and translate it into computer code or a executable program that the metatrader 5 can use to yeah apply or strategy um in a chart or at a
Symbol so what we do next is we want to create a new program so in the upper left corner you should find this new button and if you click it you will get to this um yeah this option menu here we want to select the expert advisor template it's usually pre-selected then we click on next and here we have to uh choose a name and um yeah I will call this Donan Channel um uh YT for Donan channel YouTube you can choose any name you like We have the author link and some param here but
we do not use this or we we do not change this because it's a lot easier to change this later in the code if needed then we click on next and here we can choose some predefined event handling functions um so we can choose to add them to our code you if you know that you will use one of these functions I don't think we will cover it in this tutorial but you could check these boxes but even if you do not check any boxes No worries you can add everything later in the code this
is the great thing about coding you can just add the code at any time you need it here are some more Testa specific event handling functions but it's the same you can add them here already but you can also add it later in the code and it's not even complex or complicated so usually I do not add any of these and just click on finish and this creates a new expert advisor for you so new expert advisors Generated with a template um always look like this so you have a lot of these gray lines you
have some properties you have more gray lines you have some of these um yeah we have three so-called functions which are also event handling functions so we will get to this in a second and then we have some more of these gray lines so let's have a look at the gray lines first um the the gray lines here are always comments what does this mean this Means that these are comments that are only relevant or important for the programmer so even if I write some BS stuff here it does not matter it does not have
any impact on the program at all so if I now use this compile button up here and if I click it this is exactly what I explained before once I click on this compile button the program or the computer or the The Meta editor and the compiler that is built in it will take this source code this text File pretty much and it will transform it to a executable ex5 file so how does this look um I can visualize this and this is what I like to do for myself also to make me or to
make you understand what's happening you can rightclick up here on this Don channel YouTube or whatever name you chose program and you can go to open containing folder and this will open the experts folder of your metat Trader 5 that you are currently working in and Here you will find all of the programs and you can see for example I do have this Don Channel yt. mq5 file and this is exactly the same name as this text file and this is the text file that we are currently working on then above this we have the
donin ch Channel yt. ex5 ex means executable and this is the executable file that we will then see in the meter Trader so if you go back to your meter Trader and here in the navigator on the left side usually if You do not see the Navigator you can open it by clicking view Navigator here you will see the expert advisors all of your expert advisors in the Navigator and here you will find also the expert advisor that you just created this is the ex5 file so what happens if I delete the ex5 file here
I delete the Donan Channel yt5 file and now if I go back to my metat Trader 5 and refresh here you can see the file is gone because I just deleted it right so In the metat trader you always see the ex5 files so since I deleted the ex5 file we have to get it back right so here in the in the meter editor again I do have the mq5 file here there is no ex5 but let's see what happens if I click on compile if I click on compile you will see the ex5 file
will be created here so I think I also have to refresh this right how do I refresh this let me just refresh it up here so you can see once I Did this the ex5 file is back here in this experts folder which means that it will also be back here in the metat trader file so this is the like relationship between source code and executable and everything is done using the build in compiler so the compiler is the tool that takes the human readable source code the mq5 file and turns it into the ex5
file so I'm sorry if I'm explaining this in detail and I know this takes a lot of Time but for me it is important to really understand and this is also what I built my programming tutorials on it's really the understanding of what is happening in the computer because this will make it Le less abstract and more understandable because coding is not something that is super uh weird or complex or you cannot understand it because it all follows it's it's just 100% logic so now that we have this mq5 file and we learned already that
these Are comments that do not have an impact I can delete all of these lines and compile and it doesn't change the thing but if I change but if I write something up here and try to compile without making it a comment we get a error message because this is not correct code so to make this a comment we just add these two uh slashes here and then if I compile there's no error and this will work and the compiler will skip simply ignore these lines because There is a comment you can also write multi-line
comments and they look like this so if I write multi-line comments like this it will be completely ignored by the compiler so multi-line comments start with a slash and the star and they end with a star and the slash so this is for comments do we need these comments they're not important for the program so you could delete all of them if you feel like they help you Understand your code later on or if you work with multiple programmers on the same code it's definitely a good thing to use comments maybe I will use some
comments here in this tutorial wait let me not go too quickly here okay so if we delete all of the comments we can still compile and there's no problem then we have these three properties here and let me explain what they do so in the metatrader 5 we already realized we do have this docent Channel YT executable File here and it's visible since it is here we can also use it in the chart so I could use this dony and Channel program and drag it on any chart where I want to activate the program of
course it doesn't do a lot because we didn't really add a lot of code but we could activate it already and it would just do nothing but we could activate it and what I want to show you here is that we see this copyright 2021 mid quot blah blah blah and there's a Link behind this where is this coming from this this is coming from the properties so if you go back to the code you can see we do see the copyright here so if we change this to 2024 for example and here you could
write your name for example I would type my name and then you can have a link for example my website and then you can change the version um like this so if I now go back here and activate the program again you Can see this is changed there's a different version number there's a copyright which is now the current year and my name and if I click on this it will open my website so this is really cool isn't it also by the way since we are on my website already this is a really cool
website you should check it out you can get free trading journal here free expert advisors I will explain how it works but the most important thing is in this top menu you will find The metat trader 5 Master Class so after this coding tutorial if you think that my style of teaching is good for you and you want to learn more advanced stuff about metat trader 5 definitely check this out because you will learn everything you need to know about metat trader 5 programming that can make you a fully automated Trader but let's get back
to the code here so you can see um these properties just change these basic informations when you Start the program so do you have to change this no can you change it yes yes you could also just delete everything and it still compiles so you don't need it but it's a nice information especially if you try or if you plan to sell your program or ship it to other Traders it's it's cool to have your like credentials here so everyone knows who created the program who put in the work but yeah so this is for
the properties they are like these specific properties Are just information for the for the for the user pretty much of the program there are more properties in metatrader 5 coding some of them are actually have a a a impact on the program really a functionality and you use a lot of properties for example if you create indicators but I will not dive deep into this topic because this tutorial is about creating this expert advisor so what else do we have we have three more functions left and these three functions Are so-called event handling functions usually
you can say that something thing is a event handling function because it is colorcoded in this Violet tone here and also it usually starts with on on something because it reacts or it is triggered on or by a specific event so what do we have here we have a on init function a on the init function and a ontick function what do they do they are triggered as I said at specific events so whenever the program is initially IED Or activated it will trigger the oninit function what does this mean so as I said before
we can activate our program on the chart already so let's do this let's take the donin Channel program and let's click on okay oh by the way you can see the properties or the information is now gone since I deleted it in the code but if I click okay here you can see in the top right corner of your chart it now says the name of your Program and there's this little head this means that this program is currently running on this specific chart immediat Trader five one expert advisor one program is always attached to
one specific chart so there's no possibility or it's not able to add multiple programs on one chart but you could of course open another let's say us3 chart and attach another expert advisor to this chart so now this specific program is Running in this chart which means that whenever something happens in this chart that might trigger one of these event handling functions the metat trader will then have a look at your specific program that is running in this chart and it will execute the source code or the the the statements the um yeah the the
code that is inside of this specific event handling functions function what does this say for example if we are in this oninit function we could say Whenever the program is initialized we want to write a text message in the expert's journal and we can do so using the print function and it's a very simple function um yeah let's just type on in it here so if I compile again you can see I now added something in the so-called body of this function the body is always wrapped by these curly brackets here so you can see
We have the opening curly bracket and we have the closing curly bracket by the way the placet the placing the exact placing of these curly brackets is not really important because the programming language is not I think you call it's not space sensitive or something um or the the spacing or the exact positioning of these brackets not important so if I move the opening curly bracket for example here I can still compile if I move the closing qu bracket here I can Still compile it doesn't change the code or the outcome so what you will
often see for example is a curly bracket placement like this the opening curly bracket after the function name in the parameter list we will get to this later and then the closing bracket at the very beginning of or be after the last line of the body of this function this is also the type of placement for the curly brackets that I personally like so in my code you will often see it like this but This again it doesn't have an impact on the program so what else did I do I added this print function here
and the print function is um also Violet coded because it is a predefined function it is part of the mql5 framework we can use it right away we don't have to write it or code it on our own um and then we can see that this print function also has a parameter here so after calling a function you always have to open the parameter list Or you have to provide the parameter list and some functions request multiple parameters some functions only which request one parameter some functions do not request a parameter at all this print
function here it requests one parameter which is a text so I could add any text here and have this printed in the experts Journal of my metat Trader 5 and then after providing the text we close the parameter list with a closing round bracket and we end the line with a Semicolon you end every statement with a semicolon so the program knows the statement is complete so what does this do if I now compile the program and I do have this print statement here we can go back to the metat trader and now I want
you to open the toolbox if it's not open already you can go to view toolbox and in the toolbox I want you to switch whoops Sala to The Experts top and here you can see I have a lot of on in it printed here so if I right click and Clear this and remove the program from the chart and if I activate the program again you will see that if I click on okay it prints on in it and this really really cool isn't it and this is because whenever you activate the program in the
chart this specific programs on init function will be triggered automatically what does it mean that it is triggered it means that the program or metat Trader pretty much looks inside of the body of this uninit function and it Executes whatever is written here and if we have this print statement it will just say on in it for example if I recompile it will also print this because the recompilation also triggers the on init event and for example if I switch from the daily time frame to the H4 time frame the program is also initialized and
you can read it here in the H4 time frame and it also prints the on init here So we can do the same thing in the ond init let's just print on D init like this and also make sure to note that I put the text in these quotation marks whenever something is in quotation marks and this is for most of the programming languages it means that this is a text value and this will be important later if we talk about data types so if I compile this in the on D init you will see
now if I remove the program from the Chart it will call the on init function if I activate it again it will call the on init function if I switch the time frame it will now call the on D init in the H4 first because this is the time frame where we're coming from and then the on init in the D1 and you can read about this in the expert Journal okay so this is for on init and on D init now we have a third event handling function here and this is the on tick
so if I add this and if we compile again we don't Have to do anything we just have to wait for ticks in the chart because like a tick is just a price movement essentially so if there's a price movement and we can just wait for this here if there's the next price Movement we will see that it will trigger the ontick yeah you can see there was a price move and it printed the ontick here so you can see the ontick is the one that is where we want to put most of our programming
logic right because this Is where we can really do the magic and where we can build conditions etc etc so this is what we will do in the in the in the following um oh sorry so yeah so I I think we do not really need this print statement because it's a little bit annoying if we see the information with every single tick we might keep this one also if we are wondering how do I understand all of this first of all I will tell you in this tutorial so no worries you will learn learn
everything You need for this specific program also if you want to learn more about specific functions and read about it everything is documented in the mql 5 reference which is the maybe the most important tool if you want to become a metat Trader 5 programmer so you can move your cursor your mouse cursor inside of anything you want to understand and if it is part of the mql5 framework like these functions are you can go to help and mql5 Reference if you do this will open the mq5 reference and you also jump automatically to the
entry for this specific function or variable or whatever you want to look at so you can see here what the print message does and you can see how it looks like this is the so-called signature which exists of the return type the name and the parameter list and um yeah you can see yeah actually I lied to you you can add Multiple parameters here but this a little bit um I don't really want to cover this um it's important for us now that we can print something with this print statement and you can read everything
here it will explain you exactly how the function works and this by the way only also works for the on init for example so if I move my cursor here and if I click F1 on my keyboard which is a shortcut so you don't have to go to help and MP5 reference it will Open the entry fold the on in it so you can see it is called when there's a initialized event and you can also see for example the on D inet and the on ti so everything's explained in detail but yeah this is
just just for now so let's um let's go on with the actual programming logic so we get something to work here but yeah I'm sorry if I if I if I really talk a lot about these absolute Basics but these are really the fundamentals to understand how metat TR 5 programming works if you do not understand how the structure the like this ecosystem metat Trader 5 works you will not be able to understand how coding for the meter Trader 5 works so this is really really important also as you can see here for example if
we go back here through the ontick you can also see for example at the very bottom of the entry in the Ure 5 reference there are some other articles linked for example event handling functions if I Click on this you will see this is an uh an entry in the M5 reference that gives us all of the event handling functions for example the on start the on init the on deit the on tick the on timer the on trade etc etc and you might be asking yourself why don't we see the other event handling functions
in our code this is exactly what was requested here or what we were asked in the beginning when we created the program let me show it to you with a example you Can see these are all of the event hling functions that we could have ticked in the beginning but I said we don't need them but if we would have included them they would be part of our source code but if you remember I said we can also add this later on and this is true because for example if I just want to use one
of these event handling functions let's say for example I want to use the ontimer function I can just add it here and this Is exactly the same what would have been done automatically if we would have ticked the box so you can see everything can also be done later on also so so you can see I can add all of these if I need them so yeah but now let's let's let's get to the actual coding part so if we want to write our strategy now we want to let's make sure that the first thing
we want to do is we want to figure out what the highest point of the last X bars is right because this is is the Point where we want to enter a position so here in this chart I have the donkey Channel indicator activated but in our program we don't want to use the donin channel indicator we just want to code the logic for ourselves so what we have to figure out is we have to figure out what the highest point of the last 20 bars is so there are multiple ways of doing this we
could for example write a loop to Loop through all of these bars and find the highest bar but this is not Really necessary because the metat data five Frameworks it provides so many predefined functions that you will find a function for almost anything so for example if you want to have the highest um bar of the last 20 bars you will find a function for this so you can get the highest price of the last X bars using the I highest function so what does this function do I will explain it using the mql 5
reference because this is the work uh workflow that you should Be yeah that you should get used to if you want to work with met to5 and if you want to learn more because whenever you read the mq5 reference you will understand what the function does so here it's pretty simple I highest it is the name pretty much um of this function and you can see it belongs to the time series and indicator excess uh functions but yeah this not not really important right now it Returns the index of the highest value found on the
corresponding Chart shift relative to the current bar whoa hold up hold up this is a little bit too much right yes it is but I will explain everything in a second also next thing we see the so-called signature of this function and you can see the return type is I NT or int or integer what is this I will explain this now we have different types in metat Trader programming I will write some of the most important data types here actually I think these four are the most Important this is also something you will often
see so we do have different data types and you can of course read about this in the reference so this is this is great isn't it you can just look up everything so these are basically most of the important data types I also added um the string and the date time because they're also important but you can find oh yeah these are not all of them uh you can find is it type Cting um yeah you could read a lot about this here but yeah let me explain what it does so you will have this
integer data type for example which is the return value of the I highest function I will get to return values in a second but integer data type means that this can pretty much be any number like like these numbers these are all examples for integer values and integer values are always numbers without a decimal point so these Are integer values now you might ask yourself what happens if I need a number with a decimal point then it's a double so these are double values for example all of these values are double values because they are
numbers with a decimal point then we also have Boolean data types for example and these can also can only be true or false or one or zero pretty much so they only can have two values and then we have the string data type we used this already because string Means texts these can be any text pretty much so these These are string values because they are text values and then we have the date time date time is um you will see this from time to time in programming and these are um uh dates and times
as the name says for example um like in the end it's just a number and it is the amount of seconds that cpsed since the 1st January of 1970 so this number 23 would be this would be the 1 January of 1970 uh 0 23 like this would be this specific daytime value so usually of course daytime values are a lot bigger so for example I don't know maybe it might be something like this and this might be the 26 May 2024 and this specific time I don't know I didn't count the seconds but just
to make you understand daytime values are under the hood just numbers but they are interpreted by the meter Trader 5 compiler As Time values pretty much and it's always the amount of seconds that passed since the 1st January of 1970 and you will find this concept in many programming languages by the way so these are the most important um data types you will find more data types for Example you might find the long data type which means which is pretty much just a integer number or a number without a decimal point but it can be
bigger so if you need a really really really really big number it might be a long data type and you might also see un signed long or you long this is a long value but it can only be positive this doesn't work because this is negative and u means unsigned so it means only positive Values and yeah this is for example yeah this is used in some functions um and yeah but just to make you understand that integer and long data types are also always for values without a decimal point double value are for values
with a decimal point you might also see short from time to time which is pretty much the same as double but the values cannot be as big as double values and um yeah then also you will Have boou which is either true or false it's often used for conditions we will get to this later on and then we have text values which uh is the word for this is string data type and this is always wrapped in these quotation marks so these are some important things but in most cases you can just use integer data
type double data type and then you will often see BU string and date time in programming and if you see a long you just know it can be a even bigger number And then oh yeah also to answer the question why is there even a integer and a long data type difference like why is not everything a long data type and this would theoretically work I think this comes mainly from the uh past where um computers were limited with their storage like random access memory was limited the hard drive disk space was limited and that
in in modern times this is not as important anymore so in really high level modern programming languages You might also see that there's not even a difference between in and long for example it's just all the same pretty much and the compiler takes care of it and I think it's stored um yeah I don't want to get into detail but this is mainly coming from data management issues I think because these different data types for example if we have a look at the reference again you can see they take up a different amount of space
in your Random access memory in the in the working memory of your PC and for example you can see integer data type of course it takes less bytes than the long data type because if you have more bytes in your Random Access Memory you can put more and bigger numbers into this because the numbers you might have learned this already are stored in the computer memory as binary code and it starts like like like this like for example if we have Um um yeah I don't know like this is one like this is zero this
is one and then we have uh I think this is two right this is three this is four if I'm not wrong and 8 Bits should be one bite so it looks somehow like this don't judge me if I if I if I mess up the um the binary um numbers here I'm not using this a lot because I'm a human not a computer but yeah this is how it how it works and now you can you can see that If for example a uh integer number has whoops Sal has this amount of space I
think four uh was four four bytes right uh yeah 20 23 bits so 4 * 8 Bits is four bytes so you can see the biggest integer number would be this pretty much right so this is the biggest integer number and it should be something like this or or really close to this I don't know how exactly the computer Interpretat this but it also depends I think a bit on the on the coding language Etc but yeah so this is the biggest uh integer number but if we now do have a long value you can
simply double this so you can say we have this ma much space these many bits or bites for a long value so also the numbers that we can store can be a lot bigger so this is why we have different data types and yeah I'm sorry if I get a little bit too deep into this topic and if you just Want to want to learn how to write this program but again these Essentials are so so important if you want to become a programmer you or at least for me it's really important to understand how
the computer thinks and how everything works so I can really learn how and why I use specific data types for example and it's also interesting I think so yeah this is why we have different data types here and yeah we will use these data types later on um or some of these and Then maybe we can come back to these this but or maybe let's make this a comment for now so we can we can keep this as a little reminder later on so what does this help us with the I highest function we learned
that the I highest function returns a integer value now we know integer values or integer numbers are numbers without a decimal point so the I highest function could return anything anything from zero to two billion whatever the highest integer Number was and since it returns a value we will store this value in a variable then because if we just call the function like this the value will go just anywhere but if we say we want to call this function and we store the return value in a variable like this for example it will mean that
after the function is called and processed the return value will be stored in this Variable so I will explain how this works in a second but first of all let's have a look at the function again we can see in the signature here there are 1 2 3 4 five parameters requested so this is um yeah looks confusing but it is not and these parameters are requested because the program or the metat trainer needs to know for what specific symbol for what specific time frame for what specific time series and in what specific section Of
the chart we want to get the highest value because there are so many charts and they all look different right so it it it really makes a difference if we are using the the us30 or the EUR US dollar and what what what chart time frame we use right and yeah let me get rid of the Sprint statements by the way and um yeah so we we have to tell the PC what symbol we want to use and here we could use a us30 as the symbol and you can see um in The in the
reference here you also know what specific date uh what specific um dat data type is requested for this specific parameter so the first parameter is a string parameter so string for us means text so we have to put this in quotation marks and we can for example provide us30 as a value which is the name of the chart or the symbol that we want to calculate the value for then the second parameter is here this might be a little bit conf Confusing because it requests a enum type frames eneration value I didn't really cover this
yet but enams are if we if we click on this link in the reference enams are enumerations which means that that there are a set of predefined values so you can see from example this enum time frames it can be any of these values so why did not I explain this when I talked about data types because enumerations are not really data types They are a implementation of data types or they are a um like when you use enumerations you map a value to a specific um identifier and this is mainly used to make it
easier for us as a human programmer to work with these enumerations because under the hood all of these IDs are just integer values BAS basically so if I can show you for example what's going on here if I print for example if I print one of these values here if I print uh Period M1 or period M5 let me just print this period M1 and then also I print and you can print multiple values using these commas and um yeah then just concatenate these values period M5 or also period current what happens if I print
this in the onti function so we will see this here and you can see period M1 the value is just one kind of makes sense right period M5 is five period current is zero so you can see all of these identifiers they just stand For a specific value and we could also use these values here like for example if the um I highest function requests a time frame here as we can see in the in the reference we could say for example I want the 5 minute time frame so I just go with five this
would work but it's easier for us to write period M5 because in this case it's super easy to read as a human programmer and also by saying this is a Eneration it makes make sure that the that the programmer only provides one of the uh values that are part of this eneration so yeah so this is a little Deep dive into enumerations then we have the next eneration here but whenever you see a eneration you can just look it up in the reference like the next one is the type and you can just read about
it here it's the time series that we want to get and we can also click on this link to see all of the eneration values Uh um for example mode open mode low mode High etc etc we will use mode high of course because we want the highest price so we go with mode High then the next two parameters are of type integer and this is what we know already and here we have to say how much how many candles we want to look back let's go with 20 and then we want to have the
starting value let's go with one so if we end the parameter list using the closing round bra and end the line with A semicolon we can now compile so no error everything's fine this is how it should look at your PC now and you can see if I for example write five instead of period M5 it's still the same no problem no issues but this looks way more readable for us as a human programmer right so before we get into the optimization of these values let's have a look at what we did here I created
a so-called VAR Variable variables are used to store values and if we want to store values in the random access memory remember it looks a little bit like this we have a lot of bits and bites I don't know this is uh is this eight four8 yeah so this is for example one uh this is one um these are four bytes right in the random access memory and of course the random access memory has more than four bytes so the random access Memory looks something like this has a lot of bytes right many many bytes
many many bytes many many many many many bytes so many many gigabytes actually so this is the storage of your PC and every program or most programs will use the storage of your PC so I don't know for example this part of the random access memory it might be blocked already by another program some program is using this some program is using this uh some program is Using this and it is called Random Access Memory because the memory is allocated randomly pretty much and this is a method that is that is really fast and um
it means that it's pretty much yeah everywhere in the random access memory can be some some values and if we go ahead and now declare another variable in our program here this variable will have one space reserved in the random access memory so For example if the program sees we will need this highest variable which is of type integer the program will then know that the integer data type it takes four bytes so it will go ahead and reserve four bytes some whereare so it would say for example reserved for highest variable and it will
Reserve these four bytes here in the random access memory and this highest variable or the name that we gave this variable here it Is pretty much just a pointer to this specific space in the random access memory so this is how the computer works when you declare variable it will reserve a spot in the random access memory of your PC and the name of the variable is the address pretty much of this specific spot in the in the memory of your PC so for example if we now store a value in this specific variable for
example the return value of the I highest function and let's say the value Is 13 then the program would go ahead and it would see that the 13 is is stored in this highest variable so it would go in the random access memory it would know the position because this is a pointer to this position and in this block it would know store 13 and I don't know it might look something like this this might be 13 in binary code I don't know look it up but now at this specific position there is 13 stored
so what happens if we now go Ahead and print this variable if we print this variable it means that we do not print highest because this is not a text this would be a text if we would put it in quotation marks right in this case it would would print the word highest but since it is not in quotation marks the program assumes that this is a variable and it is a variable of course so in this case it will see okay we want to print the highest variable but of course we do not print
highest as text But the program now goes into the random access memory it will search at this specific location because as we learned highest is just a pointer to a address in the random access memory and here we will find this highest variable at this specific point so it will read this information here and it means 13 so we will print 13 so let's compile this and let's see if we print 13 oh yeah of course this is not really uh it's not code so we cannot comp comp Compile like this but if I comment
this out it will work so we print uh with the next tick we should print the return value of the I highest function and it is two okay it's not 13 but it is two so it means that in the M5 time frame in the last 20 bars the highest time frame is at in index 2 so let's go to the M5 time frame and we can see that if we go back here like from the current bar we go two bars back it is the highest of the last 20 bars and what does this
two means it is a index a shift value in the chart in every chart or in programming we use this concept of bar indexing because we work a lot with with prices and bars we need a method to learn what specific bar is at what specific index so you saw it just created a new bar and now the value is three because the bars all shifted one to the left side and it is always Counted like this the current bar is index zero the previous bar is index one this bar is index two this bar
the highest bar is index three this is four five 6 7 8 9 10 blah blah blah blah blah so you can see whenever there's a new bar that appears in the chart it will automatically become index zero the bar at the left is index one the bar at the left of this one is index two etc etc so we just count the bars from the right side pretty much so yeah this is how we Can work with variables so we can store values in variables we can access values in variables using the variable name
and we can also change values in variables like this for example if I put here this this would mean that the value three is put into the highest variable but then I changed the value like this and if I compile again now you can see we're printing 13 because we can only store one value in this variable at a time so yeah this Is how it works if we want to use variables also we can use different variables we can use a double variable uh and here we could store double values we could use a
buan variable um and we could say this is true or this is false and now again you start to understand why we use these different data types because all of these three variables take up different space in the random access memory this is four bytes this is uh 8 bytes this is One by so so you can see by by by saying what specific data type it is the program is able to first of all reserve the needed space and then also if we read this variable it's also important to know the size because if
we read for example a integer variable this address will point to the first bite or bit of the variable and then if we know it's an integer variable the program knows we have to read four Bytes whoops sorry but if we read a double variable it knows we have to read eight bytes but if it's a Boolean variable we just read one bite so this is why data types are important and how everything works with variables so now we learned what a variable is and how it works we we we I I didn't really explain
why the I highest function is doing what it does know right so let me explain this I highest function it is a predefined function we don't really know One 100% how this function looks but we don't have to because it is explained here so we can just assume that the I highest function will return the index of the highest bar or of the highest price in the last X bars so what did we do we said we want to have this in the us30 we want to have this in the M5 time frame and I
can also change this to D1 for example then we say we want all of the high high prices of the last X bars the last X bars are In this case 20 course we added 20 here and then we want to start at index one so if I compile this again you can see if I go to the daily chart what does this mean it means that we start at index one and we got 20 candles back so we learned about the indexing thing so index one is this candle because this is index zero this
is one so from this candle on we we go 20 candles back so or we count 20 candles the first one is the first one Then we have 1 2 3 4 5 6 7 8 9 10 11 12 blah blah blah blah blah until we hit 20 bars somewhere here and out of these 20 bars it will give us the highest of all of the high prices of These Bars and it should be index uh 0 1 2 3 four it should be index five so with the next tick that will hopefully come here
in the next few seconds we will see or we should see that it prints five yeah so I now have to wait for the Next tick which is not not really coming here and there was a tick why don't we print this oh oh because I deleted the print statement right so if I compile it like this again you can see it should print the value five and it does because as I just explained from the first value on we will check the last 20 bars and we will get the highest out of the high
prices Of all of these bars and then it Returns the index or shift value so this is still not the high price right but now we can use this to get the high price and the price is a inte a double value of course because as you can see here prices do have a decimal point usually so we want to store it in a double variable so we would say high and we say I high and this is another function you can read about this it will return the high price of a bar at a
specific index Or shift parameter and this is great because we do have this shift parameter so what we provide here is the symbol again us30 the time frame period D1 and then the shift value and here we will just use the highest variable because it is the value five or whatever value is the highest or has the highest price so if I compile it like this you can see now we get the shift value and we also get the price of this bar here and yeah you can see this is uh in fact what is
Printed here if you want to have the exact price you can look it up in the data window press contrl D and you can see here this price is exactly the price that is um printed in the journal here and which is written here as the high price for this bar so you can see our code is working just fine um yeah let me delete this but there are some things we could optimize here already because here we hardcoded the symbol which is not really good I would never do this if I Write a program
because if you for example activate the donkey general program in um another chart let's say the German index if I would activate it here in this chart it would still calculate the value for the us30 because it is hardcoded so this is why instead of hardcoding we want to use a variable for this and the variable we want to use is underscore symbol so what does this do if we have a Look at the reference again and yeah this is what you can always do if you want to to learn something you can see this
is a string type variable it's a predefined variable and it always contains the symbol name of the current chart so if we print underscore symbol you would see it will print exactly the name of our symbol here so for example in this specific chart it will print uh us30 but if I activate it in the de40 um you can see in the de40 it will Print de40 instead um yeah as soon as we see a tick here in the de40 you you will see that it will print D40 in this chart yeah there is yeah
I don't want to wait for the tick you can do it on your on your own PC but yeah just printed D40 so you can see it always uses the the current chart which is great then also the period is hardcoded here and all of the other parameters and this is also not really Recommended I would say instead we want to give the user of the program the chance to change these values and the user can change these values if we use instead of these hardcoded values we use variables for example we can say um
instead of using period D1 we can say we create a variable which we name time frame and we say I don't know period D1 if we compile this we can now you the Use the time frame here but since this is a eneration it does not really work like this but we can also declare a variable of type enum time frames so we can use the exact eneration type or eneration here that is requested as the as the data type pretty much for this variable under the hood it's still pretty much just the integer value
but it helps us to use this variable here as a parameter for this function so we can Now use it here and also here then we do have mode High here and this is what I would keep as it is but then there's this um the count and I want to change this I want to make this a variable uh sorry int so I just write count and I just write 20 for example and yeah shift value will be hardcoded with one because it doesn't really make sense to change this but here I can now
count take count instead of 20 so what does this mean this means That whenever these values here are changed also the function here will calculate a different value because it will use these variables right so you might ask yourself what's the difference there is no real difference because the user of the program would have to go inside of the source code here to change these values but there's a trick how we can make these variables so-called input variables so let me take these two Variables let me cut them here and let's move them to the
very top of our program outside of any function this is important this cannot be inside of any of these event handling functions you have to put it outside of the functions on the so-called Global level of the program because whatever is written here on the global scale or Global level of the program can be used in any of these functions this is not the case for these local variables because if I want to Change the highest variable here for example in the onit it does not work but if I want to change it here it
works because here in this ontick function this variable is declared and it is well known here in this onti function but in the onit function the program simply does not know about this specific variable so we cannot change it here so this is a mattera of uh of of of scope and the functions all have a different Scope and functions that you declare outside of any um uh of any of any function or on the global uh scope or scale I don't know what the word is and they can be used everywhere in the program
so I can change the count here uh I can also change the count here no problems at all because I declared the variable at the global global scope here and then like yeah this concept is important because it makes your program Way more structured you might uh be wondering why we do not just declare everything on a global scope this will work for small programs but for complex programs it's not recommended at all because it's super confusing but what I want to do here is this doesn't really change a thing because the user would still
have to go inside of the source code and change these parameters right but we want the user to change it here in the in the program in the metat Trader so to achieve this we can simply make these variables input variables we just add this input modifier before we declare the variables and if we compile this again now you can see these programs uh these variables can be changed here in the inputs tab of your program so if you double click on the program here or when you activate the program the user is able to
change these values and yeah you could change these Values and after changing these values the variables here are updated and now the current values that the user chose are used so this is really really cool also you can see these were now color coded in a different color because there are input variables and I like to to make the first letter of my input variables a capital letter so I know later on that these are um input variables also you realized once I made these letters capital letters I get some Errors because mql5 as a
programming language is uh case sensitive which means that this is a different variable if I change the um if I change the first letter to a capital letter but if I now change these also to capital letters it works again so you can see the user is now able to change the program without having to look inside of the code because these are now input variables okay so now that we have the high of the last X bars um we now have to figure out The moment when the current price reaches above this specific high
right so we have multiple options here and I will show you one or two of these so the first thing we can do is create another variable which I will call just bit and I will um put the value the return value of the symbol info double function into this new bit variable the variable is named bit the type is double so it's a um value with a decimal point and I will put yeah I will let me let me finish This quickly and then I will explain what the simple info double function does it
is a system uh variable as we can see by the violet color of the um of the name of the variable and we can also find it in the mql 5 reference and it will give us a specific property for one specific symbol so this explanation is very unspecific so let me get more more specific uh we we do have two parameters and with these two parameters we will have to Define first for what specific Symbol we want to get this property and secondly what specific symbol we want to get so first thing is the
symbol and I will use underscore symbol again little reminder you could also do this but this is hardcoded and it will always work for us30 but if you use this it will always work for the current chart where you run the program in then we will have the identifier which is the second parameter and in the documentation here you can also click on the link here for the enam Symbol info double and here you will find all the possible identifiers with the description and the type so type is usually always double because we use the
symbol info double function so it makes sense and uh by this point you might also um yeah realize or imagine already that there's also a symbol info integer and also a symbol info string function so you can read about them also if you're interested but for this function we will get all of the double values for Example the current bit price or the current ask price or the point value so there are some some of these identifiers that I personally use frequently in my programming if you're interested in all the properties you can get from
the symbol just make sure to read about this enamoration in the documentation so this will give us the bit price and now we do have the bit price and the high price and what we want to check is at which moment the bid price jumps above the High price and always keep in the back of your mind all of this is done in the ontick function so with every single tick we do this calculation and we do this calculation or we update the bit price or we get the latest bit price so when there's a
tick the bit price usually changes right so with every single call of this on tick function at every tick we will have a different value for the bit variable this means that at some Point the bit value uh the bit variable might have or hold a higher value than this High variable so what we do here is we check if bit is higher than high this is what we want to check and we do have this comparison oper operator there are multiple operators like this in in in programming for example yeah we do have this
this is greater this is smaller so you're just Comparing values it's it's really really easy this is greater equals so it's greater or equals this is smaller or equals and then we do have this which is used if you compare two values that are equal and this is this can be used if you compare two values and they should not be equal equal so you can use all of these comparison operators to compare two values like for example this comparison it would be true if bit is greater like the bit price for example The bit
price is this and it's greater than this this is high currently so in this case it would be true but it would be false if the bit price is smaller than the uh the high price so if if if if bit is greater than high it's true but if bit is smaller than high this would be false I mean this is pretty pretty obvious I think um other the other way around if it would be like this if if this comparison would look like this it would be false here And true here so we're just
comparing two values really really simple then of course uh if we would have something like this then it would be true also if bit and high have the same price right and um also if it's like this it would also be true if they have the same price and it would be like this this would be true because the values are not equal right so this is how you can use comparison operators so if we if we make a comparison like this you realize that I used true and false a lot and if we remember
we had to look at the Boolean data type before which only holds the value true or false which means that we can create a Boolean variable for this which looks like this so we can store the the res of this comparison in a Boolean variable because this comparison it can only be true or false if you compare two values one of them is either higher than the other which would Mean this is true or they are equal or the first one is smaller which would mean this is false right so this is how you can
use Boolean variables and now since this is true or false this signal variable we can also check the value that is inside of this signal variable and we can do do so using a so-called if statement a if statement is a control structure a control structure is used To control your code because if you would never use control structures everything would be processed linear linear I don't know how to say everything would be processed from top to bottom and you would have no control over your code but with control structures that's where the names coming
from you can control your code because sometimes you might have a block of code that you do not want to execute with every single tick for example the Opening of a position we do not want to open a position position with every single tick this would lead to complete chaos so what we have to check if we do have a signal and for this reason we can use if statements if statements look like this you have this code word if then in round brackets you have a condition and then in curly brackets you have a
body what does this mean it means that once the code reaches this line it will See oh hey there's a if statement let me check the condition so next thing that's done is the condition is checked conditions can be true or false so it will check if this condition is true or false and in our specific example we do have the signal variable as the condition and this is enough as a condition because it can be either true or false that's what I explained before so this is our condition so if the signal is true
we will enter the body of This if statement this is the body if the signal is false we will not enter the body and we will proceed with the rest of the code so this is how a if statement as a control structure helps you to control and structure your code so what we want to do here is if we do have this signal and by the way we can do it like this and I I said I want to show you another way we can also do it like this this would be completely the
same Outcome so we can build the condition or do the comparison directly inside of these round brackets of the if statement or we can do it before with and stall the resite for example in a Boolean variable and use this Boolean variable as a um condition for the if statement both ways are totally fine mostly in programming you will see it like this I think because it just saves one line of code and it's still super readable so yeah this is how we can check our Condition and now in the body this is the part
again that is executed if the condition is true here we now want to open a position right and in metat Trader 5 programming there are two ways to open a position pretty much one way is confusing and complex so we will use the second way which is super easy and um yeah you will immediately understand it so for for the second way we will make use of a class this class is already part of our Meter Trader or meta editor whatever you want to say because we can find it in the navigator on the left
side you do have the Navigator and if you scroll down a bit you should find this include folder if you open this folder you should find the trade subfolder and if you open this folder you should find the trade. m Q file you can double click this and this will open a file which is a really big and honestly also a bit confusing but the good thing is at this Point you don't have to understand this I will not cover this in detail in this tutorial if you really want to understand this and I explain
everything as I do in this video like really carefully really slowly so I make sure that you understand it so if you really want to understand everything about classes and so on check out the links below this video I I I link the metat trader 5 master class and you will learn everything you need to know but for this Short tutorial it is just important that in this specific file there is a lot of code that is written already and we can use this code without having to write these 1,000 lines on our own so
what this code here does is in an nutshell it helps us to open close manage positions orders and Deals pretty much so let's close this include folder again this is just to show you what we are doing but in our actual code we don't need to write these thousand lines We just need to write one single line and it looks like this you just write this hashtag include and then I call this spiky brackets but in fact it's just a smaller than and greater than sign and between these spiky brackets you write trade SL trade.
mq and this is just the path to this specific file starting in the include folder so trade subfolder and then the file is named trade. mq so this is where This line is coming from and why we use it and this include means means that we now want to include all of the code that is inside of this file which means we now have access to all of these 1,000 whatever lines without having to write them and honestly also without having to understand everything that is in this file because all we have to understand is
that we can now use this C trade class which is defined in this specific file and we can work with it so for this Tutorial it is just important for you to understand that we can now use this SE trade class as a data type so to say I mean like under the hood it's a little bit more complex because this is a so-called class that is defined in this file but for this video you can see this as a data type pretty much or a class type and then this is just the creation of
a variable but but this variable does not have a simple data type like integer Double or Bo but instead the the type this variable is of type c trade so to say so what does this mean now we can now use this variable and this is where where everything starts to make sense to open close manage orders deals and positions so what we want to do is we want to use this trade and this is now a a object variable so you heard this term also and we can say trade dot this is a I
think it's it's really called a DOT operator Or something or Point operator and then we say or or you can see there is a lot there are a lot of previews for functions that we can now use so you can see it's super powerful that we now have access to this C trade class because all of these functions are defined in this 1,000 something line uh file and we can use it right away with just this include line so what we want to do is we want to say buy trade. buy and here you can
see there is a list of uh parameters that is Requested it's really easy to understand this will open a buy position and we have to say how many Lots we want to buy for example we can go with 1.0 but what I would recommend is say lots and then make this a input variable so at the very top of our program we could say input double lots and then we say 0.1 for example so it's a input variable which we can see with this input modifier it's of type double so we do have values that
can have a decimal Point and the name of the variable lot lots the basic or standard value is 0.1 and the user is able to change this because it's input variable right so then after providing the Lots we now have to say what symbol we want to buy like what Market what uh what um currency pair whatever and we will use underscore symbol again as we used before multiple times then we can say what price we want to open what stoploss value we want to have what TP value we Want to have so you can
see this buy function here it helps us to create a really a fully working position with the opening price stop loss and take profit and we can also have a position comment so we know where this position is coming from I will not fill these parameters and at this point I do not have to because you can see here in the signature of the of the of the function which is um highlighted when we uh start adding the parameters you can see there Is for example for price it says price equals o. o and then
we have SL equals o. o TP equals o and SYM um command equals null so you can see we have this equals for every value and this means if you see this in the signature of a function it means that there are predefined values so if we do not provide these values it will use the preset values so I can just stop providing more parameters like this and it will use all of these default values Like 0 0 0 and null and this means it will open at the current price without stop loss without TP
without comment but for this purpose right now this is fine because if we compile this I want to show you that even with these just like 30 lines of code I mean this is barely any code right most of this is just a blank space so with these lines of code we can already open positions so now after compiling I want you to go back to your meter Trader five and I want you to Go to view you can close the toolbox and then go to view and open the strategy tester here and then in
the strategy tester you can click on visualize then for expert you select the program that you just created for me it's Donan Channel YT you can choose a symbol you want to test on yeah I would just go with us30 to stick to this um example I used before you can select a Time frame you want to test on I will go with a daily Chart a time period I will just test for let's say the last year and then that's pretty much it I would suggest that you use every tick based on realtick because
it's the best method to like let you understand what it does and then you can also go to the next window or wait first make sure that this is ticked so you have the visual mode enabled then you go to the to the next Tab and you can see these are the inputs That we defined so the input variable so we could change them here but yeah for this uh test we will just leave it as it is and in the lower right corner you can click on this little start button it should be green
and then it will open another separate window with the simulation of this specific expert advisor in the symbol you want to test and in the time period you want to test it in the time frame you you you want to test it so this is a super powerful tool For for testing your strategy to see if it works technically this is what we do right now and then also after creating a technically working strategy you can use this tool to test it so you can see if this would have worked in the last let's say
five or 10 years so this will give you more confidence into your strategy right okay so let me fast forward this a bit so we can see if we open a position and and um yeah there we do have a position yeah my my program is lagging a Bit so let's wait until it uh it catches up here and there's a bu position so let's check if this buy position is correct and we can do so by saying we want to check if this is the highest bar out of the last 20 bars so if
we go 20 bars back uh 20 I think we will go until here and we can see this is the highest point of the last 20 bars and the price was above this point and here this is one major problem you can see we open more And more and more and more positions and this is a problem we definitely have to take care of because once we hit this trigger level here where the bid price is above the highest point of the last 20 bars and the bid price goes up even more this condition in
our code is always true with every tick and this is one big problem because we don't want to have unlimited positions once um we hit the the trigger level right so what we want to do here is we want to Make sure that this only happens once so there are multiple ways to do so we could say we want to open a position only if we did not open a position before um if we do not have any positions in the account etc etc uh what I want to do here is maybe we can yeah
I will introduce you to a for Loop so what we want to do is we can um use a for Loop to Loop through all the positions so let's say I will create this for Loop for You um and then I will explain it so for Loops usually look like this so you can see it is a little bit similar to the if statement because it also has a keyword which is four which tells the PC that there is a for Loop coming then we have something in round brackets and then we have a body
in curly brackets and if it something looks like this it usually means it is a control structure and the for Loop is also a control structure that helps us To control a code so the body is pretty easy to understand because we know this already this is executed if some kind of condition is true so let's have a look at these round brackets here in the round brackets we have a first part before the first semicolon which is the initialization of the for Loop so when the code reaches line 29 the program will see that
there is a for Loop then it will find this Initialization before the first uh semicolon here and this is executed once when this for Loop is initialized so when we reach this line it will execute this code and here we create a new variable which is called I it's of type integer and it has the value positions total minus one so what is positions total it is a system function it does exactly what the name says it gives us the number of open positions which is a integer value so we get the number of Open
positions let's say the position let's say we do have one position um and it has this ticket number for example this is one position that we currently have open so these are our positions so in this case positions total would give us the value one because we have one position right so then we substract one from positions total which would mean we would store zero in the I variable what happens if We have multiple positions let's say we do also have a position with this ticket number now we have two positions which means positions total
is now two two - one is one so in this case we would store one in this I variable so just just remember this later on so we just create a variable we store the total amounts of position minus one inside of this variable then the next thing is after the first semicolon and before the second Semicolon this is the main condition of the for Loop this is checked next so after the initialization which is processed only once at the beginning of the for Loop then we check the condition so here we check if the
value inside of this I variable is greater or equal zero so let's get back to our example if it would look like this with only one position I would be zero because 1 - 1 is Zer and then this condition would be True right because zero is greater or equal zero so would be true if we do have two positions it would also be true because I would be one in this case and one is also greater or equal zero so yeah so whenever this condition is true we then enter the body of the for
Loop body is executed if main condition is true and after whatever is written here is processed uh Uh in the round brackets so after the body is processed then we will skip to the last part after the last semicolon here and then we will process whatever is written here and this IUS minus means that I is substracted or lowered by one so this here is the same as IUS minus so we just update the variable I and we substract one from the current value of I so it means after every execution of the body of
a for Loop in this case we Will subtract one from this value I which means that I will become small smaller and smaller and smaller and I is never like this is only executed once which means that we start with some value for this I variable then we check the condition and then we lower the value in this I variable and then we do not execute this again but we check the condition again so let's this is a little bit confusing I know let's get our example going here So if we do have two positions
um positions we have two positions in this example I would be uh would have the value um one right because two positions positions total is two minus one is one so I is equals 1 in this case we see or main condition is true main condition is true and body is executed so every so Then the body here is executed after this I is lowered by the value one I is lowered by one this is what we do here that's it for the first iteration of the loop then since we lowered I by One I
is now zero which means main condition is true because I is greater or equal zero then we enter the body again which means body is executed again then I is lower at By one again which means I is now minus one which means main condition is false and body is not executed and for Loop terminates or is over I don't know it's processed whatever you want to call it so you can see this is why we have a loop because if we now add another position let's say we do have three positions in our account
positions total would be three 3 - 1 is 2 so in this case we would have one more iteration For a loop which would mean we would have the iteration for I value two I value one and I value zero and then at minus one the condition is no longer true and we leave the loop so this is how you can use four loops and four Loops always look like this they have this keyword for then in the round brackets they have the initialization before the first semicolon be be after the first Semicolon and before
the second semicolon you have the main condition which is checked uh before every execution of the body and then there there we have the I don't know I think it's called I don't I don't know how it's called it's just the the the part of code which is executed after the after every uh execution of the body of the for Loop which means that you always have to build this line so at some point The condition will be false because otherwise you have a infinite Loop and it will make your program crash but typically you
would see something like this and you will see this Loop very often in programs for metat Trader programs because you can Loop through all the positions so at this point you might ask yourself why do we do this and this a good question because right now we are just executing nothing for multiple times because there is nothing In the body of a for Loop but of course we want to have something that makes sense in or for Loop right so um yeah let's let's keep the example here for a while um so what we have
to do is we have to add some logical code here in our body of the for Loop so what we want to do is we want to get the position ticket for every single position let's say for example we have these three positions we have to get the position Ticket for all of them and we can do so by using the position get ticket function and you can see this function requests a position index and the position index you want to provide is I why do we do this because in the meter tra 5 every
position always has a index just as every bar has an index and I explained this before for bars the last bar always has index zero the previous bar has index one the previous buy is index two etc etc and same applies to Positions pretty much so the first position is index zero second position is index one third position is index two so you can see if you have less positions you just have less indices if you have more position positions you will have more indices like the next position would be index three for example and
if we do have three positions in our Loop we would also have another iteration or if we have four Positions like this because then I would be three in the beginning so you can see this is why this mechanism totally makes sense so using the position get ticket function here now we can have a quick look at the reference this will give us the position ticket of a position at a specific index and now we learned about the indexing so we see that for example if we have four positions the index go from zero to
three and if you um paid attention before you would see these are Exactly the values that are stored in the integer variable I so we can use the variable I for all of these indices to get the position ticket also this position get ticket variable will be of type or return a value of type unsigned long so position tickets in metat Trader programming are usually long values but don't be confused it's just a integer variable in big so we will use unsigned long and this U just means it's always positive this makes sense because ticket
Numbers are always positive in meter trador programming so now for every execution of the body of this for Loop we will get um a position ticket of the position at index I and this value will always be different in every execution of the body because I is always different and there's a different position at index I so now since we have this position get ticket variable executed this will already automatically select a position um you can also select It again position select by ticket I do this sometimes because it makes the code a little bit
more readable for me I don't know if it's just me so you could add this line of code uh but actually in this case you don't really have to so yeah maybe let's just not do it this time so once we get the position ticket number and use the position get ticket function to do so you can see in the reference this will automatically select the position to Work with using functions position get double position get integer and position get string and I will explain this in detail because this is super super super important and
one of the most important Concepts if you want to work with a meter Trader and meter Trader programming because after selecting a position we can use these three functions to get the properties for this position so let's have a look at for example the position get integer Function you can see the position get integer function it will turn a integer value for the currently selected position and you will also see it requests one parameter at least which is the property ID so if we have a look at this eneration here where this is explained we
can see these are all the identifiers and there's a detailed description so read this if you're interested so for example we can check the position ticket the position opening Time the position magic number the position uh type and so on and so on so how does this look actually the first thing I want to do is not check a position integer value but a position string value so we will check the position symbol and we can do so using position get string because this is a string value and we want to store it in a
string variable so here we do have three options we can use position command external ID and symbol I want to use symbol which means that this function return the position symbol of the currently selected position which could be us30 for example or could be de40 or could be EUR US dollar it depends on the position that is currently selected and this is why you do have to select a position before using position get string position get integer and position get double you can select a position using the position get ticket variable Or also position select
or position select by ticket function so now that we have the position symbol of the currently selected position we want to check if if condition if position symbol is equals or chart symbol or actually we want to check if it is not equals or chart symbol because in this case we want to continue okay okay this is going fast I know but what we do here is we just use A if statement we have the condition so we check if this currently selected position has the same symbol as our chart symbol because we know our
expert advisor should only work with the chart symbol and if this is not the same comparison operator we want to continue this here is the body of the if statement we learned before that the body is always wrapped in round brackets right so this would work like this but if I remove the round brackets it Also works and you can do this if the body of a if statement only has one line or one operation or one task or one statement so if you only have one specific thing that is done in the body of
an if statement you don't necessarily need the curly brackets but you can also add them it's up to you pretty much so what does this continue do the continue operator is pretty easily explained it will um it will skip everything that is Following in the body of the for Loop and it will automatically proceed with the next iteration of the loop so if we see that this continue or if this continue is executed everything is written here is skipped if continue is triggered and then we will proceed with the next value oh oh no we
will proceed with decreasing the I value by one and checking the condition again so this is a way of making sure that this for Loop Only uh like everything here is only executed if we found a position of the same symbol as our expert advisor should work with but there could be still positions in the same Sy that our expert advisor should not manage because they were open manually or by another expert advisor for this reason we can also check the magic number of the position and we will get this by using Position get integer
and the identifier position magic and then we can check if position magic is unequal um a magic value and then we also want to continue okay no no no you might be asking yourself if I want to make fun of you because in the beginning or I usually say that programming is not magic and it's easy and everyone can learn it and now I'm talking about magic suddenly but no worries I will explain What this is about so you can I will explain the concept of the magic number in metat Trader 5 programming so in
metat Trader 5 every position can have a magic number or actually every position has a magic magic number and if you open a position manually by clicking buy or sell in the metat trader the magic number is zero but if you open a position programmatically during using a expert advisor you can change this magic number And I want to create this input variable of with a name magic up here so I want to say input int Magic so I want the user to decide what magic number he wants to use for the positions of this
EA and this can be any number pretty much so it could be this could be this could be this could be this it does not really matter because all you have to understand is this magic number should be unique for your expert advisor so every position That this EA opened has this magic number number so later on in our code we can check the magic number of our positions in the account and compare it to the magic number of this EA so this will make sure that we only manage the pro uh the positions that
have the same magic number so little trick but very very helpful to not mess up everything so right now this magic number I mean we we we declared it it is here but our positions not open with This magic number but to make sure that this happened is we can say trade set expert magic number and then we set this magic number which which means that for this trade object here every position is now opened with this specific magic number so later on if we Loop through the positions we can find the positions using this
um yeah here it says possible loss of data because position get integer it returns a long value so you can fix this saying this is a long Variable you can also fix this yeah let's do it like this I think um position magic yeah it's a long value so let's make the input variable a long variable so then we don't see this warning okay so if we selected a position and if it it passed both of these two checks then we know that this is a position of our expert advisor and in this case what
we can do is we can say here we create a Boolean variable Which we name is position open for example and if we Loop through the positions and we find a position that passes both of these two lines then we simply say is position open is true so only if we have a open position this value will be set to true and then before opening more positions we can check if not position uh is position open so only if position is open is false we want to open positions also what did I do here is
I Combined two conditions so this is the first condition oh by the way saying exclamation mark and then variable name is the same as saying is position open is equals to false this is the same it's just a shorter form so here we are checking two com uh conditions and we can combine multiple conditions using a uh I don't know what it's called it's not a comparison operator It's a combination operator no I think it's a logical operator Ator that's the correct term I think and we have multiple of these logical operators we have this
end and we have this or or we have this uh yeah I think that's pretty much it isn't it yeah yeah I don't know so we have and and or I think there might be more but these are the most important so we can say if there are two conditions and they're combined using this end Operator it's it's it's two of these signs that's important only one is not really sufficient H if we see this it means this if statement is only true if both conditions are true so only if we do not have any
open positions and if the bid price is higher uh than this this calculated high price then this overall condition is true it would be different if this would be a all in this case it would be enough if only one is true but this doesn't make sense sense In our case so we use this end so if we compile again and if we now go to the programming uh the Met data 5 tester again and do the same back test with this program but with the updated code you will now uh quickly see that the
code is pretty much the same but we only open one position and no more as long as this one position is not closed so this is super super important in any program you should make sure that you do not open Infinite positions which will Completely mess up your account so it's always important to add some logic like this but if you add a logic like this and this logic is uh there are no errors this is completely safe so don't worry about using automated programs if your code is fine and if you made sure that
you checked everything correctly there is no room for error no worries the program will not blow your account it is safe if your code is correct so yeah let's go to the tester here and now you Can see even if the price reaches Above This high of the last 20y bars multiple times we will only see this one position because as long as it is not closed this condition here cannot be true again because is position open is not true if we have a open position and we find it because it has the symbol and
the magic number of our expert advisor so now that we learned how to open positions we have to learn how to close them and we could close them manually And this is actually very easily done because we can just do it using the trade uh class here or object variable that we have but we can also do it like pass passively with uh TPS and stop losses and this is what I want to add now so at the beginning let's create some more inputs and let's say for example we want to create a input for
the take profit in percent and uh the stop loss in percent let's choose some default values And after creating these two input variables of type double we will now calculate the TP and the stop loss before we open positions and as you if you remember we do have the option to Prov provide more parameters for our buy function here and we can add the price the stop loss the TP and also a comment and what we want to do is we want to open uh we want to provide the open price we want to provide
um TP and SL so what we can do here is first of all we Can calculate the ask price because this will be our open price for buy positions of course so just use symol info double uncore symbol symbol ask so nothing really to explain here it is completely the same that we did here before for the bid price we just changed the identifier here for the second parameter then we want to calculate the take profit and I will create another variable for this and here we want to calculate the TP so for a buy
position the TP of course has To be above the open price so we can use a calculation which looks like like this ask plus and then we want to say ask multiplied with the TP percent divided by 100 so we will add a specific amount in percent of the current ask price on top of this ask price and we will do the same thing for the stop loss but in this case of course we will substract the stoploss percent so it will look like this and now we can say we want to use this as
price here as the open price the Stop loss and the TP variables has stop loss and TP and if we compile this we can now go back to our testing environment start another back test and we will quickly see that now our position has a TP and a stop loss which is super super great because now the positions will also be closed if hit the TP or stop loss so let's wait yeah first position is here you can see we do have a TP and ASL TP is 2% SL is 1% here we have the
Stop loss and now we can see we will open multiple positions now because yeah of these um uh TP and stop loss levels that we placed and there's one problem or problem if we hit the TP because it means if we have the TP it means in 99% of all cases or 100 if I'm not wrong that we also made a new high which means that after hitting the TP we immediately open in a new position which is probably not really what we want right so we Would have to add some more code um for
this I would say because otherwise yeah after hitting the TP we will just open a new position so I think the easiest and maybe most logical fix here would be to not use a TP or make this TP at least optional so how we can do this is we can say for example TP by default is zero and only if TP percent is greater than zero then we want to update the TP value and use an actual TP because if we use TP zero it means there is no TP set for This specific position so
doing it like this make sure that there is only a TP placed if TP percent is greater than zero and again make sure we use a if statement here we don't need the curly brackets because there's only one statement following so if we make the TP perc maybe zero in the inputs and yeah if we do another test you will see there is no TP placed I mean this is pretty easy to understand uh not sure if I if I have to show you This but also in this case we have to make sure that
we do close the positions at some point so we should enable a trailing stop loss and we could for example say we want to create a variable for this a input variable of type boou and Boolean variables can only be true or false which is really cool because then you get this uh oh didn't compile then you get this little um there it is this little dropdown menu where can choose between true or False and now since we have this input we can make sure that whenever we have a position open and we selected this
position and we added the code for this already in this for Loop so if we reach this point we do have a open position selected right then we can check if the TSL input variable is true so if we want to use this TSL then we will process a TSL and we can for example say that we want to drag along the stop loss at the lowest Of the last uh of the last candle so we can say for example double stop loss the stop loss should be I low symbol time frame one what does
this mean so we know the I high function already right we used it here so we use a symbol and a time frame to define the chart and then a shift value to get the candle here I will choose the shift value one which means that this will always always give me the low of the last bar in the chart so not the current bar but the previous Bar so we will have a look in the tester in a second but what we want to check next is we want to check if stop loss is
greater than the position stop loss and we can get the position stop loss using the position get double function this still works because our position is selected so we just go for position is LS a identifier and now we can check if this new stop loss or if the low of the last bar if this is greater than the position Stop loss and in this case we want to modify the stop- loss of our position modifying the stop loss of a position again there are two ways one is a little bit confusing and complex and
the second one is using the trade C trade class and we will use this of course so we say trade position modify and then we just have to say the position ticket of the position we want to modify it is stored in our position ticket variable so we will use this then we have to use the Stop loss we want to use here we want to use the new stop loss that we calculated here then we have to provide the TP we don't want to change the TP so we will just use the existing position
take profit if I compile that's a problem because this variable doesn't exist but we can easily created here so we will just get the existing position TP from the select Ed position like this so now if I do another test we can see if I turn this is TSL option in the inputs to True and start the test we will see that we first of all we don't have a TP because TP input is now set to zero and also um we will have this trading stop loss also theoretically you could set the stop loss
for your position also to zero it depends on on your opinion if this makes sense to you to you most people say you should not trade without a stop loss but it's up to you you can code whatever you want so here we see first position is here and we will see Uh it will most likely immediately place the stop loss at the at the low of the of the last bar uh but in this case it does not because the low of the last bar is below the current stop loss and we only want
to Trail in the direction of the trade of course so let's see what happens I think this will be stopped out yeah but we saw the modification there already and let's wait for a trade that goes into profit let me fast forward so we can see this Trade and yeah there yeah you can see the the stop loss was modified and yeah this of course um sometimes works better sometimes does not work as good but you could also to say like for example this was a very nice trade you could say for example you want
to have a trading stop loss puffer like for example input double TSL puffer Percent oh it's buffer right in English buffer percent then we say like 0.5 for example so we add another 0.5 or 1.0 whatever you want you can set the stop loss the um immediate stop stop loss to 5% because we are using the daily time frame so you can say it's really easy to play around with this and now we can say for example we want to have the stop loss which is I low um blah blah blah minus [Music] Um yeah
let's just use the bit price which we still have stored in the in the bit variable here so we can say bit multiplied with TSA buffer percent divided by 100 and now you can see the trade has a little bit more room to breathe and now you should start to understand what the real power of programming is in the metat trader 5 you can I mean besides the obvious fact that you don't have to sit in front of the PC all the time if you automated your Program you can also test so many different strategies
so many different combination so many different out uh inputs in just no time and now you can see since I changed some of the values here and I modified the trailing stop loss the outcome of this program is completely different oh wait I have to update the inputs um defaults so now the outcome of the program is a completely different Outcome it's pretty much a different strategy so you can see you can test so many things and you can really find the strategies that work best for you and for your purpose what you want to
do so let's uh wait until this test loads and then we will see now the stop loss the immediate or the the initial stop loss is now no longer 1% but instead it should be 5% so the the trade has a lot more room to breathe which kind of makes sense on this higher time frames oh yeah It was automatically modified already by the trailing stop loss but um yeah the starting stop- loss would be 5% otherwis and here you can see now it is dragged along at the stop loss of the last um bar
minus 1% so you can see trades have a little bit more room to breathe yeah in this case it was not really good but we will see also trades that go into profit now with this newly uh adapted or changed trailing stop Loss and maybe there's on The Daily time frame I think it doesn't make sense to use such a close stop loss if you really want to generate some uh some huge profits and benefit from from from huge moves maybe the stop- loss should be more like uh low of last uh candle plus 5%
or something to really benefit from the huge moves but yeah you can just play around with this concept of course also I want to teach you some some more Things about programming for example how you can move parts of your code to different functions and and for example we could see that um for example let's say we want to use this for Loop in a different in a separate function so we can move it outside of this onti function because so far we only used system functions that are part of the mql 5 framework right
but you can also write your own functions and this is What I also want to show you in this tutorial because it will round up the the most important Basics about programming and metatrader programming so we cannot only use the system functions but we can use and create our own functions so let's go at the very bottom of our program and here we can create our own functions it has to be outside of existing uh event handing functions so outside on a global scope so here what We want to do is we want to save
void and then for example Loop traits this is a function name that I usually choose you can choose whatever name you want and then you add U round brackets and curly brackets if you compile you will see this works and there's no error so what did we do we first of all defined this function here the function is usually defined by the signature first the signature has three parts the return value the name of the function and and Then the parameters we know the we know a lot of this stuff already like for example if
we have a look at the function that we already know like for example the I high we know already that they have a return value which is the value that is then returned after the function is finished it has a name and parameters so this is exactly what we also have here but instead or this that's different to the system functions because for system functions in the Reference we usually do not see the body of the function but for our own functions we have to add the body so the program knows what this function does
right it's pretty pretty obvious I think so what we have here is we do have a uh function that we can now call so what I can do is I can go inside of my ontic function which is called by the meter Trader itself because it is a event handler and I can Say here for example Loop traits if I compile no error it works what does this mean it means that when the onti function is processed from top to bottom and we reach line 37 in this case and there is written Loop trades what
the program does is it will search in the program code for this specific function so it will go down here and it will find this specific function with this exact name if I change the name it does no longer work because this function does Not exist but if this function exists it will call this function here at this point and then it will execute whatever is written inside of the body of this function so what we can do is for example we can move this whole block here this whole for Loop we can cut it
from here cut and we can move it move it inside of the body of this Loop trades function so we paste it here so now it is in the body of the loop trades Function and is it is looping the traes so it kind of makes sense right right we can also say Loop positions I mean it's completely up to you uh how you want to name it but make sure to call it by its name if you want to call it so if I compile like this now we get a slight problem there are
actually two problems but we can fix them easily like the first problem is is position open is an Undeclared identifier and you might ask yourself why I mean we declared it here Right but the problem is yes we declared it here but this is the a matter of scope what I tried to explain before if we declare it here in the onti function it only lives in the onti function but not in the loop position so if we double click this error this error is not here but it appears down here in the loop trades
function so if we double click this error we should jump here because here in this Loop trades function a loop position function this is not Known what how how could we fix this we could say for example double is position open will be created here also now it also lives in the loop position function the problem is or let's do it like let's do it like this and I will show you what the problem is later then we also see another problem bit is an Undeclared identifier so it's the same problem we use bit here
in this loop loop position function but we declare it here in the ontick function so it's only Known it's only available in the on tick function so in the in the loop position we would have to do the same we can we can copy this and we can do the same thing here in this Loop position function if we paste it and compile now it is known here also so now all of our problems are gone right so let's do a test run and see if this works and oh wait let me show you one
thing first so let's let's let's move this away here let's comment this out so Now we do have the loop position function but we never call it in the ontick so what happens here let's let's do some tests and this will of course not give us good results but I think it's important for you to understand how this works and afterwards I will show you another interesting thing that you will maybe not expect because even if we do add the function to the Code it will still not work and I will explain why and this
is a really good learning so you would see we have the first position and now there is one big issue because after the first position we also see a second a third etc etc we see so many positions this is a problem we had before and if we have a look at this code it's completely obvious why this problem appears now because is position open here is false and since we do not call the loop position it's never Changed so it's always false and we all always open more positions so what happens if I now
add this Loop position to the code now this should fix our issue right let's say or let's test another let's do another test run and see if this will really fix our issue and then I will show you another another really important uh thing that you have to understand when you work with functions and to to make you understand this Concept of uh of um uh of excess or scope so here we would see there is our first position and so yeah actually my tester hangs up all the time if I do this specific test
but yeah if I stop it we can still see it still opens all the positions just too many positions and it's it's completely not correct right so why why is this happening um this is happening because if we go back to the code we can see even though we added the loop positions function here and we have The is position open variable here and we set it to True here it still opens positions and the problem is this is position open variable it is a completely different VAR variable than this one in the ontick even
if the name is the same for the computer these are completely different variables and we learned in the beginning variables just point to a specific uh address in the random access memory and this one points To a different address than this one because this one lives in the loop position function uh in the loop position function yeah and is declared here and this one lives in the ontick function and is declared here and this one is never changed it's never updated in the loop position function a completely different is position uh variable is updated so
what we have to do is or what we can do to fix This is we can say we want to return a oh wait this should be bo right this should be bo we want to return a Boolean value so if we comp now we get a problem because it says not all control path return a value which means if you choose a return value other than void oh I did not explain this by the way void means a function does not return anything but if you choose something else like double in or Bo you
have to return something in Your function for every possible outcome or control path so for example we can say at the very end of our function we say return which means we want to return something and then we say what we want to return and we we we return is position open which is this variable and the value in this variable so if we now return the value that is now stored in this variable which might be true we can use The return value of our Loop position position function and update this variable in the
onti function like this so if we do it like this now we can do the test again and now this is position open variable in the ontick function is updated and now if I do the exact same test again I did not change anything else you will see that now um it will work because the is position uh open variable is updated And now after opening one position we will not open more positions so let me fast forward here until we see this in the tester there is the position and yeah you can see I
can uh I can make the back test quicker but we only have this one position and there is no other position until this one is closed and then if we see another high we will see another position so this is how you can use your own custom Functions uh and create them and yeah you you you want to do this um if for example you have a a block of code which is pretty obvious what this block does and you just want it outside of your event handling function to make the event handling function look
cleaner or you often use functions if you have a specific part of code that you want to use multiple times because I could use this Loop position function multiple times and I don't have to write the code Multiple times because I just can call the position multiple times right there's also one more thing that I want to to to to tell you it's not really necessary that we do this but I want to show you how it's done so you know how it's done I want to create a um um a a parameter for this
function and you can create parameters like this in the round brackets you just you can just add as many parameters as you want and with different types like this for Example um variable three now we do have three parameters and if we compile we can now see this is the wrong parameter count for if we call the the loop position because if you have parameters for your function you will have to provide values for these parameters so you can see now when I call the loop position function and I open these round brackets I have
to provide Val values I have to provide a bit value which is of type double uh I Have to provide a integer value and I have to provide a Boolean value so if I provide add these three values the error is gone there's another error which says bit is already defined and this is because I create this bit variable again here in the loop positions but it's already a parameter so it's already defined here and I cannot Define it again so what I want is I want to delete it from here also I delete these
parameters they were just To explain how you can add multiple parameters and let me also delete them up here and now instead of these uh if this value I want to pass the value in this bit variable so I can pass the value of this bit variable to the function so I don't have to calculate the bit value here again I just get it from the parameter so the parameters are pretty much what you can give to the function to work with and it can be one it can be two it can be 10 and
the Return value is what you can get from the function after the function is processed so this is how it works and you can only have one return value for functions by the way so yeah if we do it like this this is now fully functioning and we can use this um Loop here to do yeah what is necessary to to be done so our program works without any problems and now you could use this program to do long-term back test and yeah test and see if you can find um actual profitable Settings actually I'm
pretty sure that you will find profitable settings for this because um yeah the strategy follows long Trends in um in in in uh in indices or stocks for example that tend to go long so this is how you can use this program and also um yeah you can also modify this program if you want if you want to learn more about programming um like in this video I covered some of the most important Basics but of course there's a lot more A lot more stuff that you could maybe do more efficient a lot more things
you can do you can learn as I said before check out the link in the video description and you will find my complete course it's definitely worth it I have so many students that are now creating their own programs that use their programs make money um in the market or that sell their programs and that also I also have students that um already work for others at as Freelancers so it can really Change your life if you learn how to write code for the metat Trader 5 platform but yeah you can see if I fast
forward now let me show you one example of a trailing stop loss so you can see everything is still working even if we move the code to this Loop trades function and then as a last thing I want to show you that uh code again so you can copy and compare it to your code if you still have some errors so yeah you can see trading stop loss is also Working here I don't know my test is really slow so let's just have a look at the code again and yeah first of all yeah maybe
let me let's move this out of the onti maybe at the very bottom of the code this was just a helper to understand how the loop works and this is the helper for the parameter uh for the variable data types and um The Operators so Yeah okay so let's have a look at the code again so at the very beginning we had these properties and these properties could be changed it's pretty much just a information for the user then we have the include line here which is really important because using this line we get access
to this trade and Cate class which helps us to open modify and close and change positions then we have a bunch of input variables input variables are the Variables that can be modified by the user so for example if you would now want to use this program on a real life chart you will see that you have this inputs tab where you can see all the input variables that can be changed by the user so this what we use input variables for and the program will then use the values that the user of the program
decided he wants to use then we declare a global variable which is called trade and it's of type c trade It's a so-called object variable because it's of like the type for this variable is a class this is a global variable it is global which means that we can use it in all of our functions and this is also necessary because in the on init function the function that is used or triggered when the program is initialized we set the m magic number for this trade object which means every position that is opened by this
trade object variable will have the Magic number here we do not use the on init in this uh program but it is triggered if the program is De initialized then we have the ontick function the one that is triggered with every single tick here first of all we get the index for the highest high in the last X bars and X is defined by the count which is a input variable the type frame is defined by the time frame input variable and the symbol here is always the chart symbol then after getting the Index we
use the index to get the actual high price of this bar using the I high function then we get the current bit price of our um of our symbol using the symbol info double function then we check if we do have open positions and we check this using our own Loop position function we also have one parameter for this Loop position function which is the current bit price so let's have a look at the Loop position function which we Define down Here this function will return a Boolean value which we need to to check if
a position is open by the way I could change this variable name here to for example um check open position and if I do this I will have to to change this name here also and also here because now the variable in this function is called check open position and um yeah and the function here uh the variable here is still named is open POS is position open this should Underline that these positions in fact do not have anything to do with each other they are completely different uh variables they point to completely different addresses
in the random access memory of your PC so yeah here we just Loop through the train using this this for loop I explained it before you always have the initialization the main condition and the part of code that is um executed after the execution of the body of the for Loop in the body of the For Loop we first of all select a position at the specific index we check if it's the chart symbol we check if it's the magic number of our program and if yes we know this belongs to our program so we
update our variable that we use to check if there's an open position and then if we use the trailing stop loss feature defined by this input variable we also calculate the new stop loss and we update the position that we have here then we return the variable That indicates if we do have a open position which will then be as a return value will be stored in this is position open variable here in the ontick function we then use this is position open variable to check if we currently have a open position we also check
if the bid price is higher than this high price if yes we calculate the ask the TP and the stop loss or the the entry the TP and the stop loss for our buy position and then we simply open this Buy position using the trade object variable and the buy function so make sure to copy everything 100% as it is here do not forget the semicolons do not forget anything and it should work exactly the same if you get any errors at this point just check the code again there is a problem in your code
it's not you don't randomly get errors I cannot really fix this error easily you just have to copy the code so as you can see this program is actually a really small Program so we have a total of uh can I fit everything here not completely but we have a total of around 70 lines of code and there are even a lot of blank uh blank lines so so you can see it's a very very short program I know I explained everything in detail and I think it's very important to do so for your first
like beginner tutorials because then you really understand how programming works and why we do the things that we do but actually If I would write a code like this just for myself it would probably take me 10 minutes so you can see once you have the power of programming for meter Trader five programs it is crazy what you can achieve you can test so many different strategies you can write strategies for yourself you can write strategies for others you can sell them you can do whatever you want it's such a powerful skill it's definitely worth
learning and again if you want to have a shortcut Check out the link in the video description and in the end if you appreciate what I have done with this very long tutorial please leave a like it would mean the world to me and recommend this video to your friends if you think some someone else need to learn some programming Basics or actually all of the most of the necessary programming basics for metat Trader 5 programming so thanks for watching if you are still around at this Point yeah it's it's it's really uh it's a
really great achievement if you follow through the whole video thanks for being here and I hope you learned something I'm out have a good time good trades bye-bye