Beginner C++: Misbehaving Vector

Joined
Mar 26, 2010
Messages
2
Reaction score
0
Hello,

I'm new to C++ and I'm trying to create an exponential moving average using vectors. When I push values onto the vector I'm getting arbitrary values when I read the resulting vector elements. Here's the code snippet:

void EMA(vector<double> prices) {
vector<double> expMA;
int i = 0;
double fma = 0.0;
double sum = 0.0;
double ema = 0.0;
double multiplier = 0.0;
const int period = 10;

multiplier = 2.0 / (period + 1);

// calculate first ema value
for (i = 0; i <= period; i++) {
sum = sum + prices;
}
fma = sum / (period);
expMA.push_back(fma);

for (i = 0; i < prices.size() - period; i++) {
ema = multiplier*(prices[i + period] - expMA) - expMA;
expMA.push_back(ema);
}
//print out of EMA values
for (unsigned int i = 0; i < expMA.size(); i++) {
cout << expMA << endl;
}
}

What would cause the vector to misbehave?

Thanks for your help,



TJ
 
Joined
Mar 26, 2010
Messages
2
Reaction score
0
Hello,

I hope I didn't waste anyones time. I realize now that my code was doing what it was supposed to - it was my formula that was in error.

This one is solved!


TJ
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top