按日期区分生命周期缺价并保留上市前现金区间

This commit is contained in:
boris
2026-09-10 19:09:21 +08:00
parent 2473cc04bb
commit 40481e8825
7 changed files with 174 additions and 45 deletions
+21 -7
View File
@@ -1047,8 +1047,6 @@ impl PortfolioState {
let unresolved_delisting = current_market_missing
&& data.instrument(&position.symbol).is_some_and(|instrument| {
instrument.is_delisted_on_or_before(date)
|| (instrument.status.eq_ignore_ascii_case("delisted")
&& instrument.delisted_at.is_none())
});
if unresolved_delisting {
position.last_price = 0.0;
@@ -1068,11 +1066,13 @@ impl PortfolioState {
position.refresh_day_pnl();
continue;
}
let confirmed_pause = data.market(date, &position.symbol).is_some_and(|row| row.paused)
|| data.candidate(date, &position.symbol).is_some_and(|row| row.is_paused);
let price = data
.price(date, &position.symbol, field)
.or_else(|| data.price_on_or_before(date, &position.symbol, field))
.or_else(|| confirmed_pause.then(|| data.price_on_or_before(date, &position.symbol, field)).flatten())
.or_else(|| {
(position.last_price.is_finite() && position.last_price > 0.0)
(confirmed_pause && position.last_price.is_finite() && position.last_price > 0.0)
.then_some(position.last_price)
})
.ok_or_else(|| DataSetError::MissingSnapshot {
@@ -1774,7 +1774,7 @@ mod tests {
}
#[test]
fn portfolio_carries_last_price_when_position_market_row_is_missing() {
fn portfolio_missing_market_requires_formal_suspension_before_carrying_price() {
let prev_date = NaiveDate::from_ymd_opt(2025, 5, 26).unwrap();
let missing_date = NaiveDate::from_ymd_opt(2025, 5, 27).unwrap();
let mut portfolio = PortfolioState::new(10_000.0);
@@ -1832,9 +1832,23 @@ mod tests {
.update_prices(prev_date, &dataset, PriceField::Close)
.expect("previous close");
portfolio.begin_trading_day();
portfolio
let error = portfolio
.update_prices(missing_date, &dataset, PriceField::Close)
.expect("missing current row should carry previous close");
.expect_err("unclassified missing current price must not be filled from history");
assert!(error.to_string().contains("601028.SH"));
let paused_dataset = DataSet::from_components(
vec![dataset.instrument("601028.SH").unwrap().clone()],
vec![dataset.market(prev_date, "601028.SH").unwrap().clone()],
Vec::new(),
vec![crate::data::CandidateEligibility {
date: missing_date, symbol: "601028.SH".into(), is_st: false, is_star_st: false,
is_new_listing: false, is_paused: true, allow_buy: false, allow_sell: false,
is_kcb: false, is_one_yuan: false, risk_level_code: None,
}],
vec![dataset.benchmark(prev_date).unwrap().clone()],
).unwrap();
portfolio.update_prices(missing_date, &paused_dataset, PriceField::Close)
.expect("dated suspension permits keeping the last known valuation, not creating a fill");
let position = portfolio.position("601028.SH").expect("position");
assert!((position.last_price - 10.3).abs() < 1e-6);