diff --git a/crates/fidc-core/src/broker.rs b/crates/fidc-core/src/broker.rs index 688053e..af8b297 100644 --- a/crates/fidc-core/src/broker.rs +++ b/crates/fidc-core/src/broker.rs @@ -78,6 +78,7 @@ pub enum SlippageModel { None, PriceRatio(f64), TickSize(f64), + LimitPrice, } pub struct BrokerSimulator { @@ -245,6 +246,7 @@ where OrderSide::Sell => raw_price - tick * ticks, } } + SlippageModel::LimitPrice => raw_price, }; self.clamp_execution_price(snapshot, side, adjusted) @@ -1593,6 +1595,8 @@ where ); (0, Vec::new()) } else { + let execution_price = + self.execution_price_with_limit_slippage(execution_price, limit_price); ( fillable_qty, vec![ExecutionLeg { @@ -2361,6 +2365,8 @@ where ); (0, Vec::new()) } else { + let execution_price = + self.execution_price_with_limit_slippage(execution_price, limit_price); let filled_qty = self.affordable_buy_quantity( date, portfolio.cash(), @@ -2888,6 +2894,17 @@ where } } + fn execution_price_with_limit_slippage( + &self, + execution_price: f64, + limit_price: Option, + ) -> f64 { + match (self.slippage_model, limit_price) { + (SlippageModel::LimitPrice, Some(limit_price)) => limit_price, + _ => execution_price, + } + } + fn limit_order_can_remain_open(partial_reason: Option<&str>) -> bool { !partial_reason.is_some_and(|reason| { reason.contains("insufficient cash") || reason.contains("value budget") @@ -2948,6 +2965,8 @@ where ) { return None; } + let execution_price = + self.execution_price_with_limit_slippage(execution_price, limit_price); let quantity = match side { OrderSide::Buy => self.affordable_buy_quantity( date, @@ -3037,6 +3056,7 @@ where ) { continue; } + let quote_price = self.execution_price_with_limit_slippage(quote_price, limit_price); let top_level_liquidity = match side { OrderSide::Buy => quote.ask1_volume, OrderSide::Sell => quote.bid1_volume, diff --git a/crates/fidc-core/src/strategy_ai.rs b/crates/fidc-core/src/strategy_ai.rs index 6a5e4fa..f410f57 100644 --- a/crates/fidc-core/src/strategy_ai.rs +++ b/crates/fidc-core/src/strategy_ai.rs @@ -116,7 +116,7 @@ pub fn built_in_strategy_manual() -> StrategyAiManual { }, ManualSection { title: "execution.matching_type / execution.slippage".to_string(), - detail: "设置撮合模式和滑点。支持 execution.matching_type(\"next_tick_last\" | \"next_tick_best_own\" | \"next_tick_best_counterparty\" | \"counterparty_offer\" | \"vwap\" | \"current_bar_close\" | \"next_bar_open\" | \"open_auction\")。其中 next_tick_last 使用 tick 的 last_price;next_tick_best_own / next_tick_best_counterparty 会按 L1 买一卖一近似 rqalpha 的 tick 最优价语义,counterparty_offer 当前也按 L1 对手方报价近似实现;vwap 会在盘中执行价链路上聚合多笔成交为单条 VWAP 成交;open_auction 使用当日集合竞价开盘价 day_open 进行撮合,且不额外施加滑点,并按竞价成交量而不是盘口一档流动性限制成交;滑点支持 execution.slippage(\"none\") / execution.slippage(\"price_ratio\", 0.001) / execution.slippage(\"tick_size\", 1)。".to_string(), + detail: "设置撮合模式和滑点。支持 execution.matching_type(\"next_tick_last\" | \"next_tick_best_own\" | \"next_tick_best_counterparty\" | \"counterparty_offer\" | \"vwap\" | \"current_bar_close\" | \"next_bar_open\" | \"open_auction\")。其中 next_tick_last 使用 tick 的 last_price;next_tick_best_own / next_tick_best_counterparty 会按 L1 买一卖一近似 rqalpha 的 tick 最优价语义,counterparty_offer 当前也按 L1 对手方报价近似实现;vwap 会在盘中执行价链路上聚合多笔成交为单条 VWAP 成交;open_auction 使用当日集合竞价开盘价 day_open 进行撮合,且不额外施加滑点,并按竞价成交量而不是盘口一档流动性限制成交;滑点支持 execution.slippage(\"none\") / execution.slippage(\"price_ratio\", 0.001) / execution.slippage(\"tick_size\", 1) / execution.slippage(\"limit_price\"),其中 limit_price 会在限价单成交时按挂单价模拟 rqalpha 的最坏成交价。".to_string(), }, ManualSection { title: "when / unless / else".to_string(), diff --git a/crates/fidc-core/tests/explicit_order_flow.rs b/crates/fidc-core/tests/explicit_order_flow.rs index e646fb6..75817ef 100644 --- a/crates/fidc-core/tests/explicit_order_flow.rs +++ b/crates/fidc-core/tests/explicit_order_flow.rs @@ -2802,6 +2802,43 @@ fn broker_keeps_limit_buy_open_until_price_becomes_marketable() { ); } +#[test] +fn broker_uses_limit_price_slippage_for_limit_orders() { + let date = NaiveDate::from_ymd_opt(2024, 1, 10).unwrap(); + let data = two_day_limit_order_data(10.0, 10.0); + let broker = BrokerSimulator::new_with_execution_price( + ChinaAShareCostModel::default(), + ChinaEquityRuleHooks::default(), + PriceField::Open, + ) + .with_slippage_model(SlippageModel::LimitPrice); + let mut portfolio = PortfolioState::new(1_000_000.0); + + let report = broker + .execute( + date, + &mut portfolio, + &data, + &StrategyDecision { + rebalance: false, + target_weights: BTreeMap::new(), + exit_symbols: BTreeSet::new(), + order_intents: vec![OrderIntent::LimitShares { + symbol: "000002.SZ".to_string(), + quantity: 200, + limit_price: 10.1, + reason: "limit_entry".to_string(), + }], + notes: Vec::new(), + diagnostics: Vec::new(), + }, + ) + .expect("broker execution"); + + assert_eq!(report.fill_events.len(), 1); + assert!((report.fill_events[0].price - 10.1).abs() < 1e-9); +} + #[test] fn broker_cancels_open_order_by_order_id() { let day1 = NaiveDate::from_ymd_opt(2024, 1, 10).unwrap();