修正目标组合全仓卖出失败回补语义

This commit is contained in:
boris
2026-07-10 12:40:42 +08:00
parent 56859dbe32
commit 5166916926
+303 -6
View File
@@ -188,6 +188,7 @@ pub struct BrokerSimulator<C, R> {
same_day_buy_close_mark_at_fill: bool, same_day_buy_close_mark_at_fill: bool,
risk_config: FidcRiskControlConfig, risk_config: FidcRiskControlConfig,
same_day_sold_symbols: RefCell<BTreeMap<NaiveDate, BTreeSet<String>>>, same_day_sold_symbols: RefCell<BTreeMap<NaiveDate, BTreeSet<String>>>,
same_day_failed_full_close_sell_symbols: RefCell<BTreeMap<NaiveDate, BTreeSet<String>>>,
intraday_execution_start_time: Option<NaiveTime>, intraday_execution_start_time: Option<NaiveTime>,
runtime_intraday_start_time: Cell<Option<NaiveTime>>, runtime_intraday_start_time: Cell<Option<NaiveTime>>,
runtime_intraday_end_time: Cell<Option<NaiveTime>>, runtime_intraday_end_time: Cell<Option<NaiveTime>>,
@@ -217,6 +218,7 @@ impl<C, R> BrokerSimulator<C, R> {
same_day_buy_close_mark_at_fill: false, same_day_buy_close_mark_at_fill: false,
risk_config: FidcRiskControlConfig::default(), risk_config: FidcRiskControlConfig::default(),
same_day_sold_symbols: RefCell::new(BTreeMap::new()), same_day_sold_symbols: RefCell::new(BTreeMap::new()),
same_day_failed_full_close_sell_symbols: RefCell::new(BTreeMap::new()),
intraday_execution_start_time: None, intraday_execution_start_time: None,
runtime_intraday_start_time: Cell::new(None), runtime_intraday_start_time: Cell::new(None),
runtime_intraday_end_time: Cell::new(None), runtime_intraday_end_time: Cell::new(None),
@@ -250,6 +252,7 @@ impl<C, R> BrokerSimulator<C, R> {
same_day_buy_close_mark_at_fill: false, same_day_buy_close_mark_at_fill: false,
risk_config: FidcRiskControlConfig::default(), risk_config: FidcRiskControlConfig::default(),
same_day_sold_symbols: RefCell::new(BTreeMap::new()), same_day_sold_symbols: RefCell::new(BTreeMap::new()),
same_day_failed_full_close_sell_symbols: RefCell::new(BTreeMap::new()),
intraday_execution_start_time: None, intraday_execution_start_time: None,
runtime_intraday_start_time: Cell::new(None), runtime_intraday_start_time: Cell::new(None),
runtime_intraday_end_time: Cell::new(None), runtime_intraday_end_time: Cell::new(None),
@@ -1547,21 +1550,45 @@ where
.insert(symbol.to_string()); .insert(symbol.to_string());
} }
fn mark_failed_full_close_sell(&self, date: NaiveDate, symbol: &str) {
self.same_day_failed_full_close_sell_symbols
.borrow_mut()
.entry(date)
.or_default()
.insert(symbol.to_string());
}
fn failed_full_close_sell_symbols_on(&self, date: NaiveDate) -> BTreeSet<String> {
self.same_day_failed_full_close_sell_symbols
.borrow()
.get(&date)
.cloned()
.unwrap_or_default()
}
fn same_day_rebuy_rejection_reason( fn same_day_rebuy_rejection_reason(
&self, &self,
date: NaiveDate, date: NaiveDate,
symbol: &str, symbol: &str,
) -> Option<&'static str> { ) -> Option<&'static str> {
if self if !self
.risk_config .risk_config
.static_rules .static_rules
.forbid_same_day_rebuy_after_sell .forbid_same_day_rebuy_after_sell
&& self
.same_day_sold_symbols
.borrow()
.get(&date)
.is_some_and(|symbols| symbols.contains(symbol))
{ {
return None;
}
let sold_today = self
.same_day_sold_symbols
.borrow()
.get(&date)
.is_some_and(|symbols| symbols.contains(symbol));
let failed_full_close_today = self
.same_day_failed_full_close_sell_symbols
.borrow()
.get(&date)
.is_some_and(|symbols| symbols.contains(symbol));
if sold_today || failed_full_close_today {
return Some("same_day_rebuy_forbidden"); return Some("same_day_rebuy_forbidden");
} }
None None
@@ -2279,6 +2306,113 @@ where
commission_state: &mut BTreeMap<u64, f64>, commission_state: &mut BTreeMap<u64, f64>,
report: &mut BrokerExecutionReport, report: &mut BrokerExecutionReport,
) -> Result<(), BacktestError> { ) -> Result<(), BacktestError> {
let failed_full_close_symbols = if self.aiquant_execution_rules {
self.failed_full_close_sell_symbols_on(date)
} else {
BTreeSet::new()
};
if !failed_full_close_symbols.is_empty()
&& failed_full_close_symbols.iter().any(|symbol| {
target_weights.contains_key(symbol) || portfolio.position(symbol).is_some()
})
{
if report.diagnostics.len() < 32 {
report.diagnostics.push(format!(
"target_portfolio_smart_fallback_after_failed_full_close symbols={}",
failed_full_close_symbols
.iter()
.take(8)
.cloned()
.collect::<Vec<_>>()
.join(",")
));
}
let limit_prices = match order_prices {
Some(TargetPortfolioOrderPricing::LimitPrices(prices)) => Some(prices),
_ => None,
};
let held_symbols = portfolio
.positions()
.keys()
.cloned()
.collect::<BTreeSet<_>>();
for symbol in held_symbols.iter() {
if target_weights.contains_key(symbol) || failed_full_close_symbols.contains(symbol)
{
continue;
}
let mut local_report = BrokerExecutionReport::default();
if let Some(limit_price) = limit_prices.and_then(|prices| prices.get(symbol)) {
self.process_limit_target_percent(
date,
portfolio,
data,
symbol,
0.0,
*limit_price,
reason,
intraday_turnover,
execution_cursors,
global_execution_cursor,
commission_state,
&mut local_report,
)?;
} else {
self.process_target_percent(
date,
portfolio,
data,
symbol,
0.0,
reason,
intraday_turnover,
execution_cursors,
global_execution_cursor,
commission_state,
&mut local_report,
)?;
}
Self::extend_report(report, local_report);
}
for (symbol, weight) in target_weights {
if weight.abs() <= f64::EPSILON {
continue;
}
let mut local_report = BrokerExecutionReport::default();
if let Some(limit_price) = limit_prices.and_then(|prices| prices.get(symbol)) {
self.process_limit_target_percent(
date,
portfolio,
data,
symbol,
*weight,
*limit_price,
reason,
intraday_turnover,
execution_cursors,
global_execution_cursor,
commission_state,
&mut local_report,
)?;
} else {
self.process_target_percent(
date,
portfolio,
data,
symbol,
*weight,
reason,
intraday_turnover,
execution_cursors,
global_execution_cursor,
commission_state,
&mut local_report,
)?;
}
Self::extend_report(report, local_report);
}
return Ok(());
}
let (target_quantities, diagnostics) = self.target_quantities_with_valuation_prices( let (target_quantities, diagnostics) = self.target_quantities_with_valuation_prices(
date, date,
portfolio, portfolio,
@@ -2946,6 +3080,7 @@ where
let Some(position) = portfolio.position(symbol) else { let Some(position) = portfolio.position(symbol) else {
return Ok(()); return Ok(());
}; };
let full_close_requested = requested_qty >= position.quantity;
let Some(snapshot) = data.market(date, symbol) else { let Some(snapshot) = data.market(date, symbol) else {
let unavailable_reason = self let unavailable_reason = self
.missing_market_execution_risk_rejection_reason(date, data, symbol, OrderSide::Sell) .missing_market_execution_risk_rejection_reason(date, data, symbol, OrderSide::Sell)
@@ -3016,6 +3151,9 @@ where
| Some("open at or below lower limit") => OrderStatus::Canceled, | Some("open at or below lower limit") => OrderStatus::Canceled,
_ => OrderStatus::Rejected, _ => OrderStatus::Rejected,
}; };
if full_close_requested {
self.mark_failed_full_close_sell(date, symbol);
}
report.order_events.push(OrderEvent { report.order_events.push(OrderEvent {
date, date,
decision_date: None, decision_date: None,
@@ -3133,6 +3271,9 @@ where
status: zero_fill_status_for_reason(&limit_reason), status: zero_fill_status_for_reason(&limit_reason),
reason: format!("{reason}: {limit_reason}"), reason: format!("{reason}: {limit_reason}"),
}); });
if full_close_requested {
self.mark_failed_full_close_sell(date, symbol);
}
Self::emit_order_process_event( Self::emit_order_process_event(
report, report,
date, date,
@@ -3202,6 +3343,9 @@ where
status: OrderStatus::Rejected, status: OrderStatus::Rejected,
reason: format!("{reason}: no sellable quantity"), reason: format!("{reason}: no sellable quantity"),
}); });
if full_close_requested {
self.mark_failed_full_close_sell(date, symbol);
}
Self::emit_order_process_event( Self::emit_order_process_event(
report, report,
date, date,
@@ -3336,6 +3480,9 @@ where
status: zero_fill_status_for_reason(detail), status: zero_fill_status_for_reason(detail),
reason: format!("{reason}: {detail}"), reason: format!("{reason}: {detail}"),
}); });
if full_close_requested {
self.mark_failed_full_close_sell(date, symbol);
}
Self::emit_order_process_event( Self::emit_order_process_event(
report, report,
date, date,
@@ -7358,6 +7505,156 @@ mod tests {
assert!(rejected.reason.contains("buy_disabled"), "{rejected:?}"); assert!(rejected.reason.contains("buy_disabled"), "{rejected:?}");
} }
#[test]
fn aiquant_target_portfolio_smart_falls_back_after_failed_full_close_sell() {
let date = chrono::NaiveDate::from_ymd_opt(2025, 1, 2).expect("valid date");
let prev_date = chrono::NaiveDate::from_ymd_opt(2025, 1, 1).expect("valid date");
let symbols = ["000001.SZ", "000002.SZ"];
let instruments = symbols
.iter()
.map(|symbol| Instrument {
symbol: (*symbol).to_string(),
name: (*symbol).to_string(),
board: "SZ".to_string(),
round_lot: 100,
listed_at: None,
delisted_at: None,
status: "active".to_string(),
})
.collect::<Vec<_>>();
let snapshots = symbols
.iter()
.map(|symbol| {
let mut snapshot = limit_test_snapshot();
snapshot.symbol = (*symbol).to_string();
if *symbol == "000001.SZ" {
snapshot.day_open = 9.0;
snapshot.open = 9.0;
snapshot.low = 9.0;
snapshot.close = 9.0;
snapshot.last_price = 9.0;
snapshot.bid1 = 9.0;
snapshot.ask1 = 9.0;
snapshot.lower_limit = 9.0;
}
snapshot
})
.collect::<Vec<_>>();
let candidates = symbols
.iter()
.map(|symbol| {
let mut candidate = limit_test_candidate(true, true);
candidate.symbol = (*symbol).to_string();
candidate
})
.collect::<Vec<_>>();
let data = DataSet::from_components_with_actions_and_quotes(
instruments,
snapshots,
Vec::new(),
candidates,
vec![limit_test_benchmark()],
Vec::new(),
Vec::new(),
)
.expect("valid dataset");
let broker = BrokerSimulator::new_with_execution_price(
ChinaAShareCostModel::default(),
ChinaEquityRuleHooks,
PriceField::Open,
)
.with_aiquant_execution_rules(true)
.with_volume_limit(false)
.with_liquidity_limit(false)
.with_inactive_limit(false);
let mut portfolio = PortfolioState::new(100_000.0);
portfolio
.position_mut("000001.SZ")
.buy(prev_date, 1_000, 10.0);
portfolio
.position_mut("000002.SZ")
.buy(prev_date, 1_000, 10.0);
let mut report = BrokerExecutionReport::default();
let mut intraday_turnover = BTreeMap::new();
let mut execution_cursors = BTreeMap::new();
let mut global_execution_cursor = None;
let mut commission_state = BTreeMap::new();
broker
.process_target_value(
date,
&mut portfolio,
&data,
"000001.SZ",
0.0,
"stop_loss_exit",
&mut intraday_turnover,
&mut execution_cursors,
&mut global_execution_cursor,
&mut commission_state,
&mut report,
)
.expect("failed lower-limit full close should be recorded");
assert_eq!(
portfolio
.position("000001.SZ")
.map(|position| position.quantity),
Some(1_000)
);
assert!(report.order_events.iter().any(|event| {
event.symbol == "000001.SZ"
&& event.side == OrderSide::Sell
&& event.status == OrderStatus::Canceled
&& event.filled_quantity == 0
}));
let mut target_weights = BTreeMap::new();
target_weights.insert("000001.SZ".to_string(), 0.50);
target_weights.insert("000002.SZ".to_string(), 0.50);
broker
.process_target_portfolio_smart(
date,
&mut portfolio,
&data,
&target_weights,
None,
None,
"signal_target_weights",
&mut intraday_turnover,
&mut execution_cursors,
&mut global_execution_cursor,
&mut commission_state,
&mut report,
)
.expect("failed full close should trigger per-symbol fallback");
assert_eq!(
portfolio
.position("000001.SZ")
.map(|position| position.quantity),
Some(1_000),
"{:?}",
report.fill_events
);
assert!(
portfolio
.position("000002.SZ")
.is_some_and(|position| position.quantity > 1_000),
"{:?}",
report.fill_events
);
assert!(report.order_events.iter().any(|event| {
event.symbol == "000001.SZ"
&& event.side == OrderSide::Buy
&& event.status == OrderStatus::Rejected
&& event.reason.contains("same_day_rebuy_forbidden")
}));
assert!(report.diagnostics.iter().any(|line| {
line.contains("target_portfolio_smart_fallback_after_failed_full_close")
&& line.contains("000001.SZ")
}));
}
#[test] #[test]
fn target_portfolio_smart_scales_buys_when_full_targets_exceed_cash_by_fees() { fn target_portfolio_smart_scales_buys_when_full_targets_exceed_cash_by_fees() {
let date = chrono::NaiveDate::from_ymd_opt(2025, 1, 2).expect("valid date"); let date = chrono::NaiveDate::from_ymd_opt(2025, 1, 2).expect("valid date");