对齐 AiQuant RQAlpha 回测语义

This commit is contained in:
boris
2026-05-15 11:48:10 +08:00
parent 94662b6e75
commit 4577657c90
7 changed files with 1377 additions and 69 deletions

View File

@@ -233,8 +233,148 @@ fn broker_order_value_rounds_to_nearest_lot_when_min_lot_is_affordable() {
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);
assert_eq!(report.fill_events[0].quantity, 100);
assert_eq!(portfolio.position(symbol).expect("position").quantity, 100);
}
#[test]
fn broker_order_value_budget_includes_buy_commission() {
let date = NaiveDate::from_ymd_opt(2025, 6, 23).unwrap();
let symbol = "605303.SH";
let data = order_value_rounding_data(date, symbol, 11.93);
let (portfolio, report) = execute_single_value_order(date, &data, symbol, 4_776.0);
assert_eq!(report.fill_events.len(), 1);
assert_eq!(report.fill_events[0].quantity, 300);
assert_eq!(portfolio.position(symbol).expect("position").quantity, 300);
let (portfolio, report) = execute_single_value_order(date, &data, symbol, 4_848.0);
assert_eq!(report.fill_events.len(), 1);
assert_eq!(report.fill_events[0].quantity, 400);
assert_eq!(portfolio.position(symbol).expect("position").quantity, 400);
}
#[test]
fn broker_delayed_limit_open_sell_uses_tick_price() {
let date = NaiveDate::from_ymd_opt(2025, 6, 27).unwrap();
let prev_date = NaiveDate::from_ymd_opt(2025, 6, 26).unwrap();
let symbol = "300635.SZ";
let data = DataSet::from_components_with_actions_and_quotes(
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("2025-06-27 09:31:00".to_string()),
day_open: 12.55,
open: 12.55,
high: 13.16,
low: 12.26,
close: 12.36,
last_price: 12.39,
bid1: 12.39,
ask1: 12.40,
prev_close: 13.24,
volume: 329_575,
tick_volume: 10_000,
bid1_volume: 10_000,
ask1_volume: 10_000,
trading_phase: Some("continuous".to_string()),
paused: false,
upper_limit: 14.56,
lower_limit: 11.92,
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,
}],
Vec::new(),
vec![IntradayExecutionQuote {
date,
symbol: symbol.to_string(),
timestamp: date.and_hms_opt(9, 31, 0).unwrap(),
last_price: 12.39,
bid1: 12.39,
ask1: 12.40,
bid1_volume: 10_000,
ask1_volume: 10_000,
volume_delta: 10_000,
amount_delta: 123_900.0,
trading_phase: Some("continuous".to_string()),
}],
)
.expect("dataset");
let mut portfolio = PortfolioState::new(1_000.0);
portfolio.position_mut(symbol).buy(prev_date, 800, 10.92);
let broker = BrokerSimulator::new_with_execution_price(
ChinaAShareCostModel::default(),
ChinaEquityRuleHooks::default(),
PriceField::Last,
)
.with_matching_type(MatchingType::NextTickLast)
.with_intraday_execution_start_time(NaiveTime::from_hms_opt(9, 31, 0).unwrap())
.with_volume_limit(false)
.with_liquidity_limit(false);
let report = broker
.execute(
date,
&mut portfolio,
&data,
&StrategyDecision {
rebalance: false,
target_weights: BTreeMap::new(),
exit_symbols: BTreeSet::new(),
order_intents: vec![OrderIntent::TargetValue {
symbol: symbol.to_string(),
target_value: 0.0,
reason: "delayed_limit_open_sell".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, 800);
assert_eq!(report.fill_events[0].price, 12.39);
assert!(portfolio.position(symbol).is_none());
}
#[test]
@@ -905,7 +1045,7 @@ fn broker_executes_order_percent_and_target_percent() {
)
.expect("percent execution");
assert_eq!(percent_report.fill_events.len(), 1);
assert_eq!(percent_report.fill_events[0].quantity, 10_000);
assert_eq!(percent_report.fill_events[0].quantity, 9_900);
let mut target_percent_portfolio = PortfolioState::new(1_000_000.0);
let target_percent_report = broker
@@ -2498,7 +2638,7 @@ fn broker_executes_algo_twap_percent_across_window_quotes() {
exit_symbols: BTreeSet::new(),
order_intents: vec![OrderIntent::AlgoPercent {
symbol: "000002.SZ".to_string(),
percent: 0.0036,
percent: 0.0037,
style: AlgoOrderStyle::Twap,
start_time: Some(NaiveTime::from_hms_opt(10, 0, 0).unwrap()),
end_time: Some(NaiveTime::from_hms_opt(10, 30, 0).unwrap()),