Array Buffer Values get mixed?!?!

 

Hello,

I have created a simple MT4 indicator that has array buffers and on tick the value of the buffers at position 0 is changed. Everything works correctly. But after some time the buffer values get either currupted or mixed. I visualize the values on indicator_separate_window as a histogram.
Any one has experience same issue? Is there a bug in MetaTrader 4? I use the build version 1356.

Here is a simplified code of how the indicator is working. I removed the complexity, but logically nothing changed. I only change the buffer at bar 0. And it works correctly for some time, but after hours the old buffers get wrong values. I see it on the histogram. 

int OnInit() {

   IndicatorBuffers(10);
   SetIndexBuffer(0,histBarUp);
   SetIndexBuffer(1,histBarDown);

   SetIndexStyle (0, DRAW_HISTOGRAM, STYLE_SOLID, 2, clrGreen);   
   SetIndexStyle (1, DRAW_HISTOGRAM, STYLE_SOLID, 2, clrRed);   

 ....
}


int start() {
   if (Close[0] > Open[0]) {
      histBarUp[0] = 1.0;
      histBarDown[0] = 0.0;
   }
   else if (Close[0] < Open[0]) {
     histBarUp[0] = 0.0;
     histBarDown[0] = 1.0;
   }

  ....
	
}
 
   SetIndexBuffer(0,histBarUp);
   ⋮
      histBarUp = 1.0;

Don't post code that will not even compile. histBarUp must be an array to make it a buffer.

Always post all relevant code (using Code button) or attach the source file.
     How To Ask Questions The Smart Way. (2004)
          Be precise and informative about your problem
 

Yes, of course i declared them as array. 

double histBarUp[], histBarDown[];


So it is not the reason for that. I am experienced MQL programmer. So the issue is a bit more complexer.

 
Dmitry Zhakov #: I am experienced MQL programmer. So the issue is a bit more complexer.

So what does array=value; mean to you, and does it compile?

 

Ah, sorry I forgot [0]. Fixed now above. But in the MQL code it is correct.

Yes, it compiles and I am running it. It looks all good, but after some time (1-2 hours) the old histogram bars get wrong values. But in the MQL code I am setting the value only to the bar 0. I am not modifying the values for the bars > 0. So it means that MetaTrader manipulates old buffers values or mixes it. 

 
Dominik Christian Egert #:
Maybe that's in fact a defective memory Module in your computer.

Sounds little bit like it.




I think it is a bug in MT4!

 
Dmitry Zhakov #: I think it is a bug in MT4!
  1. You are using the old style, non-strict form of MQL4 code, which I consider almost obsolete. Consider using the newer, stricter, MQL4+ format.
  2. From your code, you are not setting any values when close is neither above nor below the open, leaving it unset.
  3. From your code, you don't seem to be initialising your buffers or at least assigning data to all the older buffer elements.
  4. From your code, you don't seem to take into account when older data needs to be set or refreshed, according to "prev_calculated".
  5. From your code, you don't seem to to be detecting when a rebuild is necessary, due to a change in "rates_total", or a reset of "prev_calculated".
  6. I don't think it is a bug in MT4, but rather a bug in your own code.
Updated MQL4 - Language Basics - MQL4 Reference
Updated MQL4 - Language Basics - MQL4 Reference
  • docs.mql4.com
Updated MQL4 - Language Basics - MQL4 Reference
 
Fernando Carreiro #:
  1. You are using the old style, non-strict form of MQL4 code, which I consider almost obsolete. Consider using the newer, stricter, MQL4+ format.
  2. From your code, you are not setting any values when close is neither above nor below the open, leaving it unset.
  3. From your code, you don't seem to be initialising your buffers or at least assigning data to all the older buffer elements.
  4. From your code, you don't seem to take into account when older data needs to be set or refreshed, according to "prev_calculated".
  5. From your code, you don't seem to to be detecting when a rebuild is necessary, due to a change in "rates_total", or a reset of "prev_calculated".
  6. I don't think it is a bug in MT4, but rather a bug in your own code.

Thank you very much! I will check all these points, fix the code and see... 

 
Dmitry Zhakov #: Thank you very much! I will check all these points, fix the code and see... 


#property strict

#property indicator_separate_window

#property indicator_buffers   2

#property indicator_label1    "Bar Up"
#property indicator_color1    clrLimeGreen
#property indicator_width1    2
#property indicator_style1    STYLE_SOLID
#property indicator_type1     DRAW_HISTOGRAM

#property indicator_label2    "Bar Down"
#property indicator_color2    clrFireBrick
#property indicator_width2    2
#property indicator_style2    STYLE_SOLID
#property indicator_type2     DRAW_HISTOGRAM

double g_dbBufferBarUp[], g_dbBufferBarDown[];

int OnInit( void )
{
   IndicatorSetInteger( INDICATOR_DIGITS, _Digits );
   SetIndexBuffer( 0, g_dbBufferBarUp,   INDICATOR_DATA );
   SetIndexBuffer( 1, g_dbBufferBarDown, INDICATOR_DATA );
   return( INIT_SUCCEEDED );
};

int
   OnCalculate(
      const int      rates_total,
      const int      prev_calculated,
      const datetime &time[],
      const double   &open[],
      const double   &high[],
      const double   &low[],
      const double   &close[],
      const long     &tick_volume[],
      const long     &volume[],
      const int      &spread[]
   )
{
   for( int i = rates_total - ( ( prev_calculated < 1 ) ? 1 : prev_calculated ); i >= 0; i-- )
   {
      double dbCloseOpen     = close[i] - open[i];
      g_dbBufferBarUp[   i ] = dbCloseOpen > 0.0 ?  dbCloseOpen : 0.0;
      g_dbBufferBarDown[ i ] = dbCloseOpen < 0.0 ? -dbCloseOpen : 0.0;
   };

   return( rates_total ); // Return value of prev_calculated for next call
};

 

The problem is that the data I set to the buffer[0] is available only at the current time. And I can not calculate it for the past bars. So I do not have the for-loop, since I write only to the bar 0. Can you please show me how should I fix my code according to these 3 comments:

  1. From your code, you don't seem to be initialising your buffers or at least assigning data to all the older buffer elements.
  2. From your code, you don't seem to take into account when older data needs to be set or refreshed, according to "prev_calculated".
  3. From your code, you don't seem to to be detecting when a rebuild is necessary, due to a change in "rates_total", or a reset of "prev_calculated".

I would be very thankful if you can send me short code example, how to take into account these 3 steps.

In the OnInit section I already had this:

   SetIndexEmptyValue(0, 0.0);
   SetIndexEmptyValue(1, 0.0);
 
Dmitry Zhakov #: The problem is that the data I set to the buffer[0] is available only at the current time. And I can not calculate it for the past bars. So I do not have the for-loop, since I write only to the bar 0. Can you please show me how should I fix my code according to these 3 comments:
  1. From your code, you don't seem to be initialising your buffers or at least assigning data to all the older buffer elements.
  2. From your code, you don't seem to take into account when older data needs to be set or refreshed, according to "prev_calculated".
  3. From your code, you don't seem to to be detecting when a rebuild is necessary, due to a change in "rates_total", or a reset of "prev_calculated".

I would be very thankful if you can send me short code example, how to take into account these 3 steps.

In the OnInit section I already had this:

I have already provided you with an example in my previous post ... #9

As for you not being able to use a loop to update previous values, without an actual detailed explanation of what you are doing, I cannot just "guess" at what should be done for your case specifically.

Reason: