indicator_buffers & IndicatorBuffers

 

I tried doing a search buy I keep getting an error, so I'll post my question and hopefully someone can point me to a thread that answers it.

I have an indicator that I'm modifying (it's my first one). It keeps locking up MT4 (an endless loop maybe) and I have to use the Task Manager to shut it down and sometimes I have to repair MT4 in order to get it back up again. I'm an accomplished database and spreadsheet programmer so I feel confident in my abilities to program. I just haven't yet figured out how to debug effectively by only using "Print" and "Alert" and "MessageBox".

I think I have tracked down my problem to the buffers (but I'm not sure).

I cannot understand the dictionary when it talks about IndicatorBuffers & indicator_buffers.

I don't think there is a limit on how many arrays[] I can have - is that correct?

I think I need a buffer for each line drawn in the indicator - is that correct?

The dictionary talks about additional buffers for counting, but I don't understand - counting WHAT?

So what do I need buffers for?

 

There is no limit to how many regular arrays you can have

Buffers are special arrays used for holding all the values for drawing lines you can only have 8 buffers.

You need a buffer for each line

Additional buffers for counting means buffers can be used if you need to hold values in an array when each value is related to a price bar for some reason other than to draw a line. Maybe to do further calulations to those values before drawing the final line. This is because of the fact when a new bar forms an indicator buffer is automatically re-indexed so the zero index takes the new value related to the new price bar and all previous values in the buffer move up one index. Sometimes this is useful for other purposes than to draw a line.

For example, you want to create an indicator that takes the open price for each bar and adds it to the close price, then creates a moving average of that, you could put the result of Open[i] + Close[i] into a buffer so that every time a new price bar forms the buffer acurately holds the values of each Open[i]+Close[i], correctly indexed to the bar number they represent.

So when you have your buffer of Open[i]+Close[i] you can then apply your averaging algorithm to the values held in that buffer, then put the results of that average into a second buffer, the second buffer is then used to draw a line.

That is what they mean when they say the first buffer was used for counting although it is not really "counting" it is probably a quirk of the translation from Russian to English

 

You can have 8 buffers, one for each line, you need 2 for a histogram if you want a start and end point for the bars . . . Indicator buffers are "special" arrays . . . you can have plenty of arrays, not sure what the practical limit is. You can simulate Indicator buffers in EAs.

indicator_buffers this may help . . or confuse even more https://www.mql5.com/en/forum/125856


PS. search using Google and add mql4 or site:forum.mql4.com to your search query

 
SDC:

There is no limit to how many regular arrays you can have

Buffers are special arrays used for holding all the values for drawing lines you can only have 8 buffers.

You need a buffer for each line

Additional buffers for counting means buffers can be used if you need to hold values in an array when each value is related to a price bar for some reason other than to draw a line. Maybe to do further calulations to those values before drawing the final line. This is because of the fact when a new bar forms an indicator buffer is automatically re-indexed so the zero index takes the new value related to the new price bar and all previous values in the buffer move up one index. Sometimes this is useful for other purposes than to draw a line.

For example, you want to create an indicator that takes the open price for each bar and adds it to the close price, then creates a moving average of that, you could put the result of Open[i] + Close[i] into a buffer so that every time a new price bar forms the buffer acurately holds the values of each Open[i]+Close[i], correctly indexed to the bar number they represent.

So when you have your buffer of Open[i]+Close[i] you can then apply your averaging algorithm to the values held in that buffer, then put the results of that average into a second buffer, the second buffer is then used to draw a line.

That is what they mean when they say the first buffer was used for counting although it is not really "counting" it is probably a quirk of the translation from Russian to English

Alright, it makes a lot more sense now. I'm now convinced that this is where my problem is.

2 more questions. I'm going to test these myself, but it would be nice if someone can confirm the conclusions I reach.

1) Can I set indicator_buffers 0 and IndicatorBuffers(8) for 8 visual indicators (no extra buffers needed for calculations) ?

It bothers me that in the setup for the indicator, the names for the 8 indicators are "0" - "7". There is no way for the user to know which line (s)he is modifying. I want to create an "extern color UpTrend_Color" for the user to modify and then the indicator sets the color accordingly.

2) Can I simulate the action of a visual buffer by using "ArraySetAsSeries" and "ArrayResize" for a non visual buffer?

My indicator has 8 visual lines (after debugging it will be reduced to 4). Internally the 8 indicators use 4 additional arrays[] for pre-calculations. Specifically I need to check if 2 sets of EMA arrays[] cross each other during the current bar which means that I need to compare their current values with the values from the previous bar. When they cross it triggers all the visual indicators. If I can't treat the 4 EMA arrays[] like the visual buffers then I have to store the current value of the EMA arrays[] and copy them into variables when a new bar is created. Not hard to do, just interested to know if it's an option to have the EMA arrays[] automatically behave like a visual buffer.

IOW If I "ArraySetAsSeries(myEMA1,true)",when I increase the size of myEMA1 with "ArrayResize(myEMA1, Bars)" is the value of the old myEMA1[0] now in myEMA1[1] - or would I have to move all the values up one notch myself?

 
RaptorUK:

