Expand description
The public surface of brokapi: domain types, capability traits, events and errors.
This is the core crate of the workspace. It has no network code and does not depend on any
broker. A broker connector (brokapi-bcs, brokapi-tbank, brokapi-finam) implements the traits
of this crate, and your program is written against them, so the same code runs on every broker.
Documentation of the whole library, with guides: https://brokapi.ru/docs/.
§What is here
| Part | Items |
|---|---|
| Traits | OrderBookSource, MarketDataSource, InstrumentSource (market data), OrderExecutor, ExecutionSource, PortfolioSource, StopOrderExecutor (your account) |
| Events | Envelope around an Event, delivered into a tokio::sync::mpsc channel you give to the connector |
| Errors | Error with ErrorClass, one enum for all brokers |
| Capabilities | MarketDataCaps, UserDataCaps: what a connector can do, as data |
| Types | InstrumentId, OrderBook, PlaceOrderRequest, OrderSnapshot, Execution, Portfolio, Price, Qty, UnixNanos and the rest of types |
| Order ids | ClientOrderId and the CidCodec that maps it to a broker’s form |
§Rules to know before you trade
- You make the
ClientOrderIdof every order and keep a ledger of them. - Trading commands are refused until
OrderExecutor::reconcilesucceeds once (Error::NotReconciled). - A command with no answer is
Error::Unknown. The library never repeats a command, and neither should you before you have looked at the order. - Read your event channel steadily. A subscription that must not lose events overflows loudly
(
Event::Overflow) and trading stops (Error::Degraded) until you reconcile. - Shutdown never cancels orders at the exchange.
§Examples
Read an order book and handle the error by its class. The connector here is the BCS one on an in-process imitation of the broker, so the example runs without a network.
use brokapi_bcs::limits::default_limits;
use brokapi_bcs::{MarketDataConfig, MarketDataConnector};
use brokapi_core::{
ErrorClass, InstrumentId, InstrumentKey, NativeId, OrderBook, OrderBookSource, ShutdownMode,
};
use brokapi_runtime::BrokerLimiter;
// The connector writes stream events into this channel. A plain read does not need it.
let (events_tx, _events) = tokio::sync::mpsc::channel(256);
let limiter = BrokerLimiter::new(&default_limits());
let md = MarketDataConnector::start(MarketDataConfig::new(cfg, "bcs-md"), limiter, events_tx).await?;
let sber = InstrumentId::new(
InstrumentKey::new("SBER", "TQBR"),
NativeId::TickerClass { ticker: "SBER".into(), class_code: "TQBR".into() },
);
match md.order_book(&sber, 5).await {
Ok(OrderBook::Snapshot { bids, asks, .. }) => {
println!("best bid {:?}, best ask {:?}", bids.first(), asks.first());
}
Ok(_) => unreachable!("a read answers a snapshot"),
Err(e) if e.class() == ErrorClass::Retryable => println!("try again later: {e}"),
Err(e) => println!("will not work as it is: {e}"),
}
md.shutdown(ShutdownMode::Immediate).await;Re-exports§
pub use cid::Charset;pub use cid::CidCaps;pub use cid::CidCodec;pub use cid::CidError;pub use cid::HexUuidCodec;pub use cid::NativeCid;pub use error::ConfigError;pub use error::Error;pub use error::ErrorClass;pub use error::NOT_FOUND;pub use error::TransportError;pub use traits::Connector;pub use traits::ExecutionSource;pub use traits::InstrumentSource;pub use traits::MarketDataSource;pub use traits::OrderBookSource;pub use traits::OrderExecutor;pub use traits::PortfolioSource;pub use traits::StopOrderExecutor;pub use types::*;
Modules§
- cid
- The client order id codec: the mapping between the library’s
ClientOrderIdand the form a broker accepts. - error
- The error type of every connector call and the rule for what to do with each error.
- traits
- The capability traits: what a connector can do, split by what it needs.
- types
- Domain types: everything the traits take and return, flat re-exported at the crate root.
Macros§
- dec
- Makes a
Decimalfrom a literal:dec!(250.15). NeedsDecimalin scope (use brokapi_core::{Decimal, dec};). Transform a literal number directly to aDecimalat compile time.
Structs§
- Decimal
- The exact decimal number inside
Price,QtyandMoney(from therust_decimalcrate).Decimalrepresents a 128 bit representation of a fixed-precision decimal number. The finite set of values of typeDecimalare of the form m / 10e, where m is an integer such that -296 < m < 296, and e is an integer between 0 and 28 inclusive.