nautilus_model/orderbook/
analysis.rsuse std::collections::BTreeMap;
use super::{book::OrderBook, ladder::BookPrice, level::Level};
use crate::{
enums::{BookType, OrderSide},
orderbook::error::BookIntegrityError,
types::{price::Price, quantity::Quantity},
};
#[must_use]
pub fn get_quantity_for_price(
price: Price,
order_side: OrderSide,
levels: &BTreeMap<BookPrice, Level>,
) -> f64 {
let mut matched_size: f64 = 0.0;
for (book_price, level) in levels {
match order_side {
OrderSide::Buy => {
if book_price.value > price {
break;
}
}
OrderSide::Sell => {
if book_price.value < price {
break;
}
}
_ => panic!("Invalid `OrderSide` {order_side}"),
}
matched_size += level.size();
}
matched_size
}
#[must_use]
pub fn get_avg_px_for_quantity(qty: Quantity, levels: &BTreeMap<BookPrice, Level>) -> f64 {
let mut cumulative_size_raw = 0u64;
let mut cumulative_value = 0.0;
for (book_price, level) in levels {
let size_this_level = level.size_raw().min(qty.raw - cumulative_size_raw);
cumulative_size_raw += size_this_level;
cumulative_value += book_price.value.as_f64() * size_this_level as f64;
if cumulative_size_raw >= qty.raw {
break;
}
}
if cumulative_size_raw == 0 {
0.0
} else {
cumulative_value / cumulative_size_raw as f64
}
}
#[must_use]
pub fn get_avg_px_qty_for_exposure(
target_exposure: Quantity,
levels: &BTreeMap<BookPrice, Level>,
) -> (f64, f64, f64) {
let mut cumulative_exposure = 0.0;
let mut cumulative_size_raw = 0u64;
let mut final_price = levels
.first_key_value()
.map(|(price, _)| price.value.as_f64())
.unwrap_or(0.0);
for (book_price, level) in levels {
let price = book_price.value.as_f64();
final_price = price;
let level_exposure = price * level.size_raw() as f64;
let exposure_this_level =
level_exposure.min(target_exposure.raw as f64 - cumulative_exposure);
let size_this_level = (exposure_this_level / price).floor() as u64;
cumulative_exposure += price * size_this_level as f64;
cumulative_size_raw += size_this_level;
if cumulative_exposure >= target_exposure.as_f64() {
break;
}
}
if cumulative_size_raw == 0 {
(0.0, 0.0, final_price)
} else {
let avg_price = cumulative_exposure / cumulative_size_raw as f64;
(avg_price, cumulative_size_raw as f64, final_price)
}
}
pub fn book_check_integrity(book: &OrderBook) -> Result<(), BookIntegrityError> {
match book.book_type {
BookType::L1_MBP => {
if book.bids.len() > 1 {
return Err(BookIntegrityError::TooManyLevels(
OrderSide::Buy,
book.bids.len(),
));
}
if book.asks.len() > 1 {
return Err(BookIntegrityError::TooManyLevels(
OrderSide::Sell,
book.asks.len(),
));
}
}
BookType::L2_MBP => {
for bid_level in book.bids.levels.values() {
let num_orders = bid_level.orders.len();
if num_orders > 1 {
return Err(BookIntegrityError::TooManyOrders(
OrderSide::Buy,
num_orders,
));
}
}
for ask_level in book.asks.levels.values() {
let num_orders = ask_level.orders.len();
if num_orders > 1 {
return Err(BookIntegrityError::TooManyOrders(
OrderSide::Sell,
num_orders,
));
}
}
}
BookType::L3_MBO => {}
};
if let (Some(top_bid_level), Some(top_ask_level)) = (book.bids.top(), book.asks.top()) {
let best_bid = top_bid_level.price;
let best_ask = top_ask_level.price;
if best_bid.value >= best_ask.value {
return Err(BookIntegrityError::OrdersCrossed(best_bid, best_ask));
}
}
Ok(())
}