Buffer not Working

jjimmy
19

Hi

 I am creating a buffer of 20 elements to store profit for last 20 ticks .. want to calculate a weighted average from this buffer.

 However my buffer variable ProfitBuffer[i] which is declared a global seems not to hold any value. Any ideas why ?

 Cheers

// Gobal declaration

 double ProfitBuffer [20];
// My Function which maintains LIFO buffer 
double ProfitTracker()

{

double averageProfit; int i;

OrderSelect(0,SELECT_BY_POS);

for( i=20;i>1;i--)

{ProfitBuffer[i-1]=ProfitBuffer[i];}

ProfitBuffer[20]=OrderProfit();

averageProfit=0;

for( i=1;i<=20;i++)

{averageProfit = 0.5*ProfitBuffer[i]+0.5*averageProfit;}

return(averageProfit);

} 
Simon Gniadkowski
17410
JimRobo:

Hi

 I am creating a buffer of 20 elements to store profit for last 20 ticks .. want to calculate a weighted average from this buffer.

 However my buffer variable ProfitBuffer[i] which is declared a global seems not to hold any value. Any ideas why ?

 Cheers

This is an EA or Script so your buffer is not a buffer but an array,  buffers are special arrays used in Indicators,  is best to use the correct terminology so everyone knows what is being talked about 

Did your OrderSelect() work ?  why aren't you checking it's return value to determine if it worked or failed ?  if it failed your OrderProfit() will be garbage . . . 

This will help:   What are Function return values ? How do I use them ?

Your array runs from index 0 to index 19  not from 1 to 20,  you only select one order so you can't get an average from 20 entries . . . 

jjimmy
19
jjimmy  

RaptorUK:

 


This is an EA or Script so your buffer is not a buffer but an array,  buffers are special arrays used in Indicators,  is best to use the correct terminology so everyone knows what is being talked about 

Did your OrderSelect() work ?  why aren't you checking it's return value to determine if it worked or failed ?  if it failed your OrderProfit() will be garbage . . . 

This will help:   What are Function return values ? How do I use them ?

Your array runs from index 0 to index 19  not from 1 to 20,  you only select one order so you can't get an average from 20 entries . . . 

>>>> Your array runs from index 0 to index 19  not from 1 to 20,  you only select one order so you can't get an average from 20 entries . . . 

is the right answer :) i was indexing out of array bounds ... thanks for tip about buffer / array terminoloy :) .. 

I have a check for open orders before order select command  which i didn include in this code snippet to make it simple ..