Derive market order protection from visible opposing depth
This commit is contained in:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user