修正止损前弱市补仓顺序

This commit is contained in:
boris
2026-06-16 00:29:01 +08:00
parent ff145300b4
commit 2e036783bf
+175 -31
View File
@@ -6413,37 +6413,6 @@ impl Strategy for PlatformExprStrategy {
.cloned()
.collect::<BTreeSet<_>>();
if self.config.aiquant_transaction_cost
&& self.config.rotation_enabled
&& trading_ratio > 0.0
&& trading_ratio < 1.0
&& selection_limit > 0
&& !ctx.portfolio.positions().is_empty()
{
let target_value = aiquant_total_value * trading_ratio / selection_limit as f64;
if target_value.is_finite() && target_value > 0.0 {
for position in ctx.portfolio.positions().values() {
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(),
});
self.project_target_value(
ctx,
&mut projected,
execution_date,
&position.symbol,
target_value,
&mut projected_execution_state,
);
}
aiquant_available_cash = projected.cash();
}
}
for position in ctx.portfolio.positions().values() {
if delayed_sold_symbols.contains(&position.symbol) {
continue;
@@ -6544,6 +6513,43 @@ impl Strategy for PlatformExprStrategy {
}
}
if self.config.aiquant_transaction_cost
&& self.config.rotation_enabled
&& trading_ratio > 0.0
&& trading_ratio < 1.0
&& selection_limit > 0
&& !ctx.portfolio.positions().is_empty()
{
let target_value = aiquant_total_value * trading_ratio / selection_limit as f64;
if target_value.is_finite() && target_value > 0.0 {
for position in ctx.portfolio.positions().values() {
if position.quantity == 0
|| delayed_sold_symbols.contains(&position.symbol)
|| exit_symbols.contains(&position.symbol)
|| same_day_sold_symbols.contains(&position.symbol)
|| unresolved_stop_loss_symbols.contains(&position.symbol)
|| self.pending_highlimit_holdings.contains(&position.symbol)
{
continue;
}
order_intents.push(OrderIntent::TargetValue {
symbol: position.symbol.clone(),
target_value,
reason: "daily_position_target_adjust".to_string(),
});
self.project_target_value(
ctx,
&mut projected,
execution_date,
&position.symbol,
target_value,
&mut projected_execution_state,
);
}
aiquant_available_cash = projected.cash();
}
}
if self.config.daily_top_up_enabled
&& self.config.rotation_enabled
&& !periodic_rebalance
@@ -11357,6 +11363,144 @@ mod tests {
)));
}
#[test]
fn platform_stop_loss_skips_weak_market_top_up_for_exiting_position() {
let prev_date = d(2025, 2, 2);
let date = d(2025, 2, 3);
let symbols = ["000001.SZ", "000002.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("2025-02-03 10:40:00".to_string()),
day_open: 10.0,
open: 10.0,
high: 10.2,
low: 9.8,
close: 10.0,
last_price: 10.0,
bid1: 9.99,
ask1: 10.01,
prev_close: 10.0,
volume: 1_000_000,
tick_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()
.enumerate()
.map(|(index, symbol)| DailyFactorSnapshot {
date,
symbol: (*symbol).to_string(),
market_cap_bn: 10.0 + index as f64,
free_float_cap_bn: 10.0 + index as f64,
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_new_listing: false,
is_paused: false,
allow_buy: true,
allow_sell: true,
is_kcb: false,
is_one_yuan: false,
})
.collect(),
vec![BenchmarkSnapshot {
date,
benchmark: "000852.SH".to_string(),
open: 1000.0,
close: 1000.0,
prev_close: 1000.0,
volume: 1_000_000,
}],
)
.expect("dataset");
let mut portfolio = PortfolioState::new(30_000.0);
portfolio
.position_mut("000001.SZ")
.buy(prev_date, 100, 20.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 = "1000".to_string();
cfg.selection_limit_expr = "2".to_string();
cfg.stock_filter_expr = "close > 0".to_string();
cfg.exposure_expr = "0.5".to_string();
cfg.stop_loss_expr = "0.92".to_string();
cfg.take_profit_expr.clear();
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"
)));
assert!(!decision.order_intents.iter().any(|intent| matches!(
intent,
OrderIntent::TargetValue { symbol, reason, .. }
if symbol == "000001.SZ" && reason == "daily_position_target_adjust"
)));
}
#[test]
fn platform_refresh_rate_uses_stateful_aiquant_day_counter() {
let dates = [d(2025, 2, 5), d(2025, 2, 6), d(2025, 2, 7)];