保留已有行情和策略代码格式整理
This commit is contained in:
@@ -1850,9 +1850,9 @@ impl DataSet {
|
|||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.filter_map(|(symbol_id, series)| {
|
.filter_map(|(symbol_id, series)| {
|
||||||
series.as_ref().map(|series| {
|
series
|
||||||
(symbol_by_id[symbol_id].to_string(), Arc::clone(series))
|
.as_ref()
|
||||||
})
|
.map(|series| (symbol_by_id[symbol_id].to_string(), Arc::clone(series)))
|
||||||
})
|
})
|
||||||
.collect::<AHashMap<_, _>>();
|
.collect::<AHashMap<_, _>>();
|
||||||
|
|
||||||
@@ -1876,9 +1876,9 @@ impl DataSet {
|
|||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.filter_map(|(symbol_id, series)| {
|
.filter_map(|(symbol_id, series)| {
|
||||||
series.as_ref().map(|series| {
|
series
|
||||||
(symbol_by_id[symbol_id].to_string(), Arc::clone(series))
|
.as_ref()
|
||||||
})
|
.map(|series| (symbol_by_id[symbol_id].to_string(), Arc::clone(series)))
|
||||||
})
|
})
|
||||||
.collect::<AHashMap<_, _>>();
|
.collect::<AHashMap<_, _>>();
|
||||||
let factor_texts = factor_texts
|
let factor_texts = factor_texts
|
||||||
@@ -1900,16 +1900,10 @@ impl DataSet {
|
|||||||
|
|
||||||
let factor_market_cap_order_by_date =
|
let factor_market_cap_order_by_date =
|
||||||
build_factor_market_cap_order(&factor_by_date, &factor_symbol_ids_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(
|
let market_row_positions_by_date =
|
||||||
&market_by_date,
|
build_dense_row_positions(&market_by_date, &market_symbol_ids_by_date, symbol_count);
|
||||||
&market_symbol_ids_by_date,
|
let factor_row_positions_by_date =
|
||||||
symbol_count,
|
build_dense_row_positions(&factor_by_date, &factor_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(
|
let candidate_row_positions_by_date = build_dense_row_positions(
|
||||||
&candidate_by_date,
|
&candidate_by_date,
|
||||||
&candidate_symbol_ids_by_date,
|
&candidate_symbol_ids_by_date,
|
||||||
@@ -2321,7 +2315,10 @@ impl DataSet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (component, strong_count) in [
|
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",
|
"market series by symbol",
|
||||||
Arc::strong_count(&self.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())
|
.flat_map(|rows_by_symbol| rows_by_symbol.values())
|
||||||
.map(Vec::len)
|
.map(Vec::len)
|
||||||
.sum();
|
.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();
|
execution_quote_dates.sort_unstable();
|
||||||
self.execution_quotes_by_date = Arc::new(execution_quotes_by_date);
|
self.execution_quotes_by_date = Arc::new(execution_quotes_by_date);
|
||||||
self.execution_quote_dates = Arc::new(execution_quote_dates);
|
self.execution_quote_dates = Arc::new(execution_quote_dates);
|
||||||
@@ -5221,10 +5219,7 @@ mod tests {
|
|||||||
[data.symbol_id("000001.SZ").unwrap() as usize]
|
[data.symbol_id("000001.SZ").unwrap() as usize]
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(!Arc::ptr_eq(
|
assert!(!Arc::ptr_eq(&market_series_before, market_series_after));
|
||||||
&market_series_before,
|
|
||||||
market_series_after
|
|
||||||
));
|
|
||||||
assert!(Arc::ptr_eq(&daily_base_before, &market_series_after.base));
|
assert!(Arc::ptr_eq(&daily_base_before, &market_series_after.base));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
serde_json::to_value(market_series_after.snapshot_at(0)).unwrap(),
|
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 date = NaiveDate::parse_from_str("2025-01-02", "%Y-%m-%d").unwrap();
|
||||||
let quote = IntradayExecutionQuote {
|
let quote = IntradayExecutionQuote {
|
||||||
date,
|
date,
|
||||||
timestamp: NaiveDateTime::parse_from_str(
|
timestamp: NaiveDateTime::parse_from_str("2025-01-02 10:18:00", "%Y-%m-%d %H:%M:%S")
|
||||||
"2025-01-02 10:18:00",
|
.unwrap(),
|
||||||
"%Y-%m-%d %H:%M:%S",
|
|
||||||
)
|
|
||||||
.unwrap(),
|
|
||||||
symbol: "000001.SZ".to_string(),
|
symbol: "000001.SZ".to_string(),
|
||||||
last_price: 10.0,
|
last_price: 10.0,
|
||||||
bid1: 10.0,
|
bid1: 10.0,
|
||||||
@@ -6365,13 +6357,16 @@ mod tests {
|
|||||||
"'adjustment_factor_backward1'",
|
"'adjustment_factor_backward1'",
|
||||||
] {
|
] {
|
||||||
for typed_value in [None, Some(1.0)] {
|
for typed_value in [None, Some(1.0)] {
|
||||||
assert!(matches!(
|
assert!(
|
||||||
normalize_factor_snapshots(vec![snapshot(
|
matches!(
|
||||||
typed_value,
|
normalize_factor_snapshots(vec![snapshot(
|
||||||
BTreeMap::from([(Cow::Borrowed(field), 2.0)]),
|
typed_value,
|
||||||
)]),
|
BTreeMap::from([(Cow::Borrowed(field), 2.0)]),
|
||||||
Err(DataSetError::ReservedTypedFactorInExtraMap { .. })
|
)]),
|
||||||
), "reserved alias accepted: {field}");
|
Err(DataSetError::ReservedTypedFactorInExtraMap { .. })
|
||||||
|
),
|
||||||
|
"reserved alias accepted: {field}"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -454,8 +454,7 @@ pub struct BacktestEngine<S, C, R> {
|
|||||||
futures_cost_model: FuturesTransactionCostModel,
|
futures_cost_model: FuturesTransactionCostModel,
|
||||||
futures_validation_config: FuturesValidationConfig,
|
futures_validation_config: FuturesValidationConfig,
|
||||||
execution_quote_loader: Option<ExecutionQuoteLoader>,
|
execution_quote_loader: Option<ExecutionQuoteLoader>,
|
||||||
preplanned_decision_quote_symbols_by_date:
|
preplanned_decision_quote_symbols_by_date: Option<Arc<BTreeMap<NaiveDate, BTreeSet<String>>>>,
|
||||||
Option<Arc<BTreeMap<NaiveDate, BTreeSet<String>>>>,
|
|
||||||
execution_quote_request_cache:
|
execution_quote_request_cache:
|
||||||
BTreeSet<(NaiveDate, String, Option<NaiveTime>, Option<NaiveTime>)>,
|
BTreeSet<(NaiveDate, String, Option<NaiveTime>, Option<NaiveTime>)>,
|
||||||
risk_free_rate_contract: Option<RiskFreeRateContract>,
|
risk_free_rate_contract: Option<RiskFreeRateContract>,
|
||||||
@@ -2538,9 +2537,8 @@ where
|
|||||||
.map(Arc::clone)
|
.map(Arc::clone)
|
||||||
{
|
{
|
||||||
let empty_symbols = BTreeSet::new();
|
let empty_symbols = BTreeSet::new();
|
||||||
let decision_quote_symbols = preplanned
|
let decision_quote_symbols =
|
||||||
.get(&execution_date)
|
preplanned.get(&execution_date).unwrap_or(&empty_symbols);
|
||||||
.unwrap_or(&empty_symbols);
|
|
||||||
self.ensure_execution_quotes_for_symbols_at_times(
|
self.ensure_execution_quotes_for_symbols_at_times(
|
||||||
execution_date,
|
execution_date,
|
||||||
decision_quote_symbols,
|
decision_quote_symbols,
|
||||||
|
|||||||
@@ -4191,13 +4191,10 @@ impl PlatformExprStrategy {
|
|||||||
.factor_snapshot_rows_on(date)
|
.factor_snapshot_rows_on(date)
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|row| {
|
.flat_map(|row| {
|
||||||
row.extra_factors
|
row.extra_factors.keys().map(|key| key.to_string()).chain(
|
||||||
.keys()
|
row.adjustment_factor_backward1
|
||||||
.map(|key| key.to_string())
|
.map(|_| BACKWARD_ADJUSTMENT_FACTOR_FIELD.to_string()),
|
||||||
.chain(
|
)
|
||||||
row.adjustment_factor_backward1
|
|
||||||
.map(|_| BACKWARD_ADJUSTMENT_FACTOR_FIELD.to_string()),
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
} else {
|
} else {
|
||||||
@@ -10397,10 +10394,7 @@ impl PlatformExprStrategy {
|
|||||||
) -> std::ops::Range<usize> {
|
) -> std::ops::Range<usize> {
|
||||||
if !matches!(
|
if !matches!(
|
||||||
self.config.market_cap_field.as_str(),
|
self.config.market_cap_field.as_str(),
|
||||||
"market_cap"
|
"market_cap" | "market_cap_bn" | "candidate_market_cap" | "candidate_market_cap_bn"
|
||||||
| "market_cap_bn"
|
|
||||||
| "candidate_market_cap"
|
|
||||||
| "candidate_market_cap_bn"
|
|
||||||
) || !band_low.is_finite()
|
) || !band_low.is_finite()
|
||||||
|| !band_high.is_finite()
|
|| !band_high.is_finite()
|
||||||
{
|
{
|
||||||
@@ -10418,8 +10412,7 @@ impl PlatformExprStrategy {
|
|||||||
};
|
};
|
||||||
let start = symbol_ids.partition_point(|symbol_id| market_cap(*symbol_id) < band_low);
|
let start = symbol_ids.partition_point(|symbol_id| market_cap(*symbol_id) < band_low);
|
||||||
let end = start
|
let end = start
|
||||||
+ symbol_ids[start..]
|
+ symbol_ids[start..].partition_point(|symbol_id| market_cap(*symbol_id) <= band_high);
|
||||||
.partition_point(|symbol_id| market_cap(*symbol_id) <= band_high);
|
|
||||||
start..end
|
start..end
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -11553,11 +11546,8 @@ impl PlatformExprStrategy {
|
|||||||
&execution_day,
|
&execution_day,
|
||||||
&factor_day,
|
&factor_day,
|
||||||
)?;
|
)?;
|
||||||
let field_value = self.selection_field_value_from_caps(
|
let field_value =
|
||||||
market_cap_bn,
|
self.selection_field_value_from_caps(market_cap_bn, free_float_cap_bn, &stock);
|
||||||
free_float_cap_bn,
|
|
||||||
&stock,
|
|
||||||
);
|
|
||||||
if !field_value.is_finite() {
|
if !field_value.is_finite() {
|
||||||
if diagnostics.len() < 12 {
|
if diagnostics.len() < 12 {
|
||||||
diagnostics.push(format!(
|
diagnostics.push(format!(
|
||||||
@@ -16128,15 +16118,16 @@ mod tests {
|
|||||||
cfg.market_cap_field = "market_cap".to_string();
|
cfg.market_cap_field = "market_cap".to_string();
|
||||||
let strategy = PlatformExprStrategy::new(cfg.clone());
|
let strategy = PlatformExprStrategy::new(cfg.clone());
|
||||||
|
|
||||||
let range = strategy.market_cap_ordered_selection_range(
|
let range =
|
||||||
&factor_day,
|
strategy.market_cap_ordered_selection_range(&factor_day, symbol_ids, 10.0, 20.0);
|
||||||
symbol_ids,
|
|
||||||
10.0,
|
|
||||||
20.0,
|
|
||||||
);
|
|
||||||
let selected_caps = symbol_ids[range]
|
let selected_caps = symbol_ids[range]
|
||||||
.iter()
|
.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<_>>();
|
.collect::<Vec<_>>();
|
||||||
assert_eq!(selected_caps, vec![10.0, 20.0]);
|
assert_eq!(selected_caps, vec![10.0, 20.0]);
|
||||||
|
|
||||||
@@ -16147,8 +16138,12 @@ mod tests {
|
|||||||
);
|
);
|
||||||
cfg.market_cap_field = "free_float_cap".to_string();
|
cfg.market_cap_field = "free_float_cap".to_string();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
PlatformExprStrategy::new(cfg)
|
PlatformExprStrategy::new(cfg).market_cap_ordered_selection_range(
|
||||||
.market_cap_ordered_selection_range(&factor_day, symbol_ids, 10.0, 20.0),
|
&factor_day,
|
||||||
|
symbol_ids,
|
||||||
|
10.0,
|
||||||
|
20.0
|
||||||
|
),
|
||||||
0..symbol_ids.len()
|
0..symbol_ids.len()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user