Repository navigation
Expand file tree
/
Copy pathMdUserApi.cpp
More file actions
429 lines (366 loc) · 11.3 KB
/
Copy pathMdUserApi.cpp
File metadata and controls
429 lines (366 loc) · 11.3 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include "stdafx.h"
#include "MdUserApi.h"
#include "../../include/QueueEnum.h"
#include "../../include/ApiHeader.h"
#include "../../include/ApiStruct.h"
#include "../../include/toolkit.h"
#include "../../include/ApiProcess.h"
#include "../../include/queue/MsgQueue.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <mutex>
#include <vector>
#include <iostream>
using namespace std;
int Today(int day)
{
time_t now = time(0);
now += day * 86400;
struct tm *ptmNow = localtime(&now);
return (ptmNow->tm_year + 1900) * 10000
+ (ptmNow->tm_mon + 1) * 100
+ ptmNow->tm_mday;
}
void WindGetErrorMessage(LONG errCode, char* buffer, int bufferSize){
WCHAR errMsg[1024] = { 0 };
int msgLength = sizeof errMsg;
CWAPIWrapperCpp::getErrorMsg(errCode, eCHN, errMsg, msgLength);
auto strLen = wcslen(errMsg) + 1;
char* str = new char[strLen];
size_t converted;
wcstombs_s(&converted, str, strLen, errMsg, _TRUNCATE);
strcpy_s(buffer, bufferSize, str);
delete[] str;
}
void* __stdcall Query(char type, void* pApi1, void* pApi2, double double1, double double2, void* ptr1, int size1, void* ptr2, int size2, void* ptr3, int size3)
{
// 由内部调用,不用检查是否为空
CMdUserApi* pApi = (CMdUserApi*)pApi2;
pApi->QueryInThread(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3);
return nullptr;
}
CMdUserApi::CMdUserApi(void)
{
pThis = this;
m_CoInitialized = false;
// 自己维护两个消息队列
m_msgQueue = new CMsgQueue();
m_msgQueue_Query = new CMsgQueue();
m_msgQueue_Query->Register(Query);
m_msgQueue_Query->StartThread();
}
CMdUserApi::~CMdUserApi(void)
{
Disconnect();
}
void CMdUserApi::QueryInThread(char type, void* pApi1, void* pApi2, double double1, double double2, void* ptr1, int size1, void* ptr2, int size2, void* ptr3, int size3)
{
int iRet = 0;
switch (type)
{
case Start:
WindStart();
break;
case Stop:
WindStop();
break;
case QueryInstrument:
WindQryInstrument();
break;
default:
break;
}
this_thread::sleep_for(chrono::milliseconds(1));
}
void CMdUserApi::Register(void* pCallback, void* pClass)
{
m_pClass = pClass;
if (m_msgQueue == nullptr)
return;
m_msgQueue_Query->Register(Query);
m_msgQueue->Register((fnOnResponse)pCallback);
if (pCallback)
{
m_msgQueue_Query->StartThread();
m_msgQueue->StartThread();
}
else
{
m_msgQueue_Query->StopThread();
m_msgQueue->StopThread();
}
}
void CMdUserApi::Connect()
{
m_msgQueue_Query->Input_NoCopy(RequestType::Start, m_msgQueue_Query, this, 0, 0, nullptr, 0, nullptr, 0, nullptr, 0);
}
void CMdUserApi::WindStart()
{
// 弹出这个只是为了调试,以后可能要关闭
AllocConsole();
freopen("CON", "r", stdin);
freopen("CON", "w", stdout);
freopen("CON", "w", stderr);
::DeleteMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE, MF_BYCOMMAND);
if (!m_CoInitialized){
CoInitialize(nullptr);
m_CoInitialized = true;
}
m_msgQueue->Input_NoCopy(ResponseType::ResponseType_OnConnectionStatus, m_msgQueue, m_pClass, ConnectionStatus::ConnectionStatus_Connecting, 0, nullptr, 0, nullptr, 0, nullptr, 0);
auto errCode = CWAPIWrapperCpp::start();
if (errCode == 0){
//m_msgQueue->Input_NoCopy(ResponseType::ResponseType_OnConnectionStatus, m_msgQueue, m_pClass, ConnectionStatus::ConnectionStatus_Connected, 0, nullptr, 0, nullptr, 0, nullptr, 0);
m_msgQueue->Input_NoCopy(ResponseType::ResponseType_OnConnectionStatus, m_msgQueue, m_pClass, ConnectionStatus::ConnectionStatus_Done, 0, nullptr, 0, nullptr, 0, nullptr, 0);
}
else{
RspUserLoginField* pField = (RspUserLoginField*)m_msgQueue->new_block(sizeof(RspUserLoginField));
pField->RawErrorID = errCode;
WindGetErrorMessage(errCode, pField->Text, sizeof pField->Text);
m_msgQueue->Input_NoCopy(ResponseType::ResponseType_OnConnectionStatus, m_msgQueue, m_pClass, ConnectionStatus::ConnectionStatus_Disconnected, 0, pField, sizeof(RspUserLoginField), nullptr, 0, nullptr, 0);
}
}
void CMdUserApi::WindStop()
{
if (WindConnected()){
// 取消所有请求
CWAPIWrapperCpp::cancelRequest(0);
CWAPIWrapperCpp::stop();
// 全清理,只留最后一个
m_msgQueue->Clear();
m_msgQueue->Input_NoCopy(ResponseType::ResponseType_OnConnectionStatus, m_msgQueue, m_pClass, ConnectionStatus::ConnectionStatus_Disconnected, 0, nullptr, 0, nullptr, 0, nullptr, 0);
// 主动触发
m_msgQueue->Process();
}
if (m_CoInitialized){
CoUninitialize();
m_CoInitialized = false;
}
SetEvent(m_ExitEvent);
}
bool CMdUserApi::WindConnected()
{
return CWAPIWrapperCpp::isconnected();
}
void CMdUserApi::WindQryInstrument()
{
WindData wd;
WCHAR options[1024];
_snwprintf_s(options, sizeof options, _TRUNCATE, L"date=%d;sectorId=a001010100000000;field=wind_code,sec_name", Today(0));
auto errCode = CWAPIWrapperCpp::wset(wd, L"SectorConstituent", options);
if (errCode != 0){
ErrorField* pField = (ErrorField*)m_msgQueue->new_block(sizeof(ErrorField));
pField->RawErrorID = errCode;
strcpy_s(pField->Source, sizeof pField->Source, "WindQryInstrument");
WindGetErrorMessage(errCode, pField->Text, sizeof pField->Text);
m_msgQueue->Input_NoCopy(ResponseType::ResponseType_OnRtnError, m_msgQueue, m_pClass, 0, 0, pField, sizeof(ErrorField), nullptr, 0, nullptr, 0);
return;
}
}
void CMdUserApi::Disconnect()
{
// 清理查询队列
if (m_msgQueue_Query)
{
m_ExitEvent = CreateEvent(nullptr, true, false, nullptr);
m_msgQueue_Query->Input_NoCopy(RequestType::Stop, m_msgQueue_Query, this, 0, 0, nullptr, 0, nullptr, 0, nullptr, 0);
WaitForSingleObject(m_ExitEvent, INFINITE);
m_msgQueue_Query->StopThread();
m_msgQueue_Query->Register(nullptr);
m_msgQueue_Query->Clear();
delete m_msgQueue_Query;
m_msgQueue_Query = nullptr;
}
// 清理响应队列
if (m_msgQueue)
{
m_msgQueue->StopThread();
m_msgQueue->Register(nullptr);
m_msgQueue->Clear();
delete m_msgQueue;
m_msgQueue = nullptr;
}
}
CMdUserApi* CMdUserApi::pThis = nullptr;
LONG WINAPI WindCallback(ULONGLONG reqId, const WindData &wd)
{
//wsq订阅回调函数,返回数据存放在参数wd中,可以对其进行分析操作
return CMdUserApi::pThis->_WindCallback(reqId, wd);
}
struct MyWindWsq
{
double rt_date;
double rt_time;
double rt_last;
double rt_vol;
double rt_oi;
double rt_bid1;
double rt_ask1;
double rt_bsize1;
double rt_asize1;
};
string toString(const LPVARIANT data)
{
string msg;
switch (data->vt & VT_BSTR_BLOB)
{
/*case VT_I4:
msg(_T("%d"), data->intVal);
break;
case VT_I8:
msg.Format(_T("%I64d"), data->llVal);
break;
case VT_R8:
msg.Format(_T("%f"), data->dblVal);
break;
case VT_EMPTY:
msg = _T("NaN");
break;
case VT_BSTR:
msg.Format(_T("%s"), data->bstrVal);
break;
case VT_DATE:
msg = COleDateTime(data->date).Format();
break;*/
default:
break;
}
return msg;
}
LONG CMdUserApi::_WindCallback(ULONGLONG reqId, const WindData &wd)
{
int timeLength = wd.GetTimesLength();
int codeLength = wd.GetCodesLength();
int fieldsLength = wd.GetFieldsLength();
for (int i = 0; i < codeLength; ++i)
{
//for (int j = 0; j < fieldsLength; ++j)
//{
// VARIANT data;
// wcout << wd.GetDataItem(0, i, j, data) << endl;
// wcout << data.vt << endl;
//}
MyWindWsq st = { 0 };
VARIANT data;
wd.GetDataItem(0, i, 0, data);
st.rt_date = data.dblVal;
wd.GetDataItem(0, i, 1, data);
st.rt_time = data.dblVal;
wd.GetDataItem(0, i, 2, data);
st.rt_last = data.dblVal;
wd.GetDataItem(0, i, 3, data);
st.rt_vol = data.dblVal;
wd.GetDataItem(0, i, 4, data);
st.rt_oi = data.dblVal;
wd.GetDataItem(0, i, 5, data);
st.rt_bid1 = data.dblVal;
wd.GetDataItem(0, i, 6, data);
st.rt_ask1 = data.dblVal;
wd.GetDataItem(0, i, 7, data);
st.rt_bsize1 = data.dblVal;
wd.GetDataItem(0, i, 8, data);
st.rt_asize1 = data.dblVal;
//printf("rt_date:%f", st.rt_date);
//printf("rt_time:%f", st.rt_time);
//printf("rt_last:%f", st.rt_last);
//printf("rt_vol:%f", st.rt_vol);
//printf("rt_oi:%f", st.rt_oi);
//printf("rt_bid1:%f", st.rt_bid1);
//printf("rt_ask1:%f", st.rt_ask1);
//printf("rt_bsize1:%f", st.rt_bsize1);
//printf("rt_asize1:%f", st.rt_asize1);
DepthMarketDataNField* pField = (DepthMarketDataNField*)m_msgQueue->new_block(sizeof(DepthMarketDataNField) + sizeof(DepthField) * 2);
wstring wstr(wd.GetCodeByIndex(i));
std::string str(wstr.length(), '\0');
std::copy(wstr.begin(), wstr.end(), str.begin());
sprintf(pField->Symbol, "%s", str.c_str());
pField->TradingDay = st.rt_date;
pField->ActionDay = st.rt_date;
pField->UpdateTime = st.rt_time;
pField->LastPrice = st.rt_last;
pField->Volume = st.rt_vol;
//pField->Turnover = pDepthMarketData->Turnover;
pField->OpenInterest = st.rt_oi;
//pField->AveragePrice = pDepthMarketData->AveragePrice;
InitBidAsk(pField);
do
{
if (st.rt_bsize1 == 0)
break;
AddBid(pField, st.rt_bid1, st.rt_bsize1, 0);
} while (false);
do
{
if (st.rt_asize1 == 0)
break;
AddAsk(pField, st.rt_ask1, st.rt_asize1, 0);
} while (false);
m_msgQueue->Input_NoCopy(ResponseType::ResponseType_OnRtnDepthMarketData, m_msgQueue, m_pClass, 0, 0, pField, pField->Size, nullptr, 0, nullptr, 0);
}
//if (timeLength > 1)
//{
// if (fieldsLength > 1)
// {
// likeWsdList1(wd);
// }
// else //fieldsLength <= 1
// {
// if (codeLength >= 1)
// {
// likeWsdList2(wd);
// }
// }
//}
//else if (codeLength > 1)//timeLength <= 1
//{
// if (fieldsLength > 1)
// {
// likeWssList(wd);
// }
// else
// {
// likeWsdList2(wd);
// }
//}
//else //timeLength <= 1 && codeLength <=1
//{
// likeWssList(wd);
//}
//printf("==================");
return 0;
}
void CMdUserApi::Subscribe(const string& szInstrumentIDs, const string& szExchangeID)
{
ULONGLONG reqId = 0;
//WCHAR options[1024];
//_snwprintf_s(options, sizeof options, _TRUNCATE, L"date=%d;sectorId=a001010100000000;field=wind_code,sec_name", Today(0));
std::wstring wstr(szInstrumentIDs.length(), L'\0');
std::copy(szInstrumentIDs.begin(), szInstrumentIDs.end(), wstr.begin());
auto errCode = CWAPIWrapperCpp::wsq(reqId, wstr.c_str(), L"rt_date,rt_time,rt_last,rt_vol,rt_oi,rt_bid1,rt_ask1,rt_bsize1,rt_asize1", WindCallback, L"", TRUE);
if (errCode != 0) {
ErrorField* pField = (ErrorField*)m_msgQueue->new_block(sizeof(ErrorField));
pField->RawErrorID = errCode;
strcpy_s(pField->Source, sizeof pField->Source, "wsq");
WindGetErrorMessage(errCode, pField->Text, sizeof pField->Text);
m_msgQueue->Input_NoCopy(ResponseType::ResponseType_OnRtnError, m_msgQueue, m_pClass, 0, 0, pField, sizeof(ErrorField), nullptr, 0, nullptr, 0);
return;
}
// 对订阅的合约与请求号进行分类
m_map_code_reqId[szInstrumentIDs] = reqId;
}
void CMdUserApi::Subscribe(const set<string>& instrumentIDs, const string& szExchangeID)
{
}
void CMdUserApi::Unsubscribe(const string& szInstrumentIDs, const string& szExchangeID)
{
// 通过合约来查找请求号,然后再判断是否要再订阅
map<string, ULONGLONG>::iterator it = m_map_code_reqId.find(szInstrumentIDs);
if (it == m_map_code_reqId.end())
{
}
else
{
ULONGLONG reqId = it->second;
CWAPIWrapperCpp::cancelRequest(reqId);
}
}