Skip to main content

UserDataConnector

Struct UserDataConnector 

pub struct UserDataConnector { /* private fields */ }
Expand description

The user data connector of ALOR for one portfolio: implements [OrderExecutor], [StopOrderExecutor], [ExecutionSource] and [PortfolioSource].

Commands (place, cancel, replace) go by HTTP, one request each, never repeated: a lost answer is [Error::Unknown]. The streams (own orders, fills, positions) come over one WebSocket session that reconnects on its own. Quantities of the shared interface are pieces; the broker counts in lots, so the connector reads the lot size of an instrument once and converts both ways.

The portfolio and the exchange come from the config (ALOR_PORTFOLIO, ALOR_EXCHANGE). An order for an instrument of another exchange is refused with [Error::Config]: start a second connector for it.

The cid goes to the broker twice: as X-REQID of the command, and as the comment of the order. The comment is how it comes back in the order list, the stream and the trades.

§Examples

use brokapi_alor::limits::default_limits;
use brokapi_alor::util::instrument_id;
use brokapi_alor::{UserDataConfig, UserDataConnector};
use brokapi_core::{
    ClientOrderId, Decimal, OrderExecutor, OrderType, PlaceOrderRequest, Price, Qty, ShutdownMode, Side, dec,
};
use brokapi_runtime::BrokerLimiter;
use tokio::sync::mpsc;

let (tx, _events) = mpsc::channel(256);
let ud = UserDataConnector::start(UserDataConfig::new(cfg, "alor-ud"), BrokerLimiter::new(&default_limits()), tx).await?;
// Commands wait for the first reconcile.
ud.reconcile(&[]).await?;
let cid = ClientOrderId::new_v4();
let ack = ud
    .place(PlaceOrderRequest {
        cid,
        instrument: instrument_id("SBER", "TQBR"),
        side: Side::Buy,
        order_type: OrderType::Limit,
        qty: Qty::from(10), // one lot of SBER: 10 shares
        price: Some(Price::new(dec!(100.0))),
    })
    .await?;
assert_eq!(ack.cid, cid);
ud.cancel(cid).await?;
ud.shutdown(ShutdownMode::Immediate).await;

Implementations§

§

impl UserDataConnector

pub async fn start( cfg: UserDataConfig, limiter: Arc<BrokerLimiter>, out: Sender<Envelope>, ) -> Result<UserDataConnector, Error>

Starts the connector: gets the first access token, finds the portfolio, and opens the WebSocket session in the background. Commands wait for the first reconcile.

§Errors

[Error::Config] when the client cannot be built, the broker refuses the token, or no single portfolio can be told (the config names none and the token names several or none); [Error::Transport] when the authorization server cannot be reached.

pub fn raw(&self) -> AlorRaw

Direct access to any API method with this connector’s token and rate limiter. See AlorRaw.

pub fn portfolio_id(&self) -> &str

The portfolio this connector works with.

pub fn price_grid(&self) -> &PriceGrid

The price steps and lot sizes the connector checks orders against. Fill it with learn before you trade.

pub fn caps(&self) -> UserDataCaps

What the connector can do.

  • place_idempotency: LabelOnly. The broker’s documentation says a repeated X-REQID gets a copy of the first answer instead of a second order, but that is not checked on a live account and the window is unknown. The library never repeats a place whatever the value.
  • supports_replace and supports_stop_orders: yes. streams_executions: yes.
  • ws_rpc and ordered_command_channel: no (commands go by HTTP). sandbox: no (the test contour is a contour of its own, chosen in the config).

pub async fn shutdown(self, mode: ShutdownMode) -> ShutdownReport

Stops the connector. Unless the mode is Immediate it closes the trading gate first; Graceful also lets the bot’s channel take what is still buffered. Orders at the exchange are not touched.

Trait Implementations§

§

impl Connector for UserDataConnector

§

type Caps = UserDataCaps

The type that describes what this connector can do.
§

fn caps(&self) -> UserDataCaps

What this connector can do. Read it before you use a feature that not every broker has.
§

