Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix account balance and order status parsing for dYdX #2067

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nautilus_trader/adapters/dydx/common/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def __init__(self) -> None:
DYDXOrderStatus.OPEN: OrderStatus.ACCEPTED,
DYDXOrderStatus.FILLED: OrderStatus.FILLED,
DYDXOrderStatus.CANCELED: OrderStatus.CANCELED,
DYDXOrderStatus.BEST_EFFORT_CANCELED: OrderStatus.PENDING_CANCEL,
DYDXOrderStatus.BEST_EFFORT_CANCELED: OrderStatus.CANCELED,
DYDXOrderStatus.BEST_EFFORT_OPENED: OrderStatus.SUBMITTED,
}

Expand Down
22 changes: 1 addition & 21 deletions nautilus_trader/adapters/dydx/schemas/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
# ruff: noqa: N815

import datetime
from collections import defaultdict
from decimal import Decimal

import msgspec
Expand Down Expand Up @@ -548,31 +547,12 @@ def parse_to_account_balances(self) -> list[AccountBalance]:
"""
Create an account balance report.
"""
orders: dict[str, list[DYDXOrderResponse]] = defaultdict(list)

# Orders with state BEST_EFFORT_CANCELED are not considered to be
# included in the locked account balance.
if self.orders is not None:
for order in self.orders:
if (
order.status
in (
DYDXOrderStatus.OPEN,
DYDXOrderStatus.BEST_EFFORT_OPENED,
)
and order.type == DYDXOrderType.LIMIT
):
orders[order.quote_currency()].append(order)

account_balances = []

if self.subaccount is not None:
for asset_position in self.subaccount.assetPositions.values():
# Only valid for a margin account
locked = Decimal(0)

for order in orders[asset_position.symbol]:
locked += Decimal(order.size) * Decimal(order.price)

account_balance = asset_position.parse_to_account_balance(locked=locked)
account_balances.append(account_balance)

Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests/adapters/dydx/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def test_parse_nautilus_order_side(
(DYDXOrderStatus.OPEN, OrderStatus.ACCEPTED),
(DYDXOrderStatus.FILLED, OrderStatus.FILLED),
(DYDXOrderStatus.CANCELED, OrderStatus.CANCELED),
(DYDXOrderStatus.BEST_EFFORT_CANCELED, OrderStatus.PENDING_CANCEL),
(DYDXOrderStatus.BEST_EFFORT_CANCELED, OrderStatus.CANCELED),
(DYDXOrderStatus.BEST_EFFORT_OPENED, OrderStatus.SUBMITTED),
],
)
Expand Down
10 changes: 5 additions & 5 deletions tests/integration_tests/adapters/dydx/test_websocket_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ def test_account_parse_to_account_balances() -> None:
expected_result = [
AccountBalance(
total=Money(Decimal("11.62332500"), Currency.from_str("USDC")),
locked=Money(Decimal("10.00590000"), Currency.from_str("USDC")),
free=Money(Decimal("1.61742500"), Currency.from_str("USDC")),
locked=Money(Decimal("0"), Currency.from_str("USDC")),
free=Money(Decimal("11.62332500"), Currency.from_str("USDC")),
),
]

Expand All @@ -228,8 +228,8 @@ def test_account_parse_to_account_balances_order_best_effort_canceled() -> None:
expected_result = [
AccountBalance(
total=Money(Decimal("11.62332500"), Currency.from_str("USDC")),
locked=Money(Decimal("10.00590000"), Currency.from_str("USDC")),
free=Money(Decimal("1.61742500"), Currency.from_str("USDC")),
locked=Money(Decimal("0"), Currency.from_str("USDC")),
free=Money(Decimal("11.62332500"), Currency.from_str("USDC")),
),
]

Expand Down Expand Up @@ -660,7 +660,7 @@ def test_account_channel_data_order_best_effort_canceled() -> None:
order_side=OrderSide.SELL,
order_type=OrderType.LIMIT,
time_in_force=TimeInForce.IOC,
order_status=OrderStatus.PENDING_CANCEL,
order_status=OrderStatus.CANCELED,
price=Price(2519.4, 4),
quantity=Quantity(0.003, 5),
filled_qty=Quantity(0, 5),
Expand Down