From e45f990487e59971563af1b68ab7eacad8298057 Mon Sep 17 00:00:00 2001 From: boris Date: Tue, 16 Jun 2026 08:06:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E5=B9=B3=E5=8F=B0=E7=9B=AE?= =?UTF-8?q?=E6=A0=87=E8=B0=83=E4=BB=93=E6=89=A7=E8=A1=8C=E5=8F=A3=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/fidc-core/src/broker.rs | 14 +- .../fidc-core/src/platform_expr_strategy.rs | 252 ++++++++++++++++-- 2 files changed, 231 insertions(+), 35 deletions(-) diff --git a/crates/fidc-core/src/broker.rs b/crates/fidc-core/src/broker.rs index a4300df..c29257f 100644 --- a/crates/fidc-core/src/broker.rs +++ b/crates/fidc-core/src/broker.rs @@ -2849,10 +2849,8 @@ where } let current_value = if self.aiquant_rqalpha_execution_rules { - portfolio - .position(symbol) - .map(|position| position.market_value()) - .unwrap_or(0.0) + let valuation_price = self.target_value_valuation_price(date, data, symbol, snapshot); + valuation_price * current_qty as f64 } else { let valuation_price = self.target_value_valuation_price(date, data, symbol, snapshot); valuation_price * current_qty as f64 @@ -5285,7 +5283,7 @@ mod tests { } #[test] - fn aiquant_target_value_delta_uses_position_market_value() { + fn aiquant_target_value_delta_uses_scheduled_mark_price() { let date = chrono::NaiveDate::from_ymd_opt(2023, 5, 8).expect("valid date"); let symbol = "603101.SH"; let broker = BrokerSimulator::new_with_execution_price( @@ -5397,11 +5395,11 @@ mod tests { assert_eq!( portfolio.position(symbol).map(|pos| pos.quantity), - Some(21_200) + Some(21_400) ); if let Some(order) = report.order_events.last() { - assert_eq!(order.requested_quantity, 0); - assert_eq!(order.filled_quantity, 0); + assert_eq!(order.requested_quantity, 200); + assert_eq!(order.filled_quantity, 200); } } diff --git a/crates/fidc-core/src/platform_expr_strategy.rs b/crates/fidc-core/src/platform_expr_strategy.rs index a208da3..c18b43c 100644 --- a/crates/fidc-core/src/platform_expr_strategy.rs +++ b/crates/fidc-core/src/platform_expr_strategy.rs @@ -1580,7 +1580,7 @@ impl PlatformExprStrategy { } let market = ctx.data.market(date, symbol)?; let current_value = if self.config.aiquant_transaction_cost { - projected.position(symbol)?.market_value() + self.projected_position_value_at_execution_price(ctx, projected, date, symbol) } else { let valuation_price = if market.close.is_finite() && market.close > 0.0 { market.close @@ -6453,6 +6453,40 @@ impl Strategy for PlatformExprStrategy { .filter(|symbol| !delayed_sold_symbols.contains(*symbol)) .cloned() .collect::>(); + let mut pending_full_close_symbols = BTreeSet::::new(); + if self.config.aiquant_transaction_cost { + for position in ctx.portfolio.positions().values() { + if position.quantity == 0 || delayed_sold_symbols.contains(&position.symbol) { + continue; + } + let (stop_hit, profit_hit) = + self.stop_take_action_for_position(ctx, execution_date, &day, position)?; + if stop_hit { + pending_full_close_symbols.insert(position.symbol.clone()); + continue; + } + if self.config.delayed_limit_open_exit_enabled { + let stock = match self.stock_state_at_time( + ctx, + execution_date, + &position.symbol, + Some(self.intraday_execution_start_time()), + ) { + Ok(stock) => stock, + Err(BacktestError::Data(crate::data::DataSetError::MissingSnapshot { + .. + })) => continue, + Err(error) => return Err(error), + }; + if stock.upper_limit > 0.0 && stock.last >= stock.upper_limit { + continue; + } + } + if profit_hit { + pending_full_close_symbols.insert(position.symbol.clone()); + } + } + } if self.config.aiquant_transaction_cost && self.config.rotation_enabled @@ -6467,11 +6501,21 @@ impl Strategy for PlatformExprStrategy { if position.quantity == 0 || delayed_sold_symbols.contains(&position.symbol) { continue; } - order_intents.push(OrderIntent::TargetValue { - symbol: position.symbol.clone(), - target_value, - reason: "daily_position_target_adjust".to_string(), - }); + let current_value = self.projected_position_value_at_execution_price( + ctx, + &projected, + execution_date, + &position.symbol, + ); + if pending_full_close_symbols.contains(&position.symbol) + && target_value > current_value + f64::EPSILON + { + continue; + } + let before_qty = projected + .position(&position.symbol) + .map(|projected_position| projected_position.quantity) + .unwrap_or(0); self.project_target_value( ctx, &mut projected, @@ -6480,6 +6524,18 @@ impl Strategy for PlatformExprStrategy { target_value, &mut projected_execution_state, ); + let after_qty = projected + .position(&position.symbol) + .map(|projected_position| projected_position.quantity) + .unwrap_or(0); + let quantity_delta = after_qty as i32 - before_qty as i32; + if quantity_delta != 0 { + order_intents.push(OrderIntent::Shares { + symbol: position.symbol.clone(), + quantity: quantity_delta, + reason: "daily_position_target_adjust".to_string(), + }); + } } aiquant_available_cash = projected.cash(); } @@ -7428,7 +7484,7 @@ mod tests { } #[test] - fn platform_aiquant_target_value_uses_position_market_value_for_delta() { + fn platform_aiquant_target_value_uses_scheduled_mark_for_delta() { let prev_date = d(2023, 5, 11); let date = d(2023, 5, 12); let symbol = "603176.SH"; @@ -7523,7 +7579,7 @@ mod tests { ); assert!( target_value - portfolio.position(symbol).unwrap().market_value() < 100.0 * 6.5369, - "fixture must be below one lot when position.market_value is used" + "fixture must be below one lot if stale position.market_value is used" ); assert!( 18_600.0 * 6.5369 < target_value, @@ -7568,8 +7624,8 @@ mod tests { &mut execution_state, ); - assert_eq!(filled, None); - assert_eq!(projected.position(symbol).unwrap().quantity, 18_600); + assert_eq!(filled, Some(500)); + assert_eq!(projected.position(symbol).unwrap().quantity, 19_100); } #[test] @@ -9133,7 +9189,9 @@ mod tests { ) .expect("dataset"); let mut portfolio = PortfolioState::new(1_000_000.0); - portfolio.position_mut(symbol).buy(date, 11_800, 10.52); + portfolio + .position_mut(symbol) + .buy(d(2023, 5, 4), 1_000, 10.52); let subscriptions = BTreeSet::new(); let ctx = StrategyContext { execution_date: date, @@ -9163,6 +9221,145 @@ mod tests { let decision = strategy.on_day(&ctx).expect("platform decision"); + assert!(decision.order_intents.iter().any(|intent| matches!( + intent, + OrderIntent::Shares { + symbol: intent_symbol, + quantity, + reason + } if intent_symbol == symbol + && reason == "daily_position_target_adjust" + && *quantity > 0 + ))); + } + + #[test] + fn platform_aiquant_skips_positive_target_adjust_when_position_will_close() { + let prev_date = d(2023, 5, 11); + let date = d(2023, 5, 12); + let symbol = "603176.SH"; + let data = DataSet::from_components_with_actions_and_quotes( + vec![Instrument { + symbol: symbol.to_string(), + name: symbol.to_string(), + board: "SH".to_string(), + round_lot: 100, + listed_at: Some(d(2010, 1, 1)), + delisted_at: None, + status: "active".to_string(), + }], + vec![DailyMarketSnapshot { + date, + symbol: symbol.to_string(), + timestamp: Some("2023-05-12 10:40:00".to_string()), + day_open: 6.70, + open: 6.70, + high: 6.72, + low: 6.48, + close: 6.48, + last_price: 6.55, + bid1: 6.5369, + ask1: 6.5369, + prev_close: 6.72, + volume: 1_000_000, + tick_volume: 10_000, + bid1_volume: 10_000, + ask1_volume: 10_000, + trading_phase: Some("continuous".to_string()), + paused: false, + upper_limit: 7.39, + lower_limit: 6.05, + price_tick: 0.01, + }], + vec![DailyFactorSnapshot { + date, + symbol: symbol.to_string(), + market_cap_bn: 10.0, + free_float_cap_bn: 10.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: 1000.0, + prev_close: 999.0, + volume: 1_000_000, + }], + Vec::new(), + vec![IntradayExecutionQuote { + date, + symbol: symbol.to_string(), + timestamp: date.and_hms_opt(10, 40, 0).expect("timestamp"), + last_price: 6.55, + bid1: 6.5369, + ask1: 6.5369, + bid1_volume: 10_000, + ask1_volume: 10_000, + volume_delta: 10_000, + amount_delta: 65_500.0, + trading_phase: Some("continuous".to_string()), + }], + ) + .expect("dataset"); + let mut portfolio = PortfolioState::new(500_000.0); + portfolio + .position_mut(symbol) + .buy_with_mark_price(prev_date, 18_600, 7.22, 6.72); + let subscriptions = BTreeSet::new(); + let ctx = StrategyContext { + execution_date: date, + decision_date: date, + decision_index: 1, + 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.signal_symbol = symbol.to_string(); + cfg.exposure_expr = "0.5".to_string(); + cfg.selection_limit_expr = "40".to_string(); + cfg.stock_filter_expr = "false".to_string(); + cfg.stop_loss_expr = "0.92".to_string(); + cfg.take_profit_expr.clear(); + cfg.intraday_execution_time = Some(NaiveTime::from_hms_opt(10, 40, 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::Shares { + symbol: intent_symbol, + quantity, + reason + } if intent_symbol == symbol + && reason == "daily_position_target_adjust" + && *quantity > 0 + ))); assert!(decision.order_intents.iter().any(|intent| matches!( intent, OrderIntent::TargetValue { @@ -9170,9 +9367,8 @@ mod tests { target_value, reason } if intent_symbol == symbol - && reason == "daily_position_target_adjust" - && target_value.is_finite() - && *target_value > 0.0 + && reason == "stop_loss_exit" + && *target_value == 0.0 ))); } @@ -11837,7 +12033,7 @@ mod tests { } #[test] - fn platform_weak_market_adjusts_before_stop_loss_and_skips_top_up_for_exiting_position() { + fn platform_weak_market_skips_positive_adjust_for_stop_loss_position() { let prev_date = d(2025, 2, 2); let date = d(2025, 2, 3); let symbols = ["000001.SZ", "000002.SZ"]; @@ -11960,17 +12156,6 @@ mod tests { let decision = strategy.on_day(&ctx).expect("platform decision"); - let target_adjust_index = decision - .order_intents - .iter() - .position(|intent| { - matches!( - intent, - OrderIntent::TargetValue { symbol, reason, .. } - if symbol == "000001.SZ" && reason == "daily_position_target_adjust" - ) - }) - .expect("weak-market target adjustment should run before stop-loss"); let stop_loss_index = decision .order_intents .iter() @@ -11985,7 +12170,20 @@ mod tests { ) }) .expect("stop-loss exit"); - assert!(target_adjust_index < stop_loss_index); + assert!( + !decision.order_intents[..stop_loss_index] + .iter() + .any(|intent| matches!( + intent, + OrderIntent::Shares { + symbol, + quantity, + reason, + } if symbol == "000001.SZ" + && reason == "daily_position_target_adjust" + && *quantity > 0 + )) + ); assert!(!decision.order_intents.iter().any(|intent| matches!( intent, OrderIntent::Value { symbol, reason, .. } | OrderIntent::Shares { symbol, reason, .. }