CHART_FIRST_VISIBLE_BAR seems to give inconsistent result after rescaling the chart

 

Hi!

While trying to code something with chart scaling and navigation, I came across the following result:


The results above where produced by this code:

            Print("DEBUG: Reset: First bar before scaling: ",ChartGetInteger(glChartID,CHART_FIRST_VISIBLE_BAR));
            
            if (!ChartSetInteger(glChartID,CHART_SCALE,glFocus.prevChartState.scale))
            {
               
//... inform error 
               
               return;
            }
            
            Print("DEBUG: Reset: First bar after scaling: ",ChartGetInteger(glChartID,CHART_FIRST_VISIBLE_BAR));
            
            Print("DEBUG: Reset: First bar after scaling: ",ChartGetInteger(glChartID,CHART_FIRST_VISIBLE_BAR));
            
            Print("DEBUG: Reset: First bar after scaling: ",ChartGetInteger(glChartID,CHART_FIRST_VISIBLE_BAR));

As you can see, it seems that right after rescalling a chart by code, the CHART_FIRST_VISIBLE_BAR becomes temporarily inconsistent in providing the correct first visible bar's ID: the expected results is that calling CHART_FIRST_VISIBLE_BAR would produce the exact same results before and after rescalling, but it's temporarily giving a wrong result until its soon corrected (Note: considering the scaling behaviour just talked about in this thread, what I'm presenting here happens with the chart moved to the left size, away from the last produced bar).

Here goes the print of another occasion a little to the left of the case above with the same "temporarily wrong result" appearing:


And here is a complementary funny thing: the actions I performed in this last case were basically identical to another (I missed the points by one bar), and yet they produced a very different result:

In this instance, scaling didn't produce a temporary wrong return by CHART_FIRST_VISIBLE_BAR, but a definitive one. If it was always like the first 2 prints, I could probably just add a tiny Sleep() call right after any scalling operation and the problem would be mitigated, but it seems the situation is a bit worse (though adding Sleep(1000) did minimize the occurances of the first mentioned problem).

I attached an EA with a simple code which may be used to reproduce the problem and I added a "bonus bug" into it: ChartNavigate also seems untrustworthy since it fails from time to time.

Reference for scaling the chart: intentional or bug?
Reference for scaling the chart: intentional or bug?
  • 2026.02.26
  • www.mql5.com
General: Reference for scaling the chart: intentional or bug?
Files:
 
Martin Bittencourt:

Hi!

While trying to code something with chart scaling and navigation, I came across the following result:


The results above where produced by this code:

As you can see, it seems that right after rescalling a chart by code, the CHART_FIRST_VISIBLE_BAR becomes temporarily inconsistent in providing the correct first visible bar's ID: the expected results is that calling CHART_FIRST_VISIBLE_BAR would produce the exact same results before and after rescalling, but it's temporarily giving a wrong result until its soon corrected (Note: considering the scaling behaviour just talked about in this thread, what I'm presenting here happens with the chart moved to the left size, away from the last produced bar).

Here goes the print of another occasion a little to the left of the case above with the same "temporarily wrong result" appearing:

I don't know why the value is changing, that doesn't matter it's internal business, though :

- ChartSetInteger is asynchronous. ChartGetInteger is synchronuous. 

- Set after you set CHART_SCALE, it is NOT executed immediately.

- Most of the time only the 2nd or 3rd get CHART_FIRST_VISIBLE_BAR gives the correct value. It could be the first or none, or all wrong.

To get updated value, you should work with events (better than sleep).

      case CHARTEVENT_CHART_CHANGE:
         res=ChartGetInteger(ChartID(),CHART_FIRST_VISIBLE_BAR,0,v);
         printf("CHARTEVENT_CHART_CHANGE: Reset: First bar after scaling: %i - %lli - #%i",res,v,_LastError);
It will always give you the correct value.
 
Martin Bittencourt:
... I added a "bonus bug" into it: ChartNavigate also seems untrustworthy since it fails from time to time.

Why could not it fails ? If ChartNavigate returns a bool, it's because it can fail.

I can't reproduce it though, you should print the _LastError, so maybe it would be possible to understand why it fails.

 Print("Error advancing 50 bars ",_LastError);