Add matching and slippage execution options
This commit is contained in:
@@ -25,11 +25,26 @@ struct ExecutionFill {
|
||||
next_cursor: NaiveDateTime,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum MatchingType {
|
||||
CurrentBarClose,
|
||||
NextBarOpen,
|
||||
NextTickLast,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum SlippageModel {
|
||||
None,
|
||||
PriceRatio(f64),
|
||||
TickSize(f64),
|
||||
}
|
||||
|
||||
pub struct BrokerSimulator<C, R> {
|
||||
cost_model: C,
|
||||
rules: R,
|
||||
board_lot_size: u32,
|
||||
execution_price_field: PriceField,
|
||||
slippage_model: SlippageModel,
|
||||
volume_percent: f64,
|
||||
volume_limit: bool,
|
||||
inactive_limit: bool,
|
||||
@@ -44,6 +59,7 @@ impl<C, R> BrokerSimulator<C, R> {
|
||||
rules,
|
||||
board_lot_size: 100,
|
||||
execution_price_field: PriceField::Open,
|
||||
slippage_model: SlippageModel::None,
|
||||
volume_percent: 0.25,
|
||||
volume_limit: true,
|
||||
inactive_limit: true,
|
||||
@@ -62,6 +78,7 @@ impl<C, R> BrokerSimulator<C, R> {
|
||||
rules,
|
||||
board_lot_size: 100,
|
||||
execution_price_field,
|
||||
slippage_model: SlippageModel::None,
|
||||
volume_percent: 0.25,
|
||||
volume_limit: true,
|
||||
inactive_limit: true,
|
||||
@@ -94,6 +111,11 @@ impl<C, R> BrokerSimulator<C, R> {
|
||||
self.intraday_execution_start_time = Some(start_time);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_slippage_model(mut self, slippage_model: SlippageModel) -> Self {
|
||||
self.slippage_model = slippage_model;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, R> BrokerSimulator<C, R>
|
||||
@@ -118,16 +140,103 @@ where
|
||||
snapshot: &crate::data::DailyMarketSnapshot,
|
||||
side: OrderSide,
|
||||
) -> f64 {
|
||||
if self.execution_price_field == PriceField::Last
|
||||
let raw_price = if self.execution_price_field == PriceField::Last
|
||||
&& self.intraday_execution_start_time.is_some()
|
||||
{
|
||||
let _ = side;
|
||||
return snapshot.price(PriceField::Last);
|
||||
snapshot.price(PriceField::Last)
|
||||
} else {
|
||||
match side {
|
||||
OrderSide::Buy => self.buy_price(snapshot),
|
||||
OrderSide::Sell => self.sell_price(snapshot),
|
||||
}
|
||||
};
|
||||
|
||||
self.apply_slippage(snapshot, side, raw_price)
|
||||
}
|
||||
|
||||
fn apply_slippage(
|
||||
&self,
|
||||
snapshot: &crate::data::DailyMarketSnapshot,
|
||||
side: OrderSide,
|
||||
raw_price: f64,
|
||||
) -> f64 {
|
||||
if !raw_price.is_finite() || raw_price <= 0.0 {
|
||||
return raw_price;
|
||||
}
|
||||
|
||||
let adjusted = match self.slippage_model {
|
||||
SlippageModel::None => raw_price,
|
||||
SlippageModel::PriceRatio(ratio) => {
|
||||
let ratio = ratio.max(0.0);
|
||||
match side {
|
||||
OrderSide::Buy => raw_price * (1.0 + ratio),
|
||||
OrderSide::Sell => raw_price * (1.0 - ratio),
|
||||
}
|
||||
}
|
||||
SlippageModel::TickSize(ticks) => {
|
||||
let tick = snapshot.effective_price_tick();
|
||||
let ticks = ticks.max(0.0);
|
||||
match side {
|
||||
OrderSide::Buy => raw_price + tick * ticks,
|
||||
OrderSide::Sell => raw_price - tick * ticks,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
self.clamp_execution_price(snapshot, side, adjusted)
|
||||
}
|
||||
|
||||
fn clamp_execution_price(
|
||||
&self,
|
||||
snapshot: &crate::data::DailyMarketSnapshot,
|
||||
side: OrderSide,
|
||||
adjusted_price: f64,
|
||||
) -> f64 {
|
||||
if !adjusted_price.is_finite() {
|
||||
return adjusted_price;
|
||||
}
|
||||
|
||||
let mut bounded = adjusted_price.max(snapshot.effective_price_tick());
|
||||
match side {
|
||||
OrderSide::Buy => self.buy_price(snapshot),
|
||||
OrderSide::Sell => self.sell_price(snapshot),
|
||||
OrderSide::Buy => {
|
||||
if snapshot.upper_limit.is_finite() && snapshot.upper_limit > 0.0 {
|
||||
bounded = bounded.min(snapshot.upper_limit);
|
||||
}
|
||||
}
|
||||
OrderSide::Sell => {
|
||||
if snapshot.lower_limit.is_finite() && snapshot.lower_limit > 0.0 {
|
||||
bounded = bounded.max(snapshot.lower_limit);
|
||||
}
|
||||
}
|
||||
}
|
||||
bounded
|
||||
}
|
||||
|
||||
fn quote_execution_price(
|
||||
&self,
|
||||
snapshot: &crate::data::DailyMarketSnapshot,
|
||||
side: OrderSide,
|
||||
raw_price: f64,
|
||||
) -> f64 {
|
||||
self.apply_slippage(snapshot, side, raw_price)
|
||||
}
|
||||
|
||||
fn select_quote_reference_price(
|
||||
&self,
|
||||
snapshot: &crate::data::DailyMarketSnapshot,
|
||||
quote: &IntradayExecutionQuote,
|
||||
side: OrderSide,
|
||||
) -> Option<f64> {
|
||||
let raw_price = match side {
|
||||
OrderSide::Buy => quote.buy_price(),
|
||||
OrderSide::Sell => quote.sell_price(),
|
||||
}?;
|
||||
let execution_price = self.quote_execution_price(snapshot, side, raw_price);
|
||||
if execution_price.is_finite() && execution_price > 0.0 {
|
||||
Some(execution_price)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
@@ -656,6 +765,7 @@ where
|
||||
.map(|start_time| date.and_time(start_time));
|
||||
let quotes = data.execution_quotes_on(date, symbol);
|
||||
if let Some(estimated) = self.select_buy_sizing_fill(
|
||||
snapshot,
|
||||
quotes,
|
||||
start_cursor,
|
||||
max_requested_qty,
|
||||
@@ -699,6 +809,7 @@ where
|
||||
|
||||
fn select_buy_sizing_fill(
|
||||
&self,
|
||||
snapshot: &crate::data::DailyMarketSnapshot,
|
||||
quotes: &[IntradayExecutionQuote],
|
||||
start_cursor: Option<NaiveDateTime>,
|
||||
requested_qty: u32,
|
||||
@@ -729,9 +840,13 @@ where
|
||||
if quote.volume_delta == 0 {
|
||||
continue;
|
||||
}
|
||||
let Some(quote_price) = fallback_quote_price else {
|
||||
let Some(raw_quote_price) = fallback_quote_price else {
|
||||
continue;
|
||||
};
|
||||
let quote_price = self.quote_execution_price(snapshot, OrderSide::Buy, raw_quote_price);
|
||||
if !quote_price.is_finite() || quote_price <= 0.0 {
|
||||
continue;
|
||||
}
|
||||
let available_qty = quote
|
||||
.ask1_volume
|
||||
.saturating_mul(lot as u64)
|
||||
@@ -1110,6 +1225,7 @@ where
|
||||
let quotes = data.execution_quotes_on(date, symbol);
|
||||
|
||||
if let Some(fill) = self.select_execution_fill(
|
||||
snapshot,
|
||||
quotes,
|
||||
side,
|
||||
start_cursor,
|
||||
@@ -1126,6 +1242,7 @@ where
|
||||
|
||||
fn select_execution_fill(
|
||||
&self,
|
||||
snapshot: &crate::data::DailyMarketSnapshot,
|
||||
quotes: &[IntradayExecutionQuote],
|
||||
side: OrderSide,
|
||||
start_cursor: Option<NaiveDateTime>,
|
||||
@@ -1154,16 +1271,9 @@ where
|
||||
if quote.volume_delta == 0 {
|
||||
continue;
|
||||
}
|
||||
let quote_price = match side {
|
||||
OrderSide::Buy => quote.buy_price(),
|
||||
OrderSide::Sell => quote.sell_price(),
|
||||
};
|
||||
let Some(quote_price) = quote_price else {
|
||||
let Some(quote_price) = self.select_quote_reference_price(snapshot, quote, side) else {
|
||||
continue;
|
||||
};
|
||||
if !quote_price.is_finite() || quote_price <= 0.0 {
|
||||
continue;
|
||||
}
|
||||
let top_level_liquidity = match side {
|
||||
OrderSide::Buy => quote.ask1_volume,
|
||||
OrderSide::Sell => quote.bid1_volume,
|
||||
|
||||
Reference in New Issue
Block a user