In this little Expert Advisor we are going to find out if the market is closed or if it is open, like right now.
The Expert Advisor contains some external variables where you can define when the market opening and closing time is for your country. In my case here in Germany the trading hours start at 9:00 am and they end at 5:30 pm – in German notation that is 17:30 so, when we see 9:00 this line here will change to “Market is open” and when it says 17:30 the “Market is closed”.
Now how can we create an Expert Advisor that is able to read the local time and generate a signal for the market opening or closing time?
Well, to do that please click on the little button here or press F4 on your keyboard.
Now you should see the Metaeditor here and here you want to click on “File/ New/ Expert Advisor (template)” from template, continue — I will call this one: ”SimpleTime” — click on continue, continue and finish.
Now you can delete everything above the “OnTick” function and the two comment lines here. Above the “OnTick” function we are going to define 2 string variables for the opening and closing time of the market.
The modifier “extern” makes it possible that you will be able to modify the variable settings here in the Expert properties.
Let’s also define two integer variables; one is for the index of the market opening time and the second one for the index of the market closing time. And I would also like to have a string variable for the market state.
We will actually start with a closed market and in the “OnTick” function we need to find out the time with seconds and that can be done by using “TimeToStr” and we use “TimeLocal” we want to have the “TIME_DATE” and the “TIME_SECONDS”. Let’s press F1 and here you can see “TIME_DATE” will give you the year the month and the day and the output for “TIME_SECONDS” is hours minutes and seconds. That will give us the current time on our computer and now we want to find out if the opening time is in the time string, so we use “StringFind” to find the market opening time in the time with seconds that we have calculated here.
The last parameter here is used for the starting position, we want to start to search from the first character in the string and we do exactly the same for the closing time.
This time we use “StringFind” to find the market closing time in our time string and if the opening time was found the value for index market opening time will be greater than zero and in that case we want to set the market state to open, otherwise if the closing time was found the index market closing time is greater than zero and so we would set the market state to closed.
Last but not least we use the “Comment” function to output the text “Time:” followed by the current time we have calculated and the text: “Market is” followed by the market state directly on our chart.
So, let’s compile the code here by clicking on a little button here or pressing F7 and that worked without any errors and without any warnings so now we are finished and we can click on a little button here or press F4 to go back to Metatrader.
In Metatrader you want to click on “View/ Strategy Tester” or press CTRL + R that should bring up the Strategy Tester panel and here you want to select the Expert Advisor called: “SimpleTime.ex4”, please make sure that you enable the visual mode here and start your test.
Now you should see that the market is closed until the time reaches 9 am – that should be the case in a few seconds now and then it will change to market is open and it did.
So, now you know how to find out the market open and closing time, maybe to use it for entry and exit signals and you have calculated it yourself with a few lines of MQL4 code.
Download “MQL4 Tutorial - Simple TIME Expert Advisor”SimpleTime.txt – Downloaded 2041 times – 1,015.00 B