-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTraderSpi.cpp
More file actions
395 lines (348 loc) · 11.7 KB
/
TraderSpi.cpp
File metadata and controls
395 lines (348 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//
// Created by njn on 17-6-27.
//
#include <iostream>
#include <cstring>
#include <zconf.h>
using namespace std;
#include "ThostFtdcTraderApi.h"
#include "TraderSpi.h"
#pragma warning(disable : 4996)
// USER_API参数
extern CThostFtdcTraderApi* trader_pUserApi;
// 配置参数
extern char TRADER_FRONT_ADDR[]; // 前置地址
extern char BROKER_ID[]; // 经纪公司代码
extern char INVESTOR_ID[]; // 投资者代码
extern char PASSWORD[]; // 用户密码
extern char TRADER_INSTRUMENT_ID[]; // 合约代码
extern TThostFtdcPriceType LIMIT_PRICE; // 价格
extern TThostFtdcDirectionType DIRECTION; // 买卖方向
// 请求编号
extern int trader_iRequestID;
// 会话参数
TThostFtdcFrontIDType FRONT_ID; //前置编号
TThostFtdcSessionIDType SESSION_ID; //会话编号
TThostFtdcOrderRefType ORDER_REF; //报单引用
// 流控判断
bool IsFlowControl(int iResult)
{
return ((iResult == -2) || (iResult == -3));
}
void CTraderSpi::OnFrontConnected()
{
cerr << "--->>> " << "OnFrontConnected" << endl;
///用户登录请求
ReqUserLogin();
}
void CTraderSpi::ReqUserLogin()
{
CThostFtdcReqUserLoginField req;
memset(&req, 0, sizeof(req));
strcpy(req.BrokerID, BROKER_ID);
strcpy(req.UserID, INVESTOR_ID);
strcpy(req.Password, PASSWORD);
int iResult = trader_pUserApi->ReqUserLogin(&req, ++trader_iRequestID);
cerr << "--->>> 发送用户登录请求: " << iResult << ((iResult == 0) ? ", 成功" : ", 失败") << endl;
}
void CTraderSpi::OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin,
CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
cerr << "--->>> " << "OnRspUserLogin" << endl;
if (bIsLast && !IsErrorRspInfo(pRspInfo))
{
// 保存会话参数
FRONT_ID = pRspUserLogin->FrontID;
SESSION_ID = pRspUserLogin->SessionID;
int iNextOrderRef = atoi(pRspUserLogin->MaxOrderRef);
iNextOrderRef++;
sprintf(ORDER_REF, "%d", iNextOrderRef);
///获取当前交易日
cerr << "--->>> 获取当前交易日 = " << trader_pUserApi->GetTradingDay() << endl;
///投资者结算结果确认
ReqSettlementInfoConfirm();
}
}
void CTraderSpi::ReqSettlementInfoConfirm()
{
CThostFtdcSettlementInfoConfirmField req;
memset(&req, 0, sizeof(req));
strcpy(req.BrokerID, BROKER_ID);
strcpy(req.InvestorID, INVESTOR_ID);
int iResult = trader_pUserApi->ReqSettlementInfoConfirm(&req, ++trader_iRequestID);
cerr << "--->>> 投资者结算结果确认: " << iResult << ((iResult == 0) ? ", 成功" : ", 失败") << endl;
}
void CTraderSpi::OnRspSettlementInfoConfirm(CThostFtdcSettlementInfoConfirmField *pSettlementInfoConfirm, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
cerr << "--->>> " << "OnRspSettlementInfoConfirm" << endl;
if (bIsLast && !IsErrorRspInfo(pRspInfo))
{
///请求查询合约
ReqQryInstrument();
}
}
void CTraderSpi::ReqQryInstrument()
{
CThostFtdcQryInstrumentField req;
memset(&req, 0, sizeof(req));
//cout << TRADER_INSTRUMENT_ID << endl;
strcpy(req.InstrumentID, TRADER_INSTRUMENT_ID);
strcpy(req.InstrumentID, "");
while (true)
{
int iResult = trader_pUserApi->ReqQryInstrument(&req, ++trader_iRequestID);
if (!IsFlowControl(iResult))
{
cerr << "--->>> 请求查询合约: " << iResult << ((iResult == 0) ? ", 成功" : ", 失败") << endl;
sleep(1);
break;
}
else
{
cerr << "--->>> 请求查询合约: " << iResult << ", 受到流控" << endl;
sleep(1);
}
} // while
}
void CTraderSpi::OnRspQryInstrument(CThostFtdcInstrumentField *pInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
//cerr << "--->>> " << "OnRspQryInstrument" << endl;
if(NULL==pInstrument) return;
if(NULL!=pInstrument)
{
if(!pInstrument->IsTrading)
{
return;
}
if(!pInstrument->ProductClass == '1' )
{
return;
}
if( !(strcmp(pInstrument->ExchangeID,"SHFE")==0) )
{
return;
}
}
cout << "instrument: " << pInstrument->InstrumentID << " " << "exchange: " << pInstrument->ExchangeID << endl;
if (bIsLast && !IsErrorRspInfo(pRspInfo))
{
///请求查询合约
ReqQryTradingAccount();
}
}
void CTraderSpi::ReqQryTradingAccount()
{
CThostFtdcQryTradingAccountField req;
memset(&req, 0, sizeof(req));
strcpy(req.BrokerID, BROKER_ID);
strcpy(req.InvestorID, INVESTOR_ID);
while (true)
{
int iResult = trader_pUserApi->ReqQryTradingAccount(&req, ++trader_iRequestID);
if (!IsFlowControl(iResult))
{
cerr << "--->>> 请求查询资金账户: " << iResult << ((iResult == 0) ? ", 成功" : ", 失败") << endl;
//
sleep(1);
break;
}
else
{
cerr << "--->>> 请求查询资金账户: " << iResult << ", 受到流控" << endl;
sleep(1);
}
} // while
}
void CTraderSpi::OnRspQryTradingAccount(CThostFtdcTradingAccountField *pTradingAccount, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
cerr << "--->>> " << "OnRspQryTradingAccount" << endl;
if (bIsLast && !IsErrorRspInfo(pRspInfo))
{
///请求查询投资者持仓
ReqQryInvestorPosition();
}
}
void CTraderSpi::ReqQryInvestorPosition()
{
CThostFtdcQryInvestorPositionField req;
memset(&req, 0, sizeof(req));
strcpy(req.BrokerID, BROKER_ID);
strcpy(req.InvestorID, INVESTOR_ID);
strcpy(req.InstrumentID, TRADER_INSTRUMENT_ID);
while (true)
{
int iResult = trader_pUserApi->ReqQryInvestorPosition(&req, ++trader_iRequestID);
if (!IsFlowControl(iResult))
{
cerr << "--->>> 请求查询投资者持仓: " << iResult << ((iResult == 0) ? ", 成功" : ", 失败") << endl;
sleep(1);
break;
}
else
{
cerr << "--->>> 请求查询投资者持仓: " << iResult << ", 受到流控" << endl;
sleep(1);
}
} // while
}
void CTraderSpi::OnRspQryInvestorPosition(CThostFtdcInvestorPositionField *pInvestorPosition, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
cerr << "--->>> " << "OnRspQryInvestorPosition" << endl;
if (bIsLast && !IsErrorRspInfo(pRspInfo))
{
///报单录入请求
//ReqOrderInsert();
}
}
void CTraderSpi::ReqOrderInsert()
{
CThostFtdcInputOrderField req;
memset(&req, 0, sizeof(req));
///经纪公司代码
strcpy(req.BrokerID, BROKER_ID);
///投资者代码
strcpy(req.InvestorID, INVESTOR_ID);
///合约代码
strcpy(req.InstrumentID, TRADER_INSTRUMENT_ID);
///报单引用
strcpy(req.OrderRef, ORDER_REF);
///用户代码
// TThostFtdcUserIDType UserID;
///报单价格条件: 限价
req.OrderPriceType = THOST_FTDC_OPT_LimitPrice;
///买卖方向:
req.Direction = DIRECTION;
///组合开平标志: 开仓
req.CombOffsetFlag[0] = THOST_FTDC_OF_Open;
///组合投机套保标志
req.CombHedgeFlag[0] = THOST_FTDC_HF_Speculation;
///价格
req.LimitPrice = LIMIT_PRICE;
///数量: 1
req.VolumeTotalOriginal = 1;
///有效期类型: 当日有效
req.TimeCondition = THOST_FTDC_TC_GFD;
///GTD日期
// TThostFtdcDateType GTDDate;
///成交量类型: 任何数量
req.VolumeCondition = THOST_FTDC_VC_AV;
///最小成交量: 1
req.MinVolume = 1;
///触发条件: 立即
req.ContingentCondition = THOST_FTDC_CC_Immediately;
///止损价
// TThostFtdcPriceType StopPrice;
///强平原因: 非强平
req.ForceCloseReason = THOST_FTDC_FCC_NotForceClose;
///自动挂起标志: 否
req.IsAutoSuspend = 0;
///业务单元
// TThostFtdcBusinessUnitType BusinessUnit;
///请求编号
// TThostFtdcRequestIDType RequestID;
///用户强评标志: 否
req.UserForceClose = 0;
int iResult = trader_pUserApi->ReqOrderInsert(&req, ++trader_iRequestID);
cerr << "--->>> 报单录入请求: " << iResult << ((iResult == 0) ? ", 成功" : ", 失败") << endl;
}
void CTraderSpi::OnRspOrderInsert(CThostFtdcInputOrderField *pInputOrder, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
cerr << "--->>> " << "OnRspOrderInsert" << endl;
IsErrorRspInfo(pRspInfo);
}
void CTraderSpi::ReqOrderAction(CThostFtdcOrderField *pOrder)
{
static bool ORDER_ACTION_SENT = false; //是否发送了报单
if (ORDER_ACTION_SENT)
return;
CThostFtdcInputOrderActionField req;
memset(&req, 0, sizeof(req));
///经纪公司代码
strcpy(req.BrokerID, pOrder->BrokerID);
///投资者代码
strcpy(req.InvestorID, pOrder->InvestorID);
///报单操作引用
// TThostFtdcOrderActionRefType OrderActionRef;
///报单引用
strcpy(req.OrderRef, pOrder->OrderRef);
///请求编号
// TThostFtdcRequestIDType RequestID;
///前置编号
req.FrontID = FRONT_ID;
///会话编号
req.SessionID = SESSION_ID;
///交易所代码
// TThostFtdcExchangeIDType ExchangeID;
///报单编号
// TThostFtdcOrderSysIDType OrderSysID;
///操作标志
req.ActionFlag = THOST_FTDC_AF_Delete;
///价格
// TThostFtdcPriceType LimitPrice;
///数量变化
// TThostFtdcVolumeType VolumeChange;
///用户代码
// TThostFtdcUserIDType UserID;
///合约代码
strcpy(req.InstrumentID, pOrder->InstrumentID);
int iResult = trader_pUserApi->ReqOrderAction(&req, ++trader_iRequestID);
cerr << "--->>> 报单操作请求: " << iResult << ((iResult == 0) ? ", 成功" : ", 失败") << endl;
ORDER_ACTION_SENT = true;
}
void CTraderSpi::OnRspOrderAction(CThostFtdcInputOrderActionField *pInputOrderAction, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
cerr << "--->>> " << "OnRspOrderAction" << endl;
IsErrorRspInfo(pRspInfo);
}
///报单通知
void CTraderSpi::OnRtnOrder(CThostFtdcOrderField *pOrder)
{
cerr << "--->>> " << "OnRtnOrder" << endl;
/* if (IsMyOrder(pOrder))
{
if (IsTradingOrder(pOrder))
ReqOrderAction(pOrder);
else if (pOrder->OrderStatus == THOST_FTDC_OST_Canceled)
cout << "--->>> 撤单成功" << endl;
}*/
}
///成交通知
void CTraderSpi::OnRtnTrade(CThostFtdcTradeField *pTrade)
{
cerr << "--->>> " << "OnRtnTrade" << endl;
}
void CTraderSpi:: OnFrontDisconnected(int nReason)
{
cerr << "--->>> " << "OnFrontDisconnected" << endl;
cerr << "--->>> Reason = " << nReason << endl;
}
void CTraderSpi::OnHeartBeatWarning(int nTimeLapse)
{
cerr << "--->>> " << "OnHeartBeatWarning" << endl;
cerr << "--->>> nTimerLapse = " << nTimeLapse << endl;
}
void CTraderSpi::OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
cerr << "--->>> " << "OnRspError" << endl;
IsErrorRspInfo(pRspInfo);
}
bool CTraderSpi::IsErrorRspInfo(CThostFtdcRspInfoField *pRspInfo)
{
// 如果ErrorID != 0, 说明收到了错误的响应
bool bResult = ((pRspInfo) && (pRspInfo->ErrorID != 0));
if (bResult)
cerr << "--->>> ErrorID=" << pRspInfo->ErrorID << ", ErrorMsg=" << pRspInfo->ErrorMsg << endl;
return bResult;
}
bool CTraderSpi::IsMyOrder(CThostFtdcOrderField *pOrder)
{
return ((pOrder->FrontID == FRONT_ID) &&
(pOrder->SessionID == SESSION_ID) &&
(strcmp(pOrder->OrderRef, ORDER_REF) == 0));
}
bool CTraderSpi::IsTradingOrder(CThostFtdcOrderField *pOrder)
{
return ((pOrder->OrderStatus != THOST_FTDC_OST_PartTradedNotQueueing) &&
(pOrder->OrderStatus != THOST_FTDC_OST_Canceled) &&
(pOrder->OrderStatus != THOST_FTDC_OST_AllTraded));
}