MQL4 TUTORIAL BASICS – 75 SIMPLE TIME FILTER

video
play-sharp-fill

In this video we will create a filter that is going to allow or disallow trading based on the current time so let’s find out how to do that with mql4.
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, continue, I will call this file, simple time filter, click on continue, continue and finish.
Now you can remove everything above the ontick function and the two comment lines here.
We start by creating an input string for the start trading time, this one is set to 9:00 am but I will change it to 1:00 am in this case, the input modifier will allow any user to change the values in the expert properties tab without any recompilation, we also need a stop trading time, this is set to 10:00 am so trading will be allowed between 1:00 am and 10:00 am, obviously we need to find out if the current time is between those two timestamps, so let’s create a variable current time, and finally, we want to allow or disallow trading that’s done with this variable, trading is allowed, it’s a bool so it can either be true or false.
Inside of the ontick function, we start by getting the current time that’s a datetime variable and we are using the function time local, so let’s mark that and press F1 and we learn that it returns the local time of the computer where the client terminal is running so that’s your PC and in the next step we use time to string to convert the time that we have calculated, we want to use a special format called time underscore minutes, let’s mark that and press F1 and now you see that we will get this format, hours and minutes divided by a colon.
To check for the entry conditions we are going to use a function that is called check trading time and if the return value is true we consider to open a position, this function doesn’t exist so far so we need to code it later on.
The second entry condition will be if orders total returns a value of zero so this is true when we have no open orders or no open positions and if both conditions are true we are going to use order send to buy ten micro lot. Obviously, you wouldn’t do that on a real account you would add other things like entry signals from an indicator or something like that but for this simple example, it’s good enough.
Let’s add a chart output the comment statement will output if trading is allowed, the current time, the start trading time and the stop trading time that we have defined but now we need to create this function here, it is called check trading time and it’s boolean so it can either return true or false.
In mql4 that will be represented by zero for false and one would be true and to set the trading allowed variable we are going to look into the current time, we are using string substring to find a certain value and the value that we are looking for is the start trading time, let’s mark that and press F1.
String substring has an original string and we are going to look for the value inside of the string, the starting position in our case is zero, the second parameter is for the length of the substring, in our case it’s five and if we can find the defined start trading time within the current time we will set the trading is allowed variable to true.
Otherwise, if the current time contains the defined stop trading time we would set the trading is allowed variable to false.
Finally, we use the return statement to return the trading is allowed result to the main function and 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 first or maybe even the premium course on our website might be interesting for you. This was another suggestion that came from 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 F7 on your keyboard you shouldn’t get 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 we click on view, strategy tester or press control and r, please pick the new file simple time filter dot ex4, mark the option for the visual mode here and start your test.
Here we are! The current start writing time is 1:00 am so within a few seconds we should see the first position right here on the chart and the filter should switch to one, that happened right now, now let’s click on stop, go to the expert properties and we are going to change the start reading time here, restart the test and this time we should see the first trade within a few seconds.
Here it is! It happened right at the start trading time that we have defined so our little expert advisor is working as expected and in this little video you have learned how to create a time filter that is able to allow or disallow trading based on the current time and you have coded it yourself with a few lines of mql4 code.