From 192ac3f843c1d7dee8c94766fd25b83a8c817a7a Mon Sep 17 00:00:00 2001 From: boris Date: Sun, 21 Jun 2026 01:29:15 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=93=E5=AD=98=E8=B0=83=E5=BA=A6=E6=89=A7?= =?UTF-8?q?=E8=A1=8Cquote=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fidc-core/src/platform_expr_strategy.rs | 44 ++++++++++++++----- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/crates/fidc-core/src/platform_expr_strategy.rs b/crates/fidc-core/src/platform_expr_strategy.rs index 37dae48..aea971d 100644 --- a/crates/fidc-core/src/platform_expr_strategy.rs +++ b/crates/fidc-core/src/platform_expr_strategy.rs @@ -558,6 +558,10 @@ pub struct PlatformExprStrategy { stock_state_cache_date: RefCell>, stock_state_cache: RefCell), StockExpressionState>>, + scheduled_quote_cache_date: RefCell>, + scheduled_quote_cache: RefCell< + HashMap<(NaiveDate, String, NaiveDateTime), Option>, + >, } #[derive(Debug, Clone, PartialEq)] @@ -672,6 +676,8 @@ impl PlatformExprStrategy { stock_extra_factors_required, stock_state_cache_date: RefCell::new(None), stock_state_cache: RefCell::new(HashMap::new()), + scheduled_quote_cache_date: RefCell::new(None), + scheduled_quote_cache: RefCell::new(HashMap::new()), } } @@ -1192,22 +1198,22 @@ impl PlatformExprStrategy { ) } - fn aiquant_scheduled_quote<'a>( + fn aiquant_scheduled_quote( &self, - ctx: &'a StrategyContext<'_>, + ctx: &StrategyContext<'_>, date: NaiveDate, symbol: &str, - ) -> Option<&'a crate::data::IntradayExecutionQuote> { + ) -> Option { self.aiquant_scheduled_quote_at_time(ctx, date, symbol, None) } - fn aiquant_scheduled_quote_at_time<'a>( + fn aiquant_scheduled_quote_at_time( &self, - ctx: &'a StrategyContext<'_>, + ctx: &StrategyContext<'_>, date: NaiveDate, symbol: &str, execution_time: Option, - ) -> Option<&'a crate::data::IntradayExecutionQuote> { + ) -> Option { if !self.config.aiquant_transaction_cost { return None; } @@ -1218,11 +1224,28 @@ impl PlatformExprStrategy { &ProjectedExecutionState::default(), execution_time, ); - ctx.data + { + let mut cache_date = self.scheduled_quote_cache_date.borrow_mut(); + if *cache_date != Some(date) { + self.scheduled_quote_cache.borrow_mut().clear(); + *cache_date = Some(date); + } + } + let cache_key = (date, symbol.to_string(), start_cursor); + if let Some(cached) = self.scheduled_quote_cache.borrow().get(&cache_key) { + return cached.clone(); + } + let quote = ctx + .data .execution_quotes_on(date, symbol) .iter() .filter(|quote| quote.timestamp <= start_cursor) .max_by_key(|quote| quote.timestamp) + .cloned(); + self.scheduled_quote_cache + .borrow_mut() + .insert(cache_key, quote.clone()); + quote } fn aiquant_scheduled_buy_price( @@ -1232,7 +1255,7 @@ impl PlatformExprStrategy { symbol: &str, ) -> Option { self.aiquant_scheduled_quote(ctx, date, symbol) - .and_then(|quote| self.projected_quote_raw_price(quote, OrderSide::Buy)) + .and_then(|quote| self.projected_quote_raw_price("e, OrderSide::Buy)) } fn aiquant_scheduled_sell_price_at_time( @@ -1243,7 +1266,7 @@ impl PlatformExprStrategy { execution_time: Option, ) -> Option { self.aiquant_scheduled_quote_at_time(ctx, date, symbol, execution_time) - .and_then(|quote| self.projected_quote_raw_price(quote, OrderSide::Sell)) + .and_then(|quote| self.projected_quote_raw_price("e, OrderSide::Sell)) } fn aiquant_scheduled_last_price( @@ -2428,11 +2451,12 @@ impl PlatformExprStrategy { low: expression_low, close: expression_close, last: decision_quote + .as_ref() .and_then(|quote| { (quote.last_price.is_finite() && quote.last_price > 0.0) .then_some(quote.last_price) }) - .or_else(|| decision_quote.and_then(|quote| quote.buy_price())) + .or_else(|| decision_quote.as_ref().and_then(|quote| quote.buy_price())) .unwrap_or(market.last_price), prev_close: feature_market.prev_close, amount,