MQL5 TUTORIAL BASICS – 56 SIMPLE CLOSE TIMER

video

In this video, we are going to create a timer that is able to close positions that are not profitable, in this case after one hour. This was a question from one of the premium course members, so let’s find out how to code that with mql5.
To get started please click on a little icon 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 the two comment lines here.
We start with an include statement to include the file trade dot mqh, it contains the class ctrade so we create an instance of that class that will be used to open some test positions.
Inside of the on tick function we need to calculate the ask price and the bid price. This is done by using symbol info double for the current symbol on the chart. We use symbol underscore ask or symbol underscore bid and with normalize double and underscore digits we automatically calculate the number of digits behind the dot.
Afterwards, we want to open some test positions, we wouldn’t do that on a real account but to have something to close I need to open some positions in the strategy tester.
In my case, if positions total equals zero and we have no open positions we use trade dot buy or trade dot sell to open one buy and one sell position.
And afterwards, we want to call a function called check close timer, this one doesn’t exist so far so we need to create it now.
We use void because we have no return value and we use check close timer for the function name.
Let’s use a for loop to go through all the open positions. For each position, I want to use position get ticket to get the ticket number. I also want to calculate the position open time, we can get it by using position get integer. The parameter is position underscore time but this is a date time variable and I would like to use mql date time to create a structure so if you mark that and press F1 you will see that the date time structure has eight return values here.
One of the values is the hour and that’s what we need. Now we use time to struct to convert the position open time that we have calculated here into our structure and that makes it possible to extract the opening hour for this position from our structure.
Now we need to get the current hour of the local time. We start by using time local to get the date time value. Now we create another structure, this one will be called my local time.
Once again we use time to struct to convert the local time that we have calculated here into our structure and afterwards we extract the current hour because this makes it possible to calculate the difference by simply subtracting the opening hour from the current hour.
Please remember this will only work for the same trading day otherwise you would need to check if the date has changed.
Now let’s create a few print statements here.
Usually, I would prefer to use comment statements but we need these outputs for each position so let’s calculate the position profit that is done by using position get double and the parameter is position underscore profit and after some hours and in my case after the first hour is over we want to check if the position profit is negative and if that is the case we use trade dot position close for our current ticket to close the position.
Finally, we want to end the for loop and the function and that’s about it.
Well if you don’t understand what all the code here does or if this was too fast for you maybe you want to watch one of the other videos in the basic video series or maybe even the premium course on our website might be interesting for you.
For now, please click on the compile button or press F7, you shouldn’t get any errors 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 ex5, mark the visual mode here and start a test.
Here we are! The expert advisor is running, it’s opening and closing positions based on the time and in this little video you have learned how to create an expert advisor that is able to close positions that are not in the profit after one or more hours and you have coded it yourself with a few lines of mql5 code.