Update an MT4 indicator every second?

 

Does anyone know if it’s possible to get an MT4 indicator to update every second, rather than every tick?

I know that the start() only runs every time a new tick is received. I’m guessing any possible solution might involve a loop which is initiated inside that.

Creating the loop which continues until the bar is updated (and then allows the bar to update), is not the problem. This can be done by initiating the loop on the first tick of a bar, then counting down seconds within the current bar, and only allowing the loop to be broken on the last second of the bar – thus allowing the new bar to be created and the loop to start all over again. The indicator can be updated/adjusted infinitely within that loop.

The problem is that this type of loop would run practically an infinite number of times within that given bar – which is obviously not good for system resources or the already temperamental MT4 (hence part of the reason for updating by tick in the first place).

Simply adding a Sleep() period somewhere in the loop wouldn’t work, because Sleep() doesn’t work in indicators (as far as I am aware).

Does anyone know a method of adding something which would function in a similar fashion to Sleep(), or any other way around this problem? (I am at the limit of my programming skills!).

I have seen this problem asked many places before, but never actually answered as far as I could find. Sorry if I have repeated a thread. The previous answers seem to boil down to debunking the need for an indicator to update by anything other than every tick. Updating by ticks is fine for live trading, I agree (because updating before a new tick arrives is unnecessary for trading and, conversely, updating every given time period may actually lose acuity if there were many ticks which occurred within that time period).

I’m interested in this problem because I’d like to be able to modify indicator periods over the weekend (when no new ticks arrive), during visual back testing and optimization. This would involve updating the indicator every time the TimeLocal() changed by 1 second. I’m making a visual (drawn object) “slider” which modifies the period of the indicator according to the location of a slider in the chart (which can itself be moved with the mouse, relative to a drawn numerical scale). This allows the period to be easily fine tuned without having to go through the annoying routine of opening the indicator properties and changing the period manually every single time. (Useful for quickly comparing how different periods of the same indicator relate to each other, and instantly flicking back and forward to visually compare them, without having to have many periods drawn at the same time). Unfortunately, if the period is changed this way, the relevant window needs to be redrawn, which can otherwise only occur every time the start() is triggered by a new tick (unless an infinite loop is created again). I hope to post the slider at some point anyway, even if it is not able to update easily at weekends… but I hope someone might know a way around this problem.

 

...

Basic problem in metatrader 4 to do that is the following (it is a quote from mql help file)

The Sleep() function cannot be called from custom indicators since they calculate in the interface thread and may not decelerate it.

Even if you use some other (through windows API for example) way, provided that you manage to arrange a kind of yield functionality for metatrader (and all this has to be done without metatrader API, since there is no such thing, otherwise, metatrader would be dead till the custom sleep function does not allow processing continuation - it is easy to prove : set an indicator into dead lop and try to do anything else in terminal. The same thing would happen with custom sleep that does not interact with terminal to allow other threads regular execution). So, after a lengthy answer, the short conclusion : it can not be done properly in metatrader


In metatrader 5 there is already an OnTimer() event which can be used from indicators (it has minimal resolution of 1 second, which means that they were rather afraid of timer blocking the rest of processing) and with some re-entry prevention it is rather easy to that, but I guess that people are still are waiting to see what exactly is going to happen to metatrader 5

 

Thanks for the quick reply. That was the answer I was afraid of :-( At least I can stop searching now (or learn MT5...). Thanks.

 

I have just come across a possible work around which would solve the problem (at least regarding easily updating an indicator manually when there are no incoming ticks).

It seems possible to cause a refresh to occur, either by using a script which sends a fake tick (and assigning a hotkey to run it), or by pressing Alt+C+R (which runs the Refresh command from the Chart drop down menu).

Metatrader "Refresh" keyboard shortcut @ Forex Factory

 

...

Externally you can cause a refresh event for each open chart(chart, not an indicator). It would be something like this :
PostMessageA(windowHandle,WM_COMMAND,CHART_CMD_UPDATE_DATA,0);

where CHART_CMD_UPDATE_DATA equals 33324. In that case, indicator will work on tick events as well as on timer events generated by some external script or EA (unless it is an offline chart, but that is a whole different story) so it will still process every tick

Internally, from the indicator itself, it is not possible to limit it to work only on a predefined time interval (that was the question, if I understood correctly your original question)

Mimp:
I have just come across a possible work around which would solve the problem (at least regarding easily updating an indicator manually when there are no incoming ticks).

It seems possible to cause a refresh to occur, either by using a script which sends a fake tick (and assigning a hotkey to run it), or by pressing Alt+C+R (which runs the Refresh command from the Chart drop down menu).

Metatrader "Refresh" keyboard shortcut @ Forex Factory
 

You are right, that was really my original question. I think using an EA to send a chart update like that would be easier than having to press a hotkey every time, in the long run. Thanks!

p.s. - especially since pressing refresh always seems to make the WHOLE terminal hang for about 4-5 seconds anyway, which is gets tiresome in itself.

 

mladen

If you recall I gave you a external software that made MT run time wise instead of tick wise. To bad that MT changed some thing that the software no longer works after built 4xx. BUT. What you are suggesting does work. if its inside a looped script. I have a code block that does just that, with some interesting parameters. It will not only drive the window that its attached to but any other window that ask for ticks.

dont have time to post it today will do it tomorrow.

Keit

mladen:
Externally you can cause a refresh event for each open chart(chart, not an indicator). It would be something like this :
PostMessageA(windowHandle,WM_COMMAND,CHART_CMD_UPDATE_DATA,0);

where CHART_CMD_UPDATE_DATA equals 33324. In that case, indicator will work on tick events as well as on timer events generated by some external script or EA (unless it is an offline chart, but that is a whole different story) so it will still process every tick

Internally, from the indicator itself, it is not possible to limit it to work only on a predefined time interval (that was the question, if I understood correctly your original question)
 

external software that made MT run time wise instead of tick wise

Could you describe what is this external software that made MT run time wise instead of tick wise.

 

cockeyedcowboy

I have the same problem with an indicator that needs to be refreshed every minute or so. Do you have the code you can post here that can update/refresh a chart every minute but still have the price update every tick on the price/chart as it now does? It would be appreciated!!

el bee

cockeyedcowboy:
mladen

If you recall I gave you a external software that made MT run time wise instead of tick wise. To bad that MT changed some thing that the software no longer works after built 4xx. BUT. What you are suggesting does work. if its inside a looped script. I have a code block that does just that, with some interesting parameters. It will not only drive the window that its attached to but any other window that ask for ticks.

dont have time to post it today will do it tomorrow.

Keit
 
el bee:
cockeyedcowboy

I have the same problem with an indicator that needs to be refreshed every minute or so. Do you have the code you can post here that can update/refresh a chart every minute but still have the price update every tick on the price/chart as it now does? It would be appreciated!!

el bee

el bee

Take a look at this post : https://www.mql5.com/en/forum/181239

Reason: