Skip to content

Commit 1116bd2

Browse files
committed
Sort asks by (price increasing, amount decreasing), bids by (price decreasing, amount decreasing)
1 parent b6e61d1 commit 1116bd2

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/feed/listener_aggregator/top_bbo.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,32 @@ impl Aggregator {
7878
for order in sliced_bids.iter() { bids.push(order); }
7979
}
8080

81-
asks.sort_by_key(|order| (order.price, order.amount));
82-
bids.sort_by_key(|order| std::cmp::Reverse((order.price, order.amount)));
81+
// sort asks by (price increasing, amount decreasing)
82+
asks.sort_unstable_by(|a, b| {
83+
if a.price == b.price {
84+
b.amount.cmp(&a.amount)
85+
} else {
86+
a.price.cmp(&b.price)
87+
}
88+
});
89+
// sort bids by (price decreasing, amount decreasing)
90+
bids.sort_unstable_by(|a, b| {
91+
if a.price == b.price {
92+
b.amount.cmp(&a.amount)
93+
} else {
94+
b.price.cmp(&a.price)
95+
}
96+
});
8397

84-
for ask in &asks {
98+
for ask in &asks[0..feed_aggregator::TOP_N_BBO] {
8599
asks_grpc.push(
86100
orderbook::Level {
87101
exchange: ask.feed.feed_name_for_grpc_service().to_owned(),
88102
price: ask.price.to_f64().unwrap(),
89103
amount: ask.amount.to_f64().unwrap()
90104
});
91105
}
92-
for bid in &bids {
106+
for bid in &bids[0..feed_aggregator::TOP_N_BBO] {
93107
bids_grpc.push(
94108
orderbook::Level {
95109
exchange: bid.feed.feed_name_for_grpc_service().to_owned(),

0 commit comments

Comments
 (0)