Derive market order protection from visible opposing depth

This commit is contained in:
2026-12-03 07:18:25 +08:00
parent f90605bfc7
commit 5d82be0091
10 changed files with 201 additions and 33 deletions
+36
View File
@@ -43,6 +43,42 @@ impl Books {
self.markets.get(market)?.get(outcome)
}
/// The cash input to an exchange command is in cents, the share input in lots.
pub fn protection_price(
&self,
market: &str,
outcome: &str,
side: Side,
requested: i64,
limit: Option<i64>,
tif: crate::command::TimeInForce,
) -> Option<i64> {
limit.or_else(|| {
// The reference Python SDK sums one binary64 value per public
// price level, in dollars (BUY) or shares (SELL). Match its limit
// selection here; all execution and settlement use integer units.
let target = requested as f64 / 100.0;
let mut depth = 0.0;
let mut last = None;
for (price, lots) in self.get(market, outcome)?.opposing_levels(side) {
let shares = lots as f64 / crate::lots::PER_SHARE as f64;
depth += match side {
Side::Buy => shares * (price as f64 / crate::tick_size::PAR as f64),
Side::Sell => shares,
};
last = Some(price);
if depth >= target {
return last;
}
}
if tif == crate::command::TimeInForce::Fak {
last
} else {
None
}
})
}
pub fn recover(tx: &Transaction) -> Result<Self> {
let mut books = Self::default();
let mut stmt = tx.prepare(