鼎元C++期货量化/程序化教程【平滑异同移动平均线(Moving Average Convergence Divergence,MACD)计算方法及调用方法】
鼎元C++期货量化/程序化教程【平滑异同移动平均线(Moving Average Convergence Divergence,MACD)计算方法及调用方法】1、头文件变量声明:[code]
vector<double>pc;//priceclose,pc简称
vector<double>emashort,emalong,diff,dea;
double macd;//获取macd值,柱状图线数值
[/code]2、源文件加入数组vector返回 ema均线数据程式码模块:[code]
//ema计算公式开始(返回数组)
/*
从map<string,mapK>制式数据中分离需要处理的数据,然后通过这此的ema方式取得这些数据的ema平均,返回值仍然是数组vector
*/
vector<double> test::ema(vector<double> pc, int num)
{
vector<double> ema;
double emavector = 0;
int i = 0;
for (int it = 0; it < pc.size(); ++it)
{
if (i < num)
{
emavector = (emavector + pc[it]) / (i + 1);
i++;
ema.push_back(emavector);
}
else
{
emavector = (2 * pc[it] + (num - 1) * emavector) / (num + 1);
ema.push_back(emavector);
}
}
return ema;
}
[/code]3、源文件加入MACD程式码模块:[code]
RsqBar(sPeriod, sInst);
map<string,TKVALUE>::iterator it;
for (it = mapK[sPeriod][sInst].begin(); it != mapK[sPeriod][sInst].end(); it++)
{
pc.push_back(it->second.dClose); //将收盘价放入pc容器vector
}
emashort = ema(pc,12);
emalong = ema(pc,26);
for (int i = 0; i < emalong.size(); i++)
{
diff.push_back(emashort[i] - emalong[i]); //DIFF - DEA
}
dea = ema(diff,9); //dea = xaverage(diff,9)
macd = (diff[diff.size()-1] - dea[dea.size()-1])*2; //获取MACD柱数值
[/code]3、调用方法:[code]
InsertLog("diff值为: " + to_string(diff[diff.size() - 1]));
InsertLog("dea均线值为: " + to_string(dea[dea.size() - 1]));
InsertLog("MACD值为: " + to_string(macd));
[/code]类似这样。
页:
[1]