You can have 8 buffers, one for each line, you need 2 for a histogram if you want a start and end point for the bars . . . Indicator buffers are "special" arrays . . . you can have plenty of arrays, not sure what the practical limit is. You can simulate Indicator buffers in EAs.

indicator_buffers this may help . . or confuse even more https://www.mql5.com/en/forum/125856


PS. search using Google and add mql4 or site:forum.mql4.com to your search query

Nice idea for searching in Google. I'll have to remember it.

The link did confuse me more. As long as I don't do anything unusual with my buffers and arrays[], I'm fine. But if I need to do something unusual then I have more questions. Please see my response to "SDC" for my questions.

 
FoxGuy:

Alright, it makes a lot more sense now. I'm now convinced that this is where my problem is.

2 more questions. I'm going to test these myself, but it would be nice if someone can confirm the conclusions I reach.

1) Can I set indicator_buffers 0 and IndicatorBuffers(8) for 8 visual indicators (no extra buffers needed for calculations) ?

It bothers me that in the setup for the indicator, the names for the 8 indicators are "0" - "7". There is no way for the user to know which line (s)he is modifying. I want to create an "extern color UpTrend_Color" for the user to modify and then the indicator sets the color accordingly.

2) Can I simulate the action of a visual buffer by using "ArraySetAsSeries" and "ArrayResize" for a non visual buffer?

My indicator has 8 visual lines (after debugging it will be reduced to 4). Internally the 8 indicators use 4 additional arrays[] for pre-calculations. Specifically I need to check if 2 sets of EMA arrays[] cross each other during the current bar which means that I need to compare their current values with the values from the previous bar. When they cross it triggers all the visual indicators. If I can't treat the 4 EMA arrays[] like the visual buffers then I have to store the current value of the EMA arrays[] and copy them into variables when a new bar is created. Not hard to do, just interested to know if it's an option to have the EMA arrays[] automatically behave like a visual buffer.

IOW If I "ArraySetAsSeries(myEMA1,true)",when I increase the size of myEMA1 with "ArrayResize(myEMA1, Bars)" is the value of the old myEMA1[0] now in myEMA1[1] - or would I have to move all the values up one notch myself?

set indicator_buffers globaly the amount of buffers for drawing only this is usually used to corrolate with how many #property indicator_color you have listed globaly too

set IndicatorBuffers in init() the amount of total indicator buffers including counting buffers.

you can create extern color if you want to identify colors with specific indicators in a more user friendly way

yes you can treat your EMA arrays as simulated buffers with ArraySetAsSeries() and ArrayResize()

and yes you will have to move up all the values one notch yourself, with code to detect when the first tick of a new bar arrives

 
SDC:

set indicator_buffers globaly the amount of buffers for drawing only this is usually used to corrolate with how many #property indicator_color you have listed globaly too

set IndicatorBuffers in init() the amount of total indicator buffers including counting buffers.

you can create extern color if you want to identify colors with specific indicators in a more user friendly way

yes you can treat your EMA arrays as simulated buffers with ArraySetAsSeries() and ArrayResize()

and yes you will have to move up all the values one notch yourself, with code to detect when the first tick of a new bar arrives

Thanks,
 

The one thing I'm not entirely sure about is ArraySetAsSeries() I could be wrong about this but I think maybe you only really need to use that if you are going to use the array[] with iMAOnArray()

The documentation says an iMAOnArray() indicator is calculated from left to right so I assume that means it treats the array[] kind of like a data stream rather than accessing the values explicitly by their indexes, so the order would be important, I think for all other purposes the indexing order would be irrelevent as you would normaly access the array[] values by index .

Like I said I could be wrong about that maybe someone who knows more about it could clarify.

 
SDC:

set indicator_buffers globaly the amount of buffers for drawing only this is usually used to corrolate with how many #property indicator_color you have listed globaly too

set IndicatorBuffers in init() the amount of total indicator buffers including counting buffers.

you can create extern color if you want to identify colors with specific indicators in a more user friendly way

yes you can treat your EMA arrays as simulated buffers with ArraySetAsSeries() and ArrayResize()

and yes you will have to move up all the values one notch yourself, with code to detect when the first tick of a new bar arrives

It looks like I can't "property indicator_buffers 0" with "IndicatorBuffers(8)". Only 1 line shows up on the chart. Evidently you need to set the # of lines in "indicator_buffers" if it's more than 1. Completely eliminating the "indicator_buffers" line completely still allowed 1 line on the chart - same as "indicator_buffers 0"


Oh Well;

 

yes you should set #property indicator_buffers to the amount of drawing buffers, one must be the minimum number of buffers you can set. I tried -1 and it still draws 1 line so it appears if you dont want any lines you would have to do it by either not setting any line colors or by setting the all line styles to DRAW_NONE

 
SDC:

yes you should set #property indicator_buffers to the amount of drawing buffers, one must be the minimum number of buffers you can set. I tried -1 and it still draws 1 line so it appears if you dont want any lines you would have to do it by either not setting any line colors or by setting the all line styles to DRAW_NONE

That's the same thing I discovered. Nice to know that I'm on the right track in my learning MT4.
Reason: