Expose partial fill denial reasons

This commit is contained in:
boris
2026-04-23 00:26:54 -07:00
parent c7a5bedf02
commit 406cb05146
2 changed files with 251 additions and 6 deletions

View File

@@ -24,6 +24,7 @@ struct ExecutionFill {
price: f64,
quantity: u32,
next_cursor: NaiveDateTime,
unfilled_reason: Option<&'static str>,
}
#[derive(Debug, Clone)]
@@ -914,6 +915,11 @@ where
}
let sellable = position.sellable_qty(date);
let mut partial_fill_reason = if sellable < requested_qty {
Some("sellable quantity limit".to_string())
} else {
None
};
let market_limited_qty = self.market_fillable_quantity(
snapshot,
OrderSide::Sell,
@@ -924,7 +930,16 @@ where
requested_qty >= position.quantity && sellable >= position.quantity,
);
let filled_qty = match market_limited_qty {
Ok(quantity) => quantity.min(sellable),
Ok(quantity) => {
let quantity = quantity.min(sellable);
if quantity < requested_qty {
partial_fill_reason = merge_partial_fill_reason(
partial_fill_reason,
Some("market liquidity or volume limit"),
);
}
quantity
}
Err(limit_reason) => {
report.order_events.push(OrderEvent {
date,
@@ -975,6 +990,8 @@ where
if self.uses_serial_execution_cursor(reason) {
*global_execution_cursor = Some(fill.next_cursor);
}
partial_fill_reason =
merge_partial_fill_reason(partial_fill_reason, fill.unfilled_reason);
(fill.quantity, fill.price)
} else {
(filled_qty, self.sell_price(snapshot))
@@ -1002,6 +1019,17 @@ where
} else {
OrderStatus::Filled
};
let order_reason = if status == OrderStatus::PartiallyFilled {
let detail = partial_fill_reason
.as_deref()
.unwrap_or("remaining quantity could not be filled");
report.diagnostics.push(format!(
"order_partial_fill symbol={symbol} side=sell requested={requested_qty} filled={filled_qty} reason={detail}"
));
format!("{reason}: partial fill due to {detail}")
} else {
reason.to_string()
};
report.order_events.push(OrderEvent {
date,
@@ -1011,7 +1039,7 @@ where
requested_quantity: requested_qty,
filled_quantity: filled_qty,
status,
reason: reason.to_string(),
reason: order_reason,
});
report.fill_events.push(FillEvent {
date,
@@ -1279,6 +1307,7 @@ where
return Ok(());
}
let mut partial_fill_reason = None;
let market_limited_qty = self.market_fillable_quantity(
snapshot,
OrderSide::Buy,
@@ -1289,7 +1318,12 @@ where
false,
);
let constrained_qty = match market_limited_qty {
Ok(quantity) => quantity,
Ok(quantity) => {
if quantity < requested_qty {
partial_fill_reason = Some("market liquidity or volume limit".to_string());
}
quantity
}
Err(limit_reason) => {
report.order_events.push(OrderEvent {
date,
@@ -1326,6 +1360,8 @@ where
if self.uses_serial_execution_cursor(reason) {
*global_execution_cursor = Some(fill.next_cursor);
}
partial_fill_reason =
merge_partial_fill_reason(partial_fill_reason, fill.unfilled_reason);
(fill.quantity, fill.price)
} else {
let execution_price = self.snapshot_execution_price(snapshot, OrderSide::Buy);
@@ -1338,6 +1374,18 @@ where
self.minimum_order_quantity(data, symbol),
self.order_step_size(data, symbol),
);
if filled_qty < constrained_qty {
partial_fill_reason = merge_partial_fill_reason(
partial_fill_reason,
self.buy_reduction_reason(
portfolio.cash(),
value_budget.map(|budget| budget + 400.0),
execution_price,
constrained_qty,
filled_qty,
),
);
}
(filled_qty, execution_price)
};
if filled_qty == 0 {
@@ -1349,7 +1397,12 @@ where
requested_quantity: requested_qty,
filled_quantity: 0,
status: OrderStatus::Rejected,
reason: format!("{reason}: insufficient cash after fees"),
reason: format!(
"{reason}: {}",
partial_fill_reason
.as_deref()
.unwrap_or("insufficient cash after fees")
),
});
return Ok(());
}
@@ -1376,6 +1429,17 @@ where
} else {
OrderStatus::Filled
};
let order_reason = if status == OrderStatus::PartiallyFilled {
let detail = partial_fill_reason
.as_deref()
.unwrap_or("remaining quantity could not be filled");
report.diagnostics.push(format!(
"order_partial_fill symbol={symbol} side=buy requested={requested_qty} filled={filled_qty} reason={detail}"
));
format!("{reason}: partial fill due to {detail}")
} else {
reason.to_string()
};
report.order_events.push(OrderEvent {
date,
@@ -1385,7 +1449,7 @@ where
requested_quantity: requested_qty,
filled_quantity: filled_qty,
status,
reason: reason.to_string(),
reason: order_reason,
});
report.fill_events.push(FillEvent {
date,
@@ -1547,6 +1611,26 @@ where
0
}
fn buy_reduction_reason(
&self,
cash_limit: f64,
gross_limit: Option<f64>,
price: f64,
requested_qty: u32,
filled_qty: u32,
) -> Option<&'static str> {
if filled_qty >= requested_qty {
return None;
}
if gross_limit.is_some_and(|limit| price * requested_qty as f64 > limit + 1e-6) {
Some("value budget limit")
} else if cash_limit.is_finite() {
Some("insufficient cash after fees")
} else {
None
}
}
fn market_fillable_quantity(
&self,
snapshot: &crate::data::DailyMarketSnapshot,
@@ -1653,6 +1737,13 @@ where
price: execution_price,
quantity,
next_cursor,
unfilled_reason: self.buy_reduction_reason(
cash_limit.unwrap_or(f64::INFINITY),
gross_limit,
execution_price,
requested_qty,
quantity,
),
});
}
@@ -1702,11 +1793,14 @@ where
let mut filled_qty = 0_u32;
let mut gross_amount = 0.0_f64;
let mut last_timestamp = None;
let mut budget_block_reason = None;
let mut saw_quote_after_cursor = false;
for quote in quotes {
if start_cursor.is_some_and(|cursor| quote.timestamp < cursor) {
continue;
}
saw_quote_after_cursor = true;
// Approximate JoinQuant market-order fills with the evolving L1 book after
// the decision time instead of trade VWAP. This keeps quantities/prices
@@ -1745,6 +1839,7 @@ where
while take_qty > 0 {
let candidate_gross = gross_amount + quote_price * take_qty as f64;
if gross_limit.is_some_and(|limit| candidate_gross > limit + 1e-6) {
budget_block_reason = Some("value budget limit");
take_qty = self.decrement_order_quantity(
take_qty,
minimum_order_quantity,
@@ -1755,6 +1850,7 @@ where
if candidate_gross <= cash + 1e-6 {
break;
}
budget_block_reason = Some("insufficient cash after fees");
take_qty = self.decrement_order_quantity(
take_qty,
minimum_order_quantity,
@@ -1783,6 +1879,15 @@ where
price: gross_amount / filled_qty as f64,
quantity: filled_qty,
next_cursor: last_timestamp.unwrap() + Duration::seconds(1),
unfilled_reason: if filled_qty < requested_qty {
budget_block_reason.or(if saw_quote_after_cursor {
Some("intraday quote liquidity exhausted")
} else {
Some("no execution quotes after start")
})
} else {
None
},
})
}
@@ -1792,6 +1897,17 @@ where
}
}
fn merge_partial_fill_reason(current: Option<String>, next: Option<&str>) -> Option<String> {
match (current, next) {
(Some(existing), Some(next_reason)) if !existing.contains(next_reason) => {
Some(format!("{existing}; {next_reason}"))
}
(Some(existing), _) => Some(existing),
(None, Some(next_reason)) => Some(next_reason.to_string()),
(None, None) => None,
}
}
fn price_field_name(field: PriceField) -> &'static str {
match field {
PriceField::DayOpen => "day_open",