缓存日内股票表达式状态
This commit is contained in:
@@ -517,6 +517,9 @@ pub struct PlatformExprStrategy {
|
|||||||
prelude_numeric_constants: HashMap<String, f64>,
|
prelude_numeric_constants: HashMap<String, f64>,
|
||||||
compact_stock_filter_expr: String,
|
compact_stock_filter_expr: String,
|
||||||
stock_filter_quote_usage: StockFilterQuoteUsage,
|
stock_filter_quote_usage: StockFilterQuoteUsage,
|
||||||
|
stock_state_cache_date: RefCell<Option<NaiveDate>>,
|
||||||
|
stock_state_cache:
|
||||||
|
RefCell<HashMap<(NaiveDate, NaiveDate, String, Option<NaiveTime>), StockExpressionState>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
@@ -620,6 +623,8 @@ impl PlatformExprStrategy {
|
|||||||
prelude_numeric_constants,
|
prelude_numeric_constants,
|
||||||
compact_stock_filter_expr,
|
compact_stock_filter_expr,
|
||||||
stock_filter_quote_usage,
|
stock_filter_quote_usage,
|
||||||
|
stock_state_cache_date: RefCell::new(None),
|
||||||
|
stock_state_cache: RefCell::new(HashMap::new()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2239,6 +2244,18 @@ impl PlatformExprStrategy {
|
|||||||
symbol: &str,
|
symbol: &str,
|
||||||
execution_time: Option<NaiveTime>,
|
execution_time: Option<NaiveTime>,
|
||||||
) -> Result<StockExpressionState, BacktestError> {
|
) -> Result<StockExpressionState, BacktestError> {
|
||||||
|
{
|
||||||
|
let mut cache_date = self.stock_state_cache_date.borrow_mut();
|
||||||
|
if *cache_date != Some(date) {
|
||||||
|
self.stock_state_cache.borrow_mut().clear();
|
||||||
|
*cache_date = Some(date);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let cache_key = (date, factor_date, symbol.to_string(), execution_time);
|
||||||
|
if let Some(state) = self.stock_state_cache.borrow().get(&cache_key) {
|
||||||
|
return Ok(state.clone());
|
||||||
|
}
|
||||||
|
|
||||||
let market = ctx.data.require_market(date, symbol)?;
|
let market = ctx.data.require_market(date, symbol)?;
|
||||||
let feature_market = ctx.data.market(factor_date, symbol).unwrap_or(market);
|
let feature_market = ctx.data.market(factor_date, symbol).unwrap_or(market);
|
||||||
let intraday_same_day_factor = self.config.aiquant_transaction_cost && factor_date == date;
|
let intraday_same_day_factor = self.config.aiquant_transaction_cost && factor_date == date;
|
||||||
@@ -2345,7 +2362,7 @@ impl PlatformExprStrategy {
|
|||||||
feature_market.volume as f64
|
feature_market.volume as f64
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(StockExpressionState {
|
let state = StockExpressionState {
|
||||||
symbol: symbol.to_string(),
|
symbol: symbol.to_string(),
|
||||||
market_cap,
|
market_cap,
|
||||||
market_cap_bn,
|
market_cap_bn,
|
||||||
@@ -2411,7 +2428,11 @@ impl PlatformExprStrategy {
|
|||||||
.filter(|row| row.symbol == symbol)
|
.filter(|row| row.symbol == symbol)
|
||||||
.map(|row| (row.field.clone(), row.value.clone()))
|
.map(|row| (row.field.clone(), row.value.clone()))
|
||||||
.collect(),
|
.collect(),
|
||||||
})
|
};
|
||||||
|
self.stock_state_cache
|
||||||
|
.borrow_mut()
|
||||||
|
.insert(cache_key, state.clone());
|
||||||
|
Ok(state)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eval_scope(
|
fn eval_scope(
|
||||||
|
|||||||
Reference in New Issue
Block a user