MQL5 TUTORIAL BASICS – 56 SIMPLE CLOSE TIMER

video
play-sharp-fill

 

  • Introduction to Creating a Timer to Close Positions (00:00 – 00:11) Introduction to creating a timer in MQL5 that closes positions that are not profitable after one hour, as requested by a course member.
  • Opening MetaEditor and Creating a New Expert Advisor File (00:11 – 00:31) Instructions on opening MetaEditor and creating a new expert advisor file named “Simple Close Timer.”
  • Setting Up the Code Structure and Including Necessary Files (00:31 – 00:44) Deleting unnecessary code and including the ‘trade.mqh’ file to use the CTrade class for opening test positions.
  • Calculating Ask and Bid Prices (00:44 – 01:13) Using ‘SymbolInfoDouble’ to calculate the ask and bid prices for the current symbol on the chart.
  • Opening Test Positions in Strategy Tester (01:13 – 01:38) Opening test buy and sell positions in the strategy tester if no positions are currently open.
  • Creating the Check Close Timer Function (01:38 – 02:04) Creating a function called ‘checkCloseTimer’ to go through all open positions and close them based on certain conditions.
  • Calculating the Position Open Time and Current Hour (02:04 – 03:22) Using ‘PositionGetInteger’ and ‘TimeLocal’ to calculate the position open time and the current hour of the local time.
  • Extracting Opening Hour and Calculating Time Difference (03:22 – 04:03) Extracting the opening hour from the position open time and calculating the difference with the current hour.
  • Printing Position Information and Checking Profitability (04:03 – 04:59) Printing position information and checking if the position profit is negative after a certain time to close the position.
  • Compiling and Testing the Expert Advisor in MetaTrader (04:59 – 05:35) Compiling the code and instructions on testing the expert advisor in MetaTrader using the strategy tester.
  • Demonstration of the Expert Advisor in MetaTrader (05:35 – 06:07) Demonstrating the expert advisor in MetaTrader, showing how it opens and closes positions based on the timer.

 

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.