Buffer problem

 

Hi all,

I asked this question in another forum but it was moved, I think it was meant to be moved to this one but instead it got buried in another thread.

I know a little about MQL programming but not enough to solve this problem!

I'm trying to create an indicator with two buffers.

The values held in the first buffer is simply the O+H+L+C of the current bar divided by 4, no problems there.

The second buffer is where I'm having the problem. Basically the second buffer holds the value of buffer1 on the previous bar, added to the value of buffer2 on the previous bar, divided by 2.

That's not much of a problem either, except when you count back to the very first bar in the chart there is no previous bar to take data from. For that bar buffer2 needs a starting value, which is the open of that bar.

Basically I don't know how to set that up and would appreciate any help. Thanks

 
if ( bar == Bars - 1 ) {

buffer2[ bar ] = Open[ bar ];

} else {

buffer2[ bar ] = ....

}

 

Thanks Ralph I appreciate the help.

Just wondering if you could help me with where to place that bit of code. The basics I have so far are:

int start()

{

int limit;

int counted_bars=IndicatorCounted();

//---- check for possible errors

if(counted_bars<0) return(-1);

//---- last counted bar will be recounted

if(counted_bars>0) counted_bars--;

limit=Bars-counted_bars;

//---- current bar OHLC counted in first buffer

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

ind_buffer1=((Open+High+Low+Close)/4);

//---- buffer1[1]+buffer2[1]/2 counted in second bar

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

ind_buffer2=((ind_buffer1+ind_buffer2)/2);

//---- done

return(0);

}

 

Firstly you need to range from higher index to lower, which corresponds to left to right. Otherwise the code will try to compute the right-hand (lower) index before the left-hand (higher) index, which it depends upon.

Thereafter you can wedge in my previous snippet into the last "for" clause.

Also, I think you should get rid of that counted_bars fiddling (which serves no purpose apart from adding confusion)

It'd be better to make the looping as:

for ( int i = Bars - IndicatorCounted() - 1; i >= 0; i-- ) {

... deal with indication for bar i

}
 

Hi Ralph,

Thanks again for your help

I have just one more newbie question if you don't mind

This is the code I have now:

int start()

{

//---- current bar OHLC counted in first buffer

for(int i=Bars-IndicatorCounted()-1; i>=0; i--)

ind_buffer1=((Open+High+Low+Close)/4);

//---- buffer1[1]+buffer2[1]/2 counted in second bar

for(i=Bars-IndicatorCounted()-1; i>=0; i--)

{

if(bar==Bars-1)

{

ind_buffer2=Open;

}

else

{

ind_buffer2=((ind_buffer1+ind_buffer2)/2);

}

Comment("buffer1=",ind_buffer1,"\nbuffer2=",ind_buffer2,"");

}

//---- done

return(0);

}

When compiling, there is a problem because the variable "bar" is not defined. How should it be defined so that the program knows to look at the current bar number to check if it is the first bar?

Am I right in thinking that in the first code snippet you gave me, should be replaced with ?

 

yes, that's right.

 

Thanks Ralph!!!

Reason: