1
+ import time
1
2
import datetime
2
3
import operator
3
4
from typing import Callable , Dict , List , Optional , Union
@@ -299,23 +300,18 @@ def place_cover_order(
299
300
)
300
301
if cover_quantity == 0 :
301
302
return
302
- elif cover_quantity < 0 :
303
- action = Action .Buy
304
- op = max
305
- else :
306
- action = Action .Sell
307
- op = min
308
303
for price_set in price_sets :
309
- cover_quantity_set = op (cover_quantity , price_set .quantity )
310
- if cover_quantity_set :
311
- quantity_s = quantity_split (cover_quantity_set , threshold = 499 )
304
+ if price_set .quantity :
305
+ quantity_s = quantity_split (price_set .quantity , threshold = 499 )
312
306
for q in quantity_s :
313
307
trade = api .place_order (
314
308
contract = position .contract ,
315
309
order = sj .order .TFTStockOrder (
316
310
price = price_set .price ,
317
311
quantity = abs (q ),
318
- action = action ,
312
+ action = Action .Buy
313
+ if position .cond .quantity < 0
314
+ else Action .Sell ,
319
315
price_type = price_set .price_type ,
320
316
order_type = TFTOrderType .ROD ,
321
317
custom_field = self .name ,
@@ -332,7 +328,7 @@ def open_position_cover(self, onclose: bool = True):
332
328
else :
333
329
api = self .api
334
330
api .update_status ()
335
- logger .info ("start place cover order." )
331
+ logger .info (f "start place cover order. onclose: { onclose } " )
336
332
for code , position in self .positions .items ():
337
333
if position .status .cover_order_quantity and (
338
334
position .status .cover_order_quantity != position .status .cover_quantity
@@ -355,6 +351,18 @@ def open_position_cover(self, onclose: bool = True):
355
351
]:
356
352
api .cancel_order (trade , timeout = 0 )
357
353
# event wait cancel
354
+ for code , position in self .positions .items ():
355
+ for _ in range (10 ):
356
+ if (
357
+ position .status .cover_quantity
358
+ != position .status .cover_order_quantity
359
+ ):
360
+ time .sleep (1 )
361
+ if position .status .cover_quantity != position .status .cover_order_quantity :
362
+ logger .error (
363
+ f"{ code } | cancel not work, position cover order "
364
+ f"{ position .status .cover_order_quantity } , position cover { position .status .cover_quantity } "
365
+ )
358
366
if onclose :
359
367
self .positions = self .stratagy .cover_positions_onclose (self .positions )
360
368
else :
@@ -379,27 +387,40 @@ def order_handler(self, msg: Dict, position: Position):
379
387
with position .lock :
380
388
if msg ["operation" ]["op_type" ] == "New" :
381
389
order_quantity = msg ["status" ].get ("order_quantity" , 0 )
390
+ order_pirce = msg ["order" ].get ("price" , 0 )
382
391
if msg ["order" ]["action" ] == Action .Sell :
383
392
if position .cond .quantity < 0 :
384
393
position .status .entry_order_quantity -= order_quantity
385
394
logger .info (
386
- f"{ position .contract .code } | place short entry order with { order_quantity } "
395
+ f"{ position .contract .code } | place short entry order with quantity { order_quantity } , price: { order_pirce } "
396
+ )
397
+ logger .debug (
398
+ f"{ position .contract .code } | { position .status } "
387
399
)
388
400
else :
389
401
position .status .cover_order_quantity -= order_quantity
390
402
logger .info (
391
- f"{ position .contract .code } | place long cover order with { order_quantity } "
403
+ f"{ position .contract .code } | place long cover order with quantity { order_quantity } , price: { order_pirce } "
404
+ )
405
+ logger .debug (
406
+ f"{ position .contract .code } | { position .status } "
392
407
)
393
408
else :
394
409
if position .cond .quantity < 0 :
395
410
position .status .cover_order_quantity += order_quantity
396
411
logger .info (
397
- f"{ position .contract .code } | place short cover order with { order_quantity } "
412
+ f"{ position .contract .code } | place short cover order with quantity { order_quantity } , price: { order_pirce } "
413
+ )
414
+ logger .debug (
415
+ f"{ position .contract .code } | { position .status } "
398
416
)
399
417
else :
400
418
position .status .entry_order_quantity += order_quantity
401
419
logger .info (
402
- f"{ position .contract .code } | place long entry order with { order_quantity } "
420
+ f"{ position .contract .code } | place long entry order with quantity { order_quantity } , price: { order_pirce } "
421
+ )
422
+ logger .debug (
423
+ f"{ position .contract .code } | { position .status } "
403
424
)
404
425
else :
405
426
cancel_quantity = msg ["status" ].get ("cancel_quantity" , 0 )
@@ -409,22 +430,34 @@ def order_handler(self, msg: Dict, position: Position):
409
430
logger .info (
410
431
f"{ position .contract .code } | canceled short entry order with { cancel_quantity } "
411
432
)
433
+ logger .debug (
434
+ f"{ position .contract .code } | { position .status } "
435
+ )
412
436
else :
413
437
position .status .cover_order_quantity += cancel_quantity
414
438
logger .info (
415
439
f"{ position .contract .code } | canceled long cover order with { cancel_quantity } "
416
440
)
441
+ logger .debug (
442
+ f"{ position .contract .code } | { position .status } "
443
+ )
417
444
else :
418
445
if position .cond .quantity < 0 :
419
446
position .status .cover_order_quantity -= cancel_quantity
420
447
logger .info (
421
448
f"{ position .contract .code } | canel short cover order with { cancel_quantity } "
422
449
)
450
+ logger .debug (
451
+ f"{ position .contract .code } | { position .status } "
452
+ )
423
453
else :
424
454
position .status .entry_order_quantity -= cancel_quantity
425
455
logger .info (
426
456
f"{ position .contract .code } | canel long entry order with { cancel_quantity } "
427
457
)
458
+ logger .debug (
459
+ f"{ position .contract .code } | { position .status } "
460
+ )
428
461
position .status .cancel_quantity += cancel_quantity
429
462
else :
430
463
logger .error (f"Please Check: { msg } " )
@@ -440,20 +473,24 @@ def deal_handler(self, msg: Dict, position: Position):
440
473
logger .info (
441
474
f"{ position .contract .code } | short entry order deal with { deal_quantity } , price: { deal_price } "
442
475
)
476
+ logger .debug (f"{ position .contract .code } | { position .status } " )
443
477
else :
444
478
position .status .cover_quantity -= deal_quantity
445
479
logger .info (
446
480
f"{ position .contract .code } | long cover order deal with { deal_quantity } , price: { deal_price } "
447
481
)
482
+ logger .debug (f"{ position .contract .code } | { position .status } " )
448
483
else :
449
484
position .status .open_quantity += deal_quantity
450
485
if position .cond .quantity < 0 :
451
486
position .status .cover_quantity += deal_quantity
452
487
logger .info (
453
488
f"{ position .contract .code } | short cover order deal with { deal_quantity } , price: { deal_price } "
454
489
)
490
+ logger .debug (f"{ position .contract .code } | { position .status } " )
455
491
else :
456
492
position .status .entry_quantity += deal_quantity
457
493
logger .info (
458
494
f"{ position .contract .code } | long entry order deal with { deal_quantity } , price: { deal_price } "
459
495
)
496
+ logger .debug (f"{ position .contract .code } | { position .status } " )
0 commit comments