C++程序化/量化学习视频教程系列 第057节:鼎元C++量化之如何跨品种/期调用多个数据【应用场景:主合约与任一同月份合约做价差】
  
- UID
- 2
- 积分
- 2945027
- 威望
- 1422550 布
- 龙e币
- 1522477 刀
- 在线时间
- 13794 小时
- 注册时间
- 2009-12-3
- 最后登录
- 2025-4-24

|
C++程序化/量化学习视频教程系列 第057节:鼎元C++量化之如何跨品种/期调用多个数据【应用场景:主合约与任一同月份合约做价差】
论坛官方微信、群(期货热点、量化探讨、开户与绑定实盘)
|
|
|
|
|
|
  
- UID
- 2
- 积分
- 2945027
- 威望
- 1422550 布
- 龙e币
- 1522477 刀
- 在线时间
- 13794 小时
- 注册时间
- 2009-12-3
- 最后登录
- 2025-4-24

|
程式码部分:
1、头文件声明变量:- vector<double>pc;//进行数组设计时统一的数组变量,方便以后使用中统一口径;
- string contract;//价差合约
复制代码 2、源文件策略参数设计与调用
OnRun() 里面:
1、- t.Name = "指标周期"; t.Value = "20"; t.Explain = "指标周期参数,默认20"; tend(t);
- t.Name = "价差比较期货合约"; t.Value = "rb2505"; t.Explain = "前面品种后面月份"; tend(t);
复制代码 2、- length = atoi(parm["指标周期"].Value.c_str());
- contract = parm["价差比较期货合约"].Value;
复制代码 3、- RsqBar(sPeriod, sInst); //默认品种
- map<string, TKVALUE>::reverse_iterator it;//逆向迭代
- for (it = mapK[sPeriod][sInst].rbegin(); it != mapK[sPeriod][sInst].rend(); it++) //逆向遍历所有K线
- {
- pc.push_back(it->second.dClose);//将主合约收盘价赋值到pc容器(逆序)
- }
- vector<double>pc2;//新建被减合约收盘价容器
- RsqInstrument(contract);//调用被减品种
- SubscribeMarketData(contract); //调用被减品种数据
- RsqBar(sPeriod, contract); //被减品种(品种不同,周期相同)
- map<string, TKVALUE>::reverse_iterator it2;//逆向迭代
- for (it2 = mapK[sPeriod][contract].rbegin(); it2 != mapK[sPeriod][contract].rend(); it2++) //逆向遍历所有K线
- {
- pc2.push_back(it2->second.dClose);//将被减合约收盘价赋值到pc2容器(也是逆序)
- }
- vector<double>spread;
- //做两个容器数据价差
- for (size_t i = 0; i < length; i++)// length个数据的价差,从最新价向左length个数据的价差
- {
- spread.push_back(pc[i] - pc2[i]);//将价差存储入容器spread
- }
- //在日志面板输出length个上面的两个合约的收盘以及他们的价差
- for (size_t j = 0; j < length; j++)
- {
- InsertLog("第一个品种合约 " + sInst + " 第 "+ to_string(j)+ " 个bar收盘价为:" + to_string(pc[j]));
- InsertLog("第二个品种合约 " + contract + " 第 " + to_string(j) + " 个bar收盘价为:" + to_string(pc2[j]));
- InsertLog("第一合约减第二合约 第 " + to_string(j) + " 个bar的价差为:" + to_string(spread[j]));
- }
复制代码 |
|
|
|
|
|
|
  
- UID
- 2
- 积分
- 2945027
- 威望
- 1422550 布
- 龙e币
- 1522477 刀
- 在线时间
- 13794 小时
- 注册时间
- 2009-12-3
- 最后登录
- 2025-4-24

|
运行效果:
 |
|
|
|
|
|
|
  
- UID
- 2
- 积分
- 2945027
- 威望
- 1422550 布
- 龙e币
- 1522477 刀
- 在线时间
- 13794 小时
- 注册时间
- 2009-12-3
- 最后登录
- 2025-4-24

|
视频中核心程式码:- //点击运行按钮动作模块,用来加载交易界面的配置与策略参数配置(开始)
- void test::OnRun()
- {
- OnState("run");
- if (state != "run")return;
- mapPosDeta.clear();
- mapPos.clear();
- mapOrder.clear();
- mapTrade.clear();
- sound("sound\\run.wav");
- RsqRspQryOrder();
- RsqPosition();
- RsqRspQryTrade();
- //RsqPositionDetail();
- RsqAccount();
- RsqInstrument(sInst);//调用品种代码
- SubscribeMarketData(sInst); //调用品种数据
- //交易系统参数变量传递开始*******************************************************************************************************
- num = 0;
- length = atoi(parm["指标周期"].Value.c_str());
- contract = parm["价差比较期货合约"].Value;
- //hd = atoi(parm["滑点值"].Value.c_str());
- //yxpc = atoi(parm["优先平仓"].Value.c_str());
- //jg = atoi(parm["撤单时间"].Value.c_str());
- //交易系统参数变量传递结束*******************************************************************************************************
- //调用主合约bar数据
- RsqBar(sPeriod, sInst);
- map<string, TKVALUE>::reverse_iterator it;//建立逆迭代
- for (it = mapK[sPeriod][sInst].rbegin(); it != mapK[sPeriod][sInst].rend(); it++)
- {
- pc.push_back(it->second.dClose); //将主价差合约收盘价存入pc容器
- }
- vector<double>pc2;//建立被减合约收盘价容器
- RsqInstrument(contract);
- SubscribeMarketData(contract);
- RsqBar(sPeriod, contract);
- map<string, TKVALUE>::reverse_iterator it2;
- for (it2 = mapK[sPeriod][contract].rbegin(); it2!= mapK[sPeriod][contract].rend(); it2++)
- {
- pc2.push_back(it2->second.dClose);//将辅价差合约收盘价存入pc2容器
- }
- vector<double>spread;//做两个合约价差容器
- for (size_t i = 0; i < length; i++)
- {
- spread.push_back(pc[i] - pc2[i]);//做两个合约价差
- }
- //在日志面板输出两个合约length个bar的收盘价及价差
- for (size_t j = 0; j < length; j++)
- {
- InsertLog("第一个品种合约 " + sInst + " 第 " + to_string(j) + " 个bar收盘价为:" + to_string(pc[j]));//输出主合约收盘价
- InsertLog("第二个品种合约 " + contract + " 第 " + to_string(j) + " 个bar收盘价为:" + to_string(pc2[j]));
- InsertLog("第一合约减第二合约 第 " + to_string(j) + " 个bar的价差为:" + to_string(spread[j]));
- }
复制代码 |
|
|
|
|
|
|