对齐 AiQuant RQAlpha 回测语义
This commit is contained in:
@@ -111,6 +111,7 @@ pub struct BrokerSimulator<C, R> {
|
||||
inactive_limit: bool,
|
||||
liquidity_limit: bool,
|
||||
strict_value_budget: bool,
|
||||
same_day_buy_close_mark_at_fill: bool,
|
||||
intraday_execution_start_time: Option<NaiveTime>,
|
||||
runtime_intraday_start_time: Cell<Option<NaiveTime>>,
|
||||
runtime_intraday_end_time: Cell<Option<NaiveTime>>,
|
||||
@@ -132,6 +133,7 @@ impl<C, R> BrokerSimulator<C, R> {
|
||||
inactive_limit: true,
|
||||
liquidity_limit: true,
|
||||
strict_value_budget: false,
|
||||
same_day_buy_close_mark_at_fill: false,
|
||||
intraday_execution_start_time: None,
|
||||
runtime_intraday_start_time: Cell::new(None),
|
||||
runtime_intraday_end_time: Cell::new(None),
|
||||
@@ -157,6 +159,7 @@ impl<C, R> BrokerSimulator<C, R> {
|
||||
inactive_limit: true,
|
||||
liquidity_limit: true,
|
||||
strict_value_budget: false,
|
||||
same_day_buy_close_mark_at_fill: false,
|
||||
intraday_execution_start_time: None,
|
||||
runtime_intraday_start_time: Cell::new(None),
|
||||
runtime_intraday_end_time: Cell::new(None),
|
||||
@@ -185,6 +188,15 @@ impl<C, R> BrokerSimulator<C, R> {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_same_day_buy_close_mark_at_fill(mut self, enabled: bool) -> Self {
|
||||
self.same_day_buy_close_mark_at_fill = enabled;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn same_day_buy_close_mark_at_fill(&self) -> bool {
|
||||
self.same_day_buy_close_mark_at_fill
|
||||
}
|
||||
|
||||
pub fn with_volume_percent(mut self, volume_percent: f64) -> Self {
|
||||
self.volume_percent = volume_percent;
|
||||
self
|
||||
@@ -252,6 +264,34 @@ where
|
||||
snapshot.price(self.execution_price_field)
|
||||
}
|
||||
|
||||
fn value_buy_sizing_price(
|
||||
&self,
|
||||
date: NaiveDate,
|
||||
data: &DataSet,
|
||||
symbol: &str,
|
||||
snapshot: &crate::data::DailyMarketSnapshot,
|
||||
) -> f64 {
|
||||
let start_cursor = self
|
||||
.runtime_intraday_start_time
|
||||
.get()
|
||||
.or(self.intraday_execution_start_time)
|
||||
.map(|start_time| date.and_time(start_time));
|
||||
data.execution_quotes_on(date, symbol)
|
||||
.iter()
|
||||
.filter(|quote| {
|
||||
start_cursor
|
||||
.map(|cursor| quote.timestamp >= cursor)
|
||||
.unwrap_or(true)
|
||||
})
|
||||
.next()
|
||||
.and_then(|quote| match self.execution_price_field {
|
||||
PriceField::Last => (quote.last_price.is_finite() && quote.last_price > 0.0)
|
||||
.then_some(quote.last_price),
|
||||
_ => quote.buy_price(),
|
||||
})
|
||||
.unwrap_or_else(|| self.sizing_price(snapshot))
|
||||
}
|
||||
|
||||
fn snapshot_execution_price(
|
||||
&self,
|
||||
snapshot: &crate::data::DailyMarketSnapshot,
|
||||
@@ -2917,7 +2957,7 @@ where
|
||||
let round_lot = self.round_lot(data, symbol);
|
||||
let minimum_order_quantity = self.minimum_order_quantity(data, symbol);
|
||||
let order_step_size = self.order_step_size(data, symbol);
|
||||
let price = self.sizing_price(snapshot);
|
||||
let price = self.value_buy_sizing_price(date, data, symbol, snapshot);
|
||||
let snapshot_requested_qty = self.value_buy_quantity(
|
||||
date,
|
||||
value.abs(),
|
||||
@@ -3408,13 +3448,10 @@ where
|
||||
requested_qty: u32,
|
||||
reference_price: f64,
|
||||
) -> Option<f64> {
|
||||
value_budget.map(|budget| {
|
||||
if self.strict_value_budget {
|
||||
budget.max(reference_price * requested_qty as f64)
|
||||
} else {
|
||||
budget + 400.0
|
||||
}
|
||||
})
|
||||
if !self.strict_value_budget {
|
||||
return None;
|
||||
}
|
||||
value_budget.map(|budget| budget.max(reference_price * requested_qty as f64))
|
||||
}
|
||||
|
||||
fn process_buy(
|
||||
@@ -3733,7 +3770,7 @@ where
|
||||
.position_mut(symbol)
|
||||
.buy(date, leg.quantity, leg.price);
|
||||
if let Some(position) = portfolio.position_mut_if_exists(symbol) {
|
||||
position.record_trade_cost(cost.total());
|
||||
position.record_buy_trade_cost(leg.quantity, cost.total());
|
||||
}
|
||||
|
||||
report.fill_events.push(FillEvent {
|
||||
@@ -4372,7 +4409,8 @@ where
|
||||
return None;
|
||||
}
|
||||
|
||||
let quote_quantity_limited = self.quote_quantity_limited(matching_type);
|
||||
let quote_quantity_limited =
|
||||
self.quote_quantity_limited_for_window(matching_type, start_cursor, end_cursor);
|
||||
let lot = round_lot.max(1);
|
||||
let eligible_quotes: Vec<&IntradayExecutionQuote> = quotes
|
||||
.iter()
|
||||
@@ -4533,6 +4571,23 @@ where
|
||||
self.volume_limit || self.liquidity_limit || matching_type != MatchingType::NextTickLast
|
||||
}
|
||||
|
||||
fn quote_quantity_limited_for_window(
|
||||
&self,
|
||||
matching_type: MatchingType,
|
||||
start_cursor: Option<NaiveDateTime>,
|
||||
end_cursor: Option<NaiveDateTime>,
|
||||
) -> bool {
|
||||
if matching_type == MatchingType::Twap
|
||||
&& !self.volume_limit
|
||||
&& !self.liquidity_limit
|
||||
&& start_cursor.is_some()
|
||||
&& start_cursor == end_cursor
|
||||
{
|
||||
return false;
|
||||
}
|
||||
self.quote_quantity_limited(matching_type)
|
||||
}
|
||||
|
||||
fn uses_serial_execution_cursor(&self, reason: &str) -> bool {
|
||||
let _ = reason;
|
||||
false
|
||||
@@ -4629,4 +4684,26 @@ mod tests {
|
||||
assert!(volume_limited.quote_quantity_limited(MatchingType::NextTickLast));
|
||||
assert!(liquidity_limited.quote_quantity_limited(MatchingType::NextTickLast));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn instantaneous_twap_without_limits_does_not_cap_quote_quantity() {
|
||||
let broker = BrokerSimulator::new(ChinaAShareCostModel::default(), ChinaEquityRuleHooks)
|
||||
.with_volume_limit(false)
|
||||
.with_liquidity_limit(false);
|
||||
let cursor = chrono::NaiveDate::from_ymd_opt(2025, 11, 3)
|
||||
.unwrap()
|
||||
.and_hms_opt(9, 31, 0)
|
||||
.unwrap();
|
||||
|
||||
assert!(!broker.quote_quantity_limited_for_window(
|
||||
MatchingType::Twap,
|
||||
Some(cursor),
|
||||
Some(cursor)
|
||||
));
|
||||
assert!(broker.quote_quantity_limited_for_window(
|
||||
MatchingType::Twap,
|
||||
Some(cursor),
|
||||
Some(cursor + chrono::Duration::minutes(1))
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user