Skip to main content

Crate brokapi_bcs

Crate brokapi_bcs 

Source
Expand description

BCS broker connectors: the market data connector and the user data connector.

This crate is the connector layer for the BCS Trade API. It sits on top of the protocol crate brokapi-bcs-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 account (the one 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() (BcsRaw): 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 BCS account the same limiter, because the limits belong to the token.

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 BcsConfig::from_env() instead.

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

// One limiter for the whole account.
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(), "bcs-md"),
    Arc::clone(&limiter),
    md_tx,
)
.await?;
let ud = UserDataConnector::start(UserDataConfig::new(cfg, "bcs-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::BcsCidCodec;
pub use config::MarketDataConfig;
pub use config::UserDataConfig;
pub use core::MdCore;
pub use core::UdCore;
pub use core::UdFrame;
pub use core::UdRestFrame;
pub use md::MarketDataConnector;
pub use raw::BcsRaw;
pub use ud::UserDataConnector;

Modules§

cid
The client order id codec: BCS accepts a full UUID as clientOrderId.
config
Configuration of the two connectors: the BCS client settings plus the choices of the connector itself (source label, reconnect pauses, buffer size).
core
The sans-io cores of the BCS connectors.
error
Maps the errors of the protocol layer ([BcsApiError]) to the public [brokapi_core::Error].
limits
Loads limits.toml (section [bcs]) through BrokerLimits::from_toml_section.
map
Mappers from the BCS wire models to the brokapi-core domain types.
md
The market data connector: order books, trades, quotes, candles and the instrument directory.
pump
WS event pumps: parse in the receiver task, never block on a slow consumer.
raw
Direct access to any BCS 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, portfolio and fills.
util
Small helpers shared by the two connectors: instrument ids, timeframes and timestamps.