MQL4 TUTORIAL BASICS – 124 SIMPLE ORDER CLOSE TIME

video
play-sharp-fill

 

In this video we are going to create an Expert Advisor that is able to get the Order close time from the Order history. So let’s find out how to do that with MQL4. To get started please click on the little button here or Press F Four 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 Order Close Time.

 

Click on Continue, continue and finish. Now you can delete everything above the Ontick function and let’s remove the two comment lines. This is about the order history, so we need something in the history. And when Orders total equals zero, we need to open a few test positions. You wouldn’t do that on a real account, but on a demo account we can use Order Send to open a buy and a sell position, just to have something to test. And once we have done that, we use this user defined function called Check Closetime.

 

It doesn’t exist so far, so we need to create it now. The name of our user defined function is Check Close Time. We use Void here because we don’t have any return type. Now we use a for loop to go through all the open orders. And to get data for closed orders we actually use Order Select. But this time we use Mode underscore History, because Mode history will make it possible to select an order from the history pool for closed and canceled orders.

 

Now let’s continue and get the current time. We use Time Local to get a Date time object that is called My Time. We want to use MQL Date Time to create a time struct. Now we want to convert the local time called My Time that we have created here to the structure that we have created there because this will make it possible to get parts of that struct. For example, the current hour can be calculated by looking into the struct and extracting the hour.

 

We can also convert the time for our output that is done by using Time to string. We use Time Local, time Date and Time seconds. When you use Order Open time, you will get the opening time. To get the closing time you can use Order Close Time. Let’s create two more structs, one for the open and one for the close time.

 

And once again we use Time to Struct to convert it. Let’s once again use Time to String to create a string for the opening time with seconds and for the closing time with seconds. And finally, we want to create an output by using the print statement. That will output the order ticket, the open time, the Close time, the Order profit, and the local time. And we will see the output for each order in the history in our Journal tab. Finally, please close the for loop and the function and that’s about it.

 

Well if you have no idea what all the code here does or if this was too fast for you, you may want to consider to become a premium member of the premium course. You can find that one on our website and this actually was once again a suggestion of a premium course member. And if you are already a premium course member and have an idea for a video like this one please let me know. For now please click on the compile button or Press F seven on your keyboard.

 

You shouldn’t see any errors here and if that is the case you can click on a little button here or Press F four to go back to MetaTrader. And in MetaTrader you want to click on View, Strategy Tester or press CTRL and R. Please pick the new file simple Order Closetime Ex four. Mark the option for the visible mode here and start your test. Now the expert advisor is running. Let’s click on the Journal tab. Currently we don’t have any closing time, because so far our first two orders have not been closed. So let’s speed that up a little bit. That’s enough. Now click on the Journal tab and now you can see the opening time and the closing time for our test orders. So our little expert advisor actually works as designed and in this little video you have learned how to calculate and output the opening and the closing time for closed orders and you have coded it yourself with a few lines of MQL 4 code.