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