Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1bcaa0b3d8 | |||
| 1703a7aa5e |
@@ -1850,9 +1850,9 @@ impl DataSet {
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(symbol_id, series)| {
|
||||
series.as_ref().map(|series| {
|
||||
(symbol_by_id[symbol_id].to_string(), Arc::clone(series))
|
||||
})
|
||||
series
|
||||
.as_ref()
|
||||
.map(|series| (symbol_by_id[symbol_id].to_string(), Arc::clone(series)))
|
||||
})
|
||||
.collect::<AHashMap<_, _>>();
|
||||
|
||||
@@ -1876,9 +1876,9 @@ impl DataSet {
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(symbol_id, series)| {
|
||||
series.as_ref().map(|series| {
|
||||
(symbol_by_id[symbol_id].to_string(), Arc::clone(series))
|
||||
})
|
||||
series
|
||||
.as_ref()
|
||||
.map(|series| (symbol_by_id[symbol_id].to_string(), Arc::clone(series)))
|
||||
})
|
||||
.collect::<AHashMap<_, _>>();
|
||||
let factor_texts = factor_texts
|
||||
@@ -1900,16 +1900,10 @@ impl DataSet {
|
||||
|
||||
let factor_market_cap_order_by_date =
|
||||
build_factor_market_cap_order(&factor_by_date, &factor_symbol_ids_by_date);
|
||||
let market_row_positions_by_date = build_dense_row_positions(
|
||||
&market_by_date,
|
||||
&market_symbol_ids_by_date,
|
||||
symbol_count,
|
||||
);
|
||||
let factor_row_positions_by_date = build_dense_row_positions(
|
||||
&factor_by_date,
|
||||
&factor_symbol_ids_by_date,
|
||||
symbol_count,
|
||||
);
|
||||
let market_row_positions_by_date =
|
||||
build_dense_row_positions(&market_by_date, &market_symbol_ids_by_date, symbol_count);
|
||||
let factor_row_positions_by_date =
|
||||
build_dense_row_positions(&factor_by_date, &factor_symbol_ids_by_date, symbol_count);
|
||||
let candidate_row_positions_by_date = build_dense_row_positions(
|
||||
&candidate_by_date,
|
||||
&candidate_symbol_ids_by_date,
|
||||
@@ -2321,7 +2315,10 @@ impl DataSet {
|
||||
}
|
||||
|
||||
for (component, strong_count) in [
|
||||
("daily market panel", Arc::strong_count(&self.market_by_date)),
|
||||
(
|
||||
"daily market panel",
|
||||
Arc::strong_count(&self.market_by_date),
|
||||
),
|
||||
(
|
||||
"market series by symbol",
|
||||
Arc::strong_count(&self.market_series_by_symbol),
|
||||
@@ -2456,7 +2453,8 @@ impl DataSet {
|
||||
.flat_map(|rows_by_symbol| rows_by_symbol.values())
|
||||
.map(Vec::len)
|
||||
.sum();
|
||||
let mut execution_quote_dates = execution_quotes_by_date.keys().copied().collect::<Vec<_>>();
|
||||
let mut execution_quote_dates =
|
||||
execution_quotes_by_date.keys().copied().collect::<Vec<_>>();
|
||||
execution_quote_dates.sort_unstable();
|
||||
self.execution_quotes_by_date = Arc::new(execution_quotes_by_date);
|
||||
self.execution_quote_dates = Arc::new(execution_quote_dates);
|
||||
@@ -5221,10 +5219,7 @@ mod tests {
|
||||
[data.symbol_id("000001.SZ").unwrap() as usize]
|
||||
.as_ref()
|
||||
.unwrap();
|
||||
assert!(!Arc::ptr_eq(
|
||||
&market_series_before,
|
||||
market_series_after
|
||||
));
|
||||
assert!(!Arc::ptr_eq(&market_series_before, market_series_after));
|
||||
assert!(Arc::ptr_eq(&daily_base_before, &market_series_after.base));
|
||||
assert_eq!(
|
||||
serde_json::to_value(market_series_after.snapshot_at(0)).unwrap(),
|
||||
@@ -6178,11 +6173,8 @@ mod tests {
|
||||
let date = NaiveDate::parse_from_str("2025-01-02", "%Y-%m-%d").unwrap();
|
||||
let quote = IntradayExecutionQuote {
|
||||
date,
|
||||
timestamp: NaiveDateTime::parse_from_str(
|
||||
"2025-01-02 10:18:00",
|
||||
"%Y-%m-%d %H:%M:%S",
|
||||
)
|
||||
.unwrap(),
|
||||
timestamp: NaiveDateTime::parse_from_str("2025-01-02 10:18:00", "%Y-%m-%d %H:%M:%S")
|
||||
.unwrap(),
|
||||
symbol: "000001.SZ".to_string(),
|
||||
last_price: 10.0,
|
||||
bid1: 10.0,
|
||||
@@ -6365,13 +6357,16 @@ mod tests {
|
||||
"'adjustment_factor_backward1'",
|
||||
] {
|
||||
for typed_value in [None, Some(1.0)] {
|
||||
assert!(matches!(
|
||||
normalize_factor_snapshots(vec![snapshot(
|
||||
typed_value,
|
||||
BTreeMap::from([(Cow::Borrowed(field), 2.0)]),
|
||||
)]),
|
||||
Err(DataSetError::ReservedTypedFactorInExtraMap { .. })
|
||||
), "reserved alias accepted: {field}");
|
||||
assert!(
|
||||
matches!(
|
||||
normalize_factor_snapshots(vec![snapshot(
|
||||
typed_value,
|
||||
BTreeMap::from([(Cow::Borrowed(field), 2.0)]),
|
||||
)]),
|
||||
Err(DataSetError::ReservedTypedFactorInExtraMap { .. })
|
||||
),
|
||||
"reserved alias accepted: {field}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -454,8 +454,7 @@ pub struct BacktestEngine<S, C, R> {
|
||||
futures_cost_model: FuturesTransactionCostModel,
|
||||
futures_validation_config: FuturesValidationConfig,
|
||||
execution_quote_loader: Option<ExecutionQuoteLoader>,
|
||||
preplanned_decision_quote_symbols_by_date:
|
||||
Option<Arc<BTreeMap<NaiveDate, BTreeSet<String>>>>,
|
||||
preplanned_decision_quote_symbols_by_date: Option<Arc<BTreeMap<NaiveDate, BTreeSet<String>>>>,
|
||||
execution_quote_request_cache:
|
||||
BTreeSet<(NaiveDate, String, Option<NaiveTime>, Option<NaiveTime>)>,
|
||||
risk_free_rate_contract: Option<RiskFreeRateContract>,
|
||||
@@ -2538,9 +2537,8 @@ where
|
||||
.map(Arc::clone)
|
||||
{
|
||||
let empty_symbols = BTreeSet::new();
|
||||
let decision_quote_symbols = preplanned
|
||||
.get(&execution_date)
|
||||
.unwrap_or(&empty_symbols);
|
||||
let decision_quote_symbols =
|
||||
preplanned.get(&execution_date).unwrap_or(&empty_symbols);
|
||||
self.ensure_execution_quotes_for_symbols_at_times(
|
||||
execution_date,
|
||||
decision_quote_symbols,
|
||||
|
||||
@@ -4254,13 +4254,10 @@ impl PlatformExprStrategy {
|
||||
.factor_snapshot_rows_on(date)
|
||||
.iter()
|
||||
.flat_map(|row| {
|
||||
row.extra_factors
|
||||
.keys()
|
||||
.map(|key| key.to_string())
|
||||
.chain(
|
||||
row.adjustment_factor_backward1
|
||||
.map(|_| BACKWARD_ADJUSTMENT_FACTOR_FIELD.to_string()),
|
||||
)
|
||||
row.extra_factors.keys().map(|key| key.to_string()).chain(
|
||||
row.adjustment_factor_backward1
|
||||
.map(|_| BACKWARD_ADJUSTMENT_FACTOR_FIELD.to_string()),
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
} else {
|
||||
@@ -10499,10 +10496,7 @@ impl PlatformExprStrategy {
|
||||
) -> std::ops::Range<usize> {
|
||||
if !matches!(
|
||||
self.config.market_cap_field.as_str(),
|
||||
"market_cap"
|
||||
| "market_cap_bn"
|
||||
| "candidate_market_cap"
|
||||
| "candidate_market_cap_bn"
|
||||
"market_cap" | "market_cap_bn" | "candidate_market_cap" | "candidate_market_cap_bn"
|
||||
) || !band_low.is_finite()
|
||||
|| !band_high.is_finite()
|
||||
{
|
||||
@@ -10520,8 +10514,7 @@ impl PlatformExprStrategy {
|
||||
};
|
||||
let start = symbol_ids.partition_point(|symbol_id| market_cap(*symbol_id) < band_low);
|
||||
let end = start
|
||||
+ symbol_ids[start..]
|
||||
.partition_point(|symbol_id| market_cap(*symbol_id) <= band_high);
|
||||
+ symbol_ids[start..].partition_point(|symbol_id| market_cap(*symbol_id) <= band_high);
|
||||
start..end
|
||||
}
|
||||
|
||||
@@ -11667,11 +11660,8 @@ impl PlatformExprStrategy {
|
||||
&execution_day,
|
||||
&factor_day,
|
||||
)?;
|
||||
let field_value = self.selection_field_value_from_caps(
|
||||
market_cap_bn,
|
||||
free_float_cap_bn,
|
||||
&stock,
|
||||
);
|
||||
let field_value =
|
||||
self.selection_field_value_from_caps(market_cap_bn, free_float_cap_bn, &stock);
|
||||
if !field_value.is_finite() {
|
||||
if diagnostics.len() < 12 {
|
||||
diagnostics.push(format!(
|
||||
@@ -16446,15 +16436,16 @@ mod tests {
|
||||
cfg.market_cap_field = "market_cap".to_string();
|
||||
let strategy = PlatformExprStrategy::new(cfg.clone());
|
||||
|
||||
let range = strategy.market_cap_ordered_selection_range(
|
||||
&factor_day,
|
||||
symbol_ids,
|
||||
10.0,
|
||||
20.0,
|
||||
);
|
||||
let range =
|
||||
strategy.market_cap_ordered_selection_range(&factor_day, symbol_ids, 10.0, 20.0);
|
||||
let selected_caps = symbol_ids[range]
|
||||
.iter()
|
||||
.map(|symbol_id| factor_day.factor(*symbol_id).expect("factor row").market_cap_bn)
|
||||
.map(|symbol_id| {
|
||||
factor_day
|
||||
.factor(*symbol_id)
|
||||
.expect("factor row")
|
||||
.market_cap_bn
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(selected_caps, vec![10.0, 20.0]);
|
||||
|
||||
@@ -16465,8 +16456,12 @@ mod tests {
|
||||
);
|
||||
cfg.market_cap_field = "free_float_cap".to_string();
|
||||
assert_eq!(
|
||||
PlatformExprStrategy::new(cfg)
|
||||
.market_cap_ordered_selection_range(&factor_day, symbol_ids, 10.0, 20.0),
|
||||
PlatformExprStrategy::new(cfg).market_cap_ordered_selection_range(
|
||||
&factor_day,
|
||||
symbol_ids,
|
||||
10.0,
|
||||
20.0
|
||||
),
|
||||
0..symbol_ids.len()
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user