支持运行态预计算滚动均线

This commit is contained in:
boris
2026-07-17 14:54:25 +08:00
parent 7f65fda790
commit ef491340f6
+38 -1
View File
@@ -3298,6 +3298,27 @@ impl PlatformExprStrategy {
} }
} }
fn stock_current_rolling_mean(
&self,
ctx: &StrategyContext<'_>,
date: NaiveDate,
symbol: &str,
extra_factors: &BTreeMap<String, f64>,
field: &str,
lookback: usize,
) -> Option<f64> {
let precomputed = precomputed_stock_rolling_mean(extra_factors, field, lookback);
let computed = || {
ctx.data
.market_current_numeric_moving_average(date, symbol, field, lookback)
};
if self.config.prefer_precomputed_rolling_factors {
precomputed.or_else(computed)
} else {
computed().or(precomputed)
}
}
fn stock_state_at_time( fn stock_state_at_time(
&self, &self,
ctx: &StrategyContext<'_>, ctx: &StrategyContext<'_>,
@@ -5466,9 +5487,11 @@ impl PlatformExprStrategy {
"rolling_mean_current(\"{other}\", {lookback}) requires stock context" "rolling_mean_current(\"{other}\", {lookback}) requires stock context"
)) ))
})?; })?;
ctx.data.market_current_numeric_moving_average( self.stock_current_rolling_mean(
ctx,
day.date, day.date,
&stock.symbol, &stock.symbol,
&stock.extra_factors,
other, other,
lookback, lookback,
) )
@@ -28351,6 +28374,7 @@ mod tests {
fills: &[], fills: &[],
}; };
let mut cfg = PlatformExprStrategyConfig::microcap_rotation(); let mut cfg = PlatformExprStrategyConfig::microcap_rotation();
cfg.signal_symbol = symbol.to_string();
cfg.prefer_precomputed_rolling_factors = true; cfg.prefer_precomputed_rolling_factors = true;
cfg.stock_filter_expr = "stock_ma5 > 0 && stock_volume_ma5 > 0".to_string(); cfg.stock_filter_expr = "stock_ma5 > 0 && stock_volume_ma5 > 0".to_string();
let strategy = PlatformExprStrategy::new(cfg); let strategy = PlatformExprStrategy::new(cfg);
@@ -28359,6 +28383,19 @@ mod tests {
.expect("stock state"); .expect("stock state");
assert_eq!(stock.stock_ma5, 99.0); assert_eq!(stock.stock_ma5, 99.0);
assert_eq!(stock.stock_volume_ma5, 88.0); assert_eq!(stock.stock_volume_ma5, 88.0);
let day = strategy.day_state(&ctx, date).expect("day state");
assert_eq!(
strategy
.resolve_current_rolling_mean(&ctx, &day, Some(&stock), "close", 5)
.expect("precomputed current close rolling mean"),
99.0
);
assert_eq!(
strategy
.resolve_current_rolling_mean(&ctx, &day, Some(&stock), "volume", 5)
.expect("precomputed current volume rolling mean"),
88.0
);
let mut cfg = PlatformExprStrategyConfig::microcap_rotation(); let mut cfg = PlatformExprStrategyConfig::microcap_rotation();
cfg.stock_filter_expr = "stock_ma5 > 0 && stock_volume_ma5 > 0".to_string(); cfg.stock_filter_expr = "stock_ma5 > 0 && stock_volume_ma5 > 0".to_string();