Question about Indicator Drawing

 

This is probably a dumb question but its something thats been bothering me for ages.

In the mql4 documentation it says indicators are drawn from left to right, but how can that be when it is drawn in the loop

int counted_bars=IndicatorCounted();
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
for(int i=0; i<limit; i++)

so the drawing loop, is; for(i=0; i<limit; i++)

so it is starting to count from bar zero

the current bar is always bar zero so how can the indicator be drawn from left to right, it has to be drawing from right to left. right ?

surely for the indicator to be drawn from left to right it would have to use for(i=limit; i>0; i--) ?

Or is it a fact that an indicator can be drawn either way, left to right, or right to left ?

The reason for this question is because there are times when I want to access the value of the indicator line at the previous bar while the indicator is drawing, and usually it crashes the indicator I came to realise, is this because the usual way of drawing as per my first example, is actually from right to left therefore making it impossible to access the buffer value at the previous bar while the indicator is being drawn as it has not yet been calculated ?

 

A) Rather than

for(int i=0; i<limit; i++)

I use

for(int i=limit-1; i>=0; i--)

and so work from oldest to newest.

B) I (or MT4!) 'have problems' when I use a previous calc-ed buffer value immediately, so I use *TWO* loops; the first to calculate 'working' value, and second to use 'working value' to 'indicator value' i.e.

for(int i=limit-1; i>=0; i--)
{
 .. calc buffer1
}
for(int i=limit-1; i>=0; i--)
{
 ... use buffer1 to calc buffer2
}

(Actually, for one indic I wrote, I look *THREE* times!)

 

Hi brewmanz thanks for replying I'm glad I'm not the only one to have problems accessing the buffer values on the fly lol, so when you use multiple loops what does this do to indicatorcounted() on the second loop? Does it not return the total number of bars after the indicator has already been drawn once thereby causeing the second loop to only calculate the buffer at the current bar ?

EDIT: ok forget that last question I see now, the second loop is still using the same original values from indicatorcounted()

 

I've been doing some testing I seem to be able to get the value from the previous index in the same loop when I fill the buffer according to the mql4 book using a while loop instead of a for loop going from oldest bar to newest I dont really see why this should make any difference though. Does anyone know of a reason why no one seems to do that the way the mql4 book describes, with a while loop ?