Skip to main content

WalletAccount

Struct WalletAccount 

Source
pub struct WalletAccount {
    pub base: BaseAccount,
    pub balances_locked: AHashMap<(InstrumentId, Currency), Money>,
}

Fields§

§base: BaseAccount§balances_locked: AHashMap<(InstrumentId, Currency), Money>

Per-(instrument, currency) locked balances (transient, not persisted).

Implementations§

Source§

impl WalletAccount

Source

pub fn new_checked( event: AccountState, calculate_account_state: bool, ) -> CorrectnessResult<Self>

Creates a new WalletAccount instance.

§Errors

Returns an error if the initial event is not a valid wallet account state.

Source

pub fn new(event: AccountState, calculate_account_state: bool) -> Self

Creates a new WalletAccount instance.

§Panics

Panics if the initial event is not a valid wallet account state.

Source

pub fn update_balance_locked( &mut self, instrument_id: InstrumentId, locked: Money, ) -> Result<()>

Updates the locked balance for the given instrument and currency.

§Errors

Returns an error if locked is negative, the wallet has no observed balance for its currency, or the local reservations cannot produce a valid balance.

Source

pub fn clear_balance_locked(&mut self, instrument_id: InstrumentId)

Clears all locked balances for the given instrument ID.

Source

pub fn update_balances(&mut self, balances: &[AccountBalance]) -> Result<()>

Updates the account balances, rejecting negative totals.

A wallet balance is an observed on-chain amount and cannot become negative.

§Errors

Returns an error if any balance has a negative total, currencies are duplicated, or the local reservations cannot produce valid balances.

Source

pub const fn is_unleveraged(&self) -> bool

Source

pub fn recalculate_balance(&mut self, currency: Currency)

Recalculates the account balance for the specified currency based on per-instrument locks.

Sums all per-instrument locked amounts for the currency and updates the balance. If the total locked exceeds the total balance, clamps to total (free = 0).

Source§

impl WalletAccount

Source

pub fn py_new( event: AccountState, calculate_account_state: bool, ) -> PyResult<Self>

Creates a new WalletAccount instance.

Methods from Deref<Target = BaseAccount>§

Source

pub fn base_balance( &self, currency: Option<Currency>, ) -> Option<&AccountBalance>

Returns a reference to the AccountBalance for the specified currency, or None if absent.

§Panics

Panics if currency is None and self.base_currency is None.

Source

pub fn base_balance_total(&self, currency: Option<Currency>) -> Option<Money>

Returns the total Money balance for the specified currency, or None if absent.

§Panics

Panics if currency is None and self.base_currency is None.

Source

pub fn base_balances_total(&self) -> IndexMap<Currency, Money>

Source

pub fn base_balance_free(&self, currency: Option<Currency>) -> Option<Money>

Returns the free Money balance for the specified currency, or None if absent.

§Panics

Panics if currency is None and self.base_currency is None.

Source

pub fn base_balances_free(&self) -> IndexMap<Currency, Money>

Source

pub fn base_balance_locked(&self, currency: Option<Currency>) -> Option<Money>

Returns the locked Money balance for the specified currency, or None if absent.

§Panics

Panics if currency is None and self.base_currency is None.

Source

pub fn base_balances_locked(&self) -> IndexMap<Currency, Money>

Source

pub fn base_last_event(&self) -> Option<AccountState>

Source

pub fn update_balances(&mut self, balances: &[AccountBalance])

Updates the account balances with the provided list of AccountBalance instances.

Note: This method does NOT validate negative balances. Derived account types (CashAccount, MarginAccount) should perform their own validation in apply():

  • MarginAccount: allows negative balances (normal for margin trading)
  • CashAccount: rejects negative unless allow_borrowing is true
Source

pub fn update_commissions(&mut self, commission: Money)

Updates the account commissions with the provided amount.

§Panics

Panics if the accumulated commission exceeds Money bounds. Operational callers should use Self::try_update_commissions when the input is not already known to fit.

Source

pub fn try_update_commissions(&mut self, commission: Money) -> Result<()>

Updates the account commissions with the provided amount.

§Errors

Returns an error if the accumulated commission exceeds Money bounds.

Source

pub fn commission(&self, currency: &Currency) -> Option<Money>

Returns the total commission for the specified currency.

Source

pub fn commissions(&self) -> AHashMap<Currency, Money>

Returns a map of all commissions by currency.

Source

pub fn base_apply(&mut self, event: AccountState)

Applies an AccountState event, updating balances.

§Panics

Panics if event.account_id does not match this account’s ID.

Source

pub fn base_purge_account_events( &mut self, ts_now: UnixNanos, lookback_secs: u64, )

Purges all account state events which are outside the lookback window.

Guaranteed to retain at least the latest event.

§Panics

Panics if the purging implementation is changed and all events are purged.

Source

pub fn base_calculate_balance_locked( &mut self, instrument: &InstrumentAny, side: OrderSide, quantity: Quantity, price: Price, use_quote_for_inverse: Option<bool>, ) -> Result<Money>

Calculates the amount of balance to lock for a new order based on the given side, quantity, and price.

