缓存调度执行quote查询
This commit is contained in:
@@ -558,6 +558,10 @@ pub struct PlatformExprStrategy {
|
||||
stock_state_cache_date: RefCell<Option<NaiveDate>>,
|
||||
stock_state_cache:
|
||||
RefCell<HashMap<(NaiveDate, NaiveDate, String, Option<NaiveTime>), StockExpressionState>>,
|
||||
scheduled_quote_cache_date: RefCell<Option<NaiveDate>>,
|
||||
scheduled_quote_cache: RefCell<
|
||||
HashMap<(NaiveDate, String, NaiveDateTime), Option<crate::data::IntradayExecutionQuote>>,
|
||||
>,
|
||||
}
|
||||
|
||||
#[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<crate::data::IntradayExecutionQuote> {
|
||||
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<NaiveTime>,
|
||||
) -> Option<&'a crate::data::IntradayExecutionQuote> {
|
||||
) -> Option<crate::data::IntradayExecutionQuote> {
|
||||
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<f64> {
|
||||
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<NaiveTime>,
|
||||
) -> Option<f64> {
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user