In this video, we are going to create a filter for time-based entries so let’s find out how to do 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, I will call this file, simple time filter, click on continue, continue and finish.
Now you can delete everything above the ontick function and the two comment lines here.
We start with an include statement to include the file trade dot mqh, this one comes with mql5 and it will give us simplified trading functions, so let’s create an instance of the class ctrade, we will call that one trade.
Now let’s create a user-defined input variable, this is a string variable called start trading time. In my case I use 9:00 am and I would also like to have a stop trading time, this is set to 10:00 am and we will only be able to trade within the allowed trading hours and to that, we also need to create a string variable for the current time, and if the current time is not within the allowed trading hours we will set the variable, trading allowed to false, that’s also the default value here.
Inside of the ontick function we use symbol info double for the current symbol on the chart to calculate the ask price that is done by using symbol underscore ask and with normalize double and underscore digits we automatically calculate the right number of digits behind the dot.
The ask price is enough for this simple example because we are not going to use any entry signals to see if the first trade is actually opened at 9:00 am.
Now let’s use time local, this will return the local time of a computer where the client terminal is running, so this is your current time but we are going to use a string value, so we take the time here and use timetostring for the current time to get time underscore minutes and that will give us the current hours and the current minutes and because we have used timetostring this will be a string variable so now we have the current time and we are going to use a function that is called check trading time and only if this one returns true our expert advisor is allowed to open any positions and if positions total returns a value of zero – that would mean that we have no open position – and this is when we are going to use trade dot buy to open a buy trade for ten micro lot just based on the time filter.
You wouldn’t do that on a real account but for this simple example, it is good enough.
We also want to add a comment statement, this will give us the result if trading is allowed or not, the current time, and the values for the start trading and the stop trading time.
That’s it for the main function.
So let’s create this customized function that we want to call, the return value will be bool, so it’s either true or false, the name of the function is check trading time and if we can find a substring that is equal to the start trading time inside of the current time that’s when we set trading is allowed to true.
String substring will start with the very first character, you could use length equals minus one, in our case, I have set it to five and only when the start trading time is included in the current time we want to allow the expert advisor to open a new position.
Otherwise, if the stop trading time is included in the current time that’s when we want to set trading allowed to false.
Finally, we use the return statement to return the value for trading is allowed to our main function.
Okay, that’s about it.
If this was too fast for you or if you have no idea what all the code here does maybe you want to watch one of the other videos in this basic video series or maybe even the premium course on our website might be interesting for you.
By the way, this was another idea of a premium course member so if you are a premium course member already and you have an idea for a video like this one please let me know.
Now, please click on the compile button or press F7 on your keyboard, you shouldn’t see any errors here and if this is the case you can click on a little button here or press F4 to go back to Metatrader.
And inside Metatrader please click on view, strategy tester or press control and r, we are going to use a clean template without any indicators, please pick the new file simple time filter dot ex5, mark the option for the visual mode here and start a test.
Here is our expert advisor! The time is counting and as soon as it says that we have 9:00 am we should see a trade here, there it is, now let’s stop the test, change the time window from eight to ten, restart the test.
Here we are! It’s half-past seven and at 8:00 am we see the first trade so our little expert advisor is working, it can set the trading is allowed value to true or false depending on the current time and you have coded it yourself with a few lines of mql5 code.