§Errors

Returns an error if the locked amount cannot be represented in the target currency.

Source

pub fn base_calculate_pnls( &self, instrument: &InstrumentAny, fill: &OrderFilled, _position: Option<Position>, ) -> Result<Vec<Money>>

Calculates profit and loss amounts for a filled order.

For cash accounts, this calculates the balance impact of a fill:

  • BUY: gain base currency quantity, lose quote currency notional.
  • SELL: lose base currency quantity, gain quote currency notional.

Note: Unlike betting accounts, cash accounts do NOT cap to position quantity. The full fill quantity is used for PnL calculation.

§Errors

Returns an error if a PnL amount cannot be represented in the target currency.

Source

pub fn base_calculate_commission( &self, instrument: &InstrumentAny, last_qty: Quantity, last_px: Price, liquidity_side: LiquiditySide, use_quote_for_inverse: Option<bool>, ) -> Result<Money>

Calculates commission fees for a filled order.

§Errors

Returns an error if liquidity_side is invalid, the notional value cannot be calculated, or the commission cannot be represented in the target currency.

Trait Implementations§

Source§

impl Account for WalletAccount

Source§

fn id(&self) -> AccountId

Source§

fn account_type(&self) -> AccountType

Source§

fn base_currency(&self) -> Option<Currency>

Source§

fn is_cash_account(&self) -> bool

Source§

fn is_margin_account(&self) -> bool

Source§

fn calculated_account_state(&self) -> bool

Source§

fn balance_total(&self, currency: Option<Currency>) -> Option<Money>

Source§

fn balances_total(&self) -> IndexMap<Currency, Money>

Source§

fn balance_free(&self, currency: Option<Currency>) -> Option<Money>

Source§

fn balances_free(&self) -> IndexMap<Currency, Money>

Source§

fn balance_locked(&self, currency: Option<Currency>) -> Option<Money>

Source§

fn balances_locked(&self) -> IndexMap<Currency, Money>

Source§

fn balance(&self, currency: Option<Currency>) -> Option<&AccountBalance>

Source§

fn last_event(&self) -> Option<AccountState>

Source§

fn events(&self) -> Vec<AccountState>

Source§

fn event_count(&self) -> usize

Source§

fn currencies(&self) -> Vec<Currency>

Source§

fn starting_balances(&self) -> IndexMap<Currency, Money>

Source§

fn balances(&self) -> IndexMap<Currency, AccountBalance>

Source§

fn apply(&mut self, event: AccountState) -> Result<()>

Applies an account state event to update the account. Read more
Source§

fn purge_account_events(&mut self, ts_now: UnixNanos, lookback_secs: u64)

Source§

fn calculate_balance_locked( &mut self, instrument: &InstrumentAny, side: OrderSide, quantity: Quantity, price: Price, use_quote_for_inverse: Option<bool>, ) -> Result<Money>

Calculates locked balance for the order parameters. Read more
Source§

fn calculate_pnls( &self, instrument: &InstrumentAny, fill: &OrderFilled, position: Option<Position>, ) -> Result<Vec<Money>>

Calculates PnLs for the fill and position. Read more
Source§

fn calculate_commission( &self, instrument: &InstrumentAny, last_qty: Quantity, last_px: Price, liquidity_side: LiquiditySide, use_quote_for_inverse: Option<bool>, ) -> Result<Money>

Calculates commission for the order fill parameters. Read more
Source§

impl Clone for WalletAccount

Source§

fn clone(&self) -> WalletAccount

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for WalletAccount

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for WalletAccount

Source§

type Target = BaseAccount

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl DerefMut for WalletAccount

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl DerefToPyAny for WalletAccount

Source§

impl<'de> Deserialize<'de> for WalletAccount

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for WalletAccount

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for WalletAccount

Source§

impl From<WalletAccount> for AccountAny

Source§

fn from(v: WalletAccount) -> AccountAny

Converts to this type from the input type.
Source§

impl<'a, 'py> FromPyObject<'a, 'py> for WalletAccount
where Self: Clone,

Source§

type Error = PyClassGuardError<'a, 'py>

The type returned in the event of a conversion error. Read more
Source§

fn extract( obj: Borrowed<'a, 'py, PyAny>, ) -> Result<Self, <Self as FromPyObject<'a, 'py>>::Error>

Extracts Self from the bound smart pointer obj. Read more
Source§

impl<'py> IntoPyObject<'py> for WalletAccount

Source§

type Target = WalletAccount

The Python output type
Source§

type Output = Bound<'py, <WalletAccount as IntoPyObject<'py>>::Target>

The smart pointer type to use. Read more
Source§

type Error = PyErr

The type returned in the event of a conversion error.
Source§

