MQL5 TUTORIAL - Time Filter explained (in 4 min)

Publicado em: 26 Setembro 2024
no canal de: MQL5 Tutorial
931
24

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

In this video, we are going to create an Expert Advisor that trades within a specific time window. Let's see how we can do that.
We start Metaeditor by clicking the little icon or pressing F4.
First, we include the trade library to access trading functions. Then, we create an instance of the CTrade class, which will handle our trading operations.
We define three input strings: StartTradingTime, StopTradingTime, and CurrentTime. StartTradingTime and StopTradingTime specify the time window during which trading is allowed. CurrentTime will hold the current local time.
We also create a boolean variable, TradingIsAllowed, to control whether trading is permitted based on the current time.
In the OnTick function, we first get the current Ask price using SymbolInfoDouble and normalize it to the appropriate number of digits with NormalizeDouble.
Next, we get the local time using TimeLocal and convert it to a string in the format of hours and minutes using TimeToString. This string is stored in CurrentTime.
We then check if the current time falls within the trading window by calling the CheckTradingTime function. If trading is allowed and there are no open positions (checked using PositionsTotal), we open a buy trade. The trade.Buy function is called with parameters specifying the trade volume (0.10 Lots, which is 10 Micro Lots), the Ask price, no stop loss, and a take profit set 100 points above the Ask price.
We also output some information to the chart using the Comment function, displaying the trading permission status, current time, start trading time, and stop trading time.
The CheckTradingTime function determines whether trading is allowed. It compares the first five characters of CurrentTime with StartTradingTime and StopTradingTime. If CurrentTime matches StartTradingTime, trading is allowed. If it matches StopTradingTime, trading is not allowed. The function returns the value of TradingIsAllowed to the main function.
Finally, we press F7 to compile the code.
If this was too fast for you or if you don't understand what all the code is doing, you may want to check out the Premium course on our website or watch one of the basic videos first.
If the code compiles successfully, we go back to MetaTrader by pressing F4 or clicking on the icon. Back in MetaTrader, we press Control and R to start the strategy tester, pick the Expert Advisor that we have just created, enable the visual mode, and start a strategy test. We should see the Expert Advisor on the chart.
If you are already a Premium course member and have an idea for a video like this one, you can send us an email.
In this video, we have learned how to create an Expert Advisor that trades within a specific time window using a few lines of MQL code. Thanks for watching, and I will see you in the next video.


Nesta página do site você pode assistir ao vídeo on-line MQL5 TUTORIAL - Time Filter explained (in 4 min) duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário MQL5 Tutorial 26 Setembro 2024, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 931 vezes e gostou 24 espectadores. Boa visualização!