-
Notifications
You must be signed in to change notification settings - Fork 60
/
CtpCommand.h
241 lines (223 loc) · 6.12 KB
/
CtpCommand.h
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
#pragma once
#include <qthread.h>
#include <qtimer.h>
#include <memory>
#include <thread>
#include <chrono>
#include "cpp_basictool/CppQueue.hpp"
#include "ctpapi/ThostFtdcTraderApi.h"
//封装ctp命令的接口
class CtpCommand
{
public:
virtual ~CtpCommand()
{
api = nullptr;
}
virtual int execute() = 0;
protected:
CtpCommand::CtpCommand(int& requestID, CThostFtdcTraderApi *api)
:requestID(requestID)
{
this->api = api;
requestID++;
}
int& requestID;
CThostFtdcTraderApi* api;
};
/*登录*/
class LoginCommand :public CtpCommand
{
public:
LoginCommand::LoginCommand(CThostFtdcTraderApi *api, CThostFtdcReqUserLoginField& loginField, int &requestID)
:CtpCommand(requestID, api)
{
memcpy_s(&(this->loginField), sizeof(CThostFtdcReqUserLoginField), &loginField, sizeof(CThostFtdcReqUserLoginField));
}
int LoginCommand::execute()
{
return api->ReqUserLogin(&loginField, requestID);
}
private:
CThostFtdcReqUserLoginField loginField;
};
/*登出*/
class LogoutCommand : public CtpCommand
{
public:
LogoutCommand::LogoutCommand(CThostFtdcTraderApi *api, CThostFtdcUserLogoutField& logoutField, int &requestID)
: CtpCommand(requestID, api)
{
memcpy_s(&(this->logoutField), sizeof(CThostFtdcUserLogoutField), &logoutField, sizeof(CThostFtdcUserLogoutField));
}
int LogoutCommand::execute()
{
return api->ReqUserLogout(&logoutField, requestID);
}
private:
CThostFtdcUserLogoutField logoutField;
};
/*确认结算结果*/
class ComfirmSettlementCommand :public CtpCommand
{
public:
ComfirmSettlementCommand::ComfirmSettlementCommand(CThostFtdcTraderApi *api, CThostFtdcSettlementInfoConfirmField& comfirmField, int &requestID)
:CtpCommand(requestID, api)
{
memcpy_s(&(this->comfirmField), sizeof(CThostFtdcSettlementInfoConfirmField), &comfirmField, sizeof(CThostFtdcSettlementInfoConfirmField));
}
int ComfirmSettlementCommand::execute()
{
return api->ReqSettlementInfoConfirm(&comfirmField, requestID);
}
private:
CThostFtdcSettlementInfoConfirmField comfirmField;
};
/*查询资金*/
class QueryFundCommand :public CtpCommand
{
public:
QueryFundCommand::QueryFundCommand(CThostFtdcTraderApi *api, CThostFtdcQryTradingAccountField& accountField,int &requestID)
:CtpCommand(requestID, api)
{
memcpy_s(&(this->accountField), sizeof(CThostFtdcQryTradingAccountField), &accountField, sizeof(CThostFtdcQryTradingAccountField));
}
int QueryFundCommand::execute()
{
return api->ReqQryTradingAccount(&accountField, requestID);
}
private:
CThostFtdcQryTradingAccountField accountField;
};
/*查询持仓*/
class QueryPositionCommand :public CtpCommand
{
public:
QueryPositionCommand::QueryPositionCommand(CThostFtdcTraderApi *api, CThostFtdcQryInvestorPositionField& accountField,int &requestID)
:CtpCommand(requestID, api)
{
memcpy_s(&(this->accountField), sizeof(CThostFtdcQryInvestorPositionField), &accountField, sizeof(CThostFtdcQryInvestorPositionField));
}
int QueryPositionCommand::execute()
{
return api->ReqQryInvestorPosition(&accountField, requestID);
}
private:
CThostFtdcQryInvestorPositionField accountField;
};
/*报单请求*/
class InsertOrderCommand :public CtpCommand
{
public:
InsertOrderCommand::InsertOrderCommand(CThostFtdcTraderApi *api, CThostFtdcInputOrderField& orderField,int &requestID)
:CtpCommand(requestID, api)
{
memcpy_s(&(this->orderField), sizeof(CThostFtdcInputOrderField), &orderField, sizeof(CThostFtdcInputOrderField));
}
int InsertOrderCommand::execute()
{
return api->ReqOrderInsert(&orderField, requestID);
}
private:
CThostFtdcInputOrderField orderField;
};
/*撤单请求*/
class WithdrawOrderCommand :public CtpCommand
{
public:
WithdrawOrderCommand::WithdrawOrderCommand(CThostFtdcTraderApi *api, CThostFtdcInputOrderActionField& orderField,int &requestID)
:CtpCommand(requestID, api)
{
memcpy_s(&(this->orderField), sizeof(CThostFtdcInputOrderActionField), &orderField, sizeof(CThostFtdcInputOrderActionField));
}
int WithdrawOrderCommand::execute()
{
return api->ReqOrderAction(&orderField, requestID);
}
private:
CThostFtdcInputOrderActionField orderField;
};
/*合约费率查询请求*/
class InstrumentCommissionCommand :public CtpCommand
{
public:
InstrumentCommissionCommand::InstrumentCommissionCommand(CThostFtdcTraderApi *api, CThostFtdcQryInstrumentCommissionRateField& comField, int &requestID)
:CtpCommand(requestID, api)
{
memcpy_s(&(this->comField), sizeof(CThostFtdcQryInstrumentCommissionRateField), &comField, sizeof(CThostFtdcQryInstrumentCommissionRateField));
}
int InstrumentCommissionCommand::execute()
{
return api->ReqQryInstrumentCommissionRate(&comField, requestID);
}
private:
CThostFtdcQryInstrumentCommissionRateField comField;
};
//指令队列
class CommandQueue :public QThread
{
Q_OBJECT
public:
CommandQueue(int time) :sleep_time(time) {}
~CommandQueue() = default;
void cmd_begin()
{
// 开启队列开关
cmd_active.store(true);
// 启动线程
thread_active.store(true);
start();
}
void cmd_stop()
{
// 关闭队列开关,并等待任务执行完毕
cmd_active.store(false);
while (!commandQueue.empty())
{
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_time));
}
// 退出线程
thread_active.store(false);
commandQueue.notify_all(); // 通知等待在空队列上的线程
quit();
wait();
}
void CommandQueue::addCommand(std::shared_ptr<CtpCommand> newCommand)
{
if (cmd_active.load())
{
commandQueue.push_back(newCommand);
}
}
private:
ConcurrentQueue<std::shared_ptr<CtpCommand>> commandQueue;
int sleep_time{ 0 };
std::atomic<bool> thread_active{ false };
std::atomic<bool> cmd_active{ false };
private:
void CommandQueue::cut_in_command(std::shared_ptr<CtpCommand> newCommand)
{
if (cmd_active.load())
{
commandQueue.push_front(newCommand);
}
}
void CommandQueue::run()
{
while (thread_active.load())
{
// 获取任务
std::shared_ptr<CtpCommand> command;
if (commandQueue.wait_and_pop(command, -1))
{
if (command->execute())
{
// 调试很长时间导致断线重连后,点击退出按钮,发现查询队列数根本下不去,导致程序始终无法正常退出关闭,后续要查一下是什么原因。
cut_in_command(command);
}
// 发送指令成功,休息一秒(CTP要求)
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_time));
}
}
}
};