修复 AiQuant 微盘回测撮合语义

This commit is contained in:
boris
2026-05-13 18:43:02 +08:00
parent 2165831708
commit db72f6f515
8 changed files with 849 additions and 162 deletions

View File

@@ -7,6 +7,108 @@ use fidc_core::{
};
use std::collections::{BTreeMap, BTreeSet};
fn order_value_rounding_data(date: NaiveDate, symbol: &str, price: f64) -> DataSet {
DataSet::from_components(
vec![Instrument {
symbol: symbol.to_string(),
name: "Test".to_string(),
board: "SZ".to_string(),
round_lot: 100,
listed_at: None,
delisted_at: None,
status: "active".to_string(),
}],
vec![DailyMarketSnapshot {
date,
symbol: symbol.to_string(),
timestamp: Some(format!("{date} 09:33:00")),
day_open: price,
open: price,
high: price,
low: price,
close: price,
last_price: price,
bid1: price,
ask1: price,
prev_close: price,
volume: 100_000,
tick_volume: 100_000,
bid1_volume: 80_000,
ask1_volume: 80_000,
trading_phase: Some("continuous".to_string()),
paused: false,
upper_limit: price * 1.1,
lower_limit: price * 0.9,
price_tick: 0.01,
}],
vec![DailyFactorSnapshot {
date,
symbol: symbol.to_string(),
market_cap_bn: 50.0,
free_float_cap_bn: 45.0,
pe_ttm: 15.0,
turnover_ratio: Some(2.0),
effective_turnover_ratio: Some(1.8),
extra_factors: BTreeMap::new(),
}],
vec![CandidateEligibility {
date,
symbol: symbol.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")
}
fn execute_single_value_order(
date: NaiveDate,
data: &DataSet,
symbol: &str,
value: f64,
) -> (PortfolioState, fidc_core::BrokerExecutionReport) {
let mut portfolio = PortfolioState::new(20_000.0);
let broker = BrokerSimulator::new_with_execution_price(
ChinaAShareCostModel::default(),
ChinaEquityRuleHooks::default(),
PriceField::Open,
)
.with_strict_value_budget(true);
let report = broker
.execute(
date,
&mut portfolio,
data,
&StrategyDecision {
rebalance: false,
target_weights: BTreeMap::new(),
exit_symbols: BTreeSet::new(),
order_intents: vec![OrderIntent::Value {
symbol: symbol.to_string(),
value,
reason: "test_order_value_rounding".to_string(),
}],
notes: Vec::new(),
diagnostics: Vec::new(),
},
)
.expect("broker execution");
(portfolio, report)
}
#[test]
fn broker_executes_explicit_order_value_buy() {
let date = NaiveDate::from_ymd_opt(2024, 1, 10).unwrap();
@@ -122,6 +224,31 @@ fn broker_executes_explicit_order_value_buy() {
assert!(portfolio.cash() < 1_000_000.0);
}
#[test]
fn broker_order_value_rounds_to_nearest_lot_when_min_lot_is_affordable() {
let date = NaiveDate::from_ymd_opt(2025, 1, 24).unwrap();
let symbol = "003017.SZ";
let data = order_value_rounding_data(date, symbol, 19.97);
let (portfolio, report) = execute_single_value_order(date, &data, symbol, 3_938.13);
assert_eq!(report.fill_events.len(), 1);
assert_eq!(report.fill_events[0].quantity, 200);
assert_eq!(portfolio.position(symbol).expect("position").quantity, 200);
}
#[test]
fn broker_order_value_skips_when_one_lot_exceeds_budget() {
let date = NaiveDate::from_ymd_opt(2025, 1, 2).unwrap();
let symbol = "300321.SZ";
let data = order_value_rounding_data(date, symbol, 20.38);
let (portfolio, report) = execute_single_value_order(date, &data, symbol, 2_000.0);
assert!(report.fill_events.is_empty());
assert!(portfolio.position(symbol).is_none());
}
#[test]
fn broker_executes_order_shares_and_order_lots() {
let date = NaiveDate::from_ymd_opt(2024, 1, 10).unwrap();
@@ -1438,6 +1565,121 @@ fn broker_rejects_intraday_last_order_without_execution_quotes() {
assert!(portfolio.position("000002.SZ").is_none());
}
#[test]
fn broker_executes_intraday_last_on_start_quote_without_trade_delta() {
let date = NaiveDate::from_ymd_opt(2024, 1, 10).unwrap();
let data = DataSet::from_components_with_actions_and_quotes(
vec![Instrument {
symbol: "000002.SZ".to_string(),
name: "Test".to_string(),
board: "SZ".to_string(),
round_lot: 100,
listed_at: None,
delisted_at: None,
status: "active".to_string(),
}],
vec![DailyMarketSnapshot {
date,
symbol: "000002.SZ".to_string(),
timestamp: Some("2024-01-10 09:33:00".to_string()),
day_open: 15.0,
open: 15.0,
high: 15.5,
low: 14.8,
close: 15.2,
last_price: 15.2,
bid1: 15.19,
ask1: 15.21,
prev_close: 15.0,
volume: 100_000,
tick_volume: 100_000,
bid1_volume: 80_000,
ask1_volume: 80_000,
trading_phase: Some("continuous".to_string()),
paused: false,
upper_limit: 16.5,
lower_limit: 13.5,
price_tick: 0.01,
}],
vec![DailyFactorSnapshot {
date,
symbol: "000002.SZ".to_string(),
market_cap_bn: 50.0,
free_float_cap_bn: 45.0,
pe_ttm: 15.0,
turnover_ratio: Some(2.0),
effective_turnover_ratio: Some(1.8),
extra_factors: BTreeMap::new(),
}],
vec![CandidateEligibility {
date,
symbol: "000002.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,
}],
Vec::new(),
vec![IntradayExecutionQuote {
date,
symbol: "000002.SZ".to_string(),
timestamp: date.and_hms_opt(9, 33, 0).unwrap(),
last_price: 15.2,
bid1: 15.19,
ask1: 15.21,
bid1_volume: 8,
ask1_volume: 8,
volume_delta: 0,
amount_delta: 0.0,
trading_phase: Some("continuous".to_string()),
}],
)
.expect("dataset");
let mut portfolio = PortfolioState::new(1_000_000.0);
let broker = BrokerSimulator::new_with_execution_price(
ChinaAShareCostModel::default(),
ChinaEquityRuleHooks::default(),
PriceField::Last,
)
.with_intraday_execution_start_time(chrono::NaiveTime::from_hms_opt(9, 33, 0).unwrap());
let report = broker
.execute(
date,
&mut portfolio,
&data,
&StrategyDecision {
rebalance: false,
target_weights: BTreeMap::new(),
exit_symbols: BTreeSet::new(),
order_intents: vec![OrderIntent::Value {
symbol: "000002.SZ".to_string(),
value: 4_000.0,
reason: "start_quote".to_string(),
}],
notes: Vec::new(),
diagnostics: Vec::new(),
},
)
.expect("broker execution");
assert_eq!(report.fill_events.len(), 1);
assert_eq!(report.fill_events[0].quantity, 200);
assert!((report.fill_events[0].price - 15.2).abs() < 1e-9);
assert_eq!(report.order_events[0].status, OrderStatus::Filled);
}
#[test]
fn broker_cancels_market_order_remainder_when_intraday_quote_liquidity_exhausted() {
let date = NaiveDate::from_ymd_opt(2024, 1, 10).unwrap();