bourse_book/
types.rs

1//! Type aliases and order data-structures
2
3use serde::{Deserialize, Serialize};
4
5/// Order-id
6pub type OrderId = usize;
7/// Order lookup key
8pub type OrderKey = (Side, u32, u64);
9/// Simulated time
10pub type Nanos = u64;
11/// Prices
12pub type Price = u32;
13/// Order/trade volumes
14pub type Vol = u32;
15/// Id of an agent/trader
16pub type TraderId = u32;
17/// Count of orders
18pub type OrderCount = u32;
19
20/// Market side
21#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
22pub enum Side {
23    Bid,
24    Ask,
25}
26
27impl From<bool> for Side {
28    fn from(side: bool) -> Side {
29        match side {
30            true => Self::Bid,
31            false => Self::Ask,
32        }
33    }
34}
35
36impl From<Side> for bool {
37    fn from(side: Side) -> bool {
38        match side {
39            Side::Bid => true,
40            Side::Ask => false,
41        }
42    }
43}
44
45/// Order status
46#[derive(Clone, PartialEq, Eq, Copy, Debug, Serialize, Deserialize)]
47pub enum Status {
48    /// Newly created, not placed
49    New,
50    /// Active (i.e. on the market)
51    Active,
52    /// Filled
53    Filled,
54    /// Cancelled
55    Cancelled,
56    /// Rejected, e.g. a market order
57    /// placed in a no-trading period
58    Rejected,
59}
60
61impl From<Status> for u8 {
62    fn from(status: Status) -> u8 {
63        match status {
64            Status::New => 0,
65            Status::Active => 1,
66            Status::Filled => 2,
67            Status::Cancelled => 3,
68            Status::Rejected => 4,
69        }
70    }
71}
72
73/// Order data
74#[derive(Clone, Copy, Serialize, Deserialize)]
75pub struct Order {
76    /// Order side
77    pub side: Side,
78    /// Status of the order
79    pub status: Status,
80    /// Arrival time of the order
81    pub arr_time: Nanos,
82    /// End time of the order (filled,
83    /// cancelled etc.)
84    pub end_time: Nanos,
85    /// Current volume of the order
86    pub vol: Vol,
87    /// Original volume when the
88    /// order was placed
89    pub start_vol: Vol,
90    /// Price of the order
91    pub price: Price,
92    /// Id of the trader/agent who
93    /// placed the order
94    pub trader_id: TraderId,
95    /// Id of the order
96    pub order_id: OrderId,
97}
98
99/// Trade record
100#[derive(Serialize, Deserialize)]
101pub struct Trade {
102    /// Trade time
103    pub t: Nanos,
104    /// Trade side
105    pub side: Side,
106    /// trade price
107    pub price: Price,
108    /// Trade volume
109    pub vol: Vol,
110    /// Id of the aggressive order
111    pub active_order_id: OrderId,
112    /// Id of the passive order
113    pub passive_order_id: OrderId,
114}
115
116impl Order {
117    /// Initialise a buy limit-order
118    ///
119    /// # Arguments
120    ///
121    /// - `t` - Order creation time
122    /// - `vol` - Order volume
123    /// - `price` - Limit price of the order
124    /// - `trader_id` - Id of the agent/trader
125    /// - `order_id` - Id of the order
126    ///
127    pub fn buy_limit(
128        t: Nanos,
129        vol: Vol,
130        price: Price,
131        trader_id: TraderId,
132        order_id: OrderId,
133    ) -> Order {
134        Order {
135            side: Side::Bid,
136            status: Status::New,
137            arr_time: t,
138            end_time: Nanos::MAX,
139            vol,
140            start_vol: vol,
141            price,
142            trader_id,
143            order_id,
144        }
145    }
146
147    /// Initialise a buy market-order
148    ///
149    /// # Arguments
150    ///
151    /// - `t` - Order creation time
152    /// - `vol` - Order volume
153    /// - `trader_id` - Id of the agent/trader
154    /// - `order_id` - Id of the order
155    ///
156    pub fn buy_market(t: Nanos, vol: Vol, trader_id: TraderId, order_id: OrderId) -> Order {
157        Order {
158            side: Side::Bid,
159            status: Status::New,
160            arr_time: t,
161            end_time: Nanos::MAX,
162            vol,
163            start_vol: vol,
164            price: Price::MAX,
165            trader_id,
166            order_id,
167        }
168    }
169
170    /// Initialise a sell limit-order
171    ///
172    /// # Arguments
173    ///
174    /// - `t` - Order creation time
175    /// - `vol` - Order volume
176    /// - `price` - Limit price of the order
177    /// - `trader_id` - Id of the agent/trader
178    /// - `order_id` - Id of the order
179    ///
180    pub fn sell_limit(
181        t: Nanos,
182        vol: Vol,
183        price: Price,
184        trader_id: TraderId,
185        order_id: OrderId,
186    ) -> Order {
187        Order {
188            side: Side::Ask,
189            status: Status::New,
190            arr_time: t,
191            end_time: Nanos::MAX,
192            vol,
193            start_vol: vol,
194            price,
195            trader_id,
196            order_id,
197        }
198    }
199
200    /// Initialise a sell market-order
201    ///
202    /// # Arguments
203    ///
204    /// - `t` - Order creation time
205    /// - `vol` - Order volume
206    /// - `trader_id` - Id of the agent/trader
207    /// - `order_id` - Id of the order
208    ///
209    pub fn sell_market(t: Nanos, vol: Vol, trader_id: TraderId, order_id: OrderId) -> Order {
210        Order {
211            side: Side::Ask,
212            status: Status::New,
213            arr_time: t,
214            end_time: Nanos::MAX,
215            vol,
216            start_vol: vol,
217            price: 0,
218            trader_id,
219            order_id,
220        }
221    }
222}
223
224/// Order/transaction instruction
225pub enum Event {
226    /// Place an order on the market
227    New {
228        /// Id of the order to place
229        order_id: OrderId,
230    },
231    /// Cancel an order
232    Cancellation {
233        /// Id of the order to cancel
234        order_id: OrderId,
235    },
236    /// Modify an order
237    Modify {
238        // Id of the order to modify
239        order_id: OrderId,
240        /// New price of the order
241        new_price: Option<Price>,
242        /// New volume of the order
243        new_vol: Option<Vol>,
244    },
245}
246
247/// Level 1 market data
248pub struct Level1Data {
249    /// Bid touch price
250    pub bid_price: Price,
251    /// Ask touch price
252    pub ask_price: Price,
253    /// Bid total volume
254    pub bid_vol: Vol,
255    /// Ask total volume
256    pub ask_vol: Vol,
257    /// Bid touch volume
258    pub bid_touch_vol: Vol,
259    /// Ask touch volume
260    pub ask_touch_vol: Vol,
261    /// Number of bid orders at touch
262    pub bid_touch_orders: OrderCount,
263    /// Number of ask orders at touch
264    pub ask_touch_orders: OrderCount,
265}
266
267/// Level 2 market data
268pub struct Level2Data<const N: usize> {
269    /// Bid touch price
270    pub bid_price: Price,
271    /// Ask touch price
272    pub ask_price: Price,
273    /// Bid total volume
274    pub bid_vol: Vol,
275    /// Ask total volume
276    pub ask_vol: Vol,
277    /// Volume and number of bid orders at price-levels
278    pub bid_price_levels: [(Vol, OrderCount); N],
279    /// Volume and number of ask orders at price-levels
280    pub ask_price_levels: [(Vol, OrderCount); N],
281}