修正FIDC止损未成交后补仓语义

This commit is contained in:
boris
2026-07-08 00:43:25 +08:00
parent e74e2226d5
commit 749b5e3b9c
+344 -5
View File
@@ -8561,9 +8561,14 @@ impl Strategy for PlatformExprStrategy {
let mut attempted_any = false;
let mut filled_any = false;
for symbol in &stock_list {
if projected.positions().contains_key(symbol)
let released_exit_position = projected.positions().contains_key(symbol)
&& !working_symbols.contains(symbol)
&& !same_day_sold_symbols.contains(symbol)
&& !pending_full_close_symbols.contains(symbol);
if (projected.positions().contains_key(symbol) && !released_exit_position)
|| same_day_sold_symbols.contains(symbol)
|| exit_symbols.contains(symbol)
|| (exit_symbols.contains(symbol) && !released_exit_position)
|| pending_full_close_symbols.contains(symbol)
|| delayed_sold_symbols.contains(symbol)
|| intraday_attempted_buys.contains(symbol)
{
@@ -8714,7 +8719,11 @@ impl Strategy for PlatformExprStrategy {
)?;
let stock_scale = self.buy_scale(ctx, &day, &decision_stock)?;
let target_cash = fixed_buy_cash * stock_scale;
if projected.positions().contains_key(symbol) {
let released_exit_position = projected.positions().contains_key(symbol)
&& !rebalance_working_symbols.contains(symbol)
&& !same_day_sold_symbols.contains(symbol)
&& !pending_full_close_symbols.contains(symbol);
if projected.positions().contains_key(symbol) && !released_exit_position {
if self.config.aiquant_transaction_cost {
continue;
}
@@ -8776,9 +8785,11 @@ impl Strategy for PlatformExprStrategy {
if rebalance_working_symbols.len() >= selection_limit {
break;
}
if pre_rebalance_symbols.contains(symbol)
|| projected.positions().contains_key(symbol)
if ((pre_rebalance_symbols.contains(symbol)
|| projected.positions().contains_key(symbol))
&& !released_exit_position)
|| same_day_sold_symbols.contains(symbol)
|| pending_full_close_symbols.contains(symbol)
{
continue;
}
@@ -21872,6 +21883,160 @@ mod tests {
);
}
#[test]
fn platform_daily_top_up_can_rebuy_released_unsellable_stop_loss_symbol() {
let prev_date = d(2026, 3, 31);
let date = d(2026, 4, 1);
let symbols = ["000001.SZ", "000002.SZ", "000003.SZ"];
let data = DataSet::from_components(
symbols
.iter()
.map(|symbol| 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(),
})
.collect(),
symbols
.iter()
.map(|symbol| DailyMarketSnapshot {
date,
symbol: (*symbol).to_string(),
timestamp: Some("2026-04-01 09:33:00".to_string()),
day_open: 10.0,
open: 10.0,
high: 10.5,
low: 9.8,
close: 10.0,
last_price: 10.0,
bid1: 10.0,
ask1: 10.0,
prev_close: 10.0,
volume: 1_000_000,
minute_volume: 10_000,
bid1_volume: 2_000,
ask1_volume: 2_000,
trading_phase: Some("continuous".to_string()),
paused: false,
upper_limit: 11.0,
lower_limit: 9.0,
price_tick: 0.01,
})
.collect(),
symbols
.iter()
.map(|symbol| DailyFactorSnapshot {
date,
symbol: (*symbol).to_string(),
market_cap_bn: match *symbol {
"000001.SZ" => 8.0,
"000003.SZ" => 9.0,
_ => 20.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(),
})
.collect(),
symbols
.iter()
.map(|symbol| CandidateEligibility {
date,
symbol: (*symbol).to_string(),
is_st: false,
is_star_st: false,
is_new_listing: false,
is_paused: false,
allow_buy: true,
allow_sell: *symbol != "000001.SZ",
is_kcb: false,
is_one_yuan: false,
risk_level_code: None,
})
.collect(),
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(20_000.0);
portfolio
.position_mut("000001.SZ")
.buy(prev_date, 100, 12.0);
portfolio
.position_mut("000002.SZ")
.buy(prev_date, 100, 10.0);
let subscriptions = BTreeSet::new();
let ctx = StrategyContext {
execution_date: date,
decision_date: 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.signal_symbol = "000001.SZ".to_string();
cfg.refresh_rate = 99;
cfg.max_positions = 2;
cfg.benchmark_short_ma_days = 1;
cfg.benchmark_long_ma_days = 1;
cfg.market_cap_lower_expr = "0".to_string();
cfg.market_cap_upper_expr = "100".to_string();
cfg.selection_limit_expr = "2".to_string();
cfg.stock_filter_expr = "close > 0".to_string();
cfg.stop_loss_expr = "0.9".to_string();
cfg.daily_top_up_enabled = true;
let mut strategy = PlatformExprStrategy::new(cfg);
strategy.rebalance_day_counter = 2;
let decision = strategy.on_day(&ctx).expect("platform decision");
assert!(
decision.order_intents.iter().any(|intent| matches!(
intent,
OrderIntent::TargetValue {
symbol,
target_value,
reason,
} if symbol == "000001.SZ"
&& *target_value == 0.0
&& reason == "stop_loss_exit"
)),
"{:?}",
decision.order_intents
);
assert!(
decision.order_intents.iter().any(|intent| matches!(
intent,
OrderIntent::Value { symbol, reason, .. }
if symbol == "000001.SZ" && reason == "daily_top_up_buy"
)),
"{:?}",
decision.order_intents
);
}
#[test]
fn platform_daily_top_up_release_slot_flag_matches_aiquant_exit_signal_state() {
let prev_date = d(2026, 3, 31);
@@ -22311,6 +22476,180 @@ mod tests {
);
}
#[test]
fn platform_periodic_rebalance_can_rebuy_released_unsellable_stop_loss_symbol() {
let prev_date = d(2025, 5, 13);
let date = d(2025, 5, 14);
let symbols = ["000001.SZ", "000002.SZ", "000003.SZ"];
let data = DataSet::from_components_with_actions_and_quotes(
symbols
.iter()
.map(|symbol| 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(),
})
.collect(),
symbols
.iter()
.map(|symbol| DailyMarketSnapshot {
date,
symbol: (*symbol).to_string(),
timestamp: Some("2025-05-14 10:18:00".to_string()),
day_open: 10.0,
open: 10.0,
high: 10.5,
low: 9.8,
close: 10.0,
last_price: 10.0,
bid1: 10.0,
ask1: 10.0,
prev_close: 9.9,
volume: 1_000_000,
minute_volume: 10_000,
bid1_volume: 2_000,
ask1_volume: 2_000,
trading_phase: Some("continuous".to_string()),
paused: false,
upper_limit: 11.0,
lower_limit: 9.0,
price_tick: 0.01,
})
.collect(),
symbols
.iter()
.map(|symbol| DailyFactorSnapshot {
date,
symbol: (*symbol).to_string(),
market_cap_bn: match *symbol {
"000001.SZ" => 8.0,
"000003.SZ" => 9.0,
_ => 20.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(),
})
.collect(),
symbols
.iter()
.map(|symbol| CandidateEligibility {
date,
symbol: (*symbol).to_string(),
is_st: false,
is_star_st: false,
is_new_listing: false,
is_paused: false,
allow_buy: true,
allow_sell: *symbol != "000001.SZ",
is_kcb: false,
is_one_yuan: false,
risk_level_code: None,
})
.collect(),
vec![BenchmarkSnapshot {
date,
benchmark: "000852.SH".to_string(),
open: 1000.0,
close: 1002.0,
prev_close: 998.0,
volume: 1_000_000,
}],
Vec::new(),
symbols
.iter()
.map(|symbol| IntradayExecutionQuote {
date,
symbol: (*symbol).to_string(),
timestamp: date.and_hms_opt(10, 18, 0).expect("valid timestamp"),
last_price: 10.0,
bid1: 10.0,
ask1: 10.0,
bid1_volume: 10_000,
ask1_volume: 10_000,
volume_delta: 10_000,
amount_delta: 100_000.0,
trading_phase: Some("continuous".to_string()),
})
.collect(),
)
.expect("dataset");
let mut portfolio = PortfolioState::new(20_000.0);
portfolio
.position_mut("000001.SZ")
.buy(prev_date, 100, 12.0);
portfolio
.position_mut("000002.SZ")
.buy(prev_date, 100, 10.0);
let subscriptions = BTreeSet::new();
let ctx = StrategyContext {
execution_date: date,
decision_date: date,
decision_index: 20,
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.signal_symbol = "000001.SZ".to_string();
cfg.refresh_rate = 20;
cfg.max_positions = 2;
cfg.benchmark_short_ma_days = 1;
cfg.benchmark_long_ma_days = 1;
cfg.market_cap_lower_expr = "0".to_string();
cfg.market_cap_upper_expr = "100".to_string();
cfg.selection_limit_expr = "2".to_string();
cfg.stock_filter_expr = "close > 0".to_string();
cfg.stop_loss_expr = "0.9".to_string();
cfg.take_profit_expr.clear();
cfg.daily_top_up_enabled = true;
cfg.aiquant_transaction_cost = true;
cfg.intraday_execution_time = Some(NaiveTime::from_hms_opt(10, 18, 0).unwrap());
let mut strategy = PlatformExprStrategy::new(cfg);
strategy.rebalance_day_counter = 20;
let decision = strategy.on_day(&ctx).expect("platform decision");
assert!(
decision.order_intents.iter().any(|intent| matches!(
intent,
OrderIntent::TargetValue {
symbol,
target_value,
reason,
} if symbol == "000001.SZ"
&& *target_value == 0.0
&& reason == "stop_loss_exit"
)),
"{:?}",
decision.order_intents
);
assert!(
decision.order_intents.iter().any(|intent| matches!(
intent,
OrderIntent::Value { symbol, reason, .. }
if symbol == "000001.SZ" && reason == "periodic_rebalance_buy"
)),
"{:?}",
decision.order_intents
);
}
#[test]
fn platform_aiquant_periodic_rebalance_uses_cash_divided_by_top_n_buy_budget() {
let prev_date = d(2025, 5, 13);