pub struct UdCore { /* private fields */ }Expand description
Sans-io core of the user data of ONE account: order state machine, own trades, portfolio.
It links our cid to the exchange order id and the instrument, drops every message that belongs to
another account, and turns the rest into events. The connector calls the command methods
(begin_place, place_response, …) around its
requests, and feeds stream messages through [BrokerCore::on_frame] as UdFrames.
§Examples
use std::time::Instant;
use brokapi_core::{ClientOrderId, InstrumentId, InstrumentKey, NativeId, OrderType, PlaceOrderRequest, Price, Qty, Side};
use brokapi_tbank::core::UdCore;
let mut core = UdCore::new("acc-main");
let sber = InstrumentId::new(InstrumentKey::new("SBER", "TQBR"), NativeId::Uid("uid-sber".into()));
let req = PlaceOrderRequest {
cid: ClientOrderId::new_v4(),
instrument: sber,
side: Side::Buy,
order_type: OrderType::Limit,
qty: Qty::from(1),
price: Some(Price::from(250)),
};
// A cid is registered for one placement at most. The second try is refused before any send.
assert!(core.begin_place(&req, Instant::now()).is_ok());
assert!(core.begin_place(&req, Instant::now()).is_err());
assert!(core.order_state(req.cid).is_some());Implementations§
Source§impl UdCore
impl UdCore
Sourcepub fn new(account_id: impl Into<String>) -> Self
pub fn new(account_id: impl Into<String>) -> Self
A core for the account account_id. Messages with another account id are dropped.
Sourcepub fn lots(&self) -> &Arc<LotBook>
pub fn lots(&self) -> &Arc<LotBook>
The lot sizes this core knows. The connector fills the book before it feeds a message of an instrument it has not met (the messages count in lots, the events in units).
Sourcepub fn account_id(&self) -> &str
pub fn account_id(&self) -> &str
The account this core serves.
Sourcepub fn orders_mut(&mut self) -> &mut OrderRuntime
pub fn orders_mut(&mut self) -> &mut OrderRuntime
The order runtime, for the shutdown sequence.
Sourcepub fn order_state(&self, cid: ClientOrderId) -> Option<&OrderState>
pub fn order_state(&self, cid: ClientOrderId) -> Option<&OrderState>
The state this core holds for cid, or None if it never saw that cid.
Sourcepub fn take_forgotten(&mut self) -> Vec<ClientOrderId> ⓘ
pub fn take_forgotten(&mut self) -> Vec<ClientOrderId> ⓘ
Takes the cids the core forgot since the last call (orders finished long ago, see the memory window
of [OrderRuntime]). The connector drops what else it keeps for them.
Sourcepub fn take_updates(&mut self) -> Vec<SubUpdate>
pub fn take_updates(&mut self) -> Vec<SubUpdate>
Takes the subscription confirmations and refusals since the last call.
Sourcepub fn take_reconnected(&mut self) -> Vec<StreamKind>
pub fn take_reconnected(&mut self) -> Vec<StreamKind>
Streams that were reopened after a break (not the first open). The runtime refreshes order
state from GetOrders for OrderState.
Sourcepub fn add_sub(&mut self, id: SubscriptionId, kind: UdSubKind)
pub fn add_sub(&mut self, id: SubscriptionId, kind: UdSubKind)
Registers a subscription of kind under id. A later one of the same kind replaces it.
Sourcepub fn remove_sub(&mut self, kind: UdSubKind)
pub fn remove_sub(&mut self, kind: UdSubKind)
Forgets the subscription of kind, if any.
Sourcepub fn sub(&self, kind: UdSubKind) -> Option<SubscriptionId>
pub fn sub(&self, kind: UdSubKind) -> Option<SubscriptionId>
The id of the active subscription of kind, if any.
Sourcepub fn learn(&mut self, id: &InstrumentId)
pub fn learn(&mut self, id: &InstrumentId)
Teach the core an instrument (uid/figi -> ticker@class), e.g. the one an order is placed on.
Sourcepub fn resolve(
&self,
uid: &str,
figi: &str,
ticker: &str,
class_code: &str,
) -> InstrumentId
pub fn resolve( &self, uid: &str, figi: &str, ticker: &str, class_code: &str, ) -> InstrumentId
Instrument id from message fields; the ticker is filled from earlier messages when missing.
Sourcepub fn begin_place(
&mut self,
req: &PlaceOrderRequest,
now: Instant,
) -> Result<(), Error>
pub fn begin_place( &mut self, req: &PlaceOrderRequest, now: Instant, ) -> Result<(), Error>
Registers a placement before the request is sent. At most one placement per cid, ever
(no automatic retry of place).
Sourcepub fn place_response(
&mut self,
cid: ClientOrderId,
resp: &PostOrderResponse,
now: Instant,
out: &mut EventSink,
)
pub fn place_response( &mut self, cid: ClientOrderId, resp: &PostOrderResponse, now: Instant, out: &mut EventSink, )
The placement response arrived.
Sourcepub fn place_rejected(
&mut self,
cid: ClientOrderId,
reason: String,
now: Instant,
out: &mut EventSink,
)
pub fn place_rejected( &mut self, cid: ClientOrderId, reason: String, now: Instant, out: &mut EventSink, )
The broker refused the placement: nothing exists at the broker.
Sourcepub fn place_unknown(
&mut self,
cid: ClientOrderId,
now: Instant,
out: &mut EventSink,
)
pub fn place_unknown( &mut self, cid: ClientOrderId, now: Instant, out: &mut EventSink, )
Timeout or break during place: the outcome is unknown, never Rejected.
Sourcepub fn cancel_ok(
&mut self,
cid: ClientOrderId,
now: Instant,
out: &mut EventSink,
)
pub fn cancel_ok( &mut self, cid: ClientOrderId, now: Instant, out: &mut EventSink, )
The broker accepted a cancel.
Sourcepub fn begin_replace(
&mut self,
new_cid: ClientOrderId,
original: ClientOrderId,
now: Instant,
) -> Result<(), Error>
pub fn begin_replace( &mut self, new_cid: ClientOrderId, original: ClientOrderId, now: Instant, ) -> Result<(), Error>
Registers a replace before the request is sent: the new cid is placed at most once, and it inherits the instrument and side of the original when we know them.
Sourcepub fn replace_response(
&mut self,
new_cid: ClientOrderId,
resp: &PostOrderResponse,
now: Instant,
out: &mut EventSink,
)
pub fn replace_response( &mut self, new_cid: ClientOrderId, resp: &PostOrderResponse, now: Instant, out: &mut EventSink, )
The broker accepted a replace: the new cid is a new live order.
Sourcepub fn replace_failed(
&mut self,
new_cid: ClientOrderId,
unknown: bool,
reason: String,
now: Instant,
out: &mut EventSink,
)
pub fn replace_failed( &mut self, new_cid: ClientOrderId, unknown: bool, reason: String, now: Instant, out: &mut EventSink, )
The replace failed with a definite refusal or an unknown outcome.
Sourcepub fn sweep_timeouts(&mut self, now: Instant, out: &mut EventSink)
pub fn sweep_timeouts(&mut self, now: Instant, out: &mut EventSink)
Moves the placements that have had no answer for the command timeout to Unknown, one event each.
The connector calls it on a timer. It is the safety net: a command normally ends by itself, within
the request timeout.
Sourcepub fn set_command_timeout(&mut self, timeout: Duration)
pub fn set_command_timeout(&mut self, timeout: Duration)
Sets how long a placement may wait for its answer before sweep_timeouts
gives it up. Keep it above the request timeout of the connector.
Sourcepub fn abandon_inflight(
&mut self,
now: Instant,
out: &mut EventSink,
) -> Vec<ClientOrderId> ⓘ
pub fn abandon_inflight( &mut self, now: Instant, out: &mut EventSink, ) -> Vec<ClientOrderId> ⓘ
In-flight placements become Unknown at shutdown, one event each.
Sourcepub fn exchange_id(&self, cid: ClientOrderId) -> Option<&str>
pub fn exchange_id(&self, cid: ClientOrderId) -> Option<&str>
Exchange order id of a cid we know (placed here or seen on the stream).
Sourcepub fn instrument_of(&self, cid: ClientOrderId) -> Option<InstrumentId>
pub fn instrument_of(&self, cid: ClientOrderId) -> Option<InstrumentId>
Instrument of an order we know, for ordering commands per instrument.
Sourcepub fn order_basis(&self, cid: ClientOrderId) -> Option<(Qty, Option<Price>)>
pub fn order_basis(&self, cid: ClientOrderId) -> Option<(Qty, Option<Price>)>
Lots and price of an order we know, for building a replace without a round trip.
Sourcepub fn note_order(&mut self, parts: &OrderParts)
pub fn note_order(&mut self, parts: &OrderParts)
Learns ids from a GetOrderState / GetOrders answer.
Trait Implementations§
Source§impl BrokerCore for UdCore
impl BrokerCore for UdCore
Source§fn poll_transmit(&mut self) -> Option<Outbound>
fn poll_transmit(&mut self) -> Option<Outbound>
Streams are server-side and their requests are fixed: nothing to transmit.
Source§fn apply(&mut self, desired: &SubscriptionSet)
fn apply(&mut self, desired: &SubscriptionSet)
Takes the order-state and portfolio subscriptions from the set (executions: UdCore::add_sub).
Source§type Frame = UdFrame
type Frame = UdFrame
String for WebSocket text, or a decoded message.Source§fn on_frame(&mut self, frame: UdFrame, now: Instant, out: &mut EventSink)
fn on_frame(&mut self, frame: UdFrame, now: Instant, out: &mut EventSink)
now. Read moreSource§fn poll_timeout(&self) -> Option<Instant>
fn poll_timeout(&self) -> Option<Instant>
Source§fn on_timeout(&mut self, _now: Instant, _out: &mut EventSink)
fn on_timeout(&mut self, _now: Instant, _out: &mut EventSink)
now is the time the timer was due.Auto Trait Implementations§
impl Freeze for UdCore
impl RefUnwindSafe for UdCore
impl Send for UdCore
impl Sync for UdCore
impl Unpin for UdCore
impl UnsafeUnpin for UdCore
impl UnwindSafe for UdCore
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].