kaq-quant-common 0.1.81__py3-none-any.whl → 0.1.83__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kaq_quant_common/api/common/__init__.py +1 -1
- kaq_quant_common/api/common/api_interface.py +38 -38
- kaq_quant_common/api/rest/api_client_base.py +42 -42
- kaq_quant_common/api/rest/instruction/helper/order_helper.py +324 -315
- kaq_quant_common/api/rest/instruction/instruction_client.py +88 -88
- kaq_quant_common/api/rest/instruction/instruction_server_base.py +132 -132
- kaq_quant_common/api/rest/instruction/models/__init__.py +17 -17
- kaq_quant_common/api/rest/instruction/models/account.py +24 -22
- kaq_quant_common/api/rest/instruction/models/order.py +223 -220
- kaq_quant_common/api/rest/instruction/models/position.py +56 -56
- kaq_quant_common/api/rest/instruction/models/transfer.py +32 -32
- kaq_quant_common/api/ws/exchange/models.py +23 -23
- kaq_quant_common/api/ws/exchange/ws_exchange_server.py +440 -440
- kaq_quant_common/common/ddb_table_monitor.py +106 -106
- kaq_quant_common/common/http_monitor.py +69 -69
- kaq_quant_common/common/modules/limit_order_helper.py +81 -81
- kaq_quant_common/common/modules/limit_order_symbol_monitor.py +76 -76
- kaq_quant_common/common/modules/limit_order_symbol_monitor_group.py +69 -69
- kaq_quant_common/common/monitor_base.py +84 -84
- kaq_quant_common/common/monitor_group.py +97 -97
- kaq_quant_common/common/statistics/kline_history_statistics.py +188 -0
- kaq_quant_common/common/ws_wrapper.py +21 -21
- kaq_quant_common/resources/kaq_ddb_pool_stream_read_resources.py +5 -25
- kaq_quant_common/utils/logger_utils.py +5 -5
- kaq_quant_common/utils/signal_utils.py +23 -23
- kaq_quant_common/utils/uuid_utils.py +5 -5
- {kaq_quant_common-0.1.81.dist-info → kaq_quant_common-0.1.83.dist-info}/METADATA +1 -1
- {kaq_quant_common-0.1.81.dist-info → kaq_quant_common-0.1.83.dist-info}/RECORD +29 -28
- {kaq_quant_common-0.1.81.dist-info → kaq_quant_common-0.1.83.dist-info}/WHEEL +1 -1
|
@@ -1,315 +1,324 @@
|
|
|
1
|
-
import json
|
|
2
|
-
import threading
|
|
3
|
-
import time
|
|
4
|
-
from typing import Callable
|
|
5
|
-
|
|
6
|
-
from kaq_quant_common.api.rest.instruction.models.order import (
|
|
7
|
-
OrderInfo,
|
|
8
|
-
OrderSide,
|
|
9
|
-
OrderStatus,
|
|
10
|
-
PositionStatus,
|
|
11
|
-
)
|
|
12
|
-
from kaq_quant_common.api.rest.instruction.models.position import PositionSide
|
|
13
|
-
from kaq_quant_common.utils import logger_utils, uuid_utils
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
class OrderHelper:
|
|
17
|
-
def __init__(self, ins_server):
|
|
18
|
-
# 必须放在这里 延迟引入,否则会有循环引用问题
|
|
19
|
-
from kaq_quant_common.api.rest.instruction.instruction_server_base import (
|
|
20
|
-
InstructionServerBase,
|
|
21
|
-
)
|
|
22
|
-
|
|
23
|
-
self._server: InstructionServerBase = ins_server
|
|
24
|
-
self._logger = logger_utils.get_logger(self)
|
|
25
|
-
|
|
26
|
-
self._mysql_table_name_order = "kaq_futures_instruction_order"
|
|
27
|
-
self._mysql_table_name_position = "kaq_futures_instruction_position"
|
|
28
|
-
# 当前持仓
|
|
29
|
-
self._redis_key_position = "kaq_futures_instruction_position"
|
|
30
|
-
# 持仓历史
|
|
31
|
-
self._redis_key_position_history = "kaq_futures_instruction_position_history"
|
|
32
|
-
|
|
33
|
-
def _write_position_open_to_redis(
|
|
34
|
-
self,
|
|
35
|
-
position_id: str,
|
|
36
|
-
exchange: str,
|
|
37
|
-
symbol: str,
|
|
38
|
-
position_side,
|
|
39
|
-
coin_quantity: float,
|
|
40
|
-
usdt_quantity: float,
|
|
41
|
-
open_ins_id: str,
|
|
42
|
-
open_price: float,
|
|
43
|
-
open_fee: float,
|
|
44
|
-
open_fee_rate: float,
|
|
45
|
-
open_time: int,
|
|
46
|
-
):
|
|
47
|
-
redis = self._server._redis
|
|
48
|
-
if redis is None:
|
|
49
|
-
return
|
|
50
|
-
data = {
|
|
51
|
-
"id": position_id,
|
|
52
|
-
"exchange": exchange,
|
|
53
|
-
"symbol": symbol,
|
|
54
|
-
"position_side": position_side.value,
|
|
55
|
-
"coin_quantity": coin_quantity,
|
|
56
|
-
"usdt_quantity": usdt_quantity,
|
|
57
|
-
"open_ins_id": open_ins_id,
|
|
58
|
-
"open_price": open_price,
|
|
59
|
-
"open_fee": open_fee,
|
|
60
|
-
"open_fee_rate": open_fee_rate,
|
|
61
|
-
"open_time": open_time,
|
|
62
|
-
"close_ins_id": None,
|
|
63
|
-
"close_price": 0,
|
|
64
|
-
"close_time": 0,
|
|
65
|
-
"status": PositionStatus.OPEN.value,
|
|
66
|
-
}
|
|
67
|
-
redis.client.hset(self._redis_key_position, position_id, json.dumps(data))
|
|
68
|
-
|
|
69
|
-
def _write_position_close_to_redis(
|
|
70
|
-
self,
|
|
71
|
-
position_id: str,
|
|
72
|
-
exchange: str,
|
|
73
|
-
symbol: str,
|
|
74
|
-
position_side,
|
|
75
|
-
coin_quantity: float,
|
|
76
|
-
usdt_quantity: float,
|
|
77
|
-
open_ins_id: str,
|
|
78
|
-
open_price: float,
|
|
79
|
-
open_fee: float,
|
|
80
|
-
open_fee_rate: float,
|
|
81
|
-
open_time: int,
|
|
82
|
-
close_ins_id: str,
|
|
83
|
-
close_price: float,
|
|
84
|
-
close_fee: float,
|
|
85
|
-
close_fee_rate: float,
|
|
86
|
-
close_time: int,
|
|
87
|
-
):
|
|
88
|
-
redis = self._server._redis
|
|
89
|
-
if redis is None:
|
|
90
|
-
return
|
|
91
|
-
data = {
|
|
92
|
-
"id": position_id,
|
|
93
|
-
"exchange": exchange,
|
|
94
|
-
"symbol": symbol,
|
|
95
|
-
"position_side": position_side.value,
|
|
96
|
-
"coin_quantity": coin_quantity,
|
|
97
|
-
"usdt_quantity": usdt_quantity,
|
|
98
|
-
"open_ins_id": open_ins_id,
|
|
99
|
-
"open_price": open_price,
|
|
100
|
-
"open_fee": open_fee,
|
|
101
|
-
"open_fee_rate": open_fee_rate,
|
|
102
|
-
"open_time": open_time,
|
|
103
|
-
"close_ins_id": close_ins_id,
|
|
104
|
-
"close_price": close_price,
|
|
105
|
-
"close_fee": close_fee,
|
|
106
|
-
"close_fee_rate": close_fee_rate,
|
|
107
|
-
"close_time": close_time,
|
|
108
|
-
"status": PositionStatus.CLOSE.value,
|
|
109
|
-
}
|
|
110
|
-
redis.client.hdel(self._redis_key_position, position_id)
|
|
111
|
-
redis.client.rpush(self._redis_key_position_history, json.dumps(data))
|
|
112
|
-
|
|
113
|
-
def process_order(self, order: OrderInfo, get_order_result: Callable):
|
|
114
|
-
# 获取交易所
|
|
115
|
-
exchange = self._server._exchange
|
|
116
|
-
|
|
117
|
-
# 记录时间,统计成交耗时
|
|
118
|
-
start_time = time.time()
|
|
119
|
-
|
|
120
|
-
#
|
|
121
|
-
if not self._do_process_order(exchange, order, get_order_result, True, start_time):
|
|
122
|
-
# 马上执行,没有成功,开启线程执行
|
|
123
|
-
thread = threading.Thread(
|
|
124
|
-
target=self._do_process_order,
|
|
125
|
-
args=(exchange, order, get_order_result, False, start_time),
|
|
126
|
-
)
|
|
127
|
-
thread.name = f"process_order_{order.instruction_id}_{exchange}_{order.symbol}_{order.order_id}"
|
|
128
|
-
thread.daemon = True
|
|
129
|
-
thread.start()
|
|
130
|
-
|
|
131
|
-
def _do_process_order(
|
|
132
|
-
self,
|
|
133
|
-
exchange: str,
|
|
134
|
-
order: OrderInfo,
|
|
135
|
-
get_order_result: Callable,
|
|
136
|
-
first=True,
|
|
137
|
-
start_time: float = 0,
|
|
138
|
-
):
|
|
139
|
-
# 获取mysql
|
|
140
|
-
mysql = self._server._mysql
|
|
141
|
-
|
|
142
|
-
#
|
|
143
|
-
ins_id = order.instruction_id
|
|
144
|
-
order_id = order.order_id
|
|
145
|
-
symbol = order.symbol
|
|
146
|
-
side = order.side
|
|
147
|
-
position_side = order.position_side
|
|
148
|
-
|
|
149
|
-
is_open = True
|
|
150
|
-
side_str = "开仓"
|
|
151
|
-
if position_side == PositionSide.LONG:
|
|
152
|
-
# 多单是正向理解的
|
|
153
|
-
if side == OrderSide.SELL:
|
|
154
|
-
side_str = "平仓"
|
|
155
|
-
is_open = False
|
|
156
|
-
else:
|
|
157
|
-
side_str = "开仓"
|
|
158
|
-
is_open = True
|
|
159
|
-
else:
|
|
160
|
-
# 空单是反向理解的
|
|
161
|
-
if side == OrderSide.SELL:
|
|
162
|
-
side_str = "开仓"
|
|
163
|
-
is_open = True
|
|
164
|
-
else:
|
|
165
|
-
side_str = "平仓"
|
|
166
|
-
is_open = False
|
|
167
|
-
|
|
168
|
-
if first:
|
|
169
|
-
self._logger.info(f"{ins_id}_{exchange}_{symbol} step 1. {side_str}挂单成功 {order_id}")
|
|
170
|
-
|
|
171
|
-
# 步骤1.挂单成功 插入到订单记录
|
|
172
|
-
# 获取当前时间-ms
|
|
173
|
-
current_time = int(time.time() * 1000)
|
|
174
|
-
|
|
175
|
-
if mysql is not None:
|
|
176
|
-
status = OrderStatus.CREATE
|
|
177
|
-
sql = f"""
|
|
178
|
-
INSERT INTO {self._mysql_table_name_order} (ins_id, exchange, symbol, side, position_side, orig_price, orig_coin_quantity, order_id, status, create_time, last_update_time)
|
|
179
|
-
VALUES ( '{ins_id}', '{exchange}', '{symbol}', '{side.value}', '{order.position_side.value}', {order.target_price}, {order.quantity}, '{order_id}', '{status.value}', {current_time}, {current_time} );
|
|
180
|
-
"""
|
|
181
|
-
execute_ret = mysql.execute_sql(sql, True)
|
|
182
|
-
|
|
183
|
-
# 步骤2.查询订单状态 直到订单成交后
|
|
184
|
-
# 统计查询次数
|
|
185
|
-
query_counter = 0
|
|
186
|
-
while True:
|
|
187
|
-
query_counter += 1
|
|
188
|
-
# 获取订单结果
|
|
189
|
-
order_info = None
|
|
190
|
-
try:
|
|
191
|
-
order_info = get_order_result()
|
|
192
|
-
except Exception as e:
|
|
193
|
-
self._logger.error(f"{ins_id}_{exchange}_{symbol} step 2. {side_str}订单 查询状态失败 {e}")
|
|
194
|
-
|
|
195
|
-
if order_info is not None:
|
|
196
|
-
break
|
|
197
|
-
# 只查询一次
|
|
198
|
-
if first:
|
|
199
|
-
return False
|
|
200
|
-
# 等待
|
|
201
|
-
time.sleep(1)
|
|
202
|
-
|
|
203
|
-
if not first:
|
|
204
|
-
# 需要加上第一查询
|
|
205
|
-
query_counter += 1
|
|
206
|
-
|
|
207
|
-
# 记录时间,统计成交耗时
|
|
208
|
-
end_time = time.time()
|
|
209
|
-
cost_time = end_time - start_time
|
|
210
|
-
self._logger.info(
|
|
211
|
-
f"{ins_id}_{exchange}_{symbol} step 2. {side_str}订单 {order_id} 成交 耗时 {int(cost_time * 1000)}ms, 查询次数 {query_counter}"
|
|
212
|
-
)
|
|
213
|
-
|
|
214
|
-
# 步骤3.把最终持仓写进去
|
|
215
|
-
# 平均成交价格 转float
|
|
216
|
-
avg_price = float(order_info["avg_price"])
|
|
217
|
-
# 最终成交数量 转float
|
|
218
|
-
executed_qty = float(order_info["executed_qty"])
|
|
219
|
-
# 计算出usdt数量
|
|
220
|
-
executed_usdt = avg_price * executed_qty
|
|
221
|
-
# 手续费,不一定有
|
|
222
|
-
fee = float(order_info["fee"]) if "fee" in order_info else 0.0
|
|
223
|
-
# 费率
|
|
224
|
-
fee_rate = fee / executed_usdt
|
|
225
|
-
|
|
226
|
-
current_time = int(time.time() * 1000)
|
|
227
|
-
|
|
228
|
-
if mysql is None:
|
|
229
|
-
self._logger.warning(f"{ins_id}_{exchange}_{symbol} 仅操作,没有入库,请设置 mysql!!")
|
|
230
|
-
return
|
|
231
|
-
|
|
232
|
-
status = OrderStatus.FINISH
|
|
233
|
-
# 更新写入最终信息
|
|
234
|
-
sql = f"""
|
|
235
|
-
UPDATE {self._mysql_table_name_order}
|
|
236
|
-
SET price = {avg_price}, coin_quantity = {executed_qty}, usdt_quantity = {executed_usdt}, fee = {fee}, fee_rate = {fee_rate}, status = '{status.value}', last_update_time = {current_time}
|
|
237
|
-
WHERE ins_id = '{ins_id}' AND exchange = '{exchange}' AND symbol = '{symbol}';
|
|
238
|
-
"""
|
|
239
|
-
execute_ret = mysql.execute_sql(sql, True)
|
|
240
|
-
|
|
241
|
-
self._logger.info(
|
|
242
|
-
f"{ins_id}_{exchange}_{symbol} step 2. 订单成交 {order_id}, {side_str}价格 {avg_price}, {side_str}数量 {executed_qty}, {side_str}usdt {executed_usdt}"
|
|
243
|
-
)
|
|
244
|
-
if is_open:
|
|
245
|
-
# 同时插入持仓表
|
|
246
|
-
position_id = uuid_utils.generate_uuid()
|
|
247
|
-
sql = f"""
|
|
248
|
-
INSERT INTO {self._mysql_table_name_position} (id, exchange, symbol, position_side, coin_quantity, usdt_quantity, open_ins_id, open_price, open_fee, open_fee_rate, open_time, status)
|
|
249
|
-
VALUES ( '{position_id}', '{exchange}', '{symbol}', '{position_side.value}', '{executed_qty}', '{executed_usdt}', '{ins_id}', '{avg_price}', '{fee}', '{fee_rate}', {current_time}, '{PositionStatus.OPEN.value}' );
|
|
250
|
-
"""
|
|
251
|
-
execute_ret = mysql.execute_sql(sql, True)
|
|
252
|
-
|
|
253
|
-
self._logger.info(f"{ins_id}_{exchange}_{symbol} step 3. 创建持仓记录 {position_id}")
|
|
254
|
-
try:
|
|
255
|
-
self._write_position_open_to_redis(
|
|
256
|
-
position_id,
|
|
257
|
-
exchange,
|
|
258
|
-
symbol,
|
|
259
|
-
position_side,
|
|
260
|
-
executed_qty,
|
|
261
|
-
executed_usdt,
|
|
262
|
-
ins_id,
|
|
263
|
-
avg_price,
|
|
264
|
-
fee,
|
|
265
|
-
fee_rate,
|
|
266
|
-
current_time,
|
|
267
|
-
)
|
|
268
|
-
except:
|
|
269
|
-
pass
|
|
270
|
-
else:
|
|
271
|
-
# 需要找到对应的持仓记录
|
|
272
|
-
sql = f"""
|
|
273
|
-
SELECT * FROM {self._mysql_table_name_position}
|
|
274
|
-
WHERE exchange = '{exchange}' AND symbol = '{symbol}' AND position_side = '{position_side.value}' AND status = '{PositionStatus.OPEN.value}'
|
|
275
|
-
ORDER BY open_time ASC;
|
|
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
|
-
|
|
1
|
+
import json
|
|
2
|
+
import threading
|
|
3
|
+
import time
|
|
4
|
+
from typing import Callable
|
|
5
|
+
|
|
6
|
+
from kaq_quant_common.api.rest.instruction.models.order import (
|
|
7
|
+
OrderInfo,
|
|
8
|
+
OrderSide,
|
|
9
|
+
OrderStatus,
|
|
10
|
+
PositionStatus,
|
|
11
|
+
)
|
|
12
|
+
from kaq_quant_common.api.rest.instruction.models.position import PositionSide
|
|
13
|
+
from kaq_quant_common.utils import logger_utils, uuid_utils
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class OrderHelper:
|
|
17
|
+
def __init__(self, ins_server):
|
|
18
|
+
# 必须放在这里 延迟引入,否则会有循环引用问题
|
|
19
|
+
from kaq_quant_common.api.rest.instruction.instruction_server_base import (
|
|
20
|
+
InstructionServerBase,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
self._server: InstructionServerBase = ins_server
|
|
24
|
+
self._logger = logger_utils.get_logger(self)
|
|
25
|
+
|
|
26
|
+
self._mysql_table_name_order = "kaq_futures_instruction_order"
|
|
27
|
+
self._mysql_table_name_position = "kaq_futures_instruction_position"
|
|
28
|
+
# 当前持仓
|
|
29
|
+
self._redis_key_position = "kaq_futures_instruction_position"
|
|
30
|
+
# 持仓历史
|
|
31
|
+
self._redis_key_position_history = "kaq_futures_instruction_position_history"
|
|
32
|
+
|
|
33
|
+
def _write_position_open_to_redis(
|
|
34
|
+
self,
|
|
35
|
+
position_id: str,
|
|
36
|
+
exchange: str,
|
|
37
|
+
symbol: str,
|
|
38
|
+
position_side,
|
|
39
|
+
coin_quantity: float,
|
|
40
|
+
usdt_quantity: float,
|
|
41
|
+
open_ins_id: str,
|
|
42
|
+
open_price: float,
|
|
43
|
+
open_fee: float,
|
|
44
|
+
open_fee_rate: float,
|
|
45
|
+
open_time: int,
|
|
46
|
+
):
|
|
47
|
+
redis = self._server._redis
|
|
48
|
+
if redis is None:
|
|
49
|
+
return
|
|
50
|
+
data = {
|
|
51
|
+
"id": position_id,
|
|
52
|
+
"exchange": exchange,
|
|
53
|
+
"symbol": symbol,
|
|
54
|
+
"position_side": position_side.value,
|
|
55
|
+
"coin_quantity": coin_quantity,
|
|
56
|
+
"usdt_quantity": usdt_quantity,
|
|
57
|
+
"open_ins_id": open_ins_id,
|
|
58
|
+
"open_price": open_price,
|
|
59
|
+
"open_fee": open_fee,
|
|
60
|
+
"open_fee_rate": open_fee_rate,
|
|
61
|
+
"open_time": open_time,
|
|
62
|
+
"close_ins_id": None,
|
|
63
|
+
"close_price": 0,
|
|
64
|
+
"close_time": 0,
|
|
65
|
+
"status": PositionStatus.OPEN.value,
|
|
66
|
+
}
|
|
67
|
+
redis.client.hset(self._redis_key_position, position_id, json.dumps(data))
|
|
68
|
+
|
|
69
|
+
def _write_position_close_to_redis(
|
|
70
|
+
self,
|
|
71
|
+
position_id: str,
|
|
72
|
+
exchange: str,
|
|
73
|
+
symbol: str,
|
|
74
|
+
position_side,
|
|
75
|
+
coin_quantity: float,
|
|
76
|
+
usdt_quantity: float,
|
|
77
|
+
open_ins_id: str,
|
|
78
|
+
open_price: float,
|
|
79
|
+
open_fee: float,
|
|
80
|
+
open_fee_rate: float,
|
|
81
|
+
open_time: int,
|
|
82
|
+
close_ins_id: str,
|
|
83
|
+
close_price: float,
|
|
84
|
+
close_fee: float,
|
|
85
|
+
close_fee_rate: float,
|
|
86
|
+
close_time: int,
|
|
87
|
+
):
|
|
88
|
+
redis = self._server._redis
|
|
89
|
+
if redis is None:
|
|
90
|
+
return
|
|
91
|
+
data = {
|
|
92
|
+
"id": position_id,
|
|
93
|
+
"exchange": exchange,
|
|
94
|
+
"symbol": symbol,
|
|
95
|
+
"position_side": position_side.value,
|
|
96
|
+
"coin_quantity": coin_quantity,
|
|
97
|
+
"usdt_quantity": usdt_quantity,
|
|
98
|
+
"open_ins_id": open_ins_id,
|
|
99
|
+
"open_price": open_price,
|
|
100
|
+
"open_fee": open_fee,
|
|
101
|
+
"open_fee_rate": open_fee_rate,
|
|
102
|
+
"open_time": open_time,
|
|
103
|
+
"close_ins_id": close_ins_id,
|
|
104
|
+
"close_price": close_price,
|
|
105
|
+
"close_fee": close_fee,
|
|
106
|
+
"close_fee_rate": close_fee_rate,
|
|
107
|
+
"close_time": close_time,
|
|
108
|
+
"status": PositionStatus.CLOSE.value,
|
|
109
|
+
}
|
|
110
|
+
redis.client.hdel(self._redis_key_position, position_id)
|
|
111
|
+
redis.client.rpush(self._redis_key_position_history, json.dumps(data))
|
|
112
|
+
|
|
113
|
+
def process_order(self, order: OrderInfo, get_order_result: Callable):
|
|
114
|
+
# 获取交易所
|
|
115
|
+
exchange = self._server._exchange
|
|
116
|
+
|
|
117
|
+
# 记录时间,统计成交耗时
|
|
118
|
+
start_time = time.time()
|
|
119
|
+
|
|
120
|
+
#
|
|
121
|
+
if not self._do_process_order(exchange, order, get_order_result, True, start_time):
|
|
122
|
+
# 马上执行,没有成功,开启线程执行
|
|
123
|
+
thread = threading.Thread(
|
|
124
|
+
target=self._do_process_order,
|
|
125
|
+
args=(exchange, order, get_order_result, False, start_time),
|
|
126
|
+
)
|
|
127
|
+
thread.name = f"process_order_{order.instruction_id}_{exchange}_{order.symbol}_{order.order_id}"
|
|
128
|
+
thread.daemon = True
|
|
129
|
+
thread.start()
|
|
130
|
+
|
|
131
|
+
def _do_process_order(
|
|
132
|
+
self,
|
|
133
|
+
exchange: str,
|
|
134
|
+
order: OrderInfo,
|
|
135
|
+
get_order_result: Callable,
|
|
136
|
+
first=True,
|
|
137
|
+
start_time: float = 0,
|
|
138
|
+
):
|
|
139
|
+
# 获取mysql
|
|
140
|
+
mysql = self._server._mysql
|
|
141
|
+
|
|
142
|
+
#
|
|
143
|
+
ins_id = order.instruction_id
|
|
144
|
+
order_id = order.order_id
|
|
145
|
+
symbol = order.symbol
|
|
146
|
+
side = order.side
|
|
147
|
+
position_side = order.position_side
|
|
148
|
+
|
|
149
|
+
is_open = True
|
|
150
|
+
side_str = "开仓"
|
|
151
|
+
if position_side == PositionSide.LONG:
|
|
152
|
+
# 多单是正向理解的
|
|
153
|
+
if side == OrderSide.SELL:
|
|
154
|
+
side_str = "平仓"
|
|
155
|
+
is_open = False
|
|
156
|
+
else:
|
|
157
|
+
side_str = "开仓"
|
|
158
|
+
is_open = True
|
|
159
|
+
else:
|
|
160
|
+
# 空单是反向理解的
|
|
161
|
+
if side == OrderSide.SELL:
|
|
162
|
+
side_str = "开仓"
|
|
163
|
+
is_open = True
|
|
164
|
+
else:
|
|
165
|
+
side_str = "平仓"
|
|
166
|
+
is_open = False
|
|
167
|
+
|
|
168
|
+
if first:
|
|
169
|
+
self._logger.info(f"{ins_id}_{exchange}_{symbol} step 1. {side_str}挂单成功 {order_id}")
|
|
170
|
+
|
|
171
|
+
# 步骤1.挂单成功 插入到订单记录
|
|
172
|
+
# 获取当前时间-ms
|
|
173
|
+
current_time = int(time.time() * 1000)
|
|
174
|
+
|
|
175
|
+
if mysql is not None:
|
|
176
|
+
status = OrderStatus.CREATE
|
|
177
|
+
sql = f"""
|
|
178
|
+
INSERT INTO {self._mysql_table_name_order} (ins_id, exchange, symbol, side, position_side, orig_price, orig_coin_quantity, order_id, status, create_time, last_update_time)
|
|
179
|
+
VALUES ( '{ins_id}', '{exchange}', '{symbol}', '{side.value}', '{order.position_side.value}', {order.target_price}, {order.quantity}, '{order_id}', '{status.value}', {current_time}, {current_time} );
|
|
180
|
+
"""
|
|
181
|
+
execute_ret = mysql.execute_sql(sql, True)
|
|
182
|
+
|
|
183
|
+
# 步骤2.查询订单状态 直到订单成交后
|
|
184
|
+
# 统计查询次数
|
|
185
|
+
query_counter = 0
|
|
186
|
+
while True:
|
|
187
|
+
query_counter += 1
|
|
188
|
+
# 获取订单结果
|
|
189
|
+
order_info = None
|
|
190
|
+
try:
|
|
191
|
+
order_info = get_order_result()
|
|
192
|
+
except Exception as e:
|
|
193
|
+
self._logger.error(f"{ins_id}_{exchange}_{symbol} step 2. {side_str}订单 查询状态失败 {e}")
|
|
194
|
+
|
|
195
|
+
if order_info is not None:
|
|
196
|
+
break
|
|
197
|
+
# 只查询一次
|
|
198
|
+
if first:
|
|
199
|
+
return False
|
|
200
|
+
# 等待
|
|
201
|
+
time.sleep(1)
|
|
202
|
+
|
|
203
|
+
if not first:
|
|
204
|
+
# 需要加上第一查询
|
|
205
|
+
query_counter += 1
|
|
206
|
+
|
|
207
|
+
# 记录时间,统计成交耗时
|
|
208
|
+
end_time = time.time()
|
|
209
|
+
cost_time = end_time - start_time
|
|
210
|
+
self._logger.info(
|
|
211
|
+
f"{ins_id}_{exchange}_{symbol} step 2. {side_str}订单 {order_id} 成交 耗时 {int(cost_time * 1000)}ms, 查询次数 {query_counter}"
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
# 步骤3.把最终持仓写进去
|
|
215
|
+
# 平均成交价格 转float
|
|
216
|
+
avg_price = float(order_info["avg_price"])
|
|
217
|
+
# 最终成交数量 转float
|
|
218
|
+
executed_qty = float(order_info["executed_qty"])
|
|
219
|
+
# 计算出usdt数量
|
|
220
|
+
executed_usdt = avg_price * executed_qty
|
|
221
|
+
# 手续费,不一定有
|
|
222
|
+
fee = float(order_info["fee"]) if "fee" in order_info else 0.0
|
|
223
|
+
# 费率
|
|
224
|
+
fee_rate = fee / executed_usdt
|
|
225
|
+
|
|
226
|
+
current_time = int(time.time() * 1000)
|
|
227
|
+
|
|
228
|
+
if mysql is None:
|
|
229
|
+
self._logger.warning(f"{ins_id}_{exchange}_{symbol} 仅操作,没有入库,请设置 mysql!!")
|
|
230
|
+
return
|
|
231
|
+
|
|
232
|
+
status = OrderStatus.FINISH
|
|
233
|
+
# 更新写入最终信息
|
|
234
|
+
sql = f"""
|
|
235
|
+
UPDATE {self._mysql_table_name_order}
|
|
236
|
+
SET price = {avg_price}, coin_quantity = {executed_qty}, usdt_quantity = {executed_usdt}, fee = {fee}, fee_rate = {fee_rate}, status = '{status.value}', last_update_time = {current_time}
|
|
237
|
+
WHERE ins_id = '{ins_id}' AND exchange = '{exchange}' AND symbol = '{symbol}';
|
|
238
|
+
"""
|
|
239
|
+
execute_ret = mysql.execute_sql(sql, True)
|
|
240
|
+
|
|
241
|
+
self._logger.info(
|
|
242
|
+
f"{ins_id}_{exchange}_{symbol} step 2. 订单成交 {order_id}, {side_str}价格 {avg_price}, {side_str}数量 {executed_qty}, {side_str}usdt {executed_usdt}"
|
|
243
|
+
)
|
|
244
|
+
if is_open:
|
|
245
|
+
# 同时插入持仓表
|
|
246
|
+
position_id = uuid_utils.generate_uuid()
|
|
247
|
+
sql = f"""
|
|
248
|
+
INSERT INTO {self._mysql_table_name_position} (id, exchange, symbol, position_side, coin_quantity, usdt_quantity, open_ins_id, open_price, open_fee, open_fee_rate, open_time, status)
|
|
249
|
+
VALUES ( '{position_id}', '{exchange}', '{symbol}', '{position_side.value}', '{executed_qty}', '{executed_usdt}', '{ins_id}', '{avg_price}', '{fee}', '{fee_rate}', {current_time}, '{PositionStatus.OPEN.value}' );
|
|
250
|
+
"""
|
|
251
|
+
execute_ret = mysql.execute_sql(sql, True)
|
|
252
|
+
|
|
253
|
+
self._logger.info(f"{ins_id}_{exchange}_{symbol} step 3. 创建持仓记录 {position_id}")
|
|
254
|
+
try:
|
|
255
|
+
self._write_position_open_to_redis(
|
|
256
|
+
position_id,
|
|
257
|
+
exchange,
|
|
258
|
+
symbol,
|
|
259
|
+
position_side,
|
|
260
|
+
executed_qty,
|
|
261
|
+
executed_usdt,
|
|
262
|
+
ins_id,
|
|
263
|
+
avg_price,
|
|
264
|
+
fee,
|
|
265
|
+
fee_rate,
|
|
266
|
+
current_time,
|
|
267
|
+
)
|
|
268
|
+
except:
|
|
269
|
+
pass
|
|
270
|
+
else:
|
|
271
|
+
# 需要找到对应的持仓记录
|
|
272
|
+
sql = f"""
|
|
273
|
+
SELECT * FROM {self._mysql_table_name_position}
|
|
274
|
+
WHERE exchange = '{exchange}' AND symbol = '{symbol}' AND position_side = '{position_side.value}' AND status = '{PositionStatus.OPEN.value}'
|
|
275
|
+
ORDER BY open_time ASC;
|
|
276
|
+
"""
|
|
277
|
+
|
|
278
|
+
# 如果有指定仓位id,就用指定的
|
|
279
|
+
if hasattr(order, "position_id") and order.position_id:
|
|
280
|
+
sql = f"""
|
|
281
|
+
SELECT * FROM {self._mysql_table_name_position}
|
|
282
|
+
WHERE id = '{order.position_id}' AND status = '{PositionStatus.OPEN.value}'
|
|
283
|
+
"""
|
|
284
|
+
self._logger.info(f"{ins_id}_{exchange}_{symbol} get position by id {order.position_id}")
|
|
285
|
+
|
|
286
|
+
execute_ret = mysql.execute_sql(sql)
|
|
287
|
+
try:
|
|
288
|
+
row = execute_ret.fetchone()
|
|
289
|
+
position_id = row.id
|
|
290
|
+
if position_id is not None:
|
|
291
|
+
# 更新持仓信息
|
|
292
|
+
sql = f"""
|
|
293
|
+
UPDATE {self._mysql_table_name_position}
|
|
294
|
+
SET close_ins_id = '{ins_id}', close_price = {avg_price}, close_fee = '{fee}', close_fee_rate = '{fee_rate}', close_time = {current_time}, status = '{PositionStatus.CLOSE.value}'
|
|
295
|
+
WHERE id = '{position_id}';
|
|
296
|
+
"""
|
|
297
|
+
execute_ret = mysql.execute_sql(sql, True)
|
|
298
|
+
|
|
299
|
+
self._logger.info(f"{ins_id}_{exchange}_{symbol} step 3. 更新持仓记录 {position_id}")
|
|
300
|
+
try:
|
|
301
|
+
self._write_position_close_to_redis(
|
|
302
|
+
position_id,
|
|
303
|
+
exchange,
|
|
304
|
+
symbol,
|
|
305
|
+
position_side,
|
|
306
|
+
float(row.coin_quantity),
|
|
307
|
+
float(row.usdt_quantity),
|
|
308
|
+
row.open_ins_id,
|
|
309
|
+
float(row.open_price),
|
|
310
|
+
float(row.open_fee),
|
|
311
|
+
float(row.open_fee_rate),
|
|
312
|
+
int(row.open_time),
|
|
313
|
+
ins_id,
|
|
314
|
+
avg_price,
|
|
315
|
+
fee,
|
|
316
|
+
fee_rate,
|
|
317
|
+
current_time,
|
|
318
|
+
)
|
|
319
|
+
except:
|
|
320
|
+
pass
|
|
321
|
+
except:
|
|
322
|
+
pass
|
|
323
|
+
|
|
324
|
+
return True
|