[newbie] where I'm wrong? :(

 

Hi,

I want to calculate de momentum of a EMA, but the result is only a constant 0...

void dT1(double ro, int n, int ExtCountedBars)
{
int pos=Bars-2;
double MMES[];

if(ExtCountedBars>2) pos=Bars-ExtCountedBars-1;
while(pos>=0)
{
if(pos==Bars-2) MMES[pos+1]=Close[pos+1];
MMES[pos]=Close[pos]*ro+MMES[pos+1]*(1-ro);
pos--;
}


int limit=Bars-ExtCountedBars;
//---- calcolo della velocità della MMES
for(int i=0; i<limit; i++)
ExtMapBufferVelocita[i]= MMES[i]-MMES[i+n];
}

 

Hello Aesis,


1. double MMES[]; //you must reserve storage for array eg, MMES[10]


Of course, in your code, must decide range of array before you give it a size and that appears to be a runtime/dynamic issue, yes?

ie, [pos+1] and [limit+n], look like the two possible extremes/maximums **

Here, pos is locally calculated and n is actual caller input. Basically, you do not 'know' what size array should be until function entered. Also... afaik, mql4 does not actually complain about nonsensical array element accesses and instead - just resolves the access to a value of zero which accounts in some part at least for your zero result. (you could of course check this out easy enough... but better to have correct code in first place, yes?)

**Please do remember: your calculations must resolve to the maximum size needed on this call hence MathMax() could prove of use: Mathematical and trigonometric functions are included in MQL4....


Suggest you look at HowTo dynamically reserve array storage and

search on this string: The number of bars to be colored is set

at this link: Discussion on ArrayResize() and example


Have not looked further - may be other issues, but for sure - you need sort out array size on every function entry...

lol - am not really into mathematics so that's your department ;)


hth

Reason: