Support algo order pricing in smart portfolio rebalances

This commit is contained in:
boris
2026-04-23 18:48:14 -07:00
parent ac308c8d68
commit 58836a1c37
7 changed files with 427 additions and 28 deletions

View File

@@ -3,7 +3,7 @@ use fidc_core::{
AlgoOrderStyle, BenchmarkSnapshot, BrokerSimulator, CandidateEligibility, ChinaAShareCostModel,
ChinaEquityRuleHooks, DailyFactorSnapshot, DailyMarketSnapshot, DataSet, Instrument,
IntradayExecutionQuote, MatchingType, OrderIntent, OrderStatus, PortfolioState, PriceField,
ProcessEventKind, SlippageModel, StrategyDecision,
ProcessEventKind, SlippageModel, StrategyDecision, TargetPortfolioOrderPricing,
};
use std::collections::{BTreeMap, BTreeSet};
@@ -479,10 +479,10 @@ fn broker_executes_target_portfolio_smart_with_custom_prices() {
("000001.SZ".to_string(), 0.0),
("000002.SZ".to_string(), 0.5),
]),
order_prices: Some(BTreeMap::from([
order_prices: Some(TargetPortfolioOrderPricing::LimitPrices(BTreeMap::from([
("000001.SZ".to_string(), 9.8),
("000002.SZ".to_string(), 10.2),
])),
]))),
valuation_prices: Some(BTreeMap::from([
("000001.SZ".to_string(), 10.0),
("000002.SZ".to_string(), 20.0),
@@ -516,6 +516,146 @@ fn broker_executes_target_portfolio_smart_with_custom_prices() {
);
}
#[test]
fn broker_executes_target_portfolio_smart_with_algo_order_style() {
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: "New".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 10:18:00".to_string()),
day_open: 10.0,
open: 10.0,
high: 10.3,
low: 9.9,
close: 10.2,
last_price: 10.2,
bid1: 10.19,
ask1: 10.21,
prev_close: 10.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: 11.0,
lower_limit: 9.0,
price_tick: 0.01,
}],
vec![DailyFactorSnapshot {
date,
symbol: "000002.SZ".to_string(),
market_cap_bn: 45.0,
free_float_cap_bn: 40.0,
pe_ttm: 14.0,
turnover_ratio: Some(2.2),
effective_turnover_ratio: Some(2.0),
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: "000852.SH".to_string(),
open: 1000.0,
close: 1002.0,
prev_close: 998.0,
volume: 1_000_000,
}],
Vec::new(),
vec![
IntradayExecutionQuote {
date,
symbol: "000002.SZ".to_string(),
timestamp: date.and_hms_opt(9, 31, 0).unwrap(),
last_price: 10.0,
ask1: 10.01,
bid1: 9.99,
ask1_volume: 1,
bid1_volume: 1,
volume_delta: 1,
amount_delta: 0.0,
trading_phase: Some("continuous".to_string()),
},
IntradayExecutionQuote {
date,
symbol: "000002.SZ".to_string(),
timestamp: date.and_hms_opt(9, 35, 0).unwrap(),
last_price: 10.2,
ask1: 10.21,
bid1: 10.19,
ask1_volume: 1,
bid1_volume: 1,
volume_delta: 1,
amount_delta: 0.0,
trading_phase: Some("continuous".to_string()),
},
],
)
.expect("dataset");
let mut portfolio = PortfolioState::new(2_100.0);
let broker = BrokerSimulator::new(
ChinaAShareCostModel::default(),
ChinaEquityRuleHooks::default(),
);
let report = broker
.execute(
date,
&mut portfolio,
&data,
&StrategyDecision {
rebalance: false,
target_weights: BTreeMap::new(),
exit_symbols: BTreeSet::new(),
order_intents: vec![OrderIntent::TargetPortfolioSmart {
target_weights: BTreeMap::from([("000002.SZ".to_string(), 1.0)]),
order_prices: Some(TargetPortfolioOrderPricing::AlgoOrder {
style: AlgoOrderStyle::Vwap,
start_time: Some(NaiveTime::from_hms_opt(9, 31, 0).unwrap()),
end_time: Some(NaiveTime::from_hms_opt(9, 35, 0).unwrap()),
}),
valuation_prices: Some(BTreeMap::from([("000002.SZ".to_string(), 10.0)])),
reason: "test_target_portfolio_smart_algo".to_string(),
}],
notes: Vec::new(),
diagnostics: Vec::new(),
},
)
.expect("broker execution");
assert_eq!(report.order_events.len(), 1);
assert_eq!(report.fill_events.len(), 1);
assert_eq!(report.fill_events[0].symbol, "000002.SZ");
assert_eq!(report.fill_events[0].side, fidc_core::OrderSide::Buy);
assert_eq!(report.fill_events[0].quantity, 200);
assert!((report.fill_events[0].price - 10.1).abs() < 1e-9);
assert_eq!(
portfolio.position("000002.SZ").map(|pos| pos.quantity),
Some(200)
);
}
#[test]
fn broker_executes_order_percent_and_target_percent() {
let date = NaiveDate::from_ymd_opt(2024, 1, 10).unwrap();