Derive market order protection from visible opposing depth
This commit is contained in:
@@ -65,34 +65,36 @@ impl Request {
|
||||
),
|
||||
};
|
||||
let tick = crate::tick_size::TickSize::load(tx, &market)?;
|
||||
let edge = tick.as_ref().map_or(0, |t| i64::from(t.local_step().get()));
|
||||
let limit = limit.unwrap_or(if side == Side::Buy {
|
||||
crate::tick_size::PAR - edge
|
||||
} else {
|
||||
edge
|
||||
});
|
||||
let tif = tif.unwrap_or(TimeInForce::Fak);
|
||||
ensure!(
|
||||
matches!(tif, TimeInForce::Fak | TimeInForce::Fok),
|
||||
"estimate requires FAK or FOK"
|
||||
);
|
||||
ensure!(
|
||||
matches!(outcome.as_str(), "YES" | "NO")
|
||||
&& (0..=crate::tick_size::PAR).contains(&limit),
|
||||
"invalid outcome or price"
|
||||
);
|
||||
let amounts =
|
||||
crate::market_amounts::Amounts::for_order(tick.as_ref(), side, requested, limit)?;
|
||||
ensure!(matches!(outcome.as_str(), "YES" | "NO"), "invalid outcome");
|
||||
phase(tx, &market, &["continuous"])?;
|
||||
let rate = crate::fees::rate(tx, &market)?;
|
||||
if let Some(minimum) = crate::minimum_size::MinimumSize::load(tx, &market)? {
|
||||
match side {
|
||||
Side::Buy => minimum.check_budget(requested, limit, rate)?,
|
||||
Side::Sell => minimum.check_microshares(requested * crate::lots::MICROS)?,
|
||||
}
|
||||
}
|
||||
let changes = books.changes(tx)?;
|
||||
books.apply(changes);
|
||||
let protection = books.protection_price(&market, &outcome, side, requested, limit, tif);
|
||||
let amounts = match protection {
|
||||
Some(price) => {
|
||||
crate::market_amounts::Amounts::for_order(tick.as_ref(), side, requested, price)?
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
let rate = crate::fees::rate(tx, &market)?;
|
||||
if let Some(minimum) = crate::minimum_size::MinimumSize::load(tx, &market)? {
|
||||
match (side, protection) {
|
||||
(Side::Buy, Some(price)) => minimum.check_budget(requested, price, rate)?,
|
||||
(Side::Sell, _) => minimum.check_microshares(requested * crate::lots::MICROS)?,
|
||||
(Side::Buy, None) => {}
|
||||
}
|
||||
}
|
||||
// A failed FOK estimate still reports how much depth was visible.
|
||||
let limit = protection.unwrap_or(if side == Side::Buy {
|
||||
crate::tick_size::PAR
|
||||
} else {
|
||||
0
|
||||
});
|
||||
// The hypothetical incoming order follows all resting orders in time priority.
|
||||
let mut fills = books
|
||||
.get(&market, &outcome)
|
||||
@@ -117,16 +119,18 @@ impl Request {
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let fully_fillable = match side {
|
||||
Side::Buy => remaining_cash == 0,
|
||||
Side::Sell => requested == shares,
|
||||
};
|
||||
let fully_fillable = protection.is_some()
|
||||
&& match side {
|
||||
Side::Buy => remaining_cash == 0,
|
||||
Side::Sell => requested == shares,
|
||||
};
|
||||
let mut result = json!({"asset_id":assets::id(&market,&outcome),"filled_shares":crate::lots::display(shares),
|
||||
"gross_cents":crate::inventory::decimal(gross.into(),10000),"best_fill_price":fills.first().map(|f|crate::inventory::decimal(f.price.into(),100)),
|
||||
"worst_fill_price":fills.last().map(|f|crate::inventory::decimal(f.price.into(),100)),
|
||||
"average_price_micros":(i128::from(gross)*100).checked_div(i128::from(shares)),
|
||||
"fee_micros":fee,"taker_fee_rate_ppm":rate,
|
||||
"fully_fillable":fully_fillable,"executable":shares>0 && (tif==TimeInForce::Fak || fully_fillable)});
|
||||
"protection_price_micros":protection.map(|p|p * crate::tick_size::PRICE_MICROS),
|
||||
"fully_fillable":fully_fillable,"executable":protection.is_some() && shares>0 && (tif==TimeInForce::Fak || fully_fillable)});
|
||||
if let Some(amounts) = amounts {
|
||||
result["order_amounts"] = amounts.json();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user