exponential moving average

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

I have been trying to construct a function called Ema and have come to
as far as I know what to do with it. According to this page with the numbers
120 and 136 the ema should be 120.16

http://www.pandacash.com/technical-analysis/moving-average/exponential.htm

Here's my source code.

double Ema(double today, double yest, int per)
{
double exp = 2 / (per+1);
double ema = today * exp + (yest * (1 - exp));
return ema;
}

I add 1 to get the exp. Since exp is the same on each end of the
equations except for (1-exp) on the right term I just can't see the bug. I
have come as close as 120.

Bill
 
B

Ben Bacarisse

Bill Cunningham said:
I have been trying to construct a function called Ema and have come to
as far as I know what to do with it. According to this page with the numbers
120 and 136 the ema should be 120.16

http://www.pandacash.com/technical-analysis/moving-average/exponential.htm

Here's my source code.

double Ema(double today, double yest, int per)
{
double exp = 2 / (per+1);

2 is an int. per is an int. 1 is an int. The division will be done
as integer division and the result will be wrong for this purpose. If
per is 2 or higher, exp will be 0. Write 2.0 instead of 2.
 
K

Keith Thompson

Ben Bacarisse said:
2 is an int. per is an int. 1 is an int. The division will be done
as integer division and the result will be wrong for this purpose. If
per is 2 or higher, exp will be 0. Write 2.0 instead of 2.

Also, "exp" is the name of a standard function (declared in <math.h>).
Using it as a variable name is generally not a good idea. It
shouldn't cause any problems in this case, but it's a bad habit.
 
B

Bill Cunningham

Also, "exp" is the name of a standard function (declared in <math.h>).
Using it as a variable name is generally not a good idea. It
shouldn't cause any problems in this case, but it's a bad habit.

Ok no I don't want bad habits right now.

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,786
Messages
2,569,626
Members
45,323
Latest member
XOBJamel3

Latest Threads

Top