Accounting
The accounting subpackage defines both different account types and account management machinery.
There is also an ExchangeRateCalculator for calculating the exchange rate between FX and/or Crypto pairs. The AccountManager is mainly used from the Portfolio to manage accounting operations.
The AccountFactory supports customized account types for specific integrations. These custom account types can be registered with the factory and will then be instantiated when an AccountState event is received for that integration.
class CashAccount
Bases: Account
CashAccount(AccountState event, bool calculate_account_state=False)
Provides a cash account.
- Parameters:
- event (AccountState) – The initial account state event.
- calculate_account_state (bool , optional) – If the account state should be calculated from order fills.
- Raises:
ValueError – If event.account_type is not equal to
CASH
.
ACCOUNT_TYPE = 1
apply(self, AccountState event) → void
Apply the given account event to the account.
- Parameters: event (AccountState) – The account event to apply.
- Raises:
- ValueError – If event.account_type is not equal to self.type.
- ValueError – If event.account_id is not equal to self.id.
- ValueError – If event.base_currency is not equal to self.base_currency.
WARNING
System method (not intended to be called by user code).
balance(self, Currency currency=None) → AccountBalance
Return the current account balance total.
For multi-currency accounts, specify the currency for the query.
- Parameters:
currency (Currency , optional) – The currency for the query. If
None
then will use the default currency (if set). - Return type:
AccountBalance or
None
- Raises:
ValueError – If currency is
None
and base_currency isNone
.
WARNING
Returns None
if there is no applicable information for the query,
rather than Money of zero amount.
balance_free(self, Currency currency=None) → Money
Return the account balance free.
For multi-currency accounts, specify the currency for the query.
- Parameters:
currency (Currency , optional) – The currency for the query. If
None
then will use the default currency (if set). - Return type:
Money or
None
- Raises:
ValueError – If currency is
None
and base_currency isNone
.
WARNING
Returns None
if there is no applicable information for the query,
rather than Money of zero amount.
balance_impact(self, Instrument instrument, Quantity quantity, Price price, OrderSide order_side) → Money
balance_locked(self, Currency currency=None) → Money
Return the account balance locked.
For multi-currency accounts, specify the currency for the query.
- Parameters:
currency (Currency , optional) – The currency for the query. If
None
then will use the default currency (if set). - Return type:
Money or
None
- Raises:
ValueError – If currency is
None
and base_currency isNone
.
WARNING
Returns None
if there is no applicable information for the query,
rather than Money of zero amount.
balance_total(self, Currency currency=None) → Money
Return the current account balance total.
For multi-currency accounts, specify the currency for the query.
- Parameters:
currency (Currency , optional) – The currency for the query. If
None
then will use the default currency (if set). - Return type:
Money or
None
- Raises:
ValueError – If currency is
None
and base_currency isNone
.
WARNING
Returns None
if there is no applicable information for the query,
rather than Money of zero amount.
balances(self) → dict
Return the account balances totals.
balances_free(self) → dict
Return the account balances free.
balances_locked(self) → dict
Return the account balances locked.
balances_total(self) → dict
Return the account balances totals.
base_currency
The accounts base currency (None
for multi-currency accounts).
- Returns:
Currency or
None
calculate_account_state
If the accounts state should be calculated by Nautilus.
- Returns: bool
calculate_balance_locked(self, Instrument instrument, OrderSide side, Quantity quantity, Price price, bool use_quote_for_inverse=False) → Money
Calculate the locked balance.
Result will be in quote currency for standard instruments, or base currency for inverse instruments.
- Parameters:
- instrument (Instrument) – The instrument for the calculation.
- side (OrderSide {
BUY
,SELL
}) – The order side. - quantity (Quantity) – The order quantity.
- price (Price) – The order price.
- use_quote_for_inverse (bool) – If inverse instrument calculations use quote currency (instead of base).
- Return type: Money
calculate_commission(self, Instrument instrument, Quantity last_qty, Price last_px, LiquiditySide liquidity_side, bool use_quote_for_inverse=False) → Money
Calculate the commission generated from a transaction with the given parameters.
Result will be in quote currency for standard instruments, or base currency for inverse instruments.
- Parameters:
- instrument (Instrument) – The instrument for the calculation.
- last_qty (Quantity) – The transaction quantity.
- last_px (Price) – The transaction price.
- liquidity_side (LiquiditySide {
MAKER
,TAKER
}) – The liquidity side for the transaction. - use_quote_for_inverse (bool) – If inverse instrument calculations use quote currency (instead of base).
- Return type: Money
- Raises:
ValueError – If liquidity_side is
NO_LIQUIDITY_SIDE
.
calculate_pnls(self, Instrument instrument, OrderFilled fill, Position position: Position | None = None) → list
Return the calculated PnL.
The calculation does not include any commissions.
- Parameters:
- instrument (Instrument) – The instrument for the calculation.
- fill (OrderFilled) – The fill for the calculation.
- position (Position , optional) – The position for the calculation (can be None).
- Return type: list[Money]
clear_balance_locked(self, InstrumentId instrument_id) → void
Clear the balance locked for the given instrument ID.
- Parameters: instrument_id (InstrumentId) – The instrument for the locked balance to clear.
commission(self, Currency currency) → Money
Return the total commissions for the given currency.
- Parameters: currency (Currency) – The currency for the commission.
- Return type:
Money or
None
commissions(self) → dict
Return the total commissions for the account.
currencies(self) → list
Return the account currencies.
- Return type: list[Currency]
event_count
Return the count of events.
- Return type: int
events
Return all events received by the account.
- Return type: list[AccountState]
static from_dict(dict values)
id
The accounts ID.
- Returns: AccountId
is_cash_account
If the account is a type of CASH
account.
is_margin_account
If the account is a type of MARGIN
account.
is_unleveraged(self, InstrumentId instrument_id) → bool
last_event
Return the accounts last state event.
- Return type: AccountState
starting_balances(self) → dict
Return the account starting balances.
static to_dict(CashAccount obj)
type
The accounts type.
- Returns: AccountType
update_balance_locked(self, InstrumentId instrument_id, Money locked) → void
Update the balance locked for the given instrument ID.
- Parameters:
- instrument_id (InstrumentId) – The instrument ID for the update.
- locked (Money) – The locked balance for the instrument.
- Raises: ValueError – If margin_init is negative (< 0).
WARNING
System method (not intended to be called by user code).
update_balances(self, list balances, bool allow_zero=True) → void
Update the account balances.
There is no guarantee that every account currency is included in the given balances, therefore we only update included balances.
- Parameters:
- balances (list [AccountBalance ]) – The balances for the update.
- allow_zero (bool , default True) – If zero balances are allowed (will then just clear the assets balance).
- Raises: ValueError – If balances is empty.
update_commissions(self, Money commission) → void
Update the commissions.
Can be negative which represents credited commission.
- Parameters: commission (Money) – The commission to update with.
WARNING
System method (not intended to be called by user code).
class MarginAccount
Bases: Account
MarginAccount(AccountState event, bool calculate_account_state=False)
Provides a margin account.
- Parameters:
- event (AccountState) – The initial account state event.
- calculate_account_state (bool , optional) – If the account state should be calculated from order fills.
- Raises:
ValueError – If event.account_type is not equal to
MARGIN
.
apply(self, AccountState event) → void
Apply the given account event to the account.
- Parameters: event (AccountState) – The account event to apply.
- Raises:
- ValueError – If event.account_type is not equal to self.type.
- ValueError – If event.account_id is not equal to self.id.
- ValueError – If event.base_currency is not equal to self.base_currency.
WARNING
System method (not intended to be called by user code).
balance(self, Currency currency=None) → AccountBalance
Return the current account balance total.
For multi-currency accounts, specify the currency for the query.
- Parameters:
currency (Currency , optional) – The currency for the query. If
None
then will use the default currency (if set). - Return type:
AccountBalance or
None
- Raises:
ValueError – If currency is
None
and base_currency isNone
.
WARNING
Returns None
if there is no applicable information for the query,
rather than Money of zero amount.
balance_free(self, Currency currency=None) → Money
Return the account balance free.
For multi-currency accounts, specify the currency for the query.
- Parameters:
currency (Currency , optional) – The currency for the query. If
None
then will use the default currency (if set). - Return type:
Money or
None
- Raises:
ValueError – If currency is
None
and base_currency isNone
.
WARNING
Returns None
if there is no applicable information for the query,
rather than Money of zero amount.