修复AiQuant回测撮合一致性

This commit is contained in:
boris
2026-05-20 12:09:01 +08:00
parent 6e54471e57
commit db8b0bf142
7 changed files with 1327 additions and 76 deletions
+12 -12
View File
@@ -313,6 +313,7 @@ pub struct BacktestEngine<S, C, R> {
broker: BrokerSimulator<C, R>,
config: BacktestConfig,
dividend_reinvestment: bool,
cash_dividends_enabled: bool,
process_event_bus: ProcessEventBus,
dynamic_universe: Option<BTreeSet<String>>,
subscriptions: BTreeSet<String>,
@@ -338,6 +339,7 @@ impl<S, C, R> BacktestEngine<S, C, R> {
broker,
config,
dividend_reinvestment: false,
cash_dividends_enabled: true,
process_event_bus: ProcessEventBus::new(),
dynamic_universe: None,
subscriptions: BTreeSet::new(),
@@ -356,6 +358,11 @@ impl<S, C, R> BacktestEngine<S, C, R> {
self
}
pub fn with_cash_dividends(mut self, enabled: bool) -> Self {
self.cash_dividends_enabled = enabled;
self
}
pub fn with_futures_account(mut self, account: FuturesAccountState) -> Self {
self.futures_account = Some(account);
self
@@ -2521,7 +2528,7 @@ where
continue;
}
if action.share_cash.abs() > f64::EPSILON {
if self.cash_dividends_enabled && action.share_cash.abs() > f64::EPSILON {
let cash_before = portfolio.cash();
let (cash_delta, quantity_after, average_cost) = {
let position = portfolio
@@ -2990,24 +2997,17 @@ where
}
let quantity = position.quantity;
let fallback_reference_price = if position.last_price > 0.0 {
let settlement_price = if position.last_price.is_finite() && position.last_price > 0.0 {
position.last_price
} else {
} else if position.average_cost.is_finite() && position.average_cost > 0.0 {
position.average_cost
} else {
0.0
};
let effective_delisted_at = instrument
.delisted_at
.or_else(|| self.data.calendar().previous_day(date))
.unwrap_or(date);
let settlement_price = self
.data
.price_on_or_before(effective_delisted_at, &symbol, PriceField::Close)
.or_else(|| {
self.data
.price_on_or_before(date, &symbol, PriceField::Close)
})
.filter(|price| price.is_finite() && *price > 0.0)
.unwrap_or(fallback_reference_price);
if !settlement_price.is_finite() || settlement_price <= 0.0 {
return Err(BacktestError::Execution(format!(
"missing delisting settlement price for {} on {}",