MQL4 TUTORIAL BASICS – 56 SIMPLE CLOSE TIMER

video
play-sharp-fill

In this video we are going to create an expert advisor that is able to close positions that are not profitable after some time has passed, in our case after an hour so if we have no profit we should see that the position is closed, so let’s find out how to code that with mql4.
To get started please click on a little button here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, continue, I will call this file simple close timer, click on continue, continue and finish.
Now you can delete everything above the on tick function and let’s also remove the two comment lines here.
First, we want to check if we have open positions that is done by using orders total and if the return value is zero we don’t have any open positions and in that case, we are going to open some test positions here. You wouldn’t do that on a real account but we need some positions that we can close and afterwards we want to call the function check close timer that doesn’t exist so far so we need to create it now.
It has no return type so we use void and we use a for loop to go through all the open orders we select each order by using order select, let’s get the local time by using time local that will return the local time of a computer, in our case of the strategy tester and it will return a date time variable but I would like to use a structure, you can do that by using mql date time and if you mark it and press F1 you will see that the date type structure contains eight return values and one of those is the hour, that’s what we need so let’s use time to struct for the time that we have calculated here and convert it into a structure because afterwards, we can simply extract the hour from our time structure.
For the output, I would like to have a string variable that is readable so I use time to string to convert the local time to create an output that looks like this.
Now we want to get the open time for the position that can be done by using the function order open time, let’s create another structure this time it is called my open time struct, once again we use time to struct to convert the order open time that is the date time variable here to a time structure and now we extract the opening hour by getting the hour from the time struct. I would also like to have the opening time converted into a readable string variable that will look like this one and now we can calculate the time difference by simply subtracting the current hour from the opening hour please remember this will only work within one trading day otherwise you would need to check if the date has changed. Let’s use print to create the outputs we need.
In the next step, we check if a certain number of hours have passed, in my case, I want to close the position after the first hours over but my second condition is I only want to close it if the order profit is negative, so if the order type equals op sell we would have a sell position in that case we use order close for the current or order ticket and submit the ask price. Otherwise, if the order type equals op buy that would be a buy position and now we would use order close for the current ticket and submit the bid price.
Finally, we need to close the if statement, the for loop and the function and that’s about it.
Well if this was too fast for you or if you don’t understand what all the code here does maybe you want to watch one of the other videos in the basic video series or maybe even the premium course might be interesting for you. In fact, this was a question that was provided by a premium course member, for now, please click on the compile button or press F7 on your keyboard, you shouldn’t get any errors here and if that is the case you can click on a little button here or press F4 to go back to Metatrader.
And in Metatrader you want to click on view, strategy tester or press control and r, please pick the new file simple close timer dot ex4, mark the option for the visual mode here and start a test.
Here is our expert advisor, it is running. If we speed it up we see that we have several positions that have been opened and closed, here are the results, our expert advisor is working as expected and in this little video you have learned how to create an expert advisor that is able to close positions that are not profitable after a certain time and you have coded it yourself with a few lines of mql4 code.