完善平台策略回测撮合和滑点

This commit is contained in:
boris
2026-05-28 08:59:14 +08:00
parent 3499d4aa74
commit 200d5d1f41
8 changed files with 786 additions and 92 deletions
+12 -1
View File
@@ -314,6 +314,7 @@ pub struct BacktestEngine<S, C, R> {
config: BacktestConfig,
dividend_reinvestment: bool,
cash_dividends_enabled: bool,
cash_dividend_adjusts_cost_basis: bool,
process_event_bus: ProcessEventBus,
dynamic_universe: Option<BTreeSet<String>>,
subscriptions: BTreeSet<String>,
@@ -340,6 +341,7 @@ impl<S, C, R> BacktestEngine<S, C, R> {
config,
dividend_reinvestment: false,
cash_dividends_enabled: true,
cash_dividend_adjusts_cost_basis: true,
process_event_bus: ProcessEventBus::new(),
dynamic_universe: None,
subscriptions: BTreeSet::new(),
@@ -363,6 +365,11 @@ impl<S, C, R> BacktestEngine<S, C, R> {
self
}
pub fn with_cash_dividend_cost_basis_adjustment(mut self, enabled: bool) -> Self {
self.cash_dividend_adjusts_cost_basis = enabled;
self
}
pub fn with_futures_account(mut self, account: FuturesAccountState) -> Self {
self.futures_account = Some(account);
self
@@ -2534,7 +2541,11 @@ where
let position = portfolio
.position_mut_if_exists(&action.symbol)
.expect("position exists for dividend action");
let cash_delta = position.apply_cash_dividend(action.share_cash);
let cash_delta = if self.cash_dividend_adjusts_cost_basis {
position.apply_cash_dividend(action.share_cash)
} else {
position.apply_cash_dividend_preserve_cost_basis(action.share_cash)
};
(cash_delta, position.quantity, position.average_cost)
};
if cash_delta.abs() > f64::EPSILON {