Skip to main content

nanobook_broker/
lib.rs

1//! Broker trait and implementations for nanobook.
2//!
3//! Provides a generic `Broker` trait that abstracts over different brokerages.
4//! Implementations:
5//!
6//! - **IBKR** (feature `ibkr`): Interactive Brokers via TWS API
7//! - **Binance** (feature `binance`): Binance spot REST API
8
9pub mod error;
10pub mod mock;
11pub mod types;
12
13#[cfg(feature = "ibkr")]
14pub mod ibkr;
15
16#[cfg(feature = "binance")]
17pub mod binance;
18
19pub use error::BrokerError;
20pub use types::*;
21
22use nanobook::Symbol;
23
24/// A broker connection that can fetch positions, submit orders, and get quotes.
25pub trait Broker {
26    /// Connect to the broker.
27    fn connect(&mut self) -> Result<(), BrokerError>;
28
29    /// Disconnect gracefully.
30    fn disconnect(&mut self) -> Result<(), BrokerError>;
31
32    /// Get all current positions.
33    fn positions(&self) -> Result<Vec<Position>, BrokerError>;
34
35    /// Get account summary (equity, buying power, etc.).
36    fn account(&self) -> Result<Account, BrokerError>;
37
38    /// Submit an order. Returns order ID.
39    fn submit_order(&self, order: &BrokerOrder) -> Result<OrderId, BrokerError>;
40
41    /// Get status of a submitted order.
42    fn order_status(&self, id: OrderId) -> Result<BrokerOrderStatus, BrokerError>;
43
44    /// Cancel a pending order.
45    fn cancel_order(&self, id: OrderId) -> Result<(), BrokerError>;
46
47    /// Get current quote for a symbol.
48    fn quote(&self, symbol: &Symbol) -> Result<Quote, BrokerError>;
49}