鼎元C++程序化交易系统教程【跟交易相关的发单,撤单,重发,优先平今或优先平昨等程式码】
程式码:[code]void test::OnRtnOrder(CThostFtdcOrderField t){
mapOrder[t.OrderLocalID] = t;
if (!mapMd.count(t.InstrumentID))SubscribeMarketData(t.InstrumentID);
if (state != "run")return;
if (t.MacAddress == sName)lstActionOrdrSysID.push_back(t.OrderLocalID);
if (t.OrderStatus == '5' && t.MacAddress == sName && sfcd == "1" && IsActionOrdrSysID(t.OrderLocalID)) chongfa(t);
if (t.OrderStatus == '5' && t.MacAddress != sName)
{
string s1 = t.InstrumentID;
string s = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " 非本策略撤单 " + s1 + " 委托单号 " + t.OrderSysID + " " + t.MacAddress;
InsertLog(s1);
}
}
void test::OnRtnTrade(CThostFtdcTradeField t)
{
mapTrade[t.OrderSysID] = t;
if (state != "run")return;
if (t.InstrumentID == dyt && IsActionOrdrSysID(t.OrderLocalID)) dytcj = true;
if (t.InstrumentID == det && IsActionOrdrSysID(t.OrderLocalID))detcj = true;
if (dytcj && detcj && !tz)
{
tz = true;
OnState("stop");
string s = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " 两腿都有成交,套利策略停止 ";
InsertLog(s);
}
RsqPosition();
RsqPositionDetail(t.InvestorID);
}
void test::OnRsqBar(string sPeriod, string sInst, map<string, TKVALUE> mp)
{
}
void test::OnBarOpen(TKVALUE t)
{
}
void test::OnInstrument(CThostFtdcInstrumentField t)
{
mapInstrument[t.InstrumentID] = t;
}
void test::OnRspQryOrder(map<string, CThostFtdcOrderField>* m)
{
mapOrder = *m;
}
void test::OnRspQryTrade(map<string, CThostFtdcTradeField>* m)
{
mapTrade = *m;
}
void test::OnPosition(map<string, map<string, TPOSITION>>* m)
{
//InsertLog(" 持仓回报 ");
mapPos = *m;
}
void test::OnPositionDetail(map<string, TDETAIL>* m)
{
mapPosDeta = *m;
/*map<string, TDETAIL>::iterator it;
for (it = mapPosDeta.begin(); it != mapPosDeta.end(); it++)
{
string s1 = " 编号 " + it->first + " 账户 " + it->second.sAcc + " 合约 " + it->second.sInst + " 方向 " + it->second.sBuySell + " 数量 " + to_string(it->second.nVol) + " 平仓数量 " + to_string(it->second.nCloseVol) + " 价格 " + to_string(it->second.dOpenPrice) + " 时间 " + it->second.sOpenDate;
if (it->second.nVol - it->second.nCloseVol != 0)InsertLog(s1);
}*/
}
void test::OnCommissionRate(map<string, map<string, TCOMMISSION>>* m)
{
// InsertLog(" 手续费回报 ");
mapCom = *m;
}
void test::OnAccount(map<string, CThostFtdcTradingAccountField>* m)
{
//InsertLog(" 资金回报 ");
mapAcc = *m;
}
void test::OnInstrumentStatus(CThostFtdcInstrumentStatusField* t)
{
// InsertLog(" 状态改变汇报 ");
/* if (state != "run")return;
if (t.InstrumentID != sMainInst)return;
string sInst = t.InstrumentID;
string sTime = t.EnterTime;
string status;
if (t.InstrumentStatus == '0')status = "开盘前";
if (t.InstrumentStatus == '1')status = "非交易";
if (t.InstrumentStatus == '2')status = "连续交易";
if (t.InstrumentStatus == '3')status = "集合竞价报单";
if (t.InstrumentStatus == '4')status = "集合竞价价格平衡";
if (t.InstrumentStatus == '5')status = "集合竞价撮合";
if (t.InstrumentStatus == '6')status = "收盘";
InsertLog(sTime + "--->>>" + sInst + ":" + status);*/
}
void test::OnInstrumentAll(map<string, CThostFtdcInstrumentField>* m)
{
mapInstrument = *m;
}
void test::chedan()
{
map<string, CThostFtdcOrderField>::iterator it;
for (it = mapOrder.begin(); it != mapOrder.end(); it++)
{
if (it->second.MacAddress == sName)
{
if (it->second.OrderStatus == '3' || it->second.OrderStatus == '1')
{
string bs;
string soc;
if (it->second.Direction == '0')bs = "买入";
if (it->second.Direction == '1')bs = "卖出";
if (it->second.CombOffsetFlag == "0")soc = "开仓";
if (it->second.CombOffsetFlag == "1")soc = "平仓";
if (it->second.CombOffsetFlag == "3")soc = "平今仓";
if (it->second.CombOffsetFlag == "4")soc = "平昨仓";
OrderAction(it->second);
string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + it->second.InvestorID + " 有未成交委托,委托买卖方向 " + bs + soc + " 未成交数量委托数量 " + to_string((it->second.VolumeTotal - it->second.VolumeTraded)) + " 委托价格 " + to_string(it->second.LimitPrice) + " " + it->second.InstrumentID;
maps[num] = s1;
num++;
lstActionOrdrSysID.push_back(it->second.OrderLocalID);
}
}
}
if (num != 0)shuchurizhi();
}
void test::shuchurizhi()
{
map<int, string>::iterator it;
for (it = maps.begin(); it != maps.end(); it++)
{
InsertLog(it->second);
}
maps.clear();
num = 0;
}
void test::chongfa(CThostFtdcOrderField t)
{
string bs;
string soc;
if (t.Direction == '0')bs = "买入";
if (t.Direction == '1')bs = "卖出";
if (t.CombOffsetFlag == "0")soc = "开仓";
if (t.CombOffsetFlag == "1")soc = "平仓";
if (t.CombOffsetFlag == "3")soc = "平今仓";
if (t.CombOffsetFlag == "4")soc = "平昨仓";
char oc = t.CombOffsetFlag[0];
double dsj;
if (t.Direction == '0')
{
dsj = mapMd[t.InstrumentID].AskPrice1;
}
else if (t.Direction == '1')
{
dsj = mapMd[t.InstrumentID].BidPrice1;
}
OrderInsert(t.InvestorID, t.InstrumentID, t.Direction, oc, (t.VolumeTotal - t.VolumeTraded), dsj, "", "");
string s = t.InvestorID;
string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + s + " 有撤单成功重发,重发委托买卖方向 " + bs + soc + " 未成交数量委托数量 " + to_string((t.VolumeTotal - t.VolumeTraded)) + " 重发价格 " + to_string(dsj) + " " + t.InstrumentID;
InsertLog(s1);
}
// 卖出平仓 优先平昨仓
void test::closesell1(string sInvestorID, string sName, int sl, double jg)
{
//if (mapInstrument[sName].ExchangeID != "SHFE")
//{
// OrderInsert(sInvestorID, sName, '1', '1', sl, jg, "", "");
//}
//else
//{
if (mapPos[sInvestorID][sName].nLongPosYd >= sl)
{
OrderInsert(sInvestorID, sName, '1', '4', sl, jg, "", "");
string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 卖出平昨 " + to_string(sl) + " 手,账户持仓有多单今仓数量 " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + " 手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd);
InsertLog(s1);
}
else if (mapPos[sInvestorID][sName].nLongPosYd > 0)
{
OrderInsert(sInvestorID, sName, '1', '4', mapPos[sInvestorID][sName].nLongPosYd, jg, "", "");
int jys2 = min(mapPos[sInvestorID][sName].nLongPosTd, sl - mapPos[sInvestorID][sName].nLongPosYd);
if (mapPos[sInvestorID][sName].nLongPosTd > 0)
{
OrderInsert(sInvestorID, sName, '1', '3', jys2, jg, "", "");
}
string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 卖出平昨 " + to_string(mapPos[sInvestorID][sName].nLongPosYd) + " 卖出平今仓 " + to_string(jys2) + " 手,账户持仓有多单今仓数量 " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + " 手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd);
InsertLog(s1);
}
else if (mapPos[sInvestorID][sName].nLongPosTd > 0)
{
int jys3 = min(sl, mapPos[sInvestorID][sName].nLongPosTd);
OrderInsert(sInvestorID, sName, '1', '3', jys3, jg, "", "");
string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 卖出平今仓 " + to_string(jys3) + " 手,账户持仓有多单今仓数量 " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + " 手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd);
InsertLog(s1);
}
else
{
string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 账户持仓有多单今仓数量 " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + " 手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd) + " 账户持仓有空单今仓数量 " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + " 手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);;
InsertLog(s1);
}
// }
}
// 买入平仓 优先平昨仓
void test::closebuy1(string sInvestorID, string sName, int sl, double jg)
{
//if (mapInstrument[sName].ExchangeID != "SHFE")
//{
// OrderInsert(sInvestorID, sName, '0', '1', sl, jg, "", "");
//}
//else
//{
if (mapPos[sInvestorID][sName].nShortPosYd >= sl)
{
OrderInsert(sInvestorID, sName, '0', '4', sl, jg, "", "");
string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 买入平昨 " + to_string(sl) + " 手,账户持仓有空单今仓数量 " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + " 手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);
InsertLog(s1);
}
else if (mapPos[sInvestorID][sName].nShortPosYd > 0)
{
OrderInsert(sInvestorID, sName, '0', '4', mapPos[sInvestorID][sName].nShortPosYd, jg, "", "");
int jys2 = min(mapPos[sInvestorID][sName].nShortPosTd, sl - mapPos[sInvestorID][sName].nShortPosYd);
if (mapPos[sInvestorID][sName].nShortPosTd > 0)
{
OrderInsert(sInvestorID, sName, '0', '3', jys2, jg, "", "");
}
string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 买入平昨 " + to_string(mapPos[sInvestorID][sName].nShortPosYd) + " 买入平今仓 " + to_string(jys2) + " 手,账户持仓有空单今仓数量 " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + " 手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);
InsertLog(s1);
}
else if (mapPos[sInvestorID][sName].nShortPosTd > 0)
{
int jys3 = min(mapPos[sInvestorID][sName].nShortPosTd, sl);
OrderInsert(sInvestorID, sName, '0', '3', jys3, jg, "", "");
string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 买入平昨仓 " + to_string(jys3) + " 手,账户持仓有空单今仓数量 " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + " 手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);
InsertLog(s1);
}
else
{
string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 账户持仓有多单今仓数量 " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + " 手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd) + " 账户持仓有空单今仓数量 " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + " 手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);;
InsertLog(s1);
}
// }
}
// 卖出平仓 优先平今仓
void test::closesell2(string sInvestorID, string sName, int sl, double jg)
{
//if (mapInstrument[sName].ExchangeID != "SHFE")
//{
// OrderInsert(sInvestorID, sName, '1', '1', sl, jg, "", "");
//}
//else
//{
if (mapPos[sInvestorID][sName].nLongPosTd >= sl)
{
OrderInsert(sInvestorID, sName, '1', '3', sl, jg, "", "");
string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 卖出平今 " + to_string(sl) + " 手,账户持仓有多单今仓数量 " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + " 手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd);
InsertLog(s1);
}
else if (mapPos[sInvestorID][sName].nLongPosTd > 0)
{
OrderInsert(sInvestorID, sName, '1', '3', mapPos[sInvestorID][sName].nLongPosTd, jg, "", "");
int jys2 = min(mapPos[sInvestorID][sName].nLongPosYd, sl - mapPos[sInvestorID][sName].nLongPosTd);
if (mapPos[sInvestorID][sName].nLongPosYd > 0)
{
OrderInsert(sInvestorID, sName, '1', '4', jys2, jg, "", "");
}
string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 卖出平今 " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + " 卖出平昨仓 " + to_string(jys2) + " 手,账户持仓有多单今仓数量 " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + " 手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd);
InsertLog(s1);
}
else if (mapPos[sInvestorID][sName].nLongPosYd > 0)
{
int jys3 = min(mapPos[sInvestorID][sName].nLongPosYd, sl);
OrderInsert(sInvestorID, sName, '1', '4', jys3, jg, "", "");
string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 卖出平昨仓 " + to_string(jys3) + " 手,账户持仓有多单今仓数量 " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + " 手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd);
InsertLog(s1);
}
else
{
string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 账户持仓有多单今仓数量 " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + " 手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd) + " 账户持仓有空单今仓数量 " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + " 手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);;
InsertLog(s1);
}
}
//
//}
// 买入平仓 优先平今仓
void test::closebuy2(string sInvestorID, string sName, int sl, double jg)
{
//if (mapInstrument[sName].ExchangeID != "SHFE")
//{
// OrderInsert(sInvestorID, sName, '0', '1', sl, jg, "", "");
//}
//else
//{
if (mapPos[sInvestorID][sName].nShortPosTd >= sl)
{
OrderInsert(sInvestorID, sName, '0', '3', sl, jg, "", "");
string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 买入平今 " + to_string(sl) + " 手,账户持仓有空单今仓数量 " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + " 手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);
InsertLog(s1);
}
else if (mapPos[sInvestorID][sName].nShortPosTd > 0)
{
OrderInsert(sInvestorID, sName, '0', '3', mapPos[sInvestorID][sName].nShortPosTd, jg, "", "");
int jys2 = min(mapPos[sInvestorID][sName].nShortPosYd, sl - mapPos[sInvestorID][sName].nShortPosTd);
if (mapPos[sInvestorID][sName].nShortPosYd > 0)
{
OrderInsert(sInvestorID, sName, '0', '4', jys2, jg, "", "");
}
string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 买入平今 " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + " 买入平昨仓 " + to_string(jys2) + " 手,账户持仓有空单今仓数量 " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + " 手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);
InsertLog(s1);
}
else if (mapPos[sInvestorID][sName].nShortPosYd > 0)
{
int jys2 = min(mapPos[sInvestorID][sName].nShortPosYd, sl);
OrderInsert(sInvestorID, sName, '0', '4', jys2, jg, "", "");
string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 买入平昨仓 " + to_string(jys2) + " 手,账户持仓有空单今仓数量 " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + " 手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);
InsertLog(s1);
}
else
{
string s1 = t1 + ":" + t2 + ":" + t3 + ":" + t4 + " " + " 账户 " + sInvestorID + " 合约 " + sName + " 账户持仓有多单今仓数量 " + to_string(mapPos[sInvestorID][sName].nLongPosTd) + " 手持有昨仓多单数量 " + to_string(mapPos[sInvestorID][sName].nLongPosYd) + " 账户持仓有空单今仓数量 " + to_string(mapPos[sInvestorID][sName].nShortPosTd) + " 手持有昨仓空单数量 " + to_string(mapPos[sInvestorID][sName].nShortPosYd);;
InsertLog(s1);
}
}[/code]
页:
[1]