diff --git a/crates/fidc-core/src/platform_expr_strategy.rs b/crates/fidc-core/src/platform_expr_strategy.rs index 2d80519..298f66d 100644 --- a/crates/fidc-core/src/platform_expr_strategy.rs +++ b/crates/fidc-core/src/platform_expr_strategy.rs @@ -6237,6 +6237,7 @@ impl PlatformExprStrategy { date: NaiveDate, symbol: &str, execution_time: NaiveTime, + defer_execution_risk: bool, ) -> Result { let Ok(candidate) = ctx.data.require_candidate(date, symbol) else { return Ok(false); @@ -6249,6 +6250,9 @@ impl PlatformExprStrategy { else { return Ok(false); }; + if defer_execution_risk { + return Ok(true); + } if !candidate.allow_sell || candidate.is_paused { return Ok(false); } @@ -7714,6 +7718,7 @@ impl Strategy for PlatformExprStrategy { projection_date, &position.symbol, risk_level_forced_exit_time, + defer_execution_risk, )? { pending_full_close_symbols.insert(position.symbol.clone()); pending_risk_level_forced_exit_symbols.insert(position.symbol.clone()); @@ -12583,6 +12588,135 @@ mod tests { ))); } + #[test] + fn platform_next_open_risk_level_exit_ignores_decision_day_sellability() { + let decision_date = d(2023, 5, 5); + let execution_date = d(2023, 5, 8); + let symbol = "300621.SZ"; + let market = |date: NaiveDate, paused: bool| DailyMarketSnapshot { + date, + symbol: symbol.to_string(), + timestamp: Some(format!("{date} 10:18:00")), + day_open: 10.0, + open: 10.0, + high: 10.8, + low: 9.8, + close: 10.5, + last_price: 10.5, + bid1: 10.49, + ask1: 10.51, + prev_close: 10.4, + volume: if paused { 0 } else { 200_000 }, + minute_volume: if paused { 0 } else { 1_000 }, + bid1_volume: if paused { 0 } else { 2_000 }, + ask1_volume: if paused { 0 } else { 2_000 }, + trading_phase: Some(if paused { "paused" } else { "continuous" }.to_string()), + paused, + upper_limit: 11.44, + lower_limit: 9.36, + price_tick: 0.01, + }; + let candidate = |date: NaiveDate, paused: bool| CandidateEligibility { + date, + symbol: symbol.to_string(), + is_st: false, + is_star_st: false, + is_new_listing: false, + is_paused: paused, + allow_buy: !paused, + allow_sell: !paused, + is_kcb: false, + is_one_yuan: false, + risk_level_code: Some("A".to_string()), + }; + let factor = |date: NaiveDate| 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(), + }; + let benchmark = |date: NaiveDate| BenchmarkSnapshot { + date, + benchmark: "000852.SH".to_string(), + open: 1000.0, + close: 1000.0, + prev_close: 999.0, + volume: 1_000_000, + }; + 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![market(decision_date, true), market(execution_date, false)], + vec![factor(decision_date), factor(execution_date)], + vec![ + candidate(decision_date, true), + candidate(execution_date, false), + ], + vec![benchmark(decision_date), benchmark(execution_date)], + ) + .expect("dataset"); + let mut portfolio = PortfolioState::new(1_000_000.0); + portfolio + .position_mut(symbol) + .buy(d(2023, 5, 4), 30_000, 10.0); + portfolio.apply_cash_delta(-300_000.0); + let subscriptions = BTreeSet::new(); + let ctx = StrategyContext { + execution_date, + decision_date, + decision_index: 2, + data: &data, + portfolio: &portfolio, + futures_account: None, + open_orders: &[], + dynamic_universe: None, + subscriptions: &subscriptions, + process_events: &[], + active_process_event: None, + active_datetime: None, + order_events: &[], + fills: &[], + }; + let mut cfg = PlatformExprStrategyConfig::microcap_rotation(); + cfg.aiquant_transaction_cost = true; + cfg.rotation_enabled = false; + cfg.daily_top_up_enabled = false; + cfg.signal_symbol = symbol.to_string(); + cfg.stop_loss_expr.clear(); + cfg.take_profit_expr.clear(); + cfg.intraday_execution_time = Some(NaiveTime::from_hms_opt(10, 18, 0).expect("time")); + let mut strategy = PlatformExprStrategy::new(cfg); + + let decision = strategy.on_day(&ctx).expect("platform decision"); + + assert!(decision.order_intents.iter().any(|intent| matches!( + intent, + OrderIntent::TimedTargetValue { + symbol: intent_symbol, + target_value, + start_time, + end_time, + reason, + .. + } if intent_symbol == symbol + && *target_value == 0.0 + && *start_time == Some(NaiveTime::from_hms_opt(10, 18, 0).expect("time")) + && *end_time == Some(NaiveTime::from_hms_opt(10, 18, 0).expect("time")) + && reason == "risk_forced_exit" + ))); + } + #[test] fn platform_aiquant_weak_market_threshold_only_sells_overweight_positions() { let prev_date = d(2023, 5, 4);