-
Notifications
You must be signed in to change notification settings - Fork 0
Accounting
The accounting subpackage defines both different account types and account management machinery.
accounting 子包定义了不同的账户类型和账户管理机制。
There is also an
ExchangeRateCalculator
for calculating the exchange rate between FX and/or Crypto pairs. TheAccountManager
is mainly used from thePortfolio
to manage accounting operations.还有一个
ExchangeRateCalculator
用于计算外汇和/或加密货币对之间的汇率。AccountManager
主要由Portfolio
来管理记账操作。
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 anAccountState
event is received for that integration.
AccountFactory
支持针对特定集成的自定义账户类型。这些自定义账户类型可以注册到工厂中,然后在收到该集成的AccountState
事件时实例化。
class CashAccount(Account)
Provides a cash account.
提供现金账户。
Parameters:
event
(AccountState
): The initial account state event.event
(AccountState
):初始账户状态事件。calculate_account_state
(bool
, optional): If the account state should be calculated from order fills.calculate_account_state
(bool
,可选):如果应从订单成交中计算账户状态。
Raises:
ValueError
: Ifevent.account_type
is not equal toCASH
.ValueError
:如果event.account_type
不等于CASH
。
ACCOUNT_TYPE = 1
def apply(self, event: AccountState) -> 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.
"""
...
def balance(self, currency: Currency = None) -> AccountBalance | None:
"""
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).
Returns
-------
AccountBalance or None
Raises
------
ValueError
If currency is None and base_currency is None.
Warnings
--------
Returns None if there is no applicable information for the
query, rather than Money of zero amount.
"""
...
def balance_free(self, currency: Currency = None) -> Money | None:
"""
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).
Returns
-------
Money or None
Raises
------
ValueError
If currency is None and base_currency is None.
Warnings
--------
Returns None if there is no applicable information for the
query, rather than Money of zero amount.
"""
...
def balance_impact(self, instrument: Instrument, quantity: Quantity, price: Price, order_side: OrderSide) -> Money:
...
def balance_locked(self, currency: Currency = None) -> Money | None:
"""
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).
Returns
-------
Money or None
Raises
------
ValueError
If currency is None and base_currency is None.
Warnings
--------
Returns None if there is no applicable information for the
query, rather than Money of zero amount.
"""
...
def balance_total(self, currency: Currency = None) -> Money | None:
"""
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).
Returns
-------
Money or None
Raises
------
ValueError
If currency is None and base_currency is None.
Warnings
--------
Returns None if there is no applicable information for the
query, rather than Money of zero amount.
"""
...
def balances(self) -> dict:
"""
Return the account balances totals.
Returns
-------
dict[Currency, Money]
"""
...
def balances_free(self) -> dict:
"""
Return the account balances free.
Returns
-------
dict[Currency, Money]
"""
...
def balances_locked(self) -> dict:
"""
Return the account balances locked.
Returns
-------
dict[Currency, Money]
"""
...
def balances_total(self) -> dict:
"""
Return the account balances totals.
Returns
-------
dict[Currency, Money]
"""
...
@property
def base_currency(self) -> Currency | None:
"""
The accounts base currency (None for multi-currency accounts).
Returns
-------
Currency or None
"""
...
@property
def calculate_account_state(self) -> bool:
"""
If the accounts state should be calculated by Nautilus.
Returns
-------
bool
"""
...
def calculate_balance_locked(self, instrument: Instrument, side: OrderSide, quantity: Quantity, price: Price, use_quote_for_inverse: bool = 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).
Returns
-------
Money
"""
...
def calculate_commission(self, instrument: Instrument, last_qty: Quantity, last_px: Price, liquidity_side: LiquiditySide, use_quote_for_inverse: bool = 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).
Returns
-------
Money
Raises
------
ValueError
If liquidity_side is NO_LIQUIDITY_SIDE.
"""
...
def calculate_pnls(self, instrument: Instrument, fill: OrderFilled, 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).
Returns
-------
list[Money]
"""
...
def clear_balance_locked(self, instrument_id: InstrumentId) -> void:
"""
Clear the balance locked for the given instrument ID.
Parameters
----------
instrument_id : InstrumentId
The instrument for the locked balance to clear.
"""
...
def commission(self, currency: Currency) -> Money | None:
"""
Return the total commissions for the given currency.
Parameters
----------
currency : Currency
The currency for the commission.
Returns
-------
Money or None
"""
...
def commissions(self) -> dict:
...
def currencies(self) -> list:
"""
Return the account currencies.
Returns
-------
list[Currency]
"""
...
@property
def event_count(self) -> int:
"""
Return the count of events.
Returns
-------
int
"""
...
@property
def events(self) -> list[AccountState]:
"""
Return all events received by the account.
Returns
-------
list[AccountState]
"""
...
@staticmethod
def from_dict(values: dict):
...
@property
def id(self) -> AccountId:
"""
The accounts ID.
Returns
-------
AccountId
"""
...
@property
def is_cash_account(self) -> bool:
"""
If the account is a type of CASH account.
"""
...
@property
def is_margin_account(self) -> bool:
"""
If the account is a type of MARGIN account.
"""
...
def is_unleveraged(self, instrument_id: InstrumentId) -> bool:
...
@property
def last_event(self) -> AccountState:
"""
Return the accounts last state event.
Returns
-------
AccountState
"""
...
def starting_balances(self) -> dict:
"""
Return the account starting balances.
Returns
-------
dict[Currency, Money]
"""
...
@staticmethod
def to_dict(obj: CashAccount):
...
@property
def type(self) -> AccountType:
"""
The accounts type.
Returns
-------
AccountType
"""
...
def update_balance_locked(self, instrument_id: InstrumentId, locked: Money) -> 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).
"""
...
def update_balances(self, balances: list, allow_zero: bool = 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.
"""
...
def update_commissions(self, commission: Money) -> void:
"""
Update the commissions.
Can be negative which represents credited commission.
Parameters
----------
commission : Money
The commission to update with.
"""
...
class MarginAccount(Account)
Provides a margin account.
提供保证金账户。
Parameters:
event
(AccountState
): The initial account state event.event
(AccountState
):初始账户状态事件。calculate_account_state
(bool
, optional): If the account state should be calculated from order fills.calculate_account_state
(bool
,可选):如果应从订单成交中计算账户状态。
Raises:
ValueError
: Ifevent.account_type
is not equal toMARGIN
.ValueError
:如果event.account_type
不等于MARGIN
。
def apply(self, event: AccountState) -> 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.
"""
...
def balance(self, currency: Currency = None) -> AccountBalance | None:
"""
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).
Returns
-------
AccountBalance or None
Raises
------
ValueError
If currency is None and base_currency is None.
Warnings
--------
Returns None if there is no applicable information for the
query, rather than Money of zero amount.
"""
...
def balance_free(self, currency: Currency = None) -> Money | None:
"""
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).
Returns
-------
Money or None
Raises
------
ValueError
If currency is None and base_currency is None.
Warnings
--------
Returns None if there is no applicable information for the
query, rather than Money of zero amount.
"""
...
def balance_impact(self, instrument: Instrument, quantity: Quantity, price: Price, order_side: OrderSide) -> Money:
...
def balance_locked(self, currency: Currency = None) -> Money | None:
"""
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).
Returns
-------
Money or None
Raises
------
ValueError
If currency is None and base_currency is None.
Warnings
--------
Returns None if there is no applicable information for the
query, rather than Money of zero amount.
"""
...
def balance_total(self, currency: Currency = None) -> Money | None:
"""
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).
Returns
-------
Money or None
Raises
------
ValueError
If currency is None and base_currency is None.
Warnings
--------
Returns None if there is no applicable information for the
query, rather than Money of zero amount.
"""
...
def balances(self) -> dict:
"""
Return the account balances totals.
Returns
-------
dict[Currency, Money]
"""
...
def balances_free(self) -> dict:
"""
Return the account balances free.
Returns
-------
dict[Currency, Money]
"""
...
def balances_locked(self) -> dict:
"""
Return the account balances locked.
Returns
-------
dict[Currency, Money]
"""
...
def balances_total(self) -> dict:
"""
Return the account balances totals.
Returns
-------
dict[Currency, Money]
"""
...
@property
def base_currency(self) -> Currency | None:
"""
The accounts base currency (None for multi-currency accounts).
Returns
-------
Currency or None
"""
...
@property
def calculate_account_state(self) -> bool:
"""
If the accounts state should be calculated by Nautilus.
Returns
-------
bool
"""
...
def calculate_commission(self, instrument: Instrument, last_qty: Quantity, last_px: Price, liquidity_side: LiquiditySide, use_quote_for_inverse: bool = 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).
Returns
-------
Money
Raises
------
ValueError
If liquidity_side is NO_LIQUIDITY_SIDE.
"""
...
def calculate_margin_init(self, instrument: Instrument, quantity: Quantity, price: Price, use_quote_for_inverse: bool = False) -> Money:
"""
Calculate the initial (order) margin.
Result will be in quote currency for standard instruments, or base currency for inverse instruments.
Parameters
----------
instrument : Instrument
The instrument for the calculation.
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).
Returns
-------
Money
"""
...
def calculate_margin_maint(self, instrument: Instrument, side: PositionSide, quantity: Quantity, price: Price, use_quote_for_inverse: bool = False) -> Money:
"""
Calculate the maintenance (position) margin.
Result will be in quote currency for standard instruments, or base currency for inverse instruments.
Parameters
----------
instrument : Instrument
The instrument for the calculation.
side : PositionSide {LONG, SHORT}
The currency position side.
quantity : Quantity
The currency position quantity.
price : Price
The positions current price.
use_quote_for_inverse : bool
If inverse instrument calculations use quote currency (instead of base).
Returns
-------
Money
"""
...
def calculate_pnls(self, instrument: Instrument, fill: OrderFilled, 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.
Returns
-------
list[Money]
"""
...
def clear_margin(self, instrument_id: InstrumentId) -> void:
"""
Clear the maintenance (position) margins for the given instrument ID.
Parameters
----------
instrument_id : InstrumentId
The instrument for the maintenance margin to clear.
"""
...
def clear_margin_init(self, instrument_id: InstrumentId) -> void:
"""
Clear the initial (order) margins for the given instrument ID.
Parameters
----------
instrument_id : InstrumentId
The instrument for the initial margin to clear.
"""
...
def clear_margin_maint(self, instrument_id: InstrumentId) -> void:
"""
Clear the maintenance (position) margins for the given instrument ID.
Parameters
----------
instrument_id : InstrumentId
The instrument for the maintenance margin to clear.
"""
...
def commission(self, currency: Currency) -> Money | None:
"""
Return the total commissions for the given currency.
Parameters
----------
currency : Currency
The currency for the commission.
Returns
-------
Money or None
"""
...
def commissions(self) -> dict:
...
def currencies(self) -> list:
"""
Return the account currencies.
Returns
-------
list[Currency]
"""
...
@property
def default_leverage(self) -> Decimal:
"""
The accounts default leverage setting.
Returns
-------
Decimal
"""
...
@property
def event_count(self) -> int:
"""
Return the count of events.
Returns
-------
int
"""
...
@property
def events(self) -> list[AccountState]:
"""
Return all events received by the account.
Returns
-------
list[AccountState]
"""
...
@staticmethod
def from_dict(values: dict):
...
@property
def id(self) -> AccountId:
"""
The accounts ID.
Returns
-------
AccountId
"""
...
@property
def is_cash_account(self) -> bool:
"""
If the account is a type of CASH account.
"""
...
@property
def is_margin_account(self) -> bool:
"""
If the account is a type of MARGIN account.
"""
...
def is_unleveraged(self, instrument_id: InstrumentId) -> bool:
...
@property
def last_event(self) -> AccountState:
"""
Return the accounts last state event.
Returns
-------
AccountState
"""
...
def leverage(self, instrument_id: InstrumentId) -> Decimal | None:
"""
Return the leverage for the given instrument (if found).
Parameters
----------
instrument_id : InstrumentId
The instrument ID for the leverage.
Returns
-------
Decimal or None
"""
...
def leverages(self) -> dict:
"""
Return the account leverages.
Returns
-------
dict[InstrumentId, Decimal]
"""
...
def margin(self, instrument_id: InstrumentId) -> MarginBalance | None:
"""
Return the current margin balance.
Parameters
----------
instrument_id : InstrumentId
The instrument ID for the query.
Returns
-------
MarginBalance or None
Warnings
--------
Returns None if there is no applicable information for the
query, rather than MarginBalance with zero amounts.
"""
...
def margin_init(self, instrument_id: InstrumentId) -> Money | None:
"""
Return the current initial (order) margin.
Parameters
----------
instrument_id : InstrumentId
The instrument ID for the query.
Returns
-------
Money or None
Warnings
--------
Returns None if there is no applicable information for the
query, rather than Money of zero amount.
"""
...
def margin_maint(self, instrument_id: InstrumentId) -> Money | None:
"""
Return the current maintenance (position) margin.
Parameters
----------
instrument_id : InstrumentId
The instrument ID for the query.
Returns
-------
Money or None
Warnings
--------
Returns None if there is no applicable information for the
query, rather than Money of zero amount.
"""
...
def margins(self) -> dict:
"""
Return the initial (order) margins for the account.
Returns
-------
dict[InstrumentId, Money]
"""
...
def margins_init(self) -> dict:
"""
Return the initial (order) margins for the account.
Returns
-------
dict[InstrumentId, Money]
"""
...
def margins_maint(self) -> dict:
"""
Return the maintenance (position) margins for the account.
Returns
-------
dict[InstrumentId, Money]
"""
...
def set_default_leverage(self, leverage: Decimal) -> void:
"""
Set the default leverage for the account (if not specified by instrument).
Parameters
----------
leverage : Decimal
The default leverage value
Returns
-------
TypeError
If leverage is not of type Decimal.
ValueError
If leverage is not >= 1.
"""
...
def set_leverage(self, instrument_id: InstrumentId, leverage: Decimal) -> void:
"""
Set the leverage for the given instrument.
Parameters
----------
instrument_id : InstrumentId
The instrument for the leverage.
leverage : Decimal
The leverage value
Returns
-------
TypeError
If leverage is not of type Decimal.
ValueError
If leverage is not >= 1.
"""
...
def starting_balances(self) -> dict:
"""
Return the account starting balances.
Returns
-------
dict[Currency, Money]
"""
...
@staticmethod
def to_dict(obj: MarginAccount):
...
@property
def type(self) -> AccountType:
"""
The accounts type.
Returns
-------
AccountType
"""
...
def update_balances(self, balances: list, allow_zero: bool = 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.
"""
...
def update_commissions(self, commission: Money) -> void:
"""
Update the commissions.
Can be negative which represents credited commission.
Parameters
----------
commission : Money
The commission to update with.
"""
...
def update_margin(self, margin: MarginBalance) -> void:
"""
Update the margin balance.
Parameters
----------
margin : MarginBalance
"""
...
def update_margin_init(self, instrument_id: InstrumentId, margin_init: Money) -> void:
"""
Update the initial (order) margin.
Parameters
----------
instrument_id : InstrumentId
The instrument ID for the update.
margin_init : Money
The current initial (order) margin for the instrument.
Raises
------
ValueError
If margin_init is negative (< 0).
"""
...
def update_margin_maint(self, instrument_id: InstrumentId, margin_maint: Money) -> void:
"""
Update the maintenance (position) margin.
Parameters
----------
instrument_id : InstrumentId
The instrument ID for the update.
margin_maint : Money
The current maintenance (position) margin for the instrument.
Raises
------
ValueError
If margin_maint is negative (< 0).
"""
...
class ExchangeRateCalculator
Provides exchange rate calculations between currencies.
提供货币之间的汇率计算。
An exchange rate is the value of one asset versus that of another.
汇率是一种资产相对于另一种资产的价值。
def get_rate(self, from_currency: Currency, to_currency: Currency, price_type: PriceType, bid_quotes: dict, ask_quotes: dict) -> Decimal:
"""
Return the calculated exchange rate for the given price type using the given dictionary of bid and ask quotes.
Parameters
----------
from_currency : Currency
The currency to convert from.
to_currency : Currency
The currency to convert to.
price_type : PriceType
The price type for conversion.
bid_quotes : dict
The dictionary of currency pair bid quotes dict[Symbol, double].
ask_quotes : dict
The dictionary of currency pair ask quotes dict[Symbol, double].
Returns
-------
Decimal
Raises
------
ValueError
If bid_quotes length is not equal to ask_quotes length.
ValueError
If price_type is LAST.
"""
...
class RolloverInterestCalculator
Provides rollover interest rate calculations.
提供隔夜利息计算。
If
rate_data_csv_path
is empty then will default to the included short-term interest rate data csv (data since 1956).如果
rate_data_csv_path
为空,则将默认为包含的短期利率数据 csv(自 1956 年以来的数据)。
Parameters:
data
(str
): The short term interest rate data.data
(str
):短期利率数据。
def calc_overnight_rate(self, instrument_id: InstrumentId, date: date):
"""
Return the rollover interest rate between the given base currency and quote currency.
Parameters
----------
instrument_id : InstrumentId
The forex instrument ID for the calculation.
date : date
The date for the overnight rate.
Returns
-------
Decimal
Raises
------
ValueError
If instrument_id.symbol length is not in range [6, 7].
"""
...
def get_rate_data(self) -> pd.DataFrame:
"""
Return the short-term interest rate dataframe.
Returns
-------
pd.DataFrame
"""
...
class AccountFactory
Provides a factory for creating different account types.
提供用于创建不同账户类型的工厂。
@staticmethod
def create(event: AccountState) -> Account:
"""
Create an account based on the events account type.
Parameters
----------
event : AccountState
The account state event for the creation.
Returns
-------
Account
"""
...
@staticmethod
def register_account_type(issuer: str, account_cls: type):
"""
Register the given custom account type for the issuer.
Parameters
----------
issuer : str
The issuer for the account.
account_cls : type
The custom account type.
Raises
------
KeyError
If issuer has already registered a custom account type.
"""
...
@staticmethod
def register_calculated_account(issuer: str):
"""
Register for account state of the given issuer to be calculated from order fills.
Parameters
----------
issuer : str
The issuer for the account.
Raises
------
KeyError
If an issuer has already been registered for the issuer.
"""
...
class AccountsManager
Provides account management functionality.
提供账户管理功能。
Parameters:
cache
(CacheFacade
): The read-only cache for the manager.cache
(CacheFacade
):管理器的只读缓存。logger
(Logger
): The logger for the manager.logger
(Logger
):管理器的记录器。clock
(Clock
): The clock for the manager.clock
(Clock
):管理器的时钟。