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,
|
||||
|
||||
Reference in New Issue
Block a user