Align jq microcap execution with intraday snapshots

This commit is contained in:
boris
2026-04-20 12:13:59 +08:00
parent 0e2c25e4c4
commit 0fe681ff5f
10 changed files with 761 additions and 94 deletions

View File

@@ -17,7 +17,10 @@ impl Strategy for BuyThenHoldStrategy {
"buy-then-hold"
}
fn on_day(&mut self, ctx: &StrategyContext<'_>) -> Result<StrategyDecision, fidc_core::BacktestError> {
fn on_day(
&mut self,
ctx: &StrategyContext<'_>,
) -> Result<StrategyDecision, fidc_core::BacktestError> {
if ctx.decision_date == d(2025, 1, 2) && ctx.portfolio.position("000001.SZ").is_none() {
return Ok(StrategyDecision {
rebalance: false,
@@ -238,12 +241,17 @@ fn engine_settles_delisted_position_before_missing_market_snapshot_breaks_run()
let result = engine.run().expect("backtest succeeds");
assert_eq!(result.fills.len(), 2);
assert!(result
.fills
.iter()
.any(|fill| fill.reason.contains("delisted_cash_settlement") && fill.symbol == "000001.SZ"));
assert!(result
.holdings_summary
.iter()
.all(|holding| holding.symbol != "000001.SZ"));
assert!(
result
.fills
.iter()
.any(|fill| fill.reason.contains("delisted_cash_settlement")
&& fill.symbol == "000001.SZ")
);
assert!(
result
.holdings_summary
.iter()
.all(|holding| holding.symbol != "000001.SZ")
);
}

View File

@@ -289,9 +289,15 @@ fn same_day_sell_then_rebuy_reinserts_position_at_end() {
.expect("dataset");
let mut portfolio = PortfolioState::new(1_000_000.0);
portfolio.position_mut("000001.SZ").buy(prev_date, 100, 10.0);
portfolio.position_mut("000002.SZ").buy(prev_date, 100, 10.0);
portfolio.position_mut("000003.SZ").buy(prev_date, 100, 10.0);
portfolio
.position_mut("000001.SZ")
.buy(prev_date, 100, 10.0);
portfolio
.position_mut("000002.SZ")
.buy(prev_date, 100, 10.0);
portfolio
.position_mut("000003.SZ")
.buy(prev_date, 100, 10.0);
let broker = BrokerSimulator::new_with_execution_price(
ChinaAShareCostModel::default(),

View File

@@ -60,10 +60,9 @@ fn can_load_partitioned_snapshot_dir() {
.len()
== 1
);
let market_rows = data.market_snapshots_on(chrono::NaiveDate::from_ymd_opt(2024, 1, 2).unwrap());
let snapshot = market_rows
.first()
.expect("market snapshot");
let market_rows =
data.market_snapshots_on(chrono::NaiveDate::from_ymd_opt(2024, 1, 2).unwrap());
let snapshot = market_rows.first().expect("market snapshot");
assert_eq!(snapshot.day_open, 10.1);
assert_eq!(snapshot.last_price, 10.15);
assert_eq!(snapshot.price_tick, 0.01);