-
Notifications
You must be signed in to change notification settings - Fork 260
/
Copy pathtts_tradeapi.cpp
211 lines (180 loc) · 5.75 KB
/
tts_tradeapi.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
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
#include "tts_tradeapi.h"
#include "Windows.h"
#include <QDebug>
#include "json.hpp"
#include "tts_common.h"
#include <QTextCodec>
using namespace std;
using json = nlohmann::json;
TTS_TradeApi::TTS_TradeApi(const QString& dllFilePath)
{
outputUtf8 = true;
string strDllFilePath = dllFilePath.toStdString();
hDLL = LoadLibraryA(strDllFilePath.c_str());
if (0 == hDLL) {
qInfo() << "Load dll failed";
exit(-1);
}
// load functions
lpOpenTdx = (LPFN_OPENTDX) GetProcAddress(hDLL, "OpenTdx");
lpCloseTdx = (LPFN_CLOSETDX) GetProcAddress(hDLL, "CloseTdx");
lpLogon = (LPFN_LOGON) GetProcAddress(hDLL, "Logon");
lpLogoff = (LPFN_LOGOFF) GetProcAddress(hDLL, "Logoff");
lpQueryData = (LPFN_QUERYDATA) GetProcAddress(hDLL, "QueryData");
lpSendOrder = (LPFN_SENDORDER) GetProcAddress(hDLL, "SendOrder");
lpCancelOrder = (LPFN_CANCELORDER) GetProcAddress(hDLL, "CancelOrder");
lpGetQuote = (LPFN_GETQUOTE) GetProcAddress(hDLL, "GetQuote");
lpRepay = (LPFN_REPAY) GetProcAddress(hDLL, "Repay");
// end load functioins
// initialize tdx
lpOpenTdx();
errout = new char[1024];
result = new char[1024 * 1024];
// 这里强制设定本地编码为gbk
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB18030"));
}
TTS_TradeApi::~TTS_TradeApi()
{
// close tdx
lpCloseTdx();
if (hDLL) {
FreeLibrary(hDLL);
}
delete[] errout;
delete[] result;
}
void TTS_TradeApi::setOutputUtf8(bool utf8) {
outputUtf8 = utf8;
}
/**
* @brief TTS_TradeApi::logon 登陆到服务器
* @param IP 服务器ip地址
* @param Port 端口号
* @param Version 客户端版本
* @param YybID 营业部id
* @param AccountNo 帐号
* @param TradeAccount 交易帐号
* @param JyPassword 交易密码
* @param TxPassword 通讯密码
* @return data -> { "client_id" : xxx }
*
*/
json TTS_TradeApi::logon(const char* IP, const short Port,
const char* Version, short YybID,
const char* AccountNo, const char* TradeAccount,
const char* JyPassword, const char* TxPassword) {
json j;
int ret = lpLogon(IP, Port, Version, YybID, AccountNo, TradeAccount, JyPassword, TxPassword, errout);
if (ret == -1) {
j[TTS_SUCCESS] = false;
setupErrForJson(errout, j);
return j;
} else {
j[TTS_SUCCESS] = true;
j[TTS_DATA]["client_id"] = ret;
}
return j;
}
/**
* @brief TTS_TradeApi::logoff 登出
* @param ClientID cilent_id
* @return success => true/false
*/
json TTS_TradeApi::logoff(int ClientID) {
lpLogoff(ClientID);
json j;
j[TTS_SUCCESS] = true;
return j;
}
/**
* @brief TTS_TradeApi::queryData 查询信息
* @param ClientID client_id
* @param Category 信息类别
* @return [{}, {}, {} ]
*/
json TTS_TradeApi::queryData(int ClientID, int Category) {
lpQueryData(ClientID, Category, result, errout);
return convertTableToJSON(result, errout);
}
json TTS_TradeApi::sendOrder(int ClientID, int Category ,int PriceType, const char* Gddm, const char* Zqdm , float Price, int Quantity) {
lpSendOrder(ClientID, Category, PriceType, Gddm, Zqdm, Price, Quantity, result, errout);
return convertTableToJSON(result, errout);
}
json TTS_TradeApi::cancelOrder(int ClientID, const char *ExchangeID, const char *hth) {
lpCancelOrder(ClientID, ExchangeID, hth, result, errout);
return convertTableToJSON(result, errout);
}
json TTS_TradeApi::getQuote(int ClientID, const char *Zqdm) {
lpGetQuote(ClientID, Zqdm, result, errout);
return convertTableToJSON(result, errout);
}
json TTS_TradeApi::repay(int ClientID, const char *Amount) {
lpRepay(ClientID, Amount, result, errout);
return convertTableToJSON(result, errout);
}
void TTS_TradeApi::setupErrForJson(const char* errout, json& resultJSON)
{
if (outputUtf8) {
QString qErrout = QString::fromLocal8Bit(errout);
string utf8Errout = qErrout.toUtf8().constData();
resultJSON[TTS_ERROR] = utf8Errout;
} else {
resultJSON[TTS_ERROR] = errout;
}
}
/**
* @brief TTS_TradeApi::convertTableToJSON 将\n分割行\t分割字符的类似 csv格式的信息转换为json格式
* @param result
* @return json结构的 [{line1}, {line2} ... ] 信息
*/
json TTS_TradeApi::convertTableToJSON(const char *result, const char* errout) {
json resultJSON;
if (result[0] == 0) {
resultJSON[TTS_SUCCESS] = false;
setupErrForJson(errout, resultJSON);
return resultJSON;
}
json j;
j = json::array();
QString strResult = QString::fromLocal8Bit(result);
// qInfo() << strResult;
QStringList sl = strResult.split("\n");
if (sl.length() > 1) {
QString head = sl[0];
QStringList hlist = head.split("\t");
int line = 0;
for (QString row : sl) {
line++;
if (line==1) continue;
QStringList rowlist = row.split("\t");
json oneRecord = json({});
for(int i = 0; i < hlist.length(); i++) {
string key, value;
if (outputUtf8) {
key = hlist[i].toUtf8();
value = rowlist[i].toUtf8();
} else {
key = hlist[i].toLocal8Bit();
value = rowlist[i].toLocal8Bit();
}
oneRecord[key]= value;
}
j.push_back(oneRecord);
}
}
resultJSON[TTS_SUCCESS] = true;
resultJSON[TTS_DATA] = j;
return resultJSON;
}
json TTS_TradeApi::jsonError(QString str) {
string value;
if (outputUtf8) {
value = str.toUtf8();
} else {
value = str.toLocal8Bit();
}
json j;
j[TTS_SUCCESS] = false;
j[TTS_ERROR] = value;
return j;
}