MQL5 TUTORIAL - SIMPLE TIME BASED ENTRY

Опубликовано: 27 Июль 2026
на канале: MQL5 Tutorial
18,315
256

https://mql5tutorial.com/?s=entry

With MQL5 for Metatrader5 we create an Expert Advisor (also known as Trading Robot for automated forex trading) that will open a trade at a specific time. Some entry strategies rely on time based entries - like the market opening strategy...


In this video we are going to talk about Time Based Entries. This little Expert advisor will open a trade at three o'clock P.M. that's 15:00 here on the chart in German time so now it's three, two, one hour now the trade is opened. You might ask yourself what is this good for but actually there are a few strategies that use time based entries. There is a German trader who waits for the American market to open and he said that most of the time if there is a time like this the price will return to where the price was when the market opened. This is a very simple version; it will always open a trade at the same time regardless of the direction of the market so let's see how this very simple version can be created. To do that he's click on the little button here or press F4 in your Meta Trader and now you should see the Meta Editor and here you want to click on File, New, Expert Advisor from Template, Continue. I will call this one Simple Time Based Entry. Now click on Continue, Continue and Finish, and remove everything above the OnTick function and the two comment lines.
To open a trade we first need to import and include the file trade.mqh and to create an instance of C trade that is actually a class in the trade.mqh library and for a buy trade we need to get the ask price that is done by using Normalized Double, Symbol Info Double_symbol, Symbol_Ask and with _Digits we get either three or five digits and now I need to get the local time, that is done by using a date/time variable called Time and we get the local time by using the function Time Local. It returns the local time of a computer so you don't have to mess around with time zones actually we don't need the date we only need the time and we can create a formatted time string by using time to string for the time that we have calculated here and with the parameter Time_Minutes, we will get a formative time that contains the hours and the minutes. And if we do not have any open positions and if the substring for our formatted time we have created here contains 15:00 starting from the first to the fifth character in the string hours and minutes, we want to actually open a buy trade here this is only done for demonstration purposes by using trade.buy four ten micro lot on the current chart we want to buy for the ask price. We did not set any stop loss and the take profit is one hundred points above the ask price, this parameter would be a comment but we don't need one so we use no.
And the last step is to output the value of our string hours and minutes by using the comment function and if everything works well you can now click on the compile button or press F7 and you should get no errors and no warnings and in that case please click the little button here to go back to Meta Trader and in the Strategy Tester please set the Simple Time Based entry.ex5 file, let's enable the visualisation mode here and start your test. And now you should see this Expert Advisor open a trade whenever the time here contains the string you have defined in your Expert Advisor. Okay now you know how to create an Expert Advisor that will open a trade whenever a certain time is reached and you have created it yourself with a few lines of MQL 5 code.