Add futures expiration schedule

This commit is contained in:
boris
2026-04-23 20:43:25 -07:00
parent 4755a59a5b
commit 5653278576
3 changed files with 165 additions and 3 deletions

View File

@@ -30,6 +30,73 @@ fn bool_flags(values: Vec<bool>) -> String {
.join(",")
}
fn single_day_anchor_data(date: NaiveDate) -> DataSet {
DataSet::from_components(
vec![Instrument {
symbol: "000001.SZ".to_string(),
name: "Anchor".to_string(),
board: "SZ".to_string(),
round_lot: 100,
listed_at: Some(d(2020, 1, 1)),
delisted_at: None,
status: "active".to_string(),
}],
vec![DailyMarketSnapshot {
date,
symbol: "000001.SZ".to_string(),
timestamp: Some("2025-01-02 10:18:00".to_string()),
day_open: 10.0,
open: 10.0,
high: 10.0,
low: 10.0,
close: 10.0,
last_price: 10.0,
bid1: 10.0,
ask1: 10.0,
prev_close: 9.9,
volume: 1_000_000,
tick_volume: 1_000_000,
bid1_volume: 1_000_000,
ask1_volume: 1_000_000,
trading_phase: Some("continuous".to_string()),
paused: false,
upper_limit: 10.89,
lower_limit: 8.91,
price_tick: 0.01,
}],
vec![DailyFactorSnapshot {
date,
symbol: "000001.SZ".to_string(),
market_cap_bn: 100.0,
free_float_cap_bn: 80.0,
pe_ttm: 10.0,
turnover_ratio: Some(1.0),
effective_turnover_ratio: Some(1.0),
extra_factors: BTreeMap::new(),
}],
vec![CandidateEligibility {
date,
symbol: "000001.SZ".to_string(),
is_st: false,
is_new_listing: false,
is_paused: false,
allow_buy: true,
allow_sell: true,
is_kcb: false,
is_one_yuan: false,
}],
vec![BenchmarkSnapshot {
date,
benchmark: "000300.SH".to_string(),
open: 100.0,
close: 100.0,
prev_close: 99.0,
volume: 1_000_000,
}],
)
.expect("dataset")
}
struct HookProbeStrategy {
log: Rc<RefCell<Vec<String>>>,
}
@@ -983,6 +1050,54 @@ fn engine_executes_futures_order_intents_against_future_account() {
assert!((futures_account.cash() - 355_988.0).abs() < 1e-6);
}
#[test]
fn engine_settles_configured_futures_expiration_at_settlement() {
let date = d(2025, 1, 2);
let broker = BrokerSimulator::new_with_execution_price(
ChinaAShareCostModel::default(),
ChinaEquityRuleHooks::default(),
PriceField::Open,
);
let mut engine = BacktestEngine::new(
single_day_anchor_data(date),
FuturesOrderStrategy,
broker,
BacktestConfig {
initial_cash: 100_000.0,
benchmark_code: "000300.SH".to_string(),
start_date: Some(date),
end_date: Some(date),
decision_lag_trading_days: 0,
execution_price_field: PriceField::Open,
},
)
.with_futures_initial_cash(500_000.0)
.with_futures_expiration(date, "IF2501", 4010.0);
let result = engine.run().expect("backtest succeeds");
assert_eq!(
result
.order_events
.iter()
.filter(|event| event.symbol == "IF2501" && event.status == OrderStatus::Filled)
.count(),
2
);
let futures_account = engine.futures_account().expect("future account");
assert!(
futures_account
.position("IF2501", FuturesDirection::Long)
.is_none()
);
assert!((futures_account.total_cash() - 502_988.0).abs() < 1e-6);
assert!(result.process_events.iter().any(|event| {
event.symbol.as_deref() == Some("IF2501")
&& event.kind == ProcessEventKind::Trade
&& event.detail.contains("4010")
}));
}
#[test]
fn engine_runs_subscribed_tick_hooks_and_executes_tick_orders() {
let date = d(2025, 1, 2);