鼎元C++期货量化/程序化教程【加权移动平均线(Weighted Moving Average,WMA)的计算方法及调用方法】
一、头文件声明变量[code]double waverage(string period, string inst, int num);//计算Weighted Moving Average,加权移动平均线[/code]二、源文件计算公式:[code]//计算Weighted Moving Average,加权移动平均线开始double test::waverage(string period, string inst, int num)
{
double sumwma = 0;
int i = 0;
if (mapK[period][inst].size() < num) return 0; //bar数量不足时返回0
map<string, TKVALUE>::reverse_iterator it; //逆迭代器,从最新数据开始
for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); ++it)
{
sumwma += (num - i) * it->second.dClose;
++i;
if (i > num - 1) break;//循环num轮后退出
}
double d = (num + 1) * num * 0.5;
return sumwma / d;
}[/code]三、调用方法:[code]wma = waverage(sPeriod, sInst, length);[/code]
页:
[1]