Skip to main content

Crate brokapi_alor

Crate brokapi_alor 

Source
Expand description

ALOR Broker connectors: the market data connector and the user data connector.

This crate is the connector layer for the ALOR OpenAPI. It sits on top of the protocol crate brokapi-alor-protocol (HTTP and WebSocket clients, wire models) and the runtime crates (brokapi-runtime, brokapi-sansio), and it implements the traits of brokapi-core:

  • MarketDataConnector implements OrderBookSource, MarketDataSource and InstrumentSource. It is not tied to an account.
  • UserDataConnector implements OrderExecutor, StopOrderExecutor, ExecutionSource and PortfolioSource for one portfolio of the account the refresh token belongs to.

Both connectors start with an async fn start(config, limiter, out). Commands return their answer directly. Subscriptions deliver Envelopes into the channel out.

Anything the shared traits cannot express is reachable through connector.raw() (AlorRaw): the same token, endpoint and rate limiter, one API method at a time. limits::default_limits gives the bundled rate limits. Give every connector of one ALOR login the same limiter, because the limits belong to the login.

The full guide is at https://brokapi.ru/docs/.

§Examples

The example runs against an in-process imitation of the broker, so it needs no network and no account. With a real account, cfg is built with AlorConfig::from_env() instead.

use std::sync::Arc;
use brokapi_alor::limits::default_limits;
use brokapi_alor::{MarketDataConfig, MarketDataConnector, UserDataConfig, UserDataConnector};
use brokapi_core::{PortfolioSource, ShutdownMode};
use brokapi_runtime::BrokerLimiter;
use tokio::sync::mpsc;

// One limiter for the whole login.
let limiter: Arc<BrokerLimiter> = BrokerLimiter::new(&default_limits());
let (md_tx, _md_events) = mpsc::channel(256);
let (ud_tx, _ud_events) = mpsc::channel(256);

let md = MarketDataConnector::start(
    MarketDataConfig::new(cfg.clone(), "alor-md"),
    Arc::clone(&limiter),
    md_tx,
)
.await?;
let ud = UserDataConnector::start(UserDataConfig::new(cfg, "alor-ud"), limiter, ud_tx).await?;

let portfolio = ud.portfolio().await?;
println!("{} balances", portfolio.balances.len());

md.shutdown(ShutdownMode::Immediate).await;
ud.shutdown(ShutdownMode::Immediate).await;

Re-exports§

pub use cid::AlorCidCodec;
pub use config::MarketDataConfig;
pub use config::UserDataConfig;
pub use core::MdCore;
pub use core::UdCore;
pub use md::MarketDataConnector;
pub use raw::AlorRaw;
pub use ud::UserDataConnector;

Modules§

cid
The client order id codec: ALOR keeps a comment of up to 100 characters on every order, and a full UUID fits into it.
config
Configuration of the two connectors: the ALOR client settings plus the choices of the connector itself (source label, reconnect pauses, buffer size).
core
The sans-io cores of the ALOR connectors.
error
Maps the errors of the protocol layer ([AlorApiError]) to the public [brokapi_core::Error].
limits
Loads limits.toml (section [alor]) through BrokerLimits::from_toml_section.
map
Mapping of ALOR wire models to the domain types of brokapi-core.
md
The market data connector: order books, trades of the market, quotes, candles and the reference data of ALOR.
pump
WS event pumps: parse in the receiver task, never block on a slow consumer.
raw
Direct access to any ALOR API method (the “back door”): same refresh token and access token, same endpoint, same [BrokerLimiter] as the connector it came from.
subs
The subscription records a connector keeps, and the generator of subscription ids.
ud
The user data connector: orders, stop orders, fills and the portfolio of one ALOR portfolio.
util
Small helpers: the identity of an instrument, the timeframe labels and the time conversions.