fn shutdown<'async_trait>( self, mode: ShutdownMode, ) -> Pin<Box<dyn Future<Output = ShutdownReport> + Send + 'async_trait>>
where UserDataConnector: 'async_trait,

Stops the connector: closes its streams, gives up commands that have no answer yet (they become [Error::Unknown], see [ShutdownReport]) and ends its tasks. Takes the connector by value, so it cannot be used afterwards.
§

impl ExecutionSource for UserDataConnector

§

fn executions<'life0, 'async_trait>( &'life0 self, range: TimeRange, ) -> Pin<Box<dyn Future<Output = Result<Vec<Execution>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, UserDataConnector: 'async_trait,

Reads your fills in range. Read more
§

fn subscribe_executions<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<SubscriptionId, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, UserDataConnector: 'async_trait,

Starts the stream of your fills. Read more
§

impl OrderExecutor for UserDataConnector

§

fn place<'life0, 'async_trait>( &'life0 self, req: PlaceOrderRequest, ) -> Pin<Box<dyn Future<Output = Result<OrderAck, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, UserDataConnector: 'async_trait,

Puts a new order and waits for the broker’s decision. Read more
§

fn cancel<'life0, 'async_trait>( &'life0 self, cid: Uuid, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, UserDataConnector: 'async_trait,

Cancels a working order and waits for the broker to confirm. Read more
§

fn replace<'life0, 'async_trait>( &'life0 self, req: ReplaceOrderRequest, ) -> Pin<Box<dyn Future<Output = Result<OrderAck, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, UserDataConnector: 'async_trait,

Changes the quantity or price of a working order. Read more
§

fn order_state<'life0, 'async_trait>( &'life0 self, cid: Uuid, ) -> Pin<Box<dyn Future<Output = Result<OrderSnapshot, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, UserDataConnector: 'async_trait,

Reads one order by its cid, as the broker shows it now. Read more
§

fn active_orders<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Vec<OrderSnapshot>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, UserDataConnector: 'async_trait,

Lists the working orders of the account. Read more
§

fn subscribe_order_state<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<SubscriptionId, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, UserDataConnector: 'async_trait,

Starts the stream of changes of your orders. Read more
§

fn reconcile<'life0, 'life1, 'async_trait>( &'life0 self, ledger: &'life1 [Uuid], ) -> Pin<Box<dyn Future<Output = Result<ReconcileReport, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, UserDataConnector: 'async_trait,

Compares your ledger with the broker and opens the trading gate. Read more
§

impl PortfolioSource for UserDataConnector

§

fn portfolio<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Portfolio, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, UserDataConnector: 'async_trait,

Reads money and positions at one moment. Read more
§

fn positions<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Vec<Position>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, UserDataConnector: 'async_trait,

Reads the positions only. Read more
§

fn balances<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Vec<Balance>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, UserDataConnector: 'async_trait,

Reads the money balances only. Read more
§

fn subscribe_portfolio<'life0, 'async_trait>( &'life0 self, opts: SubscribeOpts, ) -> Pin<Box<dyn Future<Output = Result<SubscriptionId, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, UserDataConnector: 'async_trait,

Starts a stream of portfolio updates. Read more
§

impl StopOrderExecutor for UserDataConnector

§

fn place_stop<'life0, 'async_trait>( &'life0 self, req: PlaceStopOrderRequest, ) -> Pin<Box<dyn Future<Output = Result<OrderAck, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, UserDataConnector: 'async_trait,

Puts a stop order and waits for the broker’s decision. Read more
§

fn cancel_stop<'life0, 'async_trait>( &'life0 self, stop: StopOrderRef, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, UserDataConnector: 'async_trait,

Cancels a stop order that has not triggered. Read more
§

fn active_stop_orders<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Vec<StopOrderSnapshot>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, UserDataConnector: 'async_trait,

Lists the stop orders that wait for their trigger. Read more
§

fn subscribe_stop_orders<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<SubscriptionId, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, UserDataConnector: 'async_trait,

Stream of stop order statuses. Unsupported where the broker has no such stream. Read more

Auto Trait Implementations§

Blanket Implementations§

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<T> IntoRequest<T> for T

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
§

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

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = Infallible

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<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more