Repository navigation
Expand file tree
/
Copy pathtest.rs
More file actions
188 lines (172 loc) · 6.45 KB
/
Copy pathtest.rs
File metadata and controls
188 lines (172 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use std::{collections::BTreeMap};
use uuid::Uuid;
pub struct OpenOrder {
pub user_id: Uuid,
pub qty: u32,
pub filled_qty: u32,
pub original_order_id: Uuid,
}
pub struct Bid {
pub total_qty: u32,
pub orders: Vec<OpenOrder>
}
pub struct Ask {
pub total_qty: u32,
pub orders: Vec<OpenOrder>
}
pub struct Orderbook {
pub bids: BTreeMap<u32, Bid>,
pub asks: BTreeMap<u32, Ask>,
pub current_order_index: u32,
pub symbol: String,
}
impl Orderbook {
pub fn new(symbol: String) -> Self {
Self {
bids: BTreeMap::new(),
asks: BTreeMap::new(),
current_order_index: 0,
symbol
}
}
}
pub enum Side {
Bid,
Ask
}
#[derive(Debug)]
pub struct Match {
pub taker: Uuid,
pub maker: Uuid,
pub price: u32,
pub qty: u32
}
#[derive(Debug)]
pub struct OrderResponse {
pub filled_qty: u32, // how much of the qty was filled immedietely
pub on_book: u32, // how much of the qty was put on the book incase a limit order didnt fill completely
pub left_qty: u32, // How much qty was left to be filled for a market order incase the book wasnt liquid
pub matches: Vec<Match>
}
impl Orderbook {
pub fn create_order(&mut self, side: Side, qty: u32, user_id: Uuid, original_order_id: Uuid, price: Option<u32>) -> OrderResponse {
let mut remaining = qty;
let mut filled_qty = 0u32;
let mut matches: Vec<Match> = vec![];
let mut prices_to_remove: Vec<u32> = vec![];
match side {
Side::Bid => {
// Match against asks in ascending price order (lowest ask = best ask first)
for (ask_price, ask_level) in self.asks.iter_mut() {
if remaining == 0 { break; }
// For limit orders, only match if ask price is within bid limit
if let Some(bid_limit) = price {
if *ask_price > bid_limit { break; }
}
let mut level_fill = 0u32;
for order in ask_level.orders.iter_mut() {
if remaining == 0 { break; }
let available = order.qty - order.filled_qty;
if available == 0 { continue; }
let fill = available.min(remaining);
order.filled_qty += fill;
level_fill += fill;
filled_qty += fill;
remaining -= fill;
matches.push(Match {
taker: user_id,
maker: order.user_id,
price: *ask_price,
qty: fill,
});
}
ask_level.total_qty -= level_fill;
if ask_level.total_qty == 0 {
prices_to_remove.push(*ask_price);
}
}
for p in &prices_to_remove {
self.asks.remove(p);
}
// Place remaining qty on the book (limit orders only)
let on_book = if let Some(bid_limit) = price {
if remaining > 0 {
let level = self.bids.entry(bid_limit).or_insert(Bid { total_qty: 0, orders: vec![] });
level.total_qty += remaining;
level.orders.push(OpenOrder {
user_id,
qty: remaining,
filled_qty: 0,
original_order_id,
});
}
remaining
} else {
0
};
OrderResponse {
filled_qty,
on_book,
left_qty: if price.is_none() { remaining } else { 0 },
matches,
}
}
Side::Ask => {
// Match against bids in descending price order (highest bid = best bid first)
for (bid_price, bid_level) in self.bids.iter_mut().rev() {
if remaining == 0 { break; }
// For limit orders, only match if bid price meets ask limit
if let Some(ask_limit) = price {
if *bid_price < ask_limit { break; }
}
let mut level_fill = 0u32;
for order in bid_level.orders.iter_mut() {
if remaining == 0 { break; }
let available = order.qty - order.filled_qty;
if available == 0 { continue; }
let fill = available.min(remaining);
order.filled_qty += fill;
level_fill += fill;
filled_qty += fill;
remaining -= fill;
matches.push(Match {
taker: user_id,
maker: order.user_id,
price: *bid_price,
qty: fill,
});
}
bid_level.total_qty -= level_fill;
if bid_level.total_qty == 0 {
prices_to_remove.push(*bid_price);
}
}
for p in &prices_to_remove {
self.bids.remove(p);
}
// Place remaining qty on the book (limit orders only)
let on_book = if let Some(ask_limit) = price {
if remaining > 0 {
let level = self.asks.entry(ask_limit).or_insert(Ask { total_qty: 0, orders: vec![] });
level.total_qty += remaining;
level.orders.push(OpenOrder {
user_id,
qty: remaining,
filled_qty: 0,
original_order_id,
});
}
remaining
} else {
0
};
OrderResponse {
filled_qty,
on_book,
left_qty: if price.is_none() { remaining } else { 0 },
matches,
}
}
}
}
}