indicator values differ between completed and current bars

 
Colleagues,

Please advise why two indicators with identical mq4 code produce different picture on the chart? More precisely, what is the methodolody of indicator development, for two lines (one drawn in real-time and one produced by calculating completed bars) would be the same? Here is the screenshot I've got:

http://dev.webie.ru.fox.mtw.ru/.chucha/trading/JMA-unsync.png



Aqua line - this indicator was running 2 days long, was dropped and left unchanged since 2 days ago. I didn't update its properties, no changes in parameters, no recompilation, so there was no reason to interrupt the algorithm. Purple line is the same indicator (a copy), but dropped into the chart right now. The spot where two lines start to be different is the moment when Aqua JMA began to receive start() callback from MT4 on every incoming tick.

Observations: the lines from simple indicators don't have any difference (like Moving Average of any kind). JMA lines are different. There are some "unique" developments like RSX which changes the line all the way backward when depending on incoming tick some distant point located 10 bars ago, starts jumping around... But I have in my indicator the same issue like in JMA shown above and I can't understand how I can fix it.

How to reproduce the issue:

1. download the code of the indicator and put in well known folder for MT4 as JMA.mq4 :
http://www.abysse.co.jp/mt4/indicators/JMA%20%5Bi%5D.mq4
(or here http://www.abysse.co.jp/mt4/indicator_name_j.html >> "JMA square_bracket i square_bracket.mq4" )

2. make a copy of this indicator, naming this copy as JMA2.mq4 ;

3. to create new chart (delete everything, make it clean), drop JMA.mq4 on the chart ;

4. wait for 2 days, drop JMA2.mq4 on the same chart;

5. see the difference.

6. you can launch an optimization for any expert advisor (visual test with tick modeling). launch test, pause it, drop first JMA, release pause, let 20 bars to appear, pause it again, drop the second JMA.

I assume that the answer to my question "how shall I develop my indicator so the lines drawn wouldn't become different between realime bars and completed past bars?" must be somewhere here:

1. MT4 interrupts sometimes and announces "waiting for update" on the chart, so it's not documented what exactly happens with the indicator methods invocation. does MT4 invokes init()? does MT4 invokes start()? when? does MT4 invokes start() once for all bars or it invokes start() individually for each bar which had changed due to "update"? in that case, the solution comes here: ...

2. the indicator is developed with volatility collected during the bar being under completion. for a completed bar, start() is invoked once, but for current bar start() will be invoked for each incoming tick (change of quotes); so, at some point something extra is collected somewhere because of multiple start() calls on non-completed bar and a glitch occurs; for that case, the solution is here: ...


Thank you very much for any of your suggestions!
 

Most indicators do not maintain state variables. Current value depends only on close and previous bars.

JMA has state variables. The order that ticks come in effects those. The new indicator has no order, it only know OHLC. Eventually the new will converge to the old, but that takes time.

Once you see the difference, either right click/refresh or change timeframes and back. then both will now be the same.

 

Thank you WHRoeder, I wouldn't ask if the answer was so simple :)
Okay, me I can click on the screen before I make my decision whether I open position or not.
But isn't it tempting to use JMA within an Expert Adviser?... How to refresh indicator value from within Expert Adviser?
For this case, I need a methodology what I need to lookFor/check/change in state-dependant indicators to make it show the same line/value@bar without manual refresh....
Or, how to write an indicator which always shows the same - from the scratch, which variables or kind of algorithms I should avoid.

I developed an indicator which puts in its output buffer a value only once, at the very first tick of each bar (would you need a sample?...).
Still, the problem exists, and I see that the Variable Watch window shows one value, while a print out from EA shows the other value of the same indicator.
I know that in EA an indicator is instantiated from the zero-state, the order of init() - start() calls is different, so I need to know what exactly happens from the side of MT4 in terms of indicator functions invocation, and which is the most common case of misuse MT4 API and to get wrong indicator values.

Dear Mr.Rosh, please answer if you hear me?....