- UID
- 2
- 积分
- 2892467
- 威望
- 1396265 布
- 龙e币
- 1496202 刀
- 在线时间
- 13313 小时
- 注册时间
- 2009-12-3
- 最后登录
- 2024-12-25
|
鼎元C++程序化交易系统教程【套利模板核心策略程式码部分】
核心策略程式码部分:- //核心策略设计开始............................................................................................................................
- void test::OnMarketData(CThostFtdcDepthMarketDataField* t)
- {
- if (state != "run")return;
- mapMd[t->InstrumentID] = *t;
- string sss = t->UpdateTime;
- if (sss == "20:59:00" || sss == "08:59:00")return;
- if (t->InstrumentID != sInst)return;
- //if (t->InstrumentID != dyt && t->InstrumentID != det)return;
- if (mapMd[dyt].BidPrice1 == 0 || mapMd[dyt].AskPrice1 == 0 || mapMd[det].BidPrice1 == 0 || mapMd[det].AskPrice1 == 0)return;
- if (yfd)return;
- sss = t->UpdateTime;
- if (sss == "20:59:00" || sss == "08:59:00")return;
- if (bsfx == "0")
- {
- jc = mapMd[dyt].AskPrice1 - mapMd[det].BidPrice1;
- }
- else if (bsfx == "1")
- {
- jc = mapMd[dyt].BidPrice1 - mapMd[det].AskPrice1;
- }
- if ((tjjcfx == "1" && jc >= tjjc && mapMd[dyt].AskVolume1 > gdl && mapMd[det].BidVolume1 > gdl) || (tjjcfx == "-1" && jc <= tjjc && mapMd[dyt].BidVolume1 > gdl && mapMd[det].AskVolume1 > gdl))
- {
- if (bsfx == "0")
- {
- if (ocfx == "0")
- {
- dytcj = false; detcj = false;
- map<string, double>::iterator it;
- for (it = mapSub.begin(); it != mapSub.end(); it++)
- {
- int ss = (int)floor(it->second * sl);
- OrderInsert(it->first, dyt, '0', '0', ss, mapMd[dyt].AskPrice1 + hd * mapInstrument[det].PriceTick, "", "");
- OrderInsert(it->first, det, '1', '0', ss, mapMd[det].BidPrice1 - hd * mapInstrument[det].PriceTick, "", "");
- yfd = true;
- string s = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + it->first + " 达到套利价差交易条件买入开仓,第一腿价格 " + to_string(mapMd[dyt].AskPrice1 + hd * mapInstrument[det].PriceTick) + " 第二蹆价格 " + to_string(mapMd[det].BidPrice1 - hd * mapInstrument[det].PriceTick) + " 对手价差 " + to_string(jc);
- maps[num] = s;
- num++;
- InsertLog(it->first + " 达到套利价差交易条件买入开仓,第一腿价格 " + to_string(mapMd[dyt].AskPrice1 + hd * mapInstrument[det].PriceTick) + " 第二蹆价格 " + to_string(mapMd[det].BidPrice1 - hd * mapInstrument[det].PriceTick) + " 对手价差 " + to_string(jc));
- }
- }
- else if (ocfx == "1")
- {
- dytcj = false; detcj = false;
- map<string, double>::iterator it;
- for (it = mapSub.begin(); it != mapSub.end(); it++)
- {
- int ss = (int)floor(it->second * sl);
- OrderInsert(it->first, dyt, '0', '1', ss, mapMd[dyt].AskPrice1 + hd * mapInstrument[det].PriceTick, "", "");
- OrderInsert(it->first, det, '1', '1', ss, mapMd[det].BidPrice1 - hd * mapInstrument[det].PriceTick, "", "");
- yfd = true;
- string s = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + it->first + " 达到套利价差交易条件买入平仓,第一腿价格 " + to_string(mapMd[dyt].AskPrice1 + hd * mapInstrument[dyt].PriceTick) + " 第二蹆价格 " + to_string(mapMd[det].BidPrice1 - hd * mapInstrument[det].PriceTick) + " 对手价差 " + to_string(jc);
- maps[num] = s;
- num++;
- InsertLog(it->first + " 达到套利价差交易条件买入平仓,第一腿价格 " + to_string(mapMd[dyt].AskPrice1 + hd * mapInstrument[dyt].PriceTick) + " 第二蹆价格 " + to_string(mapMd[det].BidPrice1 - hd * mapInstrument[det].PriceTick) + " 对手价差 " + to_string(jc));
- }
- }
- else if (ocfx == "3")
- {
- dytcj = false; detcj = false;
- map<string, double>::iterator it;
- for (it = mapSub.begin(); it != mapSub.end(); it++)
- {
- int ss = (int)floor(it->second * sl);
- OrderInsert(it->first, dyt, '0', '3', ss, mapMd[dyt].AskPrice1 + hd * mapInstrument[dyt].PriceTick, "", "");
- OrderInsert(it->first, det, '1', '3', ss, mapMd[det].BidPrice1 - hd * mapInstrument[det].PriceTick, "", "");
- yfd = true;
- string s = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + it->first + " 达到套利价差交易条件买入平今,第一腿价格 " + to_string(mapMd[dyt].AskPrice1 + hd * mapInstrument[dyt].PriceTick) + " 第二蹆价格 " + to_string(mapMd[det].BidPrice1 - hd * mapInstrument[det].PriceTick) + " 对手价差 " + to_string(jc);
- maps[num] = s;
- num++;
- InsertLog(it->first + " 达到套利价差交易条件买入平今,第一腿价格 " + to_string(mapMd[dyt].AskPrice1 + hd * mapInstrument[dyt].PriceTick) + " 第二蹆价格 " + to_string(mapMd[det].BidPrice1 - hd * mapInstrument[det].PriceTick) + " 对手价差 " + to_string(jc));
- }
- }
- else if (ocfx == "4")
- {
- dytcj = false; detcj = false;
- map<string, double>::iterator it;
- for (it = mapSub.begin(); it != mapSub.end(); it++)
- {
- int ss = (int)floor(it->second * sl);
- OrderInsert(it->first, dyt, '0', '4', ss, mapMd[dyt].AskPrice1 + hd * mapInstrument[dyt].PriceTick, "", "");
- OrderInsert(it->first, det, '1', '4', ss, mapMd[det].BidPrice1 - hd * mapInstrument[det].PriceTick, "", "");
- yfd = true;
- string s = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + it->first + " 达到套利价差交易条件买入平昨,第一腿价格 " + to_string(mapMd[dyt].AskPrice1 + hd * mapInstrument[dyt].PriceTick) + " 第二蹆价格 " + to_string(mapMd[det].BidPrice1 - hd * mapInstrument[det].PriceTick) + " 对手价差 " + to_string(jc);
- maps[num] = s;
- num++;
- InsertLog(it->first + " 达到套利价差交易条件买入平昨,第一腿价格 " + to_string(mapMd[dyt].AskPrice1 + hd * mapInstrument[dyt].PriceTick) + " 第二蹆价格 " + to_string(mapMd[det].BidPrice1 - hd * mapInstrument[det].PriceTick) + " 对手价差 " + to_string(jc));
- }
- }
- }
- else if (bsfx == "1")
- {
- if (ocfx == "0")
- {
- dytcj = false; detcj = false;
- map<string, double>::iterator it;
- for (it = mapSub.begin(); it != mapSub.end(); it++)
- {
- int ss = (int)floor(it->second * sl);
- OrderInsert(it->first, dyt, '1', '0', ss, mapMd[dyt].BidPrice1 - hd * mapInstrument[dyt].PriceTick, "", "");
- OrderInsert(it->first, det, '0', '0', ss, mapMd[det].AskPrice1 + hd * mapInstrument[det].PriceTick, "", "");
- yfd = true;
- string s = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + it->first + " 达到套利价差交易条件卖出开仓,第一腿价格 " + to_string(mapMd[dyt].BidPrice1 - hd * mapInstrument[dyt].PriceTick) + " 第二蹆价格 " + to_string(mapMd[det].AskPrice1 + hd * mapInstrument[det].PriceTick) + " 对手价差 " + to_string(jc);
- maps[num] = s;
- num++;
- InsertLog(it->first + " 达到套利价差交易条件卖出开仓,第一腿价格 " + to_string(mapMd[dyt].BidPrice1 - hd * mapInstrument[dyt].PriceTick) + " 第二蹆价格 " + to_string(mapMd[det].AskPrice1 + hd * mapInstrument[det].PriceTick) + " 对手价差 " + to_string(jc));
- }
- }
- else if (ocfx == "1")
- {
- dytcj = false; detcj = false;
- map<string, double>::iterator it;
- for (it = mapSub.begin(); it != mapSub.end(); it++)
- {
- int ss = (int)floor(it->second * sl);
- OrderInsert(it->first, dyt, '1', '1', ss, mapMd[dyt].BidPrice1 - hd * mapInstrument[dyt].PriceTick, "", "");
- OrderInsert(it->first, det, '0', '1', ss, mapMd[det].AskPrice1 + hd * mapInstrument[det].PriceTick, "", "");
- yfd = true;
- string s = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + it->first + " 达到套利价差交易条件卖出平仓,第一腿价格 " + to_string(mapMd[dyt].BidPrice1 - hd * mapInstrument[dyt].PriceTick) + " 第二蹆价格 " + to_string(mapMd[det].AskPrice1 + hd * mapInstrument[det].PriceTick) + " 对手价差 " + to_string(jc);
- maps[num] = s;
- num++;
- InsertLog(it->first + " 达到套利价差交易条件卖出平仓,第一腿价格 " + to_string(mapMd[dyt].BidPrice1 - hd * mapInstrument[dyt].PriceTick) + " 第二蹆价格 " + to_string(mapMd[det].AskPrice1 + hd * mapInstrument[det].PriceTick) + " 对手价差 " + to_string(jc));
- }
- }
- else if (ocfx == "3")
- {
- dytcj = false; detcj = false;
- map<string, double>::iterator it;
- for (it = mapSub.begin(); it != mapSub.end(); it++)
- {
- int ss = (int)floor(it->second * sl);
- OrderInsert(it->first, dyt, '1', '3', ss, mapMd[dyt].BidPrice1 - hd * mapInstrument[dyt].PriceTick, "", "");
- OrderInsert(it->first, det, '0', '3', ss, mapMd[det].AskPrice1 + hd * mapInstrument[det].PriceTick, "", "");
- yfd = true;
- string s = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + it->first + " 达到套利价差交易条件,第一腿价格卖出平今 " + to_string(mapMd[dyt].BidPrice1 - hd * mapInstrument[dyt].PriceTick) + " 第二蹆价格 " + to_string(mapMd[det].AskPrice1 + hd * mapInstrument[det].PriceTick) + " 对手价差 " + to_string(jc);
- maps[num] = s;
- num++;
- InsertLog(it->first + " 达到套利价差交易条件,第一腿价格卖出平今 " + to_string(mapMd[dyt].BidPrice1 - hd * mapInstrument[dyt].PriceTick) + " 第二蹆价格 " + to_string(mapMd[det].AskPrice1 + hd * mapInstrument[det].PriceTick) + " 对手价差 " + to_string(jc));
- }
- }
- else if (ocfx == "4")
- {
- dytcj = false; detcj = false;
- map<string, double>::iterator it;
- for (it = mapSub.begin(); it != mapSub.end(); it++)
- {
- int ss = (int)floor(it->second * sl);
- OrderInsert(it->first, dyt, '1', '4', ss, mapMd[dyt].BidPrice1 - hd * mapInstrument[dyt].PriceTick, "", "");
- OrderInsert(it->first, det, '0', '4', ss, mapMd[det].AskPrice1 + hd * mapInstrument[det].PriceTick, "", "");
- yfd = true;
- string s = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + it->first + " 达到套利价差交易条件,第一腿价格卖出平昨 " + to_string(mapMd[dyt].BidPrice1 - hd * mapInstrument[dyt].PriceTick) + " 第二蹆价格 " + to_string(mapMd[det].AskPrice1 + hd * mapInstrument[det].PriceTick) + " 对手价差 " + to_string(jc);
- maps[num] = s;
- num++;
- InsertLog(it->first + " 达到套利价差交易条件,第一腿价格卖出平昨 " + to_string(mapMd[dyt].BidPrice1 - hd * mapInstrument[dyt].PriceTick) + " 第二蹆价格 " + to_string(mapMd[det].AskPrice1 + hd * mapInstrument[det].PriceTick) + " 对手价差 " + to_string(jc));
- }
- }
- }
- if (num != 0)shuchurizhi();
- tm = 0;
- }
- }
- //核心策略设计结束............................................................................................................................
复制代码 |
论坛官方微信、群(期货热点、量化探讨、开户与绑定实盘)
|