# Force Index EMA calculation for stock indicator

I am trying to smooth a 13 period EMA Elder Force Index in c++, and nobody really describes this as anything more than :

Force Index(1) = {Close (current period)  -  Close (prior period)} x Volume.
Force Index(13) = 13-period EMA of Force Index(1).


I must be a bit thick, but when you use a moving average with a period, you get an average of that period, and then use that as a reference for further day MA's. I have calculated SMA & Exp MA for exstensive data sets before, but how are you supposed to calculate smoothed Force Index for an exstensive period of time ?

void indicators::EMA(input* Close1, size_t Start, size_t Period) {

vector<double> CloseRe (Close1->Close.rbegin(), Close1->Close.rend());
vector<double> Xtemp;
for(size_t i = Start; i < Period; i++) {
Xtemp.push_back(CloseRe[i]);
}
// cout << Xtemp.size() << endl;
double InitXavg = (std::accumulate(Xtemp.begin(), Xtemp.end(), 0.0) / Xtemp.size());
ExpMA.push_back(InitXavg);

for(size_t j = Period; j < CloseRe.size(); j++) {
tmpEMA = ((CloseRe[j] - ExpMA[j-Period]) * (2.0 / (Period + 1.0))) + ExpMA[j-Period];
// cout << j << " is " << CloseRe[j] << endl;
ExpMA.push_back(tmpEMA);
tmpEMA = 0.0;
}

return;
}


The above calculates Exponential Moving Average , and the below was supposed to calculate the force index smoothed for extensive data set, with 13-day period. But I can't say I really understand how this is supposed to work, as the standard EMA uses only close price & previous day average & multiplier. How are you supposed to incorporate volume there beyond the original 13 day period ? :

void indicators::ForceIndexEMA(input* Close1, input* Volume1, size_t Start, size_t Period) {

vector<double> CloseRe (Close1->Close.rbegin(), Close1->Close.rend());
vector<double> VolumeRe(Volume1->Volume.rbegin(), Volume1->Volume.rend());
vector<double> FXtemp;
for(size_t i = Start; i < Period; i++) {
FXtemp.push_back((CloseRe[i+1] - CloseRe[i]) * VolumeRe[i]);
}

ForceXMA.push_back(std::accumulate(FXtemp.begin(), FXtemp.end(), 0.0) / FXtemp.size());
for(size_t j = Period; j < CloseRe.size(); j++) {
ForceXMA.push_back(((CloseRe[j] - ForceXMA[j-Period]) * (2.0 / (Period + 1.0))) + ForceXMA[j-Period]);
}
return;
}


You could just calculate day-to day force index with a loop, but I keep seeing everywhere how the smoothed EMA average is supposed to give more reliable signals in certain scenarios. So my question would be what I am doing wrong here.... ? I understand this is not a forum for programming, but I am not dependent on having this explained as such. I just haven't seen anyone explain it well at all ? What about the current volume ? An EMA does not in general work with volume, so using the standard way of calculating the EMA would just be nearing the day's closing prices as you looped through the data set...

EDIT: I have managed to do what I see as an "EMA" of the 1-period force index values here, but could anyone correct me if it is wrong ? :

void indicators::ForceIndex(input* Close1, input* Volume1, size_t Start, size_t Period) {

vector<double> CloseRe (Close1->Close.rbegin(), Close1->Close.rend());
vector<double> VolumeRe(Volume1->Volume.rbegin(), Volume1->Volume.rend());
vector<double> FXtemp;
for(size_t i = Start; i < Period; i++) {
FXtemp.push_back((CloseRe[i+1] - CloseRe[i]) * VolumeRe[i+1]);
}

ForceXMA.push_back(std::accumulate(FXtemp.begin(), FXtemp.end(), 0.0) / FXtemp.size());
for(size_t j = Period; j < CloseRe.size(); j++) {
double daily = ((CloseRe[j+1] - CloseRe[j]) * VolumeRe[j+1]);
ForceXMA.push_back(((daily - ForceXMA[j-Period]) * (2.0 / (Period + 1.0))) + ForceXMA[j-Period]);
daily = 0.0;
}
return;
}


As far as I can see this should weight the volume also, but as for the logic of the "average" of the calculation I am still a bit uncertain...