Skip to content

Fix account balance for dYdX #2167

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

Merged
merged 1 commit into from
Jan 2, 2025
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
10 changes: 10 additions & 0 deletions nautilus_trader/adapters/dydx/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,16 @@ async def _connect(self) -> None:
sequence=account.sequence,
)

while self.get_account() is None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we put a timeout on this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea

await asyncio.sleep(0.1)

account = self.get_account()
instruments = self._instrument_provider.get_all()

for instrument_id, instrument in instruments.items():
leverage = Decimal(1) / instrument.margin_init
account.set_leverage(instrument_id, leverage)

async def _disconnect(self) -> None:
await self._ws_client.unsubscribe_markets()
await self._ws_client.unsubscribe_block_height()
Expand Down
14 changes: 0 additions & 14 deletions nautilus_trader/adapters/dydx/schemas/account/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from nautilus_trader.core.uuid import UUID4
from nautilus_trader.execution.reports import PositionStatusReport
from nautilus_trader.model.identifiers import AccountId
from nautilus_trader.model.objects import AccountBalance
from nautilus_trader.model.objects import Currency
from nautilus_trader.model.objects import MarginBalance
from nautilus_trader.model.objects import Money
Expand Down Expand Up @@ -144,19 +143,6 @@ class DYDXAssetPosition(msgspec.Struct, forbid_unknown_fields=True):
assetId: str
subaccountNumber: int

def parse_to_account_balance(self, locked: Decimal) -> AccountBalance:
"""
Create an account balance report.
"""
currency = Currency.from_str(self.symbol)
total = Decimal(self.size)
free = total - locked
return AccountBalance(
total=Money(total, currency),
locked=Money(locked, currency),
free=Money(free, currency),
)


class DYDXSubaccount(msgspec.Struct, forbid_unknown_fields=True):
"""
Expand Down
22 changes: 16 additions & 6 deletions nautilus_trader/adapters/dydx/schemas/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import msgspec

from nautilus_trader.adapters.dydx.common.constants import DEFAULT_CURRENCY
from nautilus_trader.adapters.dydx.common.enums import DYDXEnumParser
from nautilus_trader.adapters.dydx.common.enums import DYDXFillType
from nautilus_trader.adapters.dydx.common.enums import DYDXLiquidity
Expand Down Expand Up @@ -61,6 +62,8 @@
from nautilus_trader.model.identifiers import TradeId
from nautilus_trader.model.identifiers import VenueOrderId
from nautilus_trader.model.objects import AccountBalance
from nautilus_trader.model.objects import Currency
from nautilus_trader.model.objects import Money
from nautilus_trader.model.objects import Price
from nautilus_trader.model.objects import Quantity

Expand Down Expand Up @@ -541,14 +544,21 @@ def parse_to_account_balances(self) -> list[AccountBalance]:
"""
Create an account balance report.
"""
account_balances = []
account_balances: list[AccountBalance] = []

if self.subaccount is not None:
for asset_position in self.subaccount.assetPositions.values():
# Only valid for a margin account
locked = Decimal(0)
account_balance = asset_position.parse_to_account_balance(locked=locked)
account_balances.append(account_balance)
currency = Currency.from_str(DEFAULT_CURRENCY)
free = Decimal(self.subaccount.freeCollateral)
total = Decimal(self.subaccount.equity)
locked = total - free

return [
AccountBalance(
total=Money(total, currency),
locked=Money(locked, currency),
free=Money(free, currency),
),
]

return account_balances

Expand Down
28 changes: 0 additions & 28 deletions tests/integration_tests/adapters/dydx/test_http_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
from nautilus_trader.model.identifiers import TradeId
from nautilus_trader.model.identifiers import VenueOrderId
from nautilus_trader.model.instruments import CryptoPerpetual
from nautilus_trader.model.objects import AccountBalance
from nautilus_trader.model.objects import Currency
from nautilus_trader.model.objects import MarginBalance
from nautilus_trader.model.objects import Money
Expand Down Expand Up @@ -616,30 +615,3 @@ def test_parse_to_instrument(
assert result.ts_event == expected_result.ts_event
assert result.ts_init == expected_result.ts_init
assert result.info == expected_result.info


def test_asset_positions_to_account_balance(
asset_positions_response: DYDXAssetPositionsResponse,
) -> None:
"""
Test parsing the subaccount message into an account balance.
"""
# Prepare

expected_result = AccountBalance(
total=Money(Decimal("90.461932"), Currency.from_str("USDC")),
locked=Money(Decimal("0.005"), Currency.from_str("USDC")),
free=Money(Decimal("90.461932") - Decimal("0.005"), Currency.from_str("USDC")),
)
# Act
result = asset_positions_response.positions[0].parse_to_account_balance(locked=Decimal(0.005))

# Assert
assert result == expected_result
assert result.total == expected_result.total
assert result.locked == expected_result.locked
assert result.free == expected_result.free
assert result.total.currency == expected_result.total.currency
assert result.locked.currency == expected_result.locked.currency
assert result.free.currency == expected_result.free.currency
assert result.free.currency.name == expected_result.free.currency.name
Loading