forked from corefan/TdxTradeServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtts_tradeapi.cpp
More file actions
191 lines (166 loc) · 6.69 KB
/
tts_tradeapi.cpp
File metadata and controls
191 lines (166 loc) · 6.69 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
#include "tts_tradeapi.h"
#include "Windows.h"
#include <QDebug>
#include "json.hpp"
#include "tts_common.h"
#include "tts_dll.h"
#include "tts_activeclients.h"
#include <QTextCodec>
using namespace std;
using json = nlohmann::json;
#define INVALID_CLIENT_ID "无效的clientId或者Trade.DLL无效"
TTS_TradeApi::TTS_TradeApi(const TTS_SettingObject& so):outputUtf8(true),_so(so)
{
}
TTS_TradeApi::~TTS_TradeApi()
{
}
void TTS_TradeApi::setOutputUtf8(bool utf8) {
outputUtf8 = utf8;
auto dlls = TTS_Dll::allDlls();
for (auto& it = dlls.begin(); it != dlls.end(); it++) {
std::shared_ptr<TTS_Dll> dll = it->second;
dll->setOutputUtf8(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) {
std::shared_ptr<TTS_Dll> dll = TTS_Dll::getInstance(_so, accountNo);
json j = dll->logon(ip, port, version, yybId, accountNo, tradeAccount, jyPassword, txPassword);
if (j[TTS_SUCCESS].get<bool>() == true) {
uint32_t clientId = j[TTS_DATA]["client_id"].get<uint32_t>();
uint32_t sessionId = genSessionId(dll->getSeq(), clientId);
j[TTS_DATA]["client_id"] = sessionId;
// 记录登陆行为
TTS_ActiveClients::ins()->addNewEntry(
sessionId,
clientId,
ip,
port,
version,
yybId,
accountNo,
tradeAccount
);
}
return j;
}
/**
* @brief TTS_TradeApi::logoff 登出
* @param clientId cilent_id
* @return success => true/false
*/
json TTS_TradeApi::logoff(int clientId) {
int loginId = getLoginIdBySessionId(clientId);
int seq = getSeqBySessionId(clientId);
std::shared_ptr<TTS_Dll> dll = TTS_Dll::getInstance(_so, seq);
json j = dll->logoff(loginId);
// 如果登出成功
if (j["success"].get<bool>()) {
TTS_ActiveClients::ins()->removeEntryBySessionId(clientId);
}
return (dll == nullptr) ? jsonError(INVALID_CLIENT_ID): j;
}
/**
* @brief TTS_TradeApi::queryData 查询信息
* @param clientId client_id
* @param Category 信息类别
* @return [{}, {}, {} ]
*/
json TTS_TradeApi::queryData(int clientId, int category) {
int loginId = getLoginIdBySessionId(clientId);
int seq = getSeqBySessionId(clientId);
std::shared_ptr<TTS_Dll> dll = TTS_Dll::getInstance(_so, seq);
json j = dll->queryData(loginId, category);
return (dll == nullptr) ? jsonError(INVALID_CLIENT_ID): j;
}
json TTS_TradeApi::sendOrder(int clientId, int category ,int priceType, const char* gddm, const char* zqdm , float price, int quantity) {
int loginId = getLoginIdBySessionId(clientId);
int seq = getSeqBySessionId(clientId);
std::shared_ptr<TTS_Dll> dll = TTS_Dll::getInstance(_so, seq);
json j = dll->sendOrder(loginId, category, priceType, gddm, zqdm, price, quantity);
return (dll == nullptr) ? jsonError(INVALID_CLIENT_ID): j;
}
json TTS_TradeApi::cancelOrder(int clientId, const char *exchangeID, const char *hth) {
int loginId = getLoginIdBySessionId(clientId);
int seq = getSeqBySessionId(clientId);
std::shared_ptr<TTS_Dll> dll = TTS_Dll::getInstance(_so, seq);
json j = dll->cancelOrder(loginId, exchangeID, hth);
return (dll == nullptr) ? jsonError(INVALID_CLIENT_ID): j;
}
json TTS_TradeApi::getQuote(int clientId, const char *zqdm) {
int loginId = getLoginIdBySessionId(clientId);
int seq = getSeqBySessionId(clientId);
std::shared_ptr<TTS_Dll> dll = TTS_Dll::getInstance(_so, seq);
json j = dll->getQuote(loginId, zqdm);
return (dll == nullptr) ? jsonError(INVALID_CLIENT_ID): j;
}
json TTS_TradeApi::repay(int clientId, const char *amount) {
int loginId = getLoginIdBySessionId(clientId);
int seq = getSeqBySessionId(clientId);
std::shared_ptr<TTS_Dll> dll = TTS_Dll::getInstance(_so, seq);
json j = dll->repay(loginId, amount);
return (dll == nullptr) ? jsonError(INVALID_CLIENT_ID): j;
}
json TTS_TradeApi::queryHistoryData(int clientId, int category, const char* beginDate, const char* endDate) {
int loginId = getLoginIdBySessionId(clientId);
int seq = getSeqBySessionId(clientId);
std::shared_ptr<TTS_Dll> dll = TTS_Dll::getInstance(_so, seq);
json j = dll->queryHistoryData(loginId, category, beginDate, endDate);
return (dll == nullptr) ? jsonError(INVALID_CLIENT_ID): j;
}
json TTS_TradeApi::queryDatas(int clientId, int categories[], int count) {
int loginId = getLoginIdBySessionId(clientId);
int seq = getSeqBySessionId(clientId);
std::shared_ptr<TTS_Dll> dll = TTS_Dll::getInstance(_so, seq);
json j = dll->queryDatas(loginId, categories, count);
return (dll == nullptr) ? jsonError(INVALID_CLIENT_ID): j;
}
json TTS_TradeApi::sendOrders(int clientId, int categories[], int priceTypes[], const char *gddms[], const char *zqdms[], float prices[], int quantities[], int count) {
int loginId = getLoginIdBySessionId(clientId);
int seq = getSeqBySessionId(clientId);
std::shared_ptr<TTS_Dll> dll = TTS_Dll::getInstance(_so, seq);
json j = dll->sendOrders(loginId, categories, priceTypes, gddms, zqdms, prices, quantities, count);
return (dll == nullptr) ? jsonError(INVALID_CLIENT_ID): j;
}
json TTS_TradeApi::cancelOrders(int clientId, const char *exchangeIds[], const char *hths[], int count) {
int loginId = getLoginIdBySessionId(clientId);
int seq = getSeqBySessionId(clientId);
std::shared_ptr<TTS_Dll> dll = TTS_Dll::getInstance(_so, seq);
json j = dll->cancelOrders(loginId, exchangeIds, hths, count);
return (dll == nullptr) ? jsonError(INVALID_CLIENT_ID): j;
}
json TTS_TradeApi::getQuotes(int clientId, const char *zqdms[], int count) {
int loginId = getLoginIdBySessionId(clientId);
int seq = getSeqBySessionId(clientId);
std::shared_ptr<TTS_Dll> dll = TTS_Dll::getInstance(_so, seq);
json j = dll->getQuotes(loginId, zqdms, count);
return (dll == nullptr) ? jsonError(INVALID_CLIENT_ID): j;
}
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;
}