修复 AiQuant 微盘回测撮合语义

This commit is contained in:
boris
2026-05-13 18:43:02 +08:00
parent 2165831708
commit db72f6f515
8 changed files with 849 additions and 162 deletions

View File

@@ -2918,8 +2918,9 @@ where
let minimum_order_quantity = self.minimum_order_quantity(data, symbol);
let order_step_size = self.order_step_size(data, symbol);
let price = self.sizing_price(snapshot);
let snapshot_requested_qty = self.round_buy_quantity(
((value.abs()) / price).floor() as u32,
let snapshot_requested_qty = self.value_buy_quantity(
value.abs(),
price,
minimum_order_quantity,
order_step_size,
);
@@ -3012,8 +3013,9 @@ where
let minimum_order_quantity = self.minimum_order_quantity(data, symbol);
let order_step_size = self.order_step_size(data, symbol);
let price = self.sizing_price(snapshot);
let snapshot_requested_qty = self.round_buy_quantity(
((value.abs()) / price).floor() as u32,
let snapshot_requested_qty = self.value_buy_quantity(
value.abs(),
price,
minimum_order_quantity,
order_step_size,
);
@@ -3178,8 +3180,9 @@ where
let minimum_order_quantity = self.minimum_order_quantity(data, symbol);
let order_step_size = self.order_step_size(data, symbol);
let price = self.sizing_price(snapshot);
let snapshot_requested_qty = self.round_buy_quantity(
(value.abs() / price).floor() as u32,
let snapshot_requested_qty = self.value_buy_quantity(
value.abs(),
price,
minimum_order_quantity,
order_step_size,
);
@@ -3396,10 +3399,15 @@ where
requested_qty
}
fn value_budget_gross_limit(&self, value_budget: Option<f64>) -> Option<f64> {
fn value_buy_gross_limit(
&self,
value_budget: Option<f64>,
requested_qty: u32,
reference_price: f64,
) -> Option<f64> {
value_budget.map(|budget| {
if self.strict_value_budget {
budget
budget.max(reference_price * requested_qty as f64)
} else {
budget + 400.0
}
@@ -3562,6 +3570,8 @@ where
return Ok(());
}
};
let value_gross_limit =
self.value_buy_gross_limit(value_budget, constrained_qty, self.sizing_price(snapshot));
let fill = self.resolve_execution_fill(
date,
@@ -3577,7 +3587,7 @@ where
execution_cursors,
None,
Some(portfolio.cash()),
self.value_budget_gross_limit(value_budget),
value_gross_limit,
algo_request,
limit_price,
);
@@ -3608,7 +3618,7 @@ where
let filled_qty = self.affordable_buy_quantity(
date,
portfolio.cash(),
self.value_budget_gross_limit(value_budget),
value_gross_limit,
execution_price,
constrained_qty,
self.minimum_order_quantity(data, symbol),
@@ -3619,7 +3629,7 @@ where
partial_fill_reason,
self.buy_reduction_reason(
portfolio.cash(),
self.value_budget_gross_limit(value_budget),
value_gross_limit,
execution_price,
constrained_qty,
filled_qty,
@@ -4054,6 +4064,26 @@ where
}
}
fn value_buy_quantity(
&self,
value_budget: f64,
price: f64,
minimum_order_quantity: u32,
order_step_size: u32,
) -> u32 {
if !value_budget.is_finite() || value_budget <= 0.0 || !price.is_finite() || price <= 0.0 {
return 0;
}
let minimum = minimum_order_quantity.max(1);
let step = order_step_size.max(1);
if price * minimum as f64 > value_budget + 1e-6 {
return 0;
}
let raw_steps = (value_budget / price / step as f64).round();
let requested = ((raw_steps.max(1.0) as u32) * step).max(minimum);
self.round_buy_quantity(requested, minimum_order_quantity, order_step_size)
}
fn decrement_order_quantity(
&self,
quantity: u32,
@@ -4340,7 +4370,7 @@ where
.filter(|quote| {
!start_cursor.is_some_and(|cursor| quote.timestamp < cursor)
&& !end_cursor.is_some_and(|cursor| quote.timestamp > cursor)
&& quote.volume_delta != 0
&& self.quote_has_executable_liquidity(quote, side, matching_type)
})
.collect();
let mut filled_qty = 0_u32;
@@ -4466,6 +4496,24 @@ where
})
}
fn quote_has_executable_liquidity(
&self,
quote: &IntradayExecutionQuote,
side: OrderSide,
matching_type: MatchingType,
) -> bool {
if quote.volume_delta != 0 {
return true;
}
if matches!(matching_type, MatchingType::Vwap | MatchingType::Twap) {
return false;
}
match side {
OrderSide::Buy => quote.ask1_volume > 0,
OrderSide::Sell => quote.bid1_volume > 0,
}
}
fn uses_serial_execution_cursor(&self, reason: &str) -> bool {
let _ = reason;
false