Perp: Move best_price/impact_price from Orderbook to BookSide
This commit is contained in:
parent
886a6696da
commit
0a86257309
|
@ -40,36 +40,6 @@ impl<'a> Orderbook<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn best_price(&self, now_ts: u64, oracle_price_lots: i64, side: Side) -> Option<i64> {
|
|
||||||
Some(
|
|
||||||
self.bookside(side)
|
|
||||||
.iter_valid(now_ts, oracle_price_lots)
|
|
||||||
.next()?
|
|
||||||
.price_lots,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Walk up the book `quantity` units and return the price at that level. If `quantity` units
|
|
||||||
/// not on book, return None
|
|
||||||
pub fn impact_price(
|
|
||||||
&self,
|
|
||||||
side: Side,
|
|
||||||
quantity: i64,
|
|
||||||
now_ts: u64,
|
|
||||||
oracle_price_lots: i64,
|
|
||||||
) -> Option<i64> {
|
|
||||||
let mut sum: i64 = 0;
|
|
||||||
let bookside = self.bookside(side);
|
|
||||||
let iter = bookside.iter_valid(now_ts, oracle_price_lots);
|
|
||||||
for order in iter {
|
|
||||||
cm!(sum += order.node.quantity);
|
|
||||||
if sum >= quantity {
|
|
||||||
return Some(order.price_lots);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub fn new_order(
|
pub fn new_order(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
|
|
@ -3,6 +3,7 @@ use num_enum::{IntoPrimitive, TryFromPrimitive};
|
||||||
use static_assertions::const_assert_eq;
|
use static_assertions::const_assert_eq;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::util::checked_math as cm;
|
||||||
|
|
||||||
#[derive(
|
#[derive(
|
||||||
Eq,
|
Eq,
|
||||||
|
@ -158,6 +159,28 @@ impl BookSide {
|
||||||
}
|
}
|
||||||
sum
|
sum
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return the price of the order closest to the spread
|
||||||
|
pub fn best_price(&self, now_ts: u64, oracle_price_lots: i64) -> Option<i64> {
|
||||||
|
Some(
|
||||||
|
self.iter_valid(now_ts, oracle_price_lots)
|
||||||
|
.next()?
|
||||||
|
.price_lots,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Walk up the book `quantity` units and return the price at that level. If `quantity` units
|
||||||
|
/// not on book, return None
|
||||||
|
pub fn impact_price(&self, quantity: i64, now_ts: u64, oracle_price_lots: i64) -> Option<i64> {
|
||||||
|
let mut sum: i64 = 0;
|
||||||
|
for order in self.iter_valid(now_ts, oracle_price_lots) {
|
||||||
|
cm!(sum += order.node.quantity);
|
||||||
|
if sum >= quantity {
|
||||||
|
return Some(order.price_lots);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -99,8 +99,9 @@ impl Order {
|
||||||
order_book: &Orderbook,
|
order_book: &Orderbook,
|
||||||
) -> i64 {
|
) -> i64 {
|
||||||
if order_type == PostOrderType::PostOnlySlide {
|
if order_type == PostOrderType::PostOnlySlide {
|
||||||
if let Some(best_other_price) =
|
if let Some(best_other_price) = order_book
|
||||||
order_book.best_price(now_ts, oracle_price_lots, self.side.invert_side())
|
.bookside(self.side.invert_side())
|
||||||
|
.best_price(now_ts, oracle_price_lots)
|
||||||
{
|
{
|
||||||
post_only_slide_limit(self.side, best_other_price, price_lots)
|
post_only_slide_limit(self.side, best_other_price, price_lots)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -221,8 +221,12 @@ impl PerpMarket {
|
||||||
let oracle_price_lots = self.native_price_to_lot(oracle_price);
|
let oracle_price_lots = self.native_price_to_lot(oracle_price);
|
||||||
|
|
||||||
// Get current book price & compare it to index price
|
// Get current book price & compare it to index price
|
||||||
let bid = book.impact_price(Side::Bid, self.impact_quantity, now_ts, oracle_price_lots);
|
let bid =
|
||||||
let ask = book.impact_price(Side::Ask, self.impact_quantity, now_ts, oracle_price_lots);
|
book.bookside(Side::Bid)
|
||||||
|
.impact_price(self.impact_quantity, now_ts, oracle_price_lots);
|
||||||
|
let ask =
|
||||||
|
book.bookside(Side::Ask)
|
||||||
|
.impact_price(self.impact_quantity, now_ts, oracle_price_lots);
|
||||||
|
|
||||||
let diff_price = match (bid, ask) {
|
let diff_price = match (bid, ask) {
|
||||||
(Some(bid), Some(ask)) => {
|
(Some(bid), Some(ask)) => {
|
||||||
|
|
Loading…
Reference in New Issue