增强回测demo输出与分区加载

This commit is contained in:
zsb
2026-04-07 21:25:41 -07:00
parent ec425999b0
commit a26049ff15
9 changed files with 211 additions and 63 deletions

View File

@@ -43,6 +43,7 @@ pub struct CnSmallCapRotationConfig {
pub trade_rate: f64,
pub stop_loss_pct: f64,
pub take_profit_pct: f64,
pub signal_symbol: Option<String>,
}
impl CnSmallCapRotationConfig {
@@ -60,6 +61,7 @@ impl CnSmallCapRotationConfig {
trade_rate: 0.5,
stop_loss_pct: 0.08,
take_profit_pct: 0.10,
signal_symbol: None,
}
}
}
@@ -157,10 +159,20 @@ impl Strategy for CnSmallCapRotationStrategy {
.ok_or(BacktestError::MissingBenchmark {
date: ctx.decision_date,
})?;
let benchmark_closes = ctx
.data
.benchmark_closes_up_to(ctx.decision_date, self.config.long_ma_days);
let gross_exposure = self.gross_exposure(&benchmark_closes);
let signal_symbol = self.config.signal_symbol.as_deref();
let signal_closes = if let Some(symbol) = signal_symbol {
ctx.data.market_closes_up_to(ctx.decision_date, symbol, self.config.long_ma_days)
} else {
ctx.data.benchmark_closes_up_to(ctx.decision_date, self.config.long_ma_days)
};
let signal_level = if let Some(symbol) = signal_symbol {
ctx.data
.price(ctx.decision_date, symbol, PriceField::Close)
.unwrap_or(benchmark.close)
} else {
benchmark.close
};
let gross_exposure = self.gross_exposure(&signal_closes);
let periodic_rebalance = ctx.decision_index % self.config.refresh_rate == 0;
let exposure_changed = self
.last_gross_exposure
@@ -175,8 +187,10 @@ impl Strategy for CnSmallCapRotationStrategy {
ctx.decision_date, ctx.execution_date, gross_exposure
)];
let mut diagnostics = vec![format!(
"benchmark_close={:.2} refresh_rate={} stocknum={} short_ma_days={} long_ma_days={}",
"benchmark_close={:.2} signal_level={:.2} signal_symbol={} refresh_rate={} stocknum={} short_ma_days={} long_ma_days={}",
benchmark.close,
signal_level,
signal_symbol.unwrap_or(benchmark.benchmark.as_str()),
self.config.refresh_rate,
self.config.stocknum,
self.config.short_ma_days,
@@ -187,6 +201,7 @@ impl Strategy for CnSmallCapRotationStrategy {
let selected = self.selector.select(&SelectionContext {
decision_date: ctx.decision_date,
benchmark,
reference_level: signal_level,
data: ctx.data,
});