-
Notifications
You must be signed in to change notification settings - Fork 24
/
MdSpi.cpp
99 lines (84 loc) · 2.63 KB
/
MdSpi.cpp
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
#include "MdSpi.h"
#include <iostream>
using namespace std;
#pragma warning(disable : 4996)
// USER_API参数
extern CThostFtdcMdApi* pUserApi;
// 配置参数
extern char FRONT_ADDR[];
extern TThostFtdcBrokerIDType BROKER_ID;
extern TThostFtdcInvestorIDType INVESTOR_ID;
extern TThostFtdcPasswordType PASSWORD;
extern char** ppInstrumentID;
extern int iInstrumentID;
// 请求编号
extern int iRequestID;
void CMdSpi::OnRspError(CThostFtdcRspInfoField *pRspInfo,
int nRequestID, bool bIsLast)
{
cerr << "--->>> "<< __FUNCTION__ << endl;
IsErrorRspInfo(pRspInfo);
}
void CMdSpi::OnFrontDisconnected(int nReason)
{
cerr << "--->>> " << __FUNCTION__ << endl;
cerr << "--->>> Reason = " << nReason << endl;
}
void CMdSpi::OnHeartBeatWarning(int nTimeLapse)
{
cerr << "--->>> " << __FUNCTION__ << endl;
cerr << "--->>> nTimerLapse = " << nTimeLapse << endl;
}
void CMdSpi::OnFrontConnected()
{
cerr << "--->>> " << __FUNCTION__ << endl;
///用户登录请求
ReqUserLogin();
}
void CMdSpi::ReqUserLogin()
{
CThostFtdcReqUserLoginField req;
memset(&req, 0, sizeof(req));
strcpy(req.BrokerID, BROKER_ID);
strcpy(req.UserID, INVESTOR_ID);
strcpy(req.Password, PASSWORD);
int iResult = pUserApi->ReqUserLogin(&req, ++iRequestID);
cerr << "--->>> 发送用户登录请求: " << ((iResult == 0) ? "成功" : "失败") << endl;
}
void CMdSpi::OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin,
CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
cerr << "--->>> " << __FUNCTION__ << endl;
if (bIsLast && !IsErrorRspInfo(pRspInfo))
{
///获取当前交易日
cerr << "--->>> 获取当前交易日 = " << pUserApi->GetTradingDay() << endl;
// 请求订阅行情
SubscribeMarketData();
}
}
void CMdSpi::SubscribeMarketData()
{
int iResult = pUserApi->SubscribeMarketData(ppInstrumentID, iInstrumentID);
cerr << "--->>> 发送行情订阅请求: " << ((iResult == 0) ? "成功" : "失败") << endl;
}
void CMdSpi::OnRspSubMarketData(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
cerr << __FUNCTION__ << endl;
}
void CMdSpi::OnRspUnSubMarketData(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
cerr << __FUNCTION__ << endl;
}
void CMdSpi::OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData)
{
cerr << __FUNCTION__ << endl;
}
bool CMdSpi::IsErrorRspInfo(CThostFtdcRspInfoField *pRspInfo)
{
// 如果ErrorID != 0, 说明收到了错误的响应
bool bResult = ((pRspInfo) && (pRspInfo->ErrorID != 0));
if (bResult)
cerr << "--->>> ErrorID=" << pRspInfo->ErrorID << ", ErrorMsg=" << pRspInfo->ErrorMsg << endl;
return bResult;
}