Indicator getting corrupted - page 6

 
Rosh:
Ok, I'll wait

After more than 20 hours the indicator works properly. I don't know what to say.


 

Rosh, Zoom in/Out, scroll to the left/right. Which platform are you testing on 64/32 bit?

 
AnkaSoftware:

Rosh, Zoom in/Out, scroll to the left/right. Which platform are you testing on 64/32 bit?

I can replicate the general issue on 32-bit. The only way in which the O/S seems to be relevant is the question of why your 64-bit system seems to be periodically getting new data at the start or in the middle of its bar history, whereas your 32-bit system (and RaptorUK's 64-bit system) aren't. Regardless, you appear to be intending to give your indicator to other people, and changes to the bar history are issues which you will definitely then come across in real life, if only because of your users suffering broker disconnections which lead to missing bars being inserted into the middle of the history on re-connection.

As I've already tried to explain, it is debatable whether this "bug" is in your code or in MT4. There is an expectation of how indicators will behave in MT4, and your indicator doesn't behave that way. For example, when you create a new indicator using MetaEditor, it inserts for you the line "int counted_bars=IndicatorCounted();". You're removing this and ignoring IndicatorCounted().

I can replicate the general issue using the following indicator which simply draws a line between the last 10 bar-highs when it first starts up:

#property indicator_chart_window
#property indicator_color1 Red
#property indicator_buffers 1

double indicatorvalues[];

void init()
{
   SetIndexBuffer(0, indicatorvalues);
}

void start()
{
   static bool IsFirstCall = true;
   if (IsFirstCall) {
      IsFirstCall = false;
      for (int i = 0; i < 10; i++) {
         indicatorvalues[i] = High[i];      
      }
   }
}

You can then replicate the same kind of problem by doing the following:

* Open a chart for any symbol

* Add the indicator to the chart

* Turn off "Chart autoscroll" (just to make the following steps easier; not because it has any impact on the issue)

* Go to the start of the chart, by pressing Home

* Press Page Up to force a download of extra data. (N.B. There is no problem unless some extra data is actually added to the chart at this point.)

* Go to the end of the chart, by pressing End. The red line between the highs will now be out of position. It will have been moved backwards in time.

[All this is just fleshing out what RaptorUK has already identified.]

 

OK, Can you confirm, IndicatorCounted() will return a -ve number, in this case of missing bars / corrupt indicators? Let me try reinitializing the indicator arrays and redrawing the moves from the lookback bar.

 
AnkaSoftware:

OK, Can you confirm, IndicatorCounted() will return a -ve number, in this case of missing bars / corrupt indicators?

No, what happens - as I've already said on page 5, and as you can easily test for yourself - is that MT4 resets IndicatorCounted() to zero when new bars are added at the start of the history using a method such as the one above. This will cause a normal indicator to redraw all its historic values, because it will typically use the difference between Bars and IndicatorCounted() to determine which bars are "dirty" and need to be updated. If IndicatorCounted() is zero, the indicator therefore recalculates every historic bar, i.e. because Bars - 0 = Bars.

The documentation of IndicatorCounted() (https://docs.mql4.com/customind/IndicatorCounted) provides one example of doing this, and there are alternate versions such as https://www.mql5.com/en/forum/132447 which aim for very marginally improved performance because the standard code at https://docs.mql4.com/customind/IndicatorCounted unnecessarily recalculates one bar which is "clean".
 

Adding IndicatorCounted() checks and reinitializing the indicators when IndicatorCounted() returns value 0, solves the problem of shifting indicators which are caused when history or additional bars are inserted.

However, there is another problem of corruption of indicators, which appears not to be caused by insertion of history bars. Sample code enclosed to reproduce the problem, you need to run it for 10+hrs, till atleast two reinitialization of indicators ie IndicatorCounted() returning 0 (post indicator start).

Files:
Reason: