MQL4 – SIMPLE TIME BASED ENTRY

video
play-sharp-fill
Here we are going to code a little Expert Advisor which will find out for us if the market is closed or it is open, just as you can see it here.
We begin the code with of the Expert Advisor with external variables. This enables you to set the market opening and closing time for your country manually.
I live in Germany so the trading hours start here at 9:00 am and end at 5:30 pm. – in German notation that is 17:30 so, when the time will reach 9:00 the text here will change to “Market is open” and when time will reach 17:30 it will change to “Market is closed”.
So, how can we code an Expert Advisor which is able to recognize the local time and create a signal for the market opening or closing time?
To start the code, please click on the Metaeditor button here or press F4 to use the shortcut.
When your Metaeditor is running please click on “File/ New/ Expert Advisor (template)” from template, click “Next”, let’s name the new Expert Advisor as: ”SimpleTime”, click “Next”, “Next” again then “Finish”.
Here let us remove everything above the “OnTick” function, even the two comment lines. This time we need to define two string variables above the “OnTick” function so you can set the opening and closing time of the market manually.
It is the “extern” command which is a modifier that makes this possible. When you open the properties for the Expert Advisor you can modify the variable settings.
We also need to define two integer variables; one will be the index for the time of the market opening and the other one will be the index for the time of the market closing. I want one more string variable for showing the market state.
We begin with a closed market and in the “OnTick” function we calculate the time in seconds and convert it to string by the function; “TimeToStr” and with the function “TimeLocal” we get the values of the “TIME_DATE” and the “TIME_SECONDS”. Please press F1 and here you will find out that “TIME_DATE” will give you the year the month and the day while “TIME_SECONDS” is giving you the hours, the minutes and the seconds. This provides us the data for the current time on our computer.
Next we want to check in the time string if the opening time is present by using the command: “StringFind”, this will search for the previously calculated market opening time in seconds.
Now we have the last parameter to add which is for the starting position. In the string we want to start our search exactly from the first character then we apply the same for the closing time.
For this purpose we need another command, which is: “StringFind”. This will search the time string to find the market closing time. Once it finds the opening time and the value for index market opening time is greater than zero then we set the market state to open. If this isn’t true and the closing time is being found and the index market closing time is greater than zero we set the market state to closed.
Now we have left with the text output on the chart, for that purpose we use the “Comment” function. So, we output the texts directly on the chart: “Time:” following by the current time as it was calculated and the text: “Market is” following by the market state.
Now we can compile the code by clicking on the “Compile” button here or by hitting F7 on the keyboard. We haven’t gotten any errors or warnings so we have finished the code.
Let’s go back to Metatrader by clicking this button here or by hitting F4 on the keyboard.
When you are back in Metatrader let us launch the Strategy Tester , so please click on “View/ Strategy Tester” or use CTRL + R. Let’s select the Expert Advisor we just coded: “SimpleTime.ex4”, let’s turn on the visual mode by this checkbox here then let’s start a test to see our Expert Advisor at work.
If it works well we need to see here that it says market is closed and when the time reaches 9 am – which is about to happen just in a few seconds – it should say: market is open. Yes, it said it just like that.
Well done if you reached this far! You just learned how to find out the market open and closing time with a Simple Market Time Expert Advisor which can be used for entry and exit signals and you have coded it by yourself in MQL4 with a few lines only.

Download “MQL4 - SIMPLE TIME BASED ENTRY” SimpleTime.txt – Downloaded 1604 times – 1,015.00 B