barter_execution/
lib.rs

1#![forbid(unsafe_code)]
2#![warn(
3    unused,
4    clippy::cognitive_complexity,
5    unused_crate_dependencies,
6    unused_extern_crates,
7    clippy::unused_self,
8    clippy::useless_let_if_seq,
9    missing_debug_implementations,
10    rust_2018_idioms,
11    rust_2024_compatibility
12)]
13#![allow(clippy::type_complexity, clippy::too_many_arguments, type_alias_bounds)]
14
15//! # Barter-Execution
16//! Stream private account data from financial venues, and execute (live or mock) orders. Also provides
17//! a feature rich MockExchange and MockExecutionClient to assist with backtesting and paper-trading.
18//!
19//! **It is:**
20//! * **Easy**: ExecutionClient trait provides a unified and simple language for interacting with exchanges.
21//! * **Normalised**: Allow your strategy to communicate with every real or MockExchange using the same interface.
22//! * **Extensible**: Barter-Execution is highly extensible, making it easy to contribute by adding new exchange integrations!
23//!
24//! See `README.md` for more information and examples.
25
26use crate::{
27    balance::AssetBalance,
28    order::{Order, OrderSnapshot, request::OrderResponseCancel},
29    trade::Trade,
30};
31use barter_instrument::{
32    asset::{AssetIndex, QuoteAsset, name::AssetNameExchange},
33    exchange::{ExchangeId, ExchangeIndex},
34    instrument::{InstrumentIndex, name::InstrumentNameExchange},
35};
36use barter_integration::snapshot::Snapshot;
37use derive_more::{Constructor, From};
38use order::state::OrderState;
39use serde::{Deserialize, Serialize};
40
41pub mod balance;
42pub mod client;
43pub mod error;
44pub mod exchange;
45pub mod indexer;
46pub mod map;
47pub mod order;
48pub mod trade;
49
50/// Convenient type alias for an [`AccountEvent`] keyed with [`ExchangeId`],
51/// [`AssetNameExchange`], and [`InstrumentNameExchange`].
52pub type UnindexedAccountEvent =
53    AccountEvent<ExchangeId, AssetNameExchange, InstrumentNameExchange>;
54
55/// Convenient type alias for an [`AccountSnapshot`] keyed with [`ExchangeId`],
56/// [`AssetNameExchange`], and [`InstrumentNameExchange`].
57pub type UnindexedAccountSnapshot =
58    AccountSnapshot<ExchangeId, AssetNameExchange, InstrumentNameExchange>;
59
60#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
61pub struct AccountEvent<
62    ExchangeKey = ExchangeIndex,
63    AssetKey = AssetIndex,
64    InstrumentKey = InstrumentIndex,
65> {
66    pub exchange: ExchangeKey,
67    pub kind: AccountEventKind<ExchangeKey, AssetKey, InstrumentKey>,
68}
69
70impl<ExchangeKey, AssetKey, InstrumentKey> AccountEvent<ExchangeKey, AssetKey, InstrumentKey> {
71    pub fn new<K>(exchange: ExchangeKey, kind: K) -> Self
72    where
73        K: Into<AccountEventKind<ExchangeKey, AssetKey, InstrumentKey>>,
74    {
75        Self {
76            exchange,
77            kind: kind.into(),
78        }
79    }
80}
81
82#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, From)]
83pub enum AccountEventKind<ExchangeKey, AssetKey, InstrumentKey> {
84    /// Full [`AccountSnapshot`] - replaces all existing state.
85    Snapshot(AccountSnapshot<ExchangeKey, AssetKey, InstrumentKey>),
86
87    /// Single [`AssetBalance`] snapshot - replaces existing balance state.
88    BalanceSnapshot(Snapshot<AssetBalance<AssetKey>>),
89
90    /// Single [`Order`] snapshot - used to upsert existing order state if it's more recent.
91    ///
92    /// This variant covers general order updates, and open order responses.
93    OrderSnapshot(Snapshot<Order<ExchangeKey, InstrumentKey, OrderState<AssetKey, InstrumentKey>>>),
94
95    /// Response to an [`OrderRequestCancel<ExchangeKey, InstrumentKey>`].
96    OrderCancelled(OrderResponseCancel<ExchangeKey, AssetKey, InstrumentKey>),
97
98    /// [`Order<ExchangeKey, InstrumentKey, Open>`] partial or full-fill.
99    Trade(Trade<QuoteAsset, InstrumentKey>),
100}
101
102impl<ExchangeKey, AssetKey, InstrumentKey> AccountEvent<ExchangeKey, AssetKey, InstrumentKey>
103where
104    AssetKey: Eq,
105    InstrumentKey: Eq,
106{
107    pub fn snapshot(self) -> Option<AccountSnapshot<ExchangeKey, AssetKey, InstrumentKey>> {
108        match self.kind {
109            AccountEventKind::Snapshot(snapshot) => Some(snapshot),
110            _ => None,
111        }
112    }
113}
114
115#[derive(
116    Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize, Constructor,
117)]
118pub struct AccountSnapshot<
119    ExchangeKey = ExchangeIndex,
120    AssetKey = AssetIndex,
121    InstrumentKey = InstrumentIndex,
122> {
123    pub exchange: ExchangeKey,
124    pub balances: Vec<AssetBalance<AssetKey>>,
125    pub instruments: Vec<InstrumentAccountSnapshot<ExchangeKey, AssetKey, InstrumentKey>>,
126}
127
128#[derive(
129    Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize, Constructor,
130)]
131pub struct InstrumentAccountSnapshot<
132    ExchangeKey = ExchangeIndex,
133    AssetKey = AssetIndex,
134    InstrumentKey = InstrumentIndex,
135> {
136    pub instrument: InstrumentKey,
137    #[serde(default = "Vec::new")]
138    pub orders: Vec<OrderSnapshot<ExchangeKey, AssetKey, InstrumentKey>>,
139}
140
141impl<ExchangeKey, AssetKey, InstrumentKey> AccountSnapshot<ExchangeKey, AssetKey, InstrumentKey> {
142    pub fn assets(&self) -> impl Iterator<Item = &AssetKey> {
143        self.balances.iter().map(|balance| &balance.asset)
144    }
145
146    pub fn instruments(&self) -> impl Iterator<Item = &InstrumentKey> {
147        self.instruments.iter().map(|snapshot| &snapshot.instrument)
148    }
149}