修复策略表达式卖出投影槽位释放

This commit is contained in:
boris
2026-06-19 18:33:07 +08:00
parent c3a5161db1
commit 5ecb0e7986
@@ -1518,6 +1518,9 @@ impl PlatformExprStrategy {
execution_time, execution_time,
) )
.or_else(|| { .or_else(|| {
if self.config.aiquant_transaction_cost {
return None;
}
if !self.has_execution_quote_at_or_before_at_time( if !self.has_execution_quote_at_or_before_at_time(
ctx, ctx,
date, date,
@@ -16497,6 +16500,113 @@ mod tests {
assert_eq!(projected.position(symbol).expect("position").quantity, 500); assert_eq!(projected.position(symbol).expect("position").quantity, 500);
} }
#[test]
fn platform_aiquant_projection_does_not_release_sell_slot_without_execution_quote() {
let prev_date = d(2024, 4, 12);
let date = d(2024, 4, 15);
let symbol = "002633.SZ";
let data = DataSet::from_components(
vec![Instrument {
symbol: symbol.to_string(),
name: symbol.to_string(),
board: "SZ".to_string(),
round_lot: 100,
listed_at: Some(d(2020, 1, 1)),
delisted_at: None,
status: "active".to_string(),
}],
vec![DailyMarketSnapshot {
date,
symbol: symbol.to_string(),
timestamp: Some("2024-04-15 10:18:00".to_string()),
day_open: 6.94,
open: 6.94,
high: 6.94,
low: 6.94,
close: 6.94,
last_price: 6.94,
bid1: 0.0,
ask1: 0.0,
prev_close: 7.63,
volume: 1_000_000,
tick_volume: 0,
bid1_volume: 0,
ask1_volume: 0,
trading_phase: Some("continuous".to_string()),
paused: false,
upper_limit: 8.39,
lower_limit: 6.87,
price_tick: 0.01,
}],
vec![DailyFactorSnapshot {
date,
symbol: symbol.to_string(),
market_cap_bn: 20.0,
free_float_cap_bn: 20.0,
pe_ttm: 8.0,
turnover_ratio: Some(1.0),
effective_turnover_ratio: Some(1.0),
extra_factors: BTreeMap::new(),
}],
vec![CandidateEligibility {
date,
symbol: symbol.to_string(),
is_st: false,
is_new_listing: false,
is_paused: false,
allow_buy: true,
allow_sell: true,
is_kcb: false,
is_one_yuan: false,
}],
vec![BenchmarkSnapshot {
date,
benchmark: "000852.SH".to_string(),
open: 1000.0,
close: 1002.0,
prev_close: 998.0,
volume: 1_000_000,
}],
)
.expect("dataset");
let mut portfolio = PortfolioState::new(100.0);
portfolio.position_mut(symbol).buy(prev_date, 7_400, 8.48);
let subscriptions = BTreeSet::new();
let ctx = StrategyContext {
execution_date: date,
decision_date: date,
decision_index: 40,
data: &data,
portfolio: &portfolio,
futures_account: None,
open_orders: &[],
dynamic_universe: None,
subscriptions: &subscriptions,
process_events: &[],
active_process_event: None,
active_datetime: Some(date.and_hms_opt(10, 18, 0).unwrap()),
order_events: &[],
fills: &[],
};
let mut cfg = PlatformExprStrategyConfig::microcap_rotation();
cfg.aiquant_transaction_cost = true;
cfg.intraday_execution_time = Some(NaiveTime::from_hms_opt(10, 18, 0).unwrap());
let strategy = PlatformExprStrategy::new(cfg);
let mut projected = portfolio.clone();
let mut execution_state = super::ProjectedExecutionState::default();
let filled =
strategy.project_target_zero(&ctx, &mut projected, date, symbol, &mut execution_state);
assert_eq!(filled, None);
assert_eq!(projected.cash(), portfolio.cash());
assert_eq!(
projected.position(symbol).expect("position").quantity,
7_400
);
}
#[test] #[test]
fn platform_selection_ranks_with_previous_factor_date() { fn platform_selection_ranks_with_previous_factor_date() {
let prev = d(2025, 1, 2); let prev = d(2025, 1, 2);