
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hello everyone,
I'm currently developing several multitimeframe (MTF) indicators for MT5, and I have been dealing with the well-known problem of working with MTF when the market is closed, as I have recently posted here.
I think I have successfully developed a new solution I haven't found in the forum, which consists of automatically switching to a different timeframe, and return back to the original one, using global variables of the client terminal.
I will try to summarize the problem and my solution, and I hope that it can be useful to some of you working on the same problem. I'm sorry if I make some wrong assumptions, or my explanations are not perfect, since I'm new to MT5 and I have not as much expertise as some other members of the forum. I have been reading and understanding the documentation as much as I could.
Description of the problem:
Let's say we are in M1 (TF1) but we need to access bars from D1 (TF2). We call iBarShift in TF2, but the market is closed (let's say we're on weekend) so we get an error (WRONG_VALUE or -1). That error means that TF2 bars are still not loaded in the client terminal, and the indicator won't load either, since we need bars from TF2 but we can't get them. Since it is weekend, there aren't onCalculate events as they would be when the market is open. Thus, the final result is the indicator won't load.
Some known solutions:
To manually switch to a different timeframe than TF1, let's say to M5 (TF3) and then we switch back to TF1. Sure enough, the terminal will have had time to load TF2 bars, and the indicator will fully reload. Since TF2 bars are ready, iBarShift won't fail again.
To use ChartSetSymbolPeriod(0, NULL, 0) as Alain Verleyen has suggested in several posts, like this one for example. This is similar to refresh the chart, but as I explained here, this can lead to some really strange behaviour: iBarShift alternates between failing and not, which doesn't make a lot of sense, really.
Another problem with this method is that it's like using a hammer to kill a fly, as Alain cleverly stated, because it will lead to reload all indicators for all charts using the considered symbol.
My solution: automatically switch to TF3 and back to TF1 using global terminal variables
In order to do this switch automatically we set a timer that would generate a OnTimer event. Before the timer goes by, the client terminal will have some time to load bars in TF2 (and even sometimes it will have to load TF1 bars too). When OnTimer gets called, we will use ChartSetSymbolPeriod to move to TF3.
Given that the indicator is fully re-initialized after moving to a different TF, we have to use a global variable of the client terminal to be able to do the trick, because they don't get deleted after the indicator is re-initialized. In that global variable we will store the current Timeframe (TF1), so that we can return to it later. Normal global variables, or static variables are deleted after an indicator gets re-initialized, so there would be no way to know to which TF to return to.
After we have moved automatically to TF3, we check whether our global variable exists. If it exist, we don't calculate the indicator, and we just switch back to our original timeframe TF1. When we are again in TF1, we just delete the global terminal variable.
I have tried this system with a very simple multitimeframe indicator that paints the closing value of the chosen higher timeframe candles. By switching back and forth between timeframes, the problem stated in point 2 above by Alain in the previous section dissapears. Now only the current indicator will be reloaded. I have tried to make the code clear enough so that it can be reusable: