增加通用期货策略动作并修正组合净值

This commit is contained in:
boris
2026-08-25 17:10:17 +08:00
parent 90da7f8a21
commit c284cc191e
5 changed files with 408 additions and 15 deletions
+27 -2
View File
@@ -1127,6 +1127,25 @@ where
.unwrap_or(0.0)
}
fn aggregate_unit_net_value(&self, portfolio: &PortfolioState) -> Result<f64, BacktestError> {
if self.futures_account.is_none() {
return Ok(portfolio.unit_net_value());
}
if portfolio.external_cash_flow_total().abs() > 1e-9 {
return Err(BacktestError::Execution(
"mixed stock/futures external cash flows require an aggregate unit ledger"
.to_string(),
));
}
let initial_cash = self.aggregate_initial_cash();
if !initial_cash.is_finite() || initial_cash <= 0.0 {
return Err(BacktestError::Execution(
"aggregate initial cash must be positive for stock/futures NAV".to_string(),
));
}
Ok(self.aggregate_total_equity(portfolio) / initial_cash)
}
fn submit_futures_order(
&mut self,
date: NaiveDate,
@@ -1345,6 +1364,12 @@ where
if intent.quantity == 0 {
return Some("zero futures quantity".to_string());
}
if !intent.spec.is_resolved() {
return Some(format!(
"missing futures trading parameters symbol={} date={date}",
intent.symbol
));
}
if self.futures_validation_config.enforce_active_instrument {
if let Some(instrument) = self.data.instrument(&intent.symbol) {
if !instrument.is_active_on(date) {
@@ -1927,7 +1952,7 @@ where
let aggregate_cash = self.aggregate_cash(&portfolio);
let aggregate_market_value = self.aggregate_market_value(&portfolio);
let aggregate_total_equity = self.aggregate_total_equity(&portfolio);
let unit_nav = portfolio.unit_net_value();
let unit_nav = self.aggregate_unit_net_value(&portfolio)?;
let external_cash_flow =
portfolio.external_cash_flow_total() - previous_external_cash_flow_total;
previous_external_cash_flow_total = portfolio.external_cash_flow_total();
@@ -3000,7 +3025,7 @@ where
let aggregate_cash = self.aggregate_cash(&portfolio);
let aggregate_market_value = self.aggregate_market_value(&portfolio);
let aggregate_total_equity = self.aggregate_total_equity(&portfolio);
let unit_nav = portfolio.unit_net_value();
let unit_nav = self.aggregate_unit_net_value(&portfolio)?;
let external_cash_flow =
portfolio.external_cash_flow_total() - previous_external_cash_flow_total;
previous_external_cash_flow_total = portfolio.external_cash_flow_total();