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.
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.
- You are using the old style, non-strict form of MQL4 code, which I consider almost obsolete. Consider using the newer, stricter, MQL4+ format.
- From your code, you are not setting any values when close is neither above nor below the open, leaving it unset.
- From your code, you don't seem to be initialising your buffers or at least assigning data to all the older buffer elements.
- From your code, you don't seem to take into account when older data needs to be set or refreshed, according to "prev_calculated".
- 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 don't think it is a bug in MT4, but rather a bug in your own code.
- docs.mql4.com
- You are using the old style, non-strict form of MQL4 code, which I consider almost obsolete. Consider using the newer, stricter, MQL4+ format.
- From your code, you are not setting any values when close is neither above nor below the open, leaving it unset.
- From your code, you don't seem to be initialising your buffers or at least assigning data to all the older buffer elements.
- From your code, you don't seem to take into account when older data needs to be set or refreshed, according to "prev_calculated".
- 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 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...
#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:
- From your code, you don't seem to be initialising your buffers or at least assigning data to all the older buffer elements.
- From your code, you don't seem to take into account when older data needs to be set or refreshed, according to "prev_calculated".
- 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);
- From your code, you don't seem to be initialising your buffers or at least assigning data to all the older buffer elements.
- From your code, you don't seem to take into account when older data needs to be set or refreshed, according to "prev_calculated".
- 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.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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.