pub struct AccountState { /* private fields */ }Expand description
Open orders, positions and wallet balances of one account.
Plain data (Send + Sync): it can live in any task. It does no I/O.
Implementations§
Source§impl AccountState
impl AccountState
Sourcepub fn new() -> Self
pub fn new() -> Self
An empty state; fill it with set_orders and friends or use load.
Sourcepub async fn load(client: &Client, scope: &AccountScope) -> Result<Self, Error>
pub async fn load(client: &Client, scope: &AccountScope) -> Result<Self, Error>
Create a state from REST snapshots of everything in scope.
Sourcepub async fn reload(
&mut self,
client: &Client,
scope: &AccountScope,
) -> Result<(), Error>
pub async fn reload( &mut self, client: &Client, scope: &AccountScope, ) -> Result<(), Error>
Fetch fresh REST snapshots of everything in scope and merge them
(see set_orders and
set_positions). Call it after every
(re)connect of the private stream and after
Event::Lagged.
The snapshot time is the server clock of client before the first
request, so stream updates made meanwhile are kept. On error the
categories loaded so far stay merged.
Sourcepub fn set_wallet(&mut self, balance: WalletBalance)
pub fn set_wallet(&mut self, balance: WalletBalance)
Replace the balance of balance.account_type with a REST snapshot.
Sourcepub fn set_orders(
&mut self,
category: Category,
orders: Vec<Order>,
snapshot_time: Timestamp,
)
pub fn set_orders( &mut self, category: Category, orders: Vec<Order>, snapshot_time: Timestamp, )
Merge a REST snapshot of the open orders of category.
snapshot_time is the server time of the response
(Response::time), milliseconds.
- an order from the snapshot replaces the stored one unless the stored one is newer, and is skipped if it was already closed by the stream;
- a stored order missing from the snapshot is removed, unless it was
updated after
snapshot_time(it was opened after the snapshot).
Sourcepub fn set_positions(
&mut self,
category: Category,
positions: Vec<Position>,
snapshot_time: Timestamp,
)
pub fn set_positions( &mut self, category: Category, positions: Vec<Position>, snapshot_time: Timestamp, )
Merge a REST snapshot of the positions of category.
A snapshot position replaces the stored one unless the stored one is
newer. Stored positions missing from the snapshot are kept only if
they were updated after snapshot_time.
Sourcepub fn apply(&mut self, msg: TopicMessage) -> Vec<Change>
pub fn apply(&mut self, msg: TopicMessage) -> Vec<Change>
Apply one private stream message. Topics other than order,
position and wallet are ignored.
Sourcepub fn apply_order(&mut self, msg: OrderMsg) -> Option<Change>
pub fn apply_order(&mut self, msg: OrderMsg) -> Option<Change>
Apply one order stream update. Returns None if it was stale.
Sourcepub fn apply_position(&mut self, msg: PositionMsg) -> Option<Change>
pub fn apply_position(&mut self, msg: PositionMsg) -> Option<Change>
Apply one position stream update. Returns None if it was stale.
Sourcepub fn apply_wallet(&mut self, msg: WalletMsg) -> Change
pub fn apply_wallet(&mut self, msg: WalletMsg) -> Change
Apply one wallet stream update (always a full balance).
Sourcepub fn wallet(&self, account_type: AccountType) -> Option<&WalletBalance>
pub fn wallet(&self, account_type: AccountType) -> Option<&WalletBalance>
Balance of account_type, if loaded or received.
Sourcepub fn wallets(&self) -> impl Iterator<Item = &WalletBalance>
pub fn wallets(&self) -> impl Iterator<Item = &WalletBalance>
All known wallet balances.
Sourcepub fn order_by_link_id(
&self,
category: Category,
order_link_id: &str,
) -> Option<&Order>
pub fn order_by_link_id( &self, category: Category, order_link_id: &str, ) -> Option<&Order>
Open order by the client-assigned orderLinkId.
Sourcepub fn orders_for<'a>(
&'a self,
category: Category,
symbol: &'a str,
) -> impl Iterator<Item = &'a Order>
pub fn orders_for<'a>( &'a self, category: Category, symbol: &'a str, ) -> impl Iterator<Item = &'a Order>
Open orders of one symbol.
Sourcepub fn position(
&self,
category: Category,
symbol: &str,
position_idx: PositionIdx,
) -> Option<&Position>
pub fn position( &self, category: Category, symbol: &str, position_idx: PositionIdx, ) -> Option<&Position>
Position by symbol and position index (OneWay, or Buy/Sell in hedge mode).
Sourcepub fn positions(&self) -> impl Iterator<Item = (&PositionKey, &Position)>
pub fn positions(&self) -> impl Iterator<Item = (&PositionKey, &Position)>
All known positions, including closed ones (size 0), which still carry the leverage and risk limit settings.
Sourcepub fn open_positions(&self) -> impl Iterator<Item = (&PositionKey, &Position)>
pub fn open_positions(&self) -> impl Iterator<Item = (&PositionKey, &Position)>
Positions with a non-zero size.