@@ -532,53 +532,58 @@ async def _cancel_order(self, command: CancelOrder) -> None:
532
532
533
533
async def _cancel_all_orders (self , command : CancelAllOrders ) -> None :
534
534
bybit_symbol = BybitSymbol (command .instrument_id .symbol .value )
535
- await self ._http_account .cancel_all_orders (
536
- bybit_symbol .product_type ,
537
- bybit_symbol .raw_symbol ,
535
+
536
+ if bybit_symbol .product_type == BybitProductType .INVERSE :
537
+ # Batch cancel not implemented for INVERSE
538
+ self ._log .warning (
539
+ f"Batch cancel not implemented for INVERSE, "
540
+ f"canceling all for symbol { command .instrument_id .symbol .value } " ,
541
+ )
542
+ await self ._http_account .cancel_all_orders (
543
+ bybit_symbol .product_type ,
544
+ bybit_symbol .raw_symbol ,
545
+ )
546
+ return
547
+
548
+ open_orders_strategy : list [Order ] = self ._cache .orders_open (
549
+ instrument_id = command .instrument_id ,
550
+ strategy_id = command .strategy_id ,
538
551
)
539
552
540
- # TODO: Determine signing issue for batch requests
541
- # async def _cancel_all_orders(self, command: CancelAllOrders) -> None:
542
- # open_orders_strategy: list[Order] = self._cache.orders_open(
543
- # instrument_id=command.instrument_id,
544
- # strategy_id=command.strategy_id,
545
- # )
546
- #
547
- # bybit_symbol = BybitSymbol(command.instrument_id.symbol.value)
548
- #
549
- # # Check total orders for instrument
550
- # open_orders_total_count = self._cache.orders_open_count(
551
- # instrument_id=command.instrument_id,
552
- # )
553
- # if open_orders_total_count > 10:
554
- # # This could be reimplemented later to group requests into batches of 10
555
- # self._log.warning(
556
- # f"Total {command.instrument_id.symbol.value} orders open exceeds 10, "
557
- # f"is {open_orders_total_count}: canceling all for symbol",
558
- # )
559
- # await self._http_account.cancel_all_orders(
560
- # bybit_symbol.product_type,
561
- # bybit_symbol.raw_symbol,
562
- # )
563
- # return
564
- #
565
- # cancel_batch: list[Order] = []
566
- # for order in open_orders_strategy:
567
- # cancel_batch.append(order)
568
- #
569
- # await self._http_account.batch_cancel_orders(
570
- # product_type=bybit_symbol.product_type,
571
- # symbol=bybit_symbol.raw_symbol,
572
- # orders=cancel_batch,
573
- # )
553
+ # Check total orders for instrument
554
+ open_orders_total_count = self ._cache .orders_open_count (
555
+ instrument_id = command .instrument_id ,
556
+ )
557
+ if open_orders_total_count > 10 :
558
+ # This could be reimplemented later to group requests into batches of 10
559
+ self ._log .warning (
560
+ f"Total { command .instrument_id .symbol .value } orders open exceeds 10, "
561
+ f"is { open_orders_total_count } : canceling all for symbol" ,
562
+ )
563
+ await self ._http_account .cancel_all_orders (
564
+ bybit_symbol .product_type ,
565
+ bybit_symbol .raw_symbol ,
566
+ )
567
+ return
568
+
569
+ cancel_batch : list [Order ] = []
570
+ for order in open_orders_strategy :
571
+ cancel_batch .append (order )
572
+
573
+ await self ._http_account .batch_cancel_orders (
574
+ product_type = bybit_symbol .product_type ,
575
+ symbol = bybit_symbol .raw_symbol ,
576
+ orders = cancel_batch ,
577
+ )
574
578
575
579
async def _submit_order (self , command : SubmitOrder ) -> None :
576
580
order = command .order
577
581
if order .is_closed :
578
582
self ._log .warning (f"Order { order } is already closed" )
579
583
return
580
584
581
- if not self ._check_order_validity (order ):
585
+ bybit_symbol = BybitSymbol (command .instrument_id .symbol .value )
586
+ if not self ._check_order_validity (order , bybit_symbol .product_type ):
582
587
return
583
588
584
589
self ._log .debug (f"Submitting order { order } " )
@@ -600,20 +605,28 @@ async def _submit_order(self, command: SubmitOrder) -> None:
600
605
except BybitError as e :
601
606
self ._log .error (repr (e ))
602
607
603
- def _check_order_validity (self , order : Order ) -> bool :
608
+ def _check_order_validity (self , order : Order , product_type : BybitProductType ) -> bool :
604
609
# Check order type valid
605
610
if order .order_type not in self ._enum_parser .valid_order_types :
606
611
self ._log .error (
607
612
f"Cannot submit { order } has invalid order type { order .order_type } , unsupported on Bybit" ,
608
613
)
609
614
return False
615
+
610
616
# Check post only
611
617
if order .is_post_only and order .order_type != OrderType .LIMIT :
612
618
self ._log .error (
613
619
f"Cannot submit { order } has invalid post only { order .is_post_only } , unsupported on Bybit" ,
614
620
)
615
621
return False
616
622
623
+ # Check reduce only
624
+ if order .is_reduce_only and product_type == BybitProductType .SPOT :
625
+ self ._log .error (
626
+ f"Cannot submit { order } is reduce_only, unsupported on Bybit SPOT" ,
627
+ )
628
+ return False
629
+
617
630
return True
618
631
619
632
async def _submit_market_order (self , order : MarketOrder ) -> None :
@@ -630,6 +643,7 @@ async def _submit_market_order(self, order: MarketOrder) -> None:
630
643
quote_quantity = order .is_quote_quantity ,
631
644
time_in_force = time_in_force ,
632
645
client_order_id = str (order .client_order_id ),
646
+ reduce_only = order .is_reduce_only if order .is_reduce_only else None ,
633
647
)
634
648
635
649
async def _submit_limit_order (self , order : LimitOrder ) -> None :
@@ -647,6 +661,7 @@ async def _submit_limit_order(self, order: LimitOrder) -> None:
647
661
price = str (order .price ),
648
662
time_in_force = time_in_force ,
649
663
client_order_id = str (order .client_order_id ),
664
+ reduce_only = order .is_reduce_only if order .is_reduce_only else None ,
650
665
)
651
666
652
667
def _handle_ws_message (self , raw : bytes ) -> None :
0 commit comments