perf: cache daily history by symbol
This commit is contained in:
@@ -446,6 +446,7 @@ pub struct EligibleUniverseSnapshot {
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct SymbolPriceSeries {
|
||||
snapshots: Vec<DailyMarketSnapshot>,
|
||||
dates: Vec<NaiveDate>,
|
||||
opens: Vec<f64>,
|
||||
closes: Vec<f64>,
|
||||
@@ -479,6 +480,7 @@ impl SymbolPriceSeries {
|
||||
let volume_prefix = prefix_sums(&volumes);
|
||||
|
||||
Self {
|
||||
snapshots: sorted,
|
||||
dates,
|
||||
opens,
|
||||
closes,
|
||||
@@ -514,6 +516,27 @@ impl SymbolPriceSeries {
|
||||
self.values_for(field)[start..end].to_vec()
|
||||
}
|
||||
|
||||
fn trailing_snapshots(
|
||||
&self,
|
||||
date: NaiveDate,
|
||||
lookback: usize,
|
||||
include_now: bool,
|
||||
) -> Vec<DailyMarketSnapshot> {
|
||||
if lookback == 0 {
|
||||
return Vec::new();
|
||||
}
|
||||
let end = if include_now {
|
||||
self.end_index(date)
|
||||
} else {
|
||||
self.previous_completed_end_index(date)
|
||||
};
|
||||
let Some(end) = end else {
|
||||
return Vec::new();
|
||||
};
|
||||
let start = end.saturating_sub(lookback);
|
||||
self.snapshots[start..end].to_vec()
|
||||
}
|
||||
|
||||
fn decision_price_on_or_before(&self, date: NaiveDate) -> Option<f64> {
|
||||
let end = self.decision_end_index(date)?;
|
||||
if end == 0 {
|
||||
@@ -1173,25 +1196,10 @@ impl DataSet {
|
||||
bar_count: usize,
|
||||
include_now: bool,
|
||||
) -> Vec<DailyMarketSnapshot> {
|
||||
if bar_count == 0 {
|
||||
return Vec::new();
|
||||
}
|
||||
let mut snapshots = self
|
||||
.market_by_date
|
||||
.iter()
|
||||
.filter(|(day, _)| {
|
||||
if include_now {
|
||||
**day <= date
|
||||
} else {
|
||||
**day < date
|
||||
}
|
||||
})
|
||||
.flat_map(|(_, rows)| rows.iter())
|
||||
.filter(|row| row.symbol == symbol)
|
||||
.cloned()
|
||||
.collect::<Vec<_>>();
|
||||
snapshots.sort_by_key(|row| row.date);
|
||||
take_last(snapshots, bar_count)
|
||||
self.market_series_by_symbol
|
||||
.get(symbol)
|
||||
.map(|series| series.trailing_snapshots(date, bar_count, include_now))
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn history_intraday_quotes(
|
||||
|
||||
Reference in New Issue
Block a user