Match fractional share lots and preserve order quantities across restart

This commit is contained in:
2026-12-03 07:01:24 +08:00
parent 2c4179a64e
commit 064b301ebd
27 changed files with 343 additions and 111 deletions
+27 -15
View File
@@ -1,6 +1,6 @@
use crate::{
assets,
book::gross_cents,
book::gross_micros,
command::TimeInForce,
resident::Books,
validation::{amount, phase},
@@ -24,7 +24,8 @@ pub(crate) enum Request {
Sell {
market: String,
outcome: String,
qty: i64,
qty: Option<i64>,
size_micros: Option<i64>,
min_price: Option<i64>,
tif: Option<TimeInForce>,
},
@@ -44,15 +45,25 @@ impl Request {
market,
outcome,
qty,
size_micros,
min_price,
tif,
} => (market, outcome, Side::Sell, qty, min_price, tif),
} => (
market,
outcome,
Side::Sell,
crate::lots::amount(qty, size_micros)?,
min_price,
tif,
),
};
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 { 100 - edge } else { edge });
let tif = tif.unwrap_or(TimeInForce::Fak);
amount(requested)?;
if side == Side::Buy {
amount(requested)?;
}
ensure!(
matches!(tif, TimeInForce::Fak | TimeInForce::Fok),
"estimate requires FAK or FOK"
@@ -69,7 +80,7 @@ impl Request {
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_shares(requested)?,
Side::Sell => minimum.check_microshares(requested * crate::lots::MICROS)?,
}
}
let changes = books.changes(tx)?;
@@ -78,7 +89,7 @@ impl Request {
let mut fills = books
.get(&market, &outcome)
.map(|b| match side {
Side::Buy => b.match_budget_buy(i64::MAX, limit, requested),
Side::Buy => b.match_budget_buy(i64::MAX, limit, requested * 100),
Side::Sell => b.match_incoming(&Order {
seq: i64::MAX,
side,
@@ -92,9 +103,9 @@ impl Request {
}
let fee = crate::fees::total(rate, &fills);
let shares: i64 = fills.iter().map(|f| f.quantity).sum();
let gross = gross_cents(&fills);
let gross = gross_micros(&fills);
let remaining_cash = if side == Side::Buy {
(requested - gross) * 10000 - fee
requested * 10000 - gross - fee
} else {
0
};
@@ -102,21 +113,22 @@ impl Request {
Side::Buy => remaining_cash == 0,
Side::Sell => requested == shares,
};
let mut result = json!({"asset_id":assets::id(&market,&outcome),"filled_shares":shares,
"gross_cents":gross,"best_fill_price":fills.first().map(|f|f.price),
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|f.price),
"worst_fill_price":fills.last().map(|f|f.price),
"average_price_micros":(gross*10000).checked_div(shares),
"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)});
match side {
Side::Buy => {
result["total_spent_micros"] = json!(gross * 10000 + fee);
result["unspent_cents"] = json!(remaining_cash / 10000);
result["total_spent_micros"] = json!(gross + fee);
result["unspent_cents"] =
json!(crate::inventory::decimal(remaining_cash.into(), 10000));
result["unspent_micros"] = json!(remaining_cash);
}
Side::Sell => {
result["net_proceeds_micros"] = json!(gross * 10000 - fee);
result["unsold_shares"] = json!(requested - shares);
result["net_proceeds_micros"] = json!(gross - fee);
result["unsold_shares"] = json!(crate::lots::display(requested - shares));
}
}
Ok(result)