forked from JiaRu2016/PlayWithCTP-Release
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Md.hpp
executable file
·198 lines (171 loc) · 6.18 KB
/
Md.hpp
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
#include "include/ThostFtdcMdApi.h"
#include "include/ThostFtdcTraderApi.h"
#include "include/DataCollect.h"
#include <stdio.h>
#include <cstring>
#include <unistd.h>
#include <vector>
#include <string>
#include <iostream>
#include "mappings.h"
#include "myutils.hpp"
#include "config.h"
class MdHandler : public CThostFtdcMdSpi {
private:
CThostFtdcMdApi* pApi;
int nRequestID = 0;
const char* Front = CONFIG_mdFront;
const char* BrokerID = CONFIG_BrokerID;
const char* InvestorID = CONFIG_InvestorID;
const char* Password = CONFIG_Password;
public:
const std::vector<std::string> instruments = CONFIG_instruments;
SyncEvent event_front_connected;
SyncEvent event_loggedin;
public:
MdHandler() {};
~MdHandler() {};
void connect();
void login();
void subscribe();
void run();
public:
void OnFrontConnected();
void OnFrontDisconnected(int nReason);
void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
void OnRspSubMarketData(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
virtual void OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData);
void OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
};
///////////////////////////////////////////////////////////////////////////////////////
// SECTION handler methods
///////////////////////////////////////////////////////////////////////////////////////
void MdHandler::connect() {
pApi = CThostFtdcMdApi::CreateFtdcMdApi("./flow_md/", false, false);
pApi->RegisterSpi(this);
pApi->RegisterFront((char*)Front);
pApi->Init();
event_front_connected.Wait();
};
void MdHandler::login() {
CThostFtdcReqUserLoginField loginfield;
//memset(&loginfield, 0, sizeof(loginfield));
strcpy(loginfield.Password, Password);
strcpy(loginfield.BrokerID, BrokerID);
strcpy(loginfield.UserID, Password);
int res = pApi->ReqUserLogin(&loginfield, 42);
const char* txt;
switch (res)
{
case 0: txt = "登录请求发送成功"; break;
case -1: txt = "网络连接失败"; break;
case -2: txt = "未处理请求超过许可数"; break;
case -3: txt = "每秒发送请求数超过许可数"; break;
default: break;
}
printf("<== Api::ReqUserLogin: %s\n", txt);
event_loggedin.Wait();
}
void MdHandler::subscribe() {
const int n = instruments.size();
char **ppInstrumentID = new char*[n];
for (int i = 0; i < n; i++) {
ppInstrumentID[i] = (char*)(instruments[i].c_str());
}
int res = pApi->SubscribeMarketData(ppInstrumentID, n);
const char* txt;
switch (res) {
case 0: txt = "订阅请求发送成功"; break;
case -1: txt = "网络连接失败"; break;
case -2: txt = "未处理请求超过许可数"; break;
case -3: txt = "每秒发送请求数超过许可数"; break;
default: break;
}
printf("<== Api::SubscribeMarketData: %s\n", txt);
}
void MdHandler::run() {
while (true) {
sleep(9000000);
}
}
///////////////////////////////////////////////////////////////////////////////////////
// SECTION overwrite OnXXX()
///////////////////////////////////////////////////////////////////////////////////////
void MdHandler::OnFrontConnected() {
printf("==> OnFrontConnected. 行情前置连接成功 \n");
event_front_connected.Set();
};
void MdHandler::OnFrontDisconnected(int nReason) {
const char* txt = "[unknown reason]";
switch (nReason)
{
case 0x1001: txt = "网络读失败"; break;
case 0x1002: txt = "网络写失败"; break;
case 0x2001: txt = "接收心跳超时"; break;
case 0x2002: txt = "发送心跳失败"; break;
case 0x2003: txt = "收到错误报文"; break;
default: break;
}
printf("==> OnFrontDisonnected, 行情前置断开 nReason is: %d \t %s\n", nReason, txt);
event_front_connected.Clear();
};
void MdHandler::OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {
printf(
"==> OnRspUserLogin: 行情服务器登录 %s\tnRequestID=%d, bIsLast=%d\n",
pRspInfo->ErrorID == 0 ? "成功" : "失败",
nRequestID,
bIsLast
);
printf(
"\t==> InfoField: %d %s\n",
pRspInfo->ErrorID,
pRspInfo->ErrorMsg
);
printf(
"\t==> RspUserLogin: TradingDay: %s, LoginTime: %s\n",
pRspUserLogin->TradingDay,
pRspUserLogin->LoginTime
);
if (pRspInfo && pRspInfo->ErrorID == 0) {
event_loggedin.Set();
}
};
void MdHandler::OnRspSubMarketData(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast){
printf(
"==> OnRspSubMarketData: 订阅行情 %s : %s\tnRequestID=%d, bIsLast=%d\n",
pRspInfo->ErrorID == 0 ? "成功": "失败",
(char*)pSpecificInstrument,
nRequestID,
bIsLast
);
};
void MdHandler::OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData){
printf(
"----->> OnRtnDepthMarketData: 行情推送ing... \t (ActionDay: %s) %s %s %d [%s] %f\n",
pDepthMarketData->ActionDay,
pDepthMarketData->TradingDay,
pDepthMarketData->UpdateTime,
pDepthMarketData->UpdateMillisec,
pDepthMarketData->InstrumentID,
pDepthMarketData->LastPrice
);
};
void MdHandler::OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {
printf("==> 发生错误 ❌ \nOnRspError:\n");
printf(
"\tInfoField: %d %s\n",
pRspInfo->ErrorID,
pRspInfo->ErrorMsg
);
};
// //////////////////////////////////////////////////////////////////////////////////////////////////////
// // SECTION main
// //////////////////////////////////////////////////////////////////////////////////////////////////////
// int main() {
// MdHandler* mdhandler = new MdHandler();
// mdhandler->connect();
// mdhandler->login();
// mdhandler->subscribe();
// mdhandler->run();
// return 0;
// };