fix: tolerate missing holding market rows
This commit is contained in:
@@ -551,8 +551,14 @@ impl PortfolioState {
|
||||
field: PriceField,
|
||||
) -> Result<(), DataSetError> {
|
||||
for position in self.positions.values_mut() {
|
||||
let price = data.price(date, &position.symbol, field).ok_or_else(|| {
|
||||
DataSetError::MissingSnapshot {
|
||||
let price = data
|
||||
.price(date, &position.symbol, field)
|
||||
.or_else(|| data.price_on_or_before(date, &position.symbol, field))
|
||||
.or_else(|| {
|
||||
(position.last_price.is_finite() && position.last_price > 0.0)
|
||||
.then_some(position.last_price)
|
||||
})
|
||||
.ok_or_else(|| DataSetError::MissingSnapshot {
|
||||
kind: match field {
|
||||
PriceField::DayOpen => "day open price",
|
||||
PriceField::Open => "open price",
|
||||
@@ -561,8 +567,7 @@ impl PortfolioState {
|
||||
},
|
||||
date,
|
||||
symbol: position.symbol.clone(),
|
||||
}
|
||||
})?;
|
||||
})?;
|
||||
position.last_price = price;
|
||||
position.refresh_day_pnl();
|
||||
}
|
||||
@@ -992,6 +997,75 @@ mod tests {
|
||||
assert!((position.trading_pnl + 5.0).abs() < 1e-6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn portfolio_carries_last_price_when_position_market_row_is_missing() {
|
||||
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);
|
||||
portfolio
|
||||
.position_mut("601028.SH")
|
||||
.buy(prev_date, 100, 10.0);
|
||||
|
||||
let dataset = DataSet::from_components(
|
||||
vec![Instrument {
|
||||
symbol: "601028.SH".to_string(),
|
||||
name: "Missing Row Test".to_string(),
|
||||
board: "SH".to_string(),
|
||||
round_lot: 100,
|
||||
listed_at: None,
|
||||
delisted_at: None,
|
||||
status: "active".to_string(),
|
||||
}],
|
||||
vec![DailyMarketSnapshot {
|
||||
date: prev_date,
|
||||
symbol: "601028.SH".to_string(),
|
||||
timestamp: None,
|
||||
day_open: 10.2,
|
||||
open: 10.2,
|
||||
high: 10.4,
|
||||
low: 9.9,
|
||||
close: 10.3,
|
||||
last_price: 10.3,
|
||||
bid1: 10.29,
|
||||
ask1: 10.31,
|
||||
prev_close: 10.0,
|
||||
volume: 1000,
|
||||
tick_volume: 1000,
|
||||
bid1_volume: 1000,
|
||||
ask1_volume: 1000,
|
||||
trading_phase: None,
|
||||
paused: false,
|
||||
upper_limit: 11.0,
|
||||
lower_limit: 9.0,
|
||||
price_tick: 0.01,
|
||||
}],
|
||||
Vec::new(),
|
||||
Vec::new(),
|
||||
vec![BenchmarkSnapshot {
|
||||
date: prev_date,
|
||||
benchmark: "000852.SH".to_string(),
|
||||
open: 1000.0,
|
||||
close: 1000.0,
|
||||
prev_close: 999.0,
|
||||
volume: 1000,
|
||||
}],
|
||||
)
|
||||
.expect("dataset");
|
||||
|
||||
portfolio
|
||||
.update_prices(prev_date, &dataset, PriceField::Close)
|
||||
.expect("previous close");
|
||||
portfolio.begin_trading_day();
|
||||
portfolio
|
||||
.update_prices(missing_date, &dataset, PriceField::Close)
|
||||
.expect("missing current row should carry previous close");
|
||||
|
||||
let position = portfolio.position("601028.SH").expect("position");
|
||||
assert!((position.last_price - 10.3).abs() < 1e-6);
|
||||
assert!((position.market_value() - 1030.0).abs() < 1e-6);
|
||||
assert!(position.position_pnl.abs() < 1e-6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn position_tracks_day_lifecycle_fields() {
|
||||
let prev_date = NaiveDate::from_ymd_opt(2025, 1, 2).unwrap();
|
||||
|
||||
Reference in New Issue
Block a user