Buff MA - help to code(translate) it

 

Hi,

as I'm not well-versed in MT4 coding I need help with a simple formula called Buff MA; I've the corresponding formula in Metastock language as the following one:

BUFF:=Sum(V*C, 32) / (Cum(V) - Ref( Cum(V), -32));

BUFF

If someone could compile it for MT4 I'd be glad.

Buff MA helps in determining the most probable side to trade... when price is above Buff and aligned with it* only long positions are allowed... viceversa for short ones.

*(that's if Buff and price both are upward it's OK to be long... viceversa for shorting)

Thanks a lot

ontherun

ps: I've been looking for the Sum() and Cum() corresponding functions in the MetaEditor but I've not found anything related to those functions...

 

Someone familiar with Metastock may correct me, but by the looks of it, you have a "BUFF" function that looks at the 32 most recent bars, adds up the volume times the close price for them, and divides that by the total volume for those bars. It could look like the following in MT4:

double buff()

{

double sum = 0;

double v = 0;

for ( int i = 0; i < 32; i++ ) {

sum += Volume[ i ] * Close[ i ];

v += Volume[ i ];

}

return( sum / v );

}
 
 

no, right; it will need to compute the value relative to prior bars, and assign BUFF with the values.... like

int start()

{

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

indicate( bar );

}

}

void indicate(int bar)

{

double sum=0;

double v=0;

for ( int i=0; i<32; i++ ) {

sum += Volume[ bar + i ] * Close[ bar + i ];

v += Volume[ bar + i ];

}

BUFF[ bar ] = sum/v;

}

Just replace your start() function with the above two functions, and it should do the trick.

 

Ralph,

as I ve not yet got it working, can you attach the formula as .mq4 file

Thanks

ontherun

 

You can try :

int start()

{

for ( int bar = Bars-32-IndicatorCounted(); bar >= 0; bar-- ) {

indicate( bar );

}

}
 

Michel,

thanks for answering.

The Buff formula with your modification plots correct as for the calculated values (I compare them to those plotted by the Buff in Metastock) but it doesn't update... that's it stops plotting just at the bar developing the moment I attach it to the chart.

ontherun

 

You are right, so please try this code which should be the right way to code this kind of indic (I am not able to check it myself now):

int start()

{

int i,counted_bars=IndicatorCounted();

if(Bars<=32) return(0);

if(counted_bars<1) for(i=1;i<=32;i++) BUFF=EMPTY_VALUE;

i=Bars-33;

if(counted_bars>=32) i=Bars-counted_bars-1;

while(i>=0)

{

indicate(i);

i--;

}

return(0);

}
 

Great work!

Thanks, Michel.

Actually I didn't think coding this was so demanding comparing to Metastock language.

Thanks again and thanks to Ralph too.

Have a great weekend.

ontherun

Reason: