Skip to content

Commit 654e0ed

Browse files
committed
Fix position ID handling for Binance Futures and exec algo
1 parent 247c0c0 commit 654e0ed

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

RELEASES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ and introduces support for Linux on ARM64 architecture.
3838
- Fixed overflow error in trailing stop calculations
3939
- Fixed missing `SymbolFilterType` enum member for Binance (#2495), thanks @sunlei
4040
- Fixed `ts_event` for Bybit bars (#2502), thanks @davidsblom
41+
- Fixed position ID handling for Binance Futures in hedging mode with execution algorithm order (#2504), thanks for reporting @Oxygen923
4142

4243
### Documentation Updates
4344
- Removed obsolete bar limitations in portfolio docs (#2501), thanks @stefansimik

nautilus_trader/adapters/binance/execution.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -634,27 +634,21 @@ def _determine_reduce_only_str(self, order: Order) -> str | None:
634634
def _get_position_side_from_position_id(
635635
self,
636636
position_id: PositionId | None,
637+
exec_spawn_id: ClientOrderId | None,
637638
) -> BinanceFuturesPositionSide | None:
638-
"""
639-
Parse the position side from the position ID.
640-
641-
Parameters
642-
----------
643-
position_id : PositionId | None
644-
The position ID, can be `None`.
645-
Position ID must end with either 'LONG' or 'SHORT' for Binance Futures Hedge position mode.
646-
647-
Returns
648-
-------
649-
BinanceFuturesPositionSide | None
639+
# Position ID must end with either 'LONG', 'SHORT' or 'BOTH' for Binance Futures Hedge position mode
650640

651-
"""
652641
position_side = None
653642
if self._binance_account_type.is_spot_or_margin: # Spot or Margin mode
654643
return position_side
655644
elif not self._is_dual_side_position: # One-way position mode
656645
return BinanceFuturesPositionSide.BOTH
657646

647+
if position_id is None and exec_spawn_id is not None:
648+
primary_order = self._cache.order(exec_spawn_id)
649+
if primary_order is not None:
650+
position_id = primary_order.position_id
651+
658652
# For Binance Futures Hedge mode, the position side must be specified in the position_id
659653
PyCondition.not_none(position_id, "position_id")
660654
position_side = self._enum_parser.parse_position_id_to_binance_futures_position_side(
@@ -670,7 +664,10 @@ def _get_position_side_from_position_id(
670664
return position_side
671665

672666
async def _submit_order(self, command: SubmitOrder) -> None:
673-
position_side = self._get_position_side_from_position_id(command.position_id)
667+
position_side = self._get_position_side_from_position_id(
668+
position_id=command.position_id,
669+
exec_spawn_id=command.order.exec_spawn_id,
670+
)
674671
await self._submit_order_inner(command.order, position_side)
675672

676673
async def _submit_order_inner(
@@ -790,7 +787,11 @@ async def _submit_stop_limit_order(
790787
)
791788

792789
async def _submit_order_list(self, command: SubmitOrderList) -> None:
793-
position_side = self._get_position_side_from_position_id(command.position_id)
790+
position_side = self._get_position_side_from_position_id(
791+
position_id=command.position_id,
792+
exec_spawn_id=None,
793+
)
794+
794795
for order in command.order_list.orders:
795796
self.generate_order_submitted(
796797
strategy_id=order.strategy_id,

tests/integration_tests/adapters/binance/test_parsing_common.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ def test_parse_parse_bar_ws(self, resolution, expected_type):
236236
)
237237
def test_parse_position_id_to_binance_futures_position_side(self, position_id, expected):
238238
# Arrange, Act
239-
print(position_id)
240239
result = self._futures_enum_parser.parse_position_id_to_binance_futures_position_side(
241240
position_id,
242241
)

0 commit comments

Comments
 (0)