fn into_pyobject( self, py: Python<'py>, ) -> Result<<Self as IntoPyObject<'_>>::Output, <Self as IntoPyObject<'_>>::Error>

Performs the conversion.
Source§

impl PartialEq for WalletAccount

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PyClass for WalletAccount

Source§

const NAME: &str = "WalletAccount"

Name of the class. Read more
Source§

type Frozen = False

Whether the pyclass is frozen. Read more
Source§

impl PyClassImpl for WalletAccount

Source§

const MODULE: Option<&str>

Module which the class will be associated with. Read more
Source§

const IS_BASETYPE: bool = false

#[pyclass(subclass)]
Source§

const IS_SUBCLASS: bool = false

#[pyclass(extends=…)]
Source§

const IS_MAPPING: bool = false

#[pyclass(mapping)]
Source§

const IS_SEQUENCE: bool = false

#[pyclass(sequence)]
Source§

const IS_IMMUTABLE_TYPE: bool = false

#[pyclass(immutable_type)]
Source§

const RAW_DOC: &'static CStr = c"\x00"

Docstring for the class provided on the struct or enum. Read more
Source§

const DOC: &'static CStr

Fully rendered class doc, including the text_signature if a constructor is defined. Read more
Source§

type Layout = <<WalletAccount as PyClassImpl>::BaseNativeType as PyClassBaseType>::Layout<WalletAccount>

Description of how this class is laid out in memory
Source§

type BaseType = PyAny

Base class
Source§

type ThreadChecker = NoopThreadChecker

This handles following two situations: Read more
Source§

type Inventory = Pyo3MethodsInventoryForWalletAccount

Source§

type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild

Immutable or mutable
Source§

type Dict = PyClassDummySlot

Specify this class has #[pyclass(dict)] or not.
Source§

type WeakRef = PyClassDummySlot

Specify this class has #[pyclass(weakref)] or not.
Source§

type BaseNativeType = PyAny

The closest native ancestor. This is PyAny by default, and when you declare #[pyclass(extends=PyDict)], it’s PyDict.
Source§

fn items_iter() -> PyClassItemsIter

Source§

fn lazy_type_object() -> &'static LazyTypeObject<Self>

§

fn dict_offset() -> Option<PyObjectOffset>

Used to provide the dictoffset slot (equivalent to tp_dictoffset)
§

fn weaklist_offset() -> Option<PyObjectOffset>

Used to provide the weaklistoffset slot (equivalent to tp_weaklistoffset
Source§

impl PyClassNewTextSignature for WalletAccount

Source§

const TEXT_SIGNATURE: &'static str = "(event, calculate_account_state)"

Source§

impl PyStubType for WalletAccount

Source§

fn type_output() -> TypeInfo

The type to be used in the output signature, i.e. return type of the Python function or methods.
§

fn type_input() -> TypeInfo

The type to be used in the input signature, i.e. the arguments of the Python function or methods. Read more
Source§

impl PyTypeInfo for WalletAccount

Source§

const NAME: &str = <Self as ::pyo3::PyClass>::NAME

👎Deprecated since 0.28.0:

prefer using ::type_object(py).name() to get the correct runtime value

Class name.
Source§

const MODULE: Option<&str> = <Self as ::pyo3::impl_::pyclass::PyClassImpl>::MODULE

👎Deprecated since 0.28.0:

prefer using ::type_object(py).module() to get the correct runtime value

Module name, if any.
Source§

fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject

Returns the PyTypeObject instance for this type.
§

fn type_object(py: Python<'_>) -> Bound<'_, PyType>

Returns the safe abstraction over the type object.
§

fn is_type_of(object: &Bound<'_, PyAny>) -> bool

Checks if object is an instance of this type or a subclass of this type.
§

fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool

Checks if object is an instance of this type.
Source§

impl Serialize for WalletAccount

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl TryInto<WalletAccount> for AccountAny

Source§

type Error = &'static str

The type returned in the event of a conversion error.
Source§

fn try_into( self, ) -> Result<WalletAccount, <Self as TryInto<WalletAccount>>::Error>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<'py, T> FromPyObjectOwned<'py> for T
where T: for<'a> FromPyObject<'a, 'py>,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<'py, T> IntoPyObjectExt<'py> for T
where T: IntoPyObject<'py>,

§

fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>

Converts self into an owned Python object, dropping type information.
§

fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>

Converts self into an owned Python object, dropping type information and unbinding it from the 'py lifetime.
§

fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>

Converts self into a Python object. Read more
§

impl<'py, T> IntoPyObjectNautilusExt<'py> for T
where T: IntoPyObjectExt<'py>,

§

fn into_py_any_unwrap(self, py: Python<'py>) -> Py<PyAny>
where Self: IntoPyObject<'py, Error = !>,

Converts self into a [Py<PyAny>] when the underlying conversion is infallible.
§

impl<T> PyErrArguments for T
where T: for<'py> IntoPyObject<'py> + Send + Sync,

§

fn arguments(self, py: Python<'_>) -> Py<PyAny>

Arguments for exception
§

impl<T> PyTypeCheck for T
where T: PyTypeInfo,

§

fn type_check(object: &Bound<'_, PyAny>) -> bool

Checks if object is an instance of Self, which may include a subtype. Read more
§

fn classinfo_object(py: Python<'_>) -> Bound<'_, PyAny>

Returns the expected type as a possible argument for the isinstance and issubclass function. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> Ungil for T
where T: Send,