Transcript for:
Creating Time Range Breakout EA for MT5

hi this is Toby and in today's video I'll show you how to coat a Time range breakout EA for metatrona 5 so you can achieve results like this one for yourself okay let's go so this here is my master a my own EA and of course our EA won't have a graphical user interface like this one but we will create an expert advisor that is basically able to generate the same trades like the strategy here and as you can see the strategy here runs on a live account for over one year now and I'm pretty happy with the performance and you will see that the logic behind the strategy is pretty simple okay let's switch to metatar 5 now okay so here we are in the metatrader5 platform and before we start coding I want to say if you are new to Empire 5 you may check out my previous videos on mcl5 coding we did a simple time entry EA and also a simple moving average crossover a because this EA will be a little bit more complex to code and if you are new you may check out the previous videos okay but now let's take a look at the logic of the of this EA so we will have a simple time range so let's say our range Starts Here For example and ends Here and Now what the EA will do is simply to save the highest price of this range for example here and also the lowest price for this range and now if we break above the highest price after the range we will take a Buy trade and of course if we break below the range we will take a sell trade so in this case we would buy right here and they ever also have a close time where we close all positions here for example and in this case our byte rate would be closed here in profit so that's the simple logic of the cea okay so let's switch to the meta editor and start coding okay so let's create a new expert advisor so click on new top left corner expert advisor template let's name it time range EA click on next finish and now the first step I like to do is to clean up a little bit um because I just don't like the formatting from meta quotes here but this of course is a personal preference the gray text is just a comment so it's actually not important for the code to run but it's for the programmer um to describe what the code is about okay we also don't need the last line here okay so let's start with the input variables so we copy this comment section here to create a new one and we call it inputs inputs and now of course we want a input for the start time of the range so we write input integer input range start and to also redefine this variable with 600 for example and you also write a comment behind this variable and let's just write range start time in minutes okay so why we use a integer type for the time input um it because we entered the start time of the range in minute minutes after midnight because as a lot of people when they code a range time-based EA for example they use um maybe string inputs for the start hour and they have another input for the minute but I don't like this option so we have one input for the start time and we will have another one for the end time and this way we minimize the number of inputs and it's later also easier for optimization and stuff so I like to do it this way and another input we need of course is the end time of the range and the end time of the range we will Define it by the duration so you also use a integer type we write input range duration Maybe because now the range end is defined by the start time plus the minutes we enter here in the duration variable so let's say if we want a range of uh two hours we would just enter 120 Minutes so we also write a comment here and minutes okay and then of course we want a close time so input integer input Range close and we predefined this variable with um let's say 1000 200. okay we ought to write a comment range close time so of course this is a little bit um it's not that intuitive to enter the times but you will get used to it so 600 would be 10 o'clock for example because at minute after midnight and 120 the range is 2 hours long and we will close the trade in the evening at 8 pm okay and okay maybe right time minutes here also so now we also need um a input for alert size because the user of course should be able to change the lot size so input double and input plot Lots and we predefine it with 0.01 we write a comment um lot size and we also want a input for the magic number of this expert advisor because later when we close the trades we want to filter by the magic number so we don't close trades from other EAS or trade with it manually so we write input and this will be a long variable type input magic number you predefine it for example one two three four five and this can of course be any number you just have to make sure that this number here is unique for the metadata 5 platform okay we write a comment magic number okay that's for the input okay now let's first of all compile to see if we got any errors everything looks fine here so what I'd like to do next is to check the inputs from the user because we don't want uh wrong inputs of course in our EA so to prevent this from happening we can go to the on init function this function is called once the a starts and if you want to know more about a function you can always highlight it or select it and press F1 on your keyboard and this will bring up the meta quotes help and there's also a example in most of the most of the time so let's just write a simple if statement for the inputs we start with the magic number so the metric number is wrong if it's below or equal to zero so if that is the case we want to display maybe a message to the user let's use the alert function for this magic number below or equals zero and we also want the a to stop at this point so we just write return and init parameters incorrect okay so that's it for the magic number um we also want to check the Lots inputs so we just change this to input input slots and if the lots are below zero it's of course a wrong input but also we write um aura in input Lots is let's say created and then which is a lot of money [Music] um maybe we changed it to one because I don't think one of you will create more than one lot so and if you do you can change this number of course but it's for the safety for now so we write Lots [Music] um or credit and one okay we compile okay perfect so now let's take a look um at the range start input we again copy and paste and we now use the range start input so for the range start input um this should not be below zero but of course it can be zero so that would be midnight and the range start should not exceed or be equal to 1440 because this these are the minute of of one day Also let's write here range start it's below zero like this we do the same for the duration so the duration cannot be zero and it also needs to be below this number here okay we also write here and duration like this okay now we only need to check the close time the close time um can be zero but of course it can't exceed this number here close time below zero okay let's compile okay and what we also want to check is that the close time here is not equal to the start time plus the range duration so we just edit this if statement here or the input range start plus the input Branch duration is equal to the input branch close and of course this here can exceed the minutes of the day so we need to add a motor law like that so this should do the trick and of course we want to edit the message too um end time equal to the close time so now let's compile again and we check um if this is working so we switch the metadata platform and now under the Navigator under expert advisor you should see your expert your EA your time range EA if we just drag and drop it on the chart um you can switch to the inputs Tab and now let's enter for example for the start time 1200 and for the range duration 300 so this should be um 60 minutes after the midnight after midnight and this of course should give us a error message if we click ok now and yeah we see we got a error message the a is not starting so let's test again for example if we put a negative value for the lot size we also get a message if we put 0 for the magic number you also get a message so our check for the inputs seems to work now if we do it like this the predefined variables the a should start okay we see here top right corner the a is running so for now let's just remove it and we go back to the meta editor okay so now we have our inputs we check the input and now we can define a few Global variables we need for this EA so let's just copy this comment and create another section here and we call it Global variables and all the variables for the range we will put them together in a structure so we just write [Music] um structure the structure is named range structure and here we now can Define different variables of different type so the first one would be the start time of the range so this is a um daytime type start time we also have a date time for the end time so this is later the calculated time for the current range so so that's why we use a end time and not the duration and also um and also we need a close time close time then of course we want the high and the low of the range so this would be of type double I sub low um now we also need a a flag if we are inside the range so this would be of type Boolean um black entry let's call it just flag entry we need a flag for put a high Breakout and also the same for a low Breakout okay and we also want to define a Constructor for the structure so we just write range structure and here we just predefine our variables so for example um start time and then in Brackets the value and the next one would be the end time then we predefined a close time so this is just because if we Define the structure later we use it in a variable all these um members here of the structure are predefined with these values here so for the high we will write zero or a low we will write a big number and now also the flag entry is false at the start and the flag I break out is also false and the low breakout is also false okay so now we write the brackets like this and now the structure is complete we compile we have no errors perfect okay and also what you should do is to add a comment after each variable maybe I scroll up a little bit so you can see and my face cam is not in a way so you should add a comment after each variable so let's do this real quick um start off the range end of the range um close time high of the range low low of the range um leg if we are inside the range and here lag is a pipe break out um occurred and also flag of the a low breaker record so you also see these comments when you are using the structure later so now we can define a variable of the type range structure here and we just call it range and now if we compile and if you use the range variable now and press Dot you see all the members of the structure and here at the end you also see the comment from each variable so that's pretty handy and you know what the variable is for okay okay so now we need a few more variables and one of the M Square five McQueen tick type and it's the previous tick and also of the McQueen tick type a variable called last tick and for our trade to open and close we also use the trade class [Music] um in this EA so we write C trade and we call it a variable trait and in order to for this to work we of course need to input uh include the C trade class so we copy this code section here paste it we'll call it include and now we need to write hashtag include and the directory of the C trade class is I think trade backslash trade.mqh [Music] let's compile and see perfect okay so now we can use the trade variable we have a range variable of our structure and we also have two variables of McQueen tick type okay so the next part would be to code the actual logic of the a in the untick function but I think we will do this in the next part because this video is already pretty long and if this video was helpful for you you can leave a like and if you don't want to miss the next part you can subscribe to my channel and any questions about mqf5 about this EA or in general about automated trading you can just write a comment and I will see you in the next video bye