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, bool allow_borrowing=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.
 - allow_borrowing (bool , optional) – If borrowing is allowed (negative balances).
 
 - Raises:
ValueError – If event.account_type is not equal to 
CASH. 
ACCOUNT_TYPE = 1
allow_borrowing
If borrowing is allowed (negative balances).
- Returns: bool
 
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 
Nonethen will use the default currency (if set). - Return type:
AccountBalance or 
None - Raises:
ValueError – If currency is 
Noneand 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 
Nonethen will use the default currency (if set). - Return type:
Money or 
None - Raises:
ValueError – If currency is 
Noneand 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 
Nonethen will use the default currency (if set). - Return type:
Money or 
None - Raises:
ValueError – If currency is 
Noneand 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 
Nonethen will use the default currency (if set). - Return type:
Money or 
None - Raises:
ValueError – If currency is 
Noneand 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.