鼎元C++期货量化/程序化教程如何使用容器(vector)对bar数据进行高级处理【vector容器使用范例:正迭代计算MACD值】
1、test文件声明变量:[code]vector<double>pc;//进行数组设计时统一的数组变量,方便以后使用中统一口径;
[/code]2、源文件主计算程式 码:[code]
//调用bar数据,要包括品种合约,运行周期
RsqBar(sPeriod, sInst);
//装收盘价装后容器变量PC中
map<string, TKVALUE>::iterator it;//迭代
for (it = mapK[sPeriod][sInst].begin(); it != mapK[sPeriod][sInst].end(); it++) //遍历所有K线
{
pc.push_back(it->second.dClose);//将收盘价存储到pc容器中,按正序排序
}
vector<double> ema12 = emaarray(pc, 12);
vector<double> ema26 = emaarray(pc, 26);
vector<double> diff;
for (size_t i = 0; i < ema12.size(); i++)
{
diff.push_back(ema12[i] - ema26[i]);
}
vector<double> dea;
dea = emaarray(diff, 9);
vector <double>macd;
for (size_t i = 0; i < dea.size(); i++)
{
macd.push_back((diff[i] - dea[i])*2);
}
InsertLog("diff : " + to_string(diff[diff.size()-1])); //将新变量最新值输出到日志
InsertLog("dea : " + to_string(dea[dea.size()-1])); //将新变量最新值输出到日志
InsertLog("macd bar: " + to_string(macd[macd.size() - 1]));
[/code]
页:
[1]