Add limit price slippage support
This commit is contained in:
@@ -78,6 +78,7 @@ pub enum SlippageModel {
|
||||
None,
|
||||
PriceRatio(f64),
|
||||
TickSize(f64),
|
||||
LimitPrice,
|
||||
}
|
||||
|
||||
pub struct BrokerSimulator<C, R> {
|
||||
@@ -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>,
|
||||
) -> 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,
|
||||
|
||||
@@ -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(),
|
||||
|
||||
Reference in New Issue
Block a user