Add futures expiration settlement

This commit is contained in:
boris
2026-04-23 20:39:40 -07:00
parent 3439b5d8d0
commit dfd39fd8a3
3 changed files with 86 additions and 2 deletions

View File

@@ -161,3 +161,30 @@ fn futures_open_order_rejects_when_margin_is_insufficient() {
assert!(account.position("IF2501", FuturesDirection::Long).is_none());
assert!((account.total_cash() - 10_000.0).abs() < 1e-6);
}
#[test]
fn futures_expiration_settlement_closes_all_contract_directions() {
let spec = FuturesContractSpec::new(300.0, 0.12, 0.14);
let mut account = FuturesAccountState::new(1_000_000.0);
account.open("IF2501", FuturesDirection::Long, spec, 2, 4000.0, 0.0);
account.open("IF2501", FuturesDirection::Short, spec, 1, 4000.0, 0.0);
let report = account.expire_contract(d(2025, 1, 17), "IF2501", 4010.0, "contract expired");
assert_eq!(report.order_events.len(), 2);
assert_eq!(report.fill_events.len(), 2);
assert!(
report
.order_events
.iter()
.all(|event| event.status == OrderStatus::Filled)
);
assert!(account.position("IF2501", FuturesDirection::Long).is_none());
assert!(
account
.position("IF2501", FuturesDirection::Short)
.is_none()
);
assert!((account.total_cash() - 1_003_000.0).abs() < 1e-6);
}