@@ -340,12 +340,9 @@ pub struct ExecutionQuoteIterator<'a> {
|
|||||||
heap: BinaryHeap<Reverse<(NaiveDateTime, usize, usize)>>,
|
heap: BinaryHeap<Reverse<(NaiveDateTime, usize, usize)>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
type ExecutionQuotesBySymbol = HashMap<String, Arc<Vec<IntradayExecutionQuote>>>;
|
|
||||||
type ExecutionQuotesByDate = HashMap<NaiveDate, Arc<ExecutionQuotesBySymbol>>;
|
|
||||||
|
|
||||||
impl<'a> ExecutionQuoteIterator<'a> {
|
impl<'a> ExecutionQuoteIterator<'a> {
|
||||||
fn new(
|
fn new(
|
||||||
rows_by_symbol: Option<&'a ExecutionQuotesBySymbol>,
|
rows_by_symbol: Option<&'a HashMap<String, Vec<IntradayExecutionQuote>>>,
|
||||||
symbols: Option<&BTreeSet<String>>,
|
symbols: Option<&BTreeSet<String>>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let mut streams = rows_by_symbol
|
let mut streams = rows_by_symbol
|
||||||
@@ -1317,7 +1314,7 @@ pub struct DataSet {
|
|||||||
candidate_symbol_ids_by_date: Arc<BTreeMap<NaiveDate, Vec<u32>>>,
|
candidate_symbol_ids_by_date: Arc<BTreeMap<NaiveDate, Vec<u32>>>,
|
||||||
candidate_row_positions_by_date: Arc<Option<DenseRowPositionIndex>>,
|
candidate_row_positions_by_date: Arc<Option<DenseRowPositionIndex>>,
|
||||||
corporate_actions_by_date: Arc<BTreeMap<NaiveDate, Vec<CorporateAction>>>,
|
corporate_actions_by_date: Arc<BTreeMap<NaiveDate, Vec<CorporateAction>>>,
|
||||||
execution_quotes_by_date: Arc<ExecutionQuotesByDate>,
|
execution_quotes_by_date: Arc<HashMap<NaiveDate, HashMap<String, Vec<IntradayExecutionQuote>>>>,
|
||||||
execution_quote_dates: Arc<Vec<NaiveDate>>,
|
execution_quote_dates: Arc<Vec<NaiveDate>>,
|
||||||
order_book_depth_index: Arc<HashMap<(NaiveDate, String), Vec<IntradayOrderBookDepthLevel>>>,
|
order_book_depth_index: Arc<HashMap<(NaiveDate, String), Vec<IntradayOrderBookDepthLevel>>>,
|
||||||
benchmark_by_date: Arc<BTreeMap<NaiveDate, BenchmarkSnapshot>>,
|
benchmark_by_date: Arc<BTreeMap<NaiveDate, BenchmarkSnapshot>>,
|
||||||
@@ -2186,7 +2183,7 @@ impl DataSet {
|
|||||||
self.execution_quotes_by_date
|
self.execution_quotes_by_date
|
||||||
.get(&date)
|
.get(&date)
|
||||||
.and_then(|rows_by_symbol| rows_by_symbol.get(symbol))
|
.and_then(|rows_by_symbol| rows_by_symbol.get(symbol))
|
||||||
.map(|rows| rows.as_slice())
|
.map(Vec::as_slice)
|
||||||
.unwrap_or(&[])
|
.unwrap_or(&[])
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2212,7 +2209,7 @@ impl DataSet {
|
|||||||
self.execution_quotes_by_date
|
self.execution_quotes_by_date
|
||||||
.values()
|
.values()
|
||||||
.flat_map(|rows_by_symbol| rows_by_symbol.values())
|
.flat_map(|rows_by_symbol| rows_by_symbol.values())
|
||||||
.map(|rows| rows.len())
|
.map(Vec::len)
|
||||||
.sum()
|
.sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2231,22 +2228,14 @@ impl DataSet {
|
|||||||
let execution_quotes_by_date = Arc::make_mut(&mut self.execution_quotes_by_date);
|
let execution_quotes_by_date = Arc::make_mut(&mut self.execution_quotes_by_date);
|
||||||
for (date, rows_by_symbol) in grouped {
|
for (date, rows_by_symbol) in grouped {
|
||||||
let date_is_new = !execution_quotes_by_date.contains_key(&date);
|
let date_is_new = !execution_quotes_by_date.contains_key(&date);
|
||||||
let target_by_symbol = Arc::make_mut(
|
let target_by_symbol = execution_quotes_by_date.entry(date).or_default();
|
||||||
execution_quotes_by_date
|
|
||||||
.entry(date)
|
|
||||||
.or_insert_with(|| Arc::new(HashMap::new())),
|
|
||||||
);
|
|
||||||
if date_is_new {
|
if date_is_new {
|
||||||
new_dates.push(date);
|
new_dates.push(date);
|
||||||
}
|
}
|
||||||
for (symbol, mut incoming) in rows_by_symbol {
|
for (symbol, mut incoming) in rows_by_symbol {
|
||||||
incoming.sort_by_key(|quote| quote.timestamp);
|
incoming.sort_by_key(|quote| quote.timestamp);
|
||||||
incoming.dedup_by(|left, right| left.timestamp == right.timestamp);
|
incoming.dedup_by(|left, right| left.timestamp == right.timestamp);
|
||||||
let target = Arc::make_mut(
|
let target = target_by_symbol.entry(symbol).or_default();
|
||||||
target_by_symbol
|
|
||||||
.entry(symbol)
|
|
||||||
.or_insert_with(|| Arc::new(Vec::new())),
|
|
||||||
);
|
|
||||||
if target.is_empty() {
|
if target.is_empty() {
|
||||||
added = added.saturating_add(incoming.len());
|
added = added.saturating_add(incoming.len());
|
||||||
*target = incoming;
|
*target = incoming;
|
||||||
@@ -2311,10 +2300,7 @@ impl DataSet {
|
|||||||
date: NaiveDate,
|
date: NaiveDate,
|
||||||
symbols: Option<&BTreeSet<String>>,
|
symbols: Option<&BTreeSet<String>>,
|
||||||
) -> ExecutionQuoteIterator<'_> {
|
) -> ExecutionQuoteIterator<'_> {
|
||||||
ExecutionQuoteIterator::new(
|
ExecutionQuoteIterator::new(self.execution_quotes_by_date.get(&date), symbols)
|
||||||
self.execution_quotes_by_date.get(&date).map(Arc::as_ref),
|
|
||||||
symbols,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn execution_quotes_on_date_for_symbols(
|
pub fn execution_quotes_on_date_for_symbols(
|
||||||
@@ -2336,14 +2322,14 @@ impl DataSet {
|
|||||||
if let Ok(index) = dates.binary_search(&date) {
|
if let Ok(index) = dates.binary_search(&date) {
|
||||||
dates.remove(index);
|
dates.remove(index);
|
||||||
}
|
}
|
||||||
rows_by_symbol.values().map(|rows| rows.len()).sum()
|
rows_by_symbol.into_values().map(|rows| rows.len()).sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn release_execution_quotes_on_date(&mut self, date: NaiveDate) -> usize {
|
pub fn release_execution_quotes_on_date(&mut self, date: NaiveDate) -> usize {
|
||||||
let row_count = self
|
let row_count = self
|
||||||
.execution_quotes_by_date
|
.execution_quotes_by_date
|
||||||
.get(&date)
|
.get(&date)
|
||||||
.map(|rows_by_symbol| rows_by_symbol.values().map(|rows| rows.len()).sum())
|
.map(|rows_by_symbol| rows_by_symbol.values().map(Vec::len).sum())
|
||||||
.unwrap_or(0);
|
.unwrap_or(0);
|
||||||
// Run data shares this immutable map with the prepared-data cache. Arc::make_mut here
|
// Run data shares this immutable map with the prepared-data cache. Arc::make_mut here
|
||||||
// would clone every date just to remove one entry and would not release the cached base.
|
// would clone every date just to remove one entry and would not release the cached base.
|
||||||
@@ -4543,7 +4529,7 @@ fn build_futures_params_index(
|
|||||||
|
|
||||||
fn build_execution_quote_index(
|
fn build_execution_quote_index(
|
||||||
execution_quotes: Vec<IntradayExecutionQuote>,
|
execution_quotes: Vec<IntradayExecutionQuote>,
|
||||||
) -> ExecutionQuotesByDate {
|
) -> HashMap<NaiveDate, HashMap<String, Vec<IntradayExecutionQuote>>> {
|
||||||
let mut grouped = HashMap::<NaiveDate, HashMap<String, Vec<IntradayExecutionQuote>>>::new();
|
let mut grouped = HashMap::<NaiveDate, HashMap<String, Vec<IntradayExecutionQuote>>>::new();
|
||||||
for quote in execution_quotes {
|
for quote in execution_quotes {
|
||||||
grouped
|
grouped
|
||||||
@@ -4561,15 +4547,6 @@ fn build_execution_quote_index(
|
|||||||
}
|
}
|
||||||
|
|
||||||
grouped
|
grouped
|
||||||
.into_iter()
|
|
||||||
.map(|(date, rows_by_symbol)| {
|
|
||||||
let rows_by_symbol = rows_by_symbol
|
|
||||||
.into_iter()
|
|
||||||
.map(|(symbol, rows)| (symbol, Arc::new(rows)))
|
|
||||||
.collect();
|
|
||||||
(date, Arc::new(rows_by_symbol))
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_order_book_depth_index(
|
fn build_order_book_depth_index(
|
||||||
@@ -4872,68 +4849,6 @@ mod tests {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn execution_quote_updates_copy_only_the_modified_date_and_symbol_path() {
|
|
||||||
let first_date = NaiveDate::parse_from_str("2025-01-02", "%Y-%m-%d").unwrap();
|
|
||||||
let second_date = NaiveDate::parse_from_str("2025-01-03", "%Y-%m-%d").unwrap();
|
|
||||||
let quote = |date: NaiveDate, symbol: &str, time: &str| IntradayExecutionQuote {
|
|
||||||
date,
|
|
||||||
timestamp: NaiveDateTime::parse_from_str(
|
|
||||||
&format!("{date} {time}"),
|
|
||||||
"%Y-%m-%d %H:%M:%S",
|
|
||||||
)
|
|
||||||
.unwrap(),
|
|
||||||
symbol: symbol.to_string(),
|
|
||||||
last_price: 10.0,
|
|
||||||
bid1: 9.99,
|
|
||||||
ask1: 10.01,
|
|
||||||
bid1_volume: 10_000,
|
|
||||||
ask1_volume: 10_000,
|
|
||||||
volume_delta: 10_000,
|
|
||||||
amount_delta: 100_000.0,
|
|
||||||
trading_phase: Some("continuous".to_string()),
|
|
||||||
};
|
|
||||||
let data = DataSet::from_components_with_actions_and_quotes(
|
|
||||||
Vec::new(),
|
|
||||||
Vec::new(),
|
|
||||||
Vec::new(),
|
|
||||||
Vec::new(),
|
|
||||||
vec![benchmark_row("2025-01-02", 12.0), benchmark_row("2025-01-03", 12.1)],
|
|
||||||
Vec::new(),
|
|
||||||
vec![
|
|
||||||
quote(first_date, "000001.SZ", "10:18:00"),
|
|
||||||
quote(first_date, "000002.SZ", "10:18:00"),
|
|
||||||
quote(second_date, "000001.SZ", "10:18:00"),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
let mut run_data = data.clone();
|
|
||||||
|
|
||||||
run_data.add_execution_quotes(vec![quote(first_date, "000001.SZ", "10:19:00")]);
|
|
||||||
|
|
||||||
assert!(!Arc::ptr_eq(
|
|
||||||
&data.execution_quotes_by_date,
|
|
||||||
&run_data.execution_quotes_by_date
|
|
||||||
));
|
|
||||||
let base_first = data.execution_quotes_by_date.get(&first_date).unwrap();
|
|
||||||
let run_first = run_data.execution_quotes_by_date.get(&first_date).unwrap();
|
|
||||||
assert!(!Arc::ptr_eq(base_first, run_first));
|
|
||||||
assert!(!Arc::ptr_eq(
|
|
||||||
base_first.get("000001.SZ").unwrap(),
|
|
||||||
run_first.get("000001.SZ").unwrap()
|
|
||||||
));
|
|
||||||
assert!(Arc::ptr_eq(
|
|
||||||
base_first.get("000002.SZ").unwrap(),
|
|
||||||
run_first.get("000002.SZ").unwrap()
|
|
||||||
));
|
|
||||||
assert!(Arc::ptr_eq(
|
|
||||||
data.execution_quotes_by_date.get(&second_date).unwrap(),
|
|
||||||
run_data.execution_quotes_by_date.get(&second_date).unwrap()
|
|
||||||
));
|
|
||||||
assert_eq!(data.execution_quotes_on(first_date, "000001.SZ").len(), 1);
|
|
||||||
assert_eq!(run_data.execution_quotes_on(first_date, "000001.SZ").len(), 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn daily_bundle_constructor_matches_flat_component_constructor() {
|
fn daily_bundle_constructor_matches_flat_component_constructor() {
|
||||||
let dates = [
|
let dates = [
|
||||||
|
|||||||
Reference in New Issue
Block a user