Skip to main content

Crate brokapi_tbank

Crate brokapi_tbank 

Source
Expand description

T-Bank (T-Invest) connectors: MarketDataConnector + UserDataConnector over gRPC.

This crate is the connector layer for T-Bank. It sits on the protocol layer brokapi-tbank-protocol (generated gRPC client, endpoints, token) and implements the capability traits of brokapi-core, so code written against those traits also works with the other brokers. Timing, rate limits and stream supervision come from brokapi-runtime and brokapi-sansio.

  • MarketDataConnector - order book, trades, quotes, candles, instrument directory. Not tied to an account.
  • UserDataConnector - orders, own trades, portfolio and stop orders of ONE account (account_id in the config). Several accounts need several connectors with one token.
  • TbankRaw (connector.raw()) - any other method of the API, with the connector’s token, endpoint and rate limiter.
  • Rate limits are per user: give every connector of one user the same Arc<BrokerLimiter> (see limits::default_limits).

T-Bank specifics worth knowing before you trade:

  • Quantities are in UNITS of the instrument (shares, bonds, contracts), as at every broker. T-Bank counts in lots, and the connectors convert both ways (see lots). An order that is not a whole number of lots is refused before it is sent. Prices are per one instrument.
  • A trading command that gets no answer is never repeated. The result is Error::Unknown and the caller checks the order first.
  • reconcile must succeed once before the first order. It opens the command gate.

The full guide is in the book: https://brokapi.ru/docs/.

§Examples

The example starts the market data connector against the in-process mock of the broker and reads an order book. Nothing touches the network.

use brokapi_core::{InstrumentId, InstrumentKey, NativeId, OrderBook, OrderBookSource, ShutdownMode};
use brokapi_runtime::BrokerLimiter;
use brokapi_tbank::limits::default_limits;
use brokapi_tbank::{MarketDataConfig, MarketDataConnector};
use tokio::sync::mpsc;

// In real code: `let cfg = brokapi_tbank_protocol::TbankConfig::sandbox("t.your-token");`
let (events_tx, _events) = mpsc::channel(256);
let limiter = BrokerLimiter::new(&default_limits());
let md = MarketDataConnector::start(MarketDataConfig::new(cfg, "tbank-md"), limiter, events_tx).await?;

let sber = InstrumentId::new(InstrumentKey::new("SBER", "TQBR"), NativeId::Uid("uid-sber".into()));
let OrderBook::Snapshot { bids, asks, .. } = md.order_book(&sber, 5).await? else {
    panic!("T-Bank answers a snapshot")
};
assert!(!bids.is_empty() && !asks.is_empty());

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

For trading see UserDataConnector: it has the full place, cancel and refusal example.

Re-exports§

pub use cid::TbankCidCodec;
pub use config::MarketDataConfig;
pub use config::UserDataConfig;
pub use core::MdCore;
pub use core::MdFrame;
pub use core::UdCore;
pub use core::UdFrame;
pub use md::MarketDataConnector;
pub use raw::TbankRaw;
pub use ud::UserDataConnector;

Modules§

cid
T-Bank client order id codec
config
Connector configuration
core
Sans-io cores: protocol state machines without sockets, timers or async runtime.
emit
Egress buffers in front of the shared channel: the code is shared with BCS, see brokapi_runtime::Emitter.
error
gRPC status -> public connector errors.
ids
Instrument identifiers and clocks. Conversions never touch the network.
limits
Loads limits.toml (section [tbank]) through BrokerLimits::from_toml_section.
lots
Lots. T-Bank counts orders, order book levels, trades and candle volumes in LOTS, the library counts in units of the instrument (see [Qty]). This module is the bridge: the lot size of each instrument, learned from the directory once and remembered, and the conversions both ways.
map
T-Bank protocol messages -> brokapi-core domain types.
md
T-Bank market data connector
raw
Direct access to any T-Bank API method (“back door”): same token, same endpoint, same [BrokerLimiter] as the connector it came from.
stream
Stream supervision: open, read, reconnect with backoff. No protocol logic here.
ud
T-Bank user data connector, one account.
util
Small shared helpers: subscription ids, call wrappers, limiter access.