MQL5 TUTORIAL BASICS – 124 SIMPLE ORDER CLOSE TIME

video
play-rounded-fill

 

In this video we are going to create an Expert Advisor to calculate the closing time for already closed positions. So let’s find out how to do that with MQL 5. To get started, please, 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 Time. Click on Continue, continue and finish.

 

Now you can delete everything above the on tick function and let’s also delete the two comment lines here. We start with an Include statement to include the file Trade.Mqh. This contains the class Ctrade and we are going to create an instance called Trade. Inside of the On Tick function we want to calculate the Ask price and the Bid price.

 

That can be done by using Symbol Info Double for the current symbol on the chart. We either use symbol Ask or symbol Bid. And with normalize double and underscore digits we make sure to calculate the right number of digits behind the dot. As we want to calculate the time for Order closing, we obviously need to open a few positions. We use Trade Buy or Trade Sell to open a test position for ten microlot. You wouldn’t do that on a real account, but that’s what demo accounts are good for. And obviously we need positions or we wouldn’t be able to calculate any close times.

 

And to do that we want to call a user defined function called Get Closetime. This function doesn’t exist so far, so we need to create it now. So let’s go on. The name of the user defined function is Get close Time. We use Void here because we don’t need to return any values.

 

We need to declare a few variables. You may not need all of them, but I have used them in another video. And this one is the most important thing for us. We want to calculate the time when a position was closed. So let’s get started and use History select, starting from the current time.

 

We want to go through all the closed orders and positions in our case and for each entry we want to get the ticket number. That can be done by using History Deal Get ticket for the current counter value. And if that is greater than zero we start to read out the position properties. For example, the order profit will be calculated by using History Deal Get double for the current ticket number. And we use Deal underscore Profit here. The order type can be calculated by using History Deal Get integer for the current ticket number. And this time we use Deal underscore type. Let’s continue and calculate the current symbol by using history deal get string for the ticket number and we use Deal underscore Symbol. The next item is the entry type.

 

The Deal entry will be calculated by using history deal get integer for the current ticket number and we use deal underscore entry. And now comes the interesting part because we want to get the deal time. That is done by using history deal get integer for the current ticket number and this time we use deal under score time. But as you see we will receive an integer value that is not very good to read for humans.

 

So the closing time in our case needs to be reformatted by using time to string for the deal time that we have calculated here. And we use time underscore date and time underscore seconds to make it more readable. And if the current symbol for the deal fits and is equal to the symbol on the chart, we want to find out if it is a buy or a sell order because we don’t want to calculate anything for pending orders. And we also want to know if the order was actually closed. That would be the case when the deal entry value equals one. And if all conditions apply, we will calculate our result. This is a string value that we will print out in our Journal.

 

It contains the values for the profit, the ticket number and the closing time. So let’s use the print statement to print it out to the Journal tab. Finally, we need to close the loops and the function and that’s about it. Well, if this was too fast for you or if you have no idea what all the code here does, you maybe might want to consider to become a premium member. You can find the premium course on our website and this actually was a suggestion of a premium course member.

 

So if you are a premium member and have an idea for a video like this one, please let me know. So let’s remove the empty lines here, press the compile button or Press F seven on your keyboard. And if everything went well, you shouldn’t get any errors here. And if that is the case, you can click on this little button or press a four to go back to Meta trader. And in Metatrader please pick the new ex5 file that we have created here.

 

Mark the option for the visible mode and start your test. Okay, this is the Expert Advisor running. Let’s speed that up a little bit. So far we have no order that has been closed. So we clear the Journal here and once we hit a take profit or a stop loss we should see the ticket items appear here.

 

Let’s speed it up again. So here we are. Now you can see all the information that you need including the closing time. So our little expert advisor works as designed. And in this little video you have learned how to calculate the closing time for positions. And you have coded it yourself with a few lines of MQL 5 code.