鼎元C++期货量化/程序化教程如何使用容器(vector)对bar数据进行高级处理【vector容器使用范例:正迭代计算xaverage/ema值】
  
- UID
- 2
- 积分
- 2933694
- 威望
- 1416883 布
- 龙e币
- 1516811 刀
- 在线时间
- 13696 小时
- 注册时间
- 2009-12-3
- 最后登录
- 2025-4-10

|
鼎元C++期货量化/程序化教程如何使用容器(vector)对bar数据进行高级处理【vector容器使用范例:正迭代计算xaverage/ema值】
我将在这个主题中通过两个方式来计算xaverage/ema的值。分别是公式调用计算方式和主程序计算方式,计算出来的都是数组的形式,不过要到具体的值也很方便。现在 开始。
1、主程序计算xaverage/ema方法
头文件声明变量:- vector<double>pc;//进行数组设计时统一的数组变量,方便以后使用中统一口径;
- vector<double> re;
- double d=0;
复制代码 主程序计算xaverage/ema公式算法:- //将收盘价装入容器变量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计算xaverage/ema值
- for (int i = 0; i < pc.size(); i++)
- {
- if (i < length) //如果bar的数目不够基本的周期,则返回加总除以bar数目+1代替
- {
- d = (d + pc[i])/(i+1);
- re.push_back(d);
- }
- else //如果bar数目大于或等于基本周期length个数据时
- {
- d = (2 * pc[i] + (length-1) * d)/(length+1); //[ema计算方法:ema = a*close + (1-a)*ema[1],a一般为2/(n+1)]
- re.push_back(d);
- }
- }
- InsertLog("正迭代之加权均线值主程序算法: " + to_string(re[re.size() - 1])); //将新变量最新值输出到日志
复制代码 解析:
1、在头文件声明变量
2、在主程序调用数据。先将收盘价装入PC容器。然后开始算法计算
1、若是bar数据少于设置的周期length数目时,计算方法是所有收盘价加总,然后除以bar数目+1;
2、若是bar数据多于设置的周期length数目时,计算方法是新ema值 = 2*收盘价 + (length-1)*ema前值 这两项的和再除以length+1 后就获得ema最新bar的xaverage/ema值。 |
论坛官方微信、群(期货热点、量化探讨、开户与绑定实盘)
|
|
|
|
|
|
  
- UID
- 2
- 积分
- 2933694
- 威望
- 1416883 布
- 龙e币
- 1516811 刀
- 在线时间
- 13696 小时
- 注册时间
- 2009-12-3
- 最后登录
- 2025-4-10

|
2、调用函数计算xaverage/ema方法
1、将收盘价存储到PC容器变量中- //调用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容器中,按正序排序
- }
复制代码 2、建立ema调用函数算法
头文件声明变量:- vector<double>emaarray(vector<double> pc, int num); //ema计算公式(返回数组);
复制代码 公式区调用函数算法:- //ema计算公式开始(返回数组)
- vector<double> test::emaarray(vector<double> pc, int num)
- {
- vector<double> r;
- double d = 0;
- for (int i = 0; i < pc.size(); i++)
- {
- if (i < num)
- {
- d = (d + pc[i])/(i+1);
- r.push_back(d);
- }
- else
- {
- d = (2*pc[i]+(num-1)*d)/(num+1);
- r.push_back(d);
- }
- }
- return r;
- }
复制代码 3、调用办法:- vector
- <double>emavalue;
- emavalue = emaarray(pc, length);
-
- InsertLog("正迭代之ema均线值引用算法: " + to_string(emavalue[emavalue.size() - 1])); //将新变量最新值输出到日志
复制代码 |
|
|
|
|
|
|
  
- UID
- 2
- 积分
- 2933694
- 威望
- 1416883 布
- 龙e币
- 1516811 刀
- 在线时间
- 13696 小时
- 注册时间
- 2009-12-3
- 最后登录
- 2025-4-10

|
3、函数调用数值法。非2中的数组式函数法。- //ema计算公式开始
- double test::ema(string period, string inst, int num)
- {
- double d = 0;
- int r = 0;
- map<string, TKVALUE >::iterator it;
- for (it = mapK[period][inst].begin(); it != mapK[period][inst].end(); ++it)
- {
- if (r < num)//当数据不够num个bar时用总和/(bar个数加1)
- {
- d=(d+it->second.dClose)/(r+1);
- r++;
- }
- else //当bar的总个数大于num个周期时,ema = a*close + (1-a)*ema[1],a一般为2/(num+1)
- {
- d=(2*it->second.dClose+(num-1)*d)/(num+1);
- }
- }
- return d;
- }
复制代码 调用办法:- InsertLog("逆迭代之ema均线值引用算法: " + to_string(ema(sPeriod, sInst, length))); //将新变量最新值输出到日志
复制代码 |
|
|
|
|
|
|