Add matching and slippage execution options

This commit is contained in:
boris
2026-04-22 21:39:26 -07:00
parent 8db0f37cae
commit 081686185a
4 changed files with 342 additions and 20 deletions

View File

@@ -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,

View File

@@ -6,14 +6,14 @@ pub mod engine;
pub mod events;
pub mod instrument;
pub mod metrics;
pub mod portfolio;
pub mod platform_expr_strategy;
pub mod portfolio;
pub mod rules;
pub mod strategy;
pub mod strategy_ai;
pub mod universe;
pub use broker::{BrokerExecutionReport, BrokerSimulator};
pub use broker::{BrokerExecutionReport, BrokerSimulator, MatchingType, SlippageModel};
pub use calendar::TradingCalendar;
pub use cost::{ChinaAShareCostModel, CostModel, TradingCost};
pub use data::{
@@ -28,8 +28,8 @@ pub use engine::{
pub use events::{AccountEvent, FillEvent, OrderEvent, OrderSide, OrderStatus, PositionEvent};
pub use instrument::Instrument;
pub use metrics::{BacktestMetrics, compute_backtest_metrics};
pub use portfolio::{CashReceivable, HoldingSummary, PortfolioState, Position};
pub use platform_expr_strategy::{PlatformExprStrategy, PlatformExprStrategyConfig};
pub use portfolio::{CashReceivable, HoldingSummary, PortfolioState, Position};
pub use rules::{ChinaEquityRuleHooks, EquityRuleHooks, RuleCheck};
pub use strategy::{
CnSmallCapRotationConfig, CnSmallCapRotationStrategy, JqMicroCapConfig, JqMicroCapStrategy,

View File

@@ -106,6 +106,10 @@ pub fn built_in_strategy_manual() -> StrategyAiManual {
title: "filter.stock_expr / risk.stop_loss / risk.take_profit / allocation.buy_scale".to_string(),
detail: "表达式型规则支持多条组合。stop_loss/take_profit 多条按 OR 组合filter.stock_expr 多条按 AND 组合。".to_string(),
},
ManualSection {
title: "execution.matching_type / execution.slippage".to_string(),
detail: "设置撮合模式和滑点。支持 execution.matching_type(\"next_tick_last\" | \"current_bar_close\" | \"next_bar_open\"),以及 execution.slippage(\"none\") / execution.slippage(\"price_ratio\", 0.001) / execution.slippage(\"tick_size\", 1)。".to_string(),
},
ManualSection {
title: "when / unless / else".to_string(),
detail: "条件块支持按日期、指数、仓位等动态切换规则。".to_string(),
@@ -190,6 +194,10 @@ pub fn built_in_strategy_manual() -> StrategyAiManual {
title: "涨停触达后满仓,否则半仓".to_string(),
code: "allocation.buy_scale(touched_upper_limit ? 1.0 : 0.5)".to_string(),
},
ManualExample {
title: "next tick 撮合 + tick 滑点".to_string(),
code: "execution.matching_type(\"next_tick_last\")\nexecution.slippage(\"tick_size\", 1)".to_string(),
},
],
}
}
@@ -259,7 +267,10 @@ pub fn render_manual_markdown(manual: &StrategyAiManual) -> String {
}
out.push_str("## 示例\n");
for example in &manual.examples {
out.push_str(&format!("### {}\n```txt\n{}\n```\n\n", example.title, example.code));
out.push_str(&format!(
"### {}\n```txt\n{}\n```\n\n",
example.title, example.code
));
}
out
}
@@ -313,8 +324,7 @@ pub fn build_optimization_prompt(
}
prompt.push_str("结果摘要 JSON\n```json\n");
prompt.push_str(
&serde_json::to_string_pretty(&request.result_summary)
.unwrap_or_else(|_| "{}".to_string()),
&serde_json::to_string_pretty(&request.result_summary).unwrap_or_else(|_| "{}".to_string()),
);
prompt.push_str("\n```\n\n");
prompt.push_str("详细手册如下:\n\n");