补齐AiQuant风控清仓优先级
This commit is contained in:
@@ -2198,6 +2198,17 @@ impl PlatformExprStrategy {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn candidate_requires_forced_exit(
|
||||||
|
&self,
|
||||||
|
ctx: &StrategyContext<'_>,
|
||||||
|
date: NaiveDate,
|
||||||
|
symbol: &str,
|
||||||
|
) -> bool {
|
||||||
|
ctx.data.candidate(date, symbol).is_some_and(|candidate| {
|
||||||
|
!candidate.allow_buy && candidate.allow_sell && !candidate.is_paused
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn stock_state_with_factor_date_and_time(
|
fn stock_state_with_factor_date_and_time(
|
||||||
&self,
|
&self,
|
||||||
ctx: &StrategyContext<'_>,
|
ctx: &StrategyContext<'_>,
|
||||||
@@ -6820,6 +6831,10 @@ impl Strategy for PlatformExprStrategy {
|
|||||||
)? {
|
)? {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if self.candidate_requires_forced_exit(ctx, execution_date, &position.symbol) {
|
||||||
|
pending_full_close_symbols.insert(position.symbol.clone());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
let (stop_hit, profit_hit) =
|
let (stop_hit, profit_hit) =
|
||||||
self.stop_take_action_for_position(ctx, execution_date, &day, position)?;
|
self.stop_take_action_for_position(ctx, execution_date, &day, position)?;
|
||||||
if stop_hit {
|
if stop_hit {
|
||||||
@@ -6854,6 +6869,52 @@ impl Strategy for PlatformExprStrategy {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if self.config.aiquant_transaction_cost {
|
||||||
|
for position in ctx.portfolio.positions().values() {
|
||||||
|
if position.quantity == 0 || delayed_sold_symbols.contains(&position.symbol) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if !pending_full_close_symbols.contains(&position.symbol)
|
||||||
|
|| !self.candidate_requires_forced_exit(ctx, execution_date, &position.symbol)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if self.regular_sell_should_wait_due_to_highlimit(
|
||||||
|
ctx,
|
||||||
|
execution_date,
|
||||||
|
&position.symbol,
|
||||||
|
self.intraday_execution_start_time(),
|
||||||
|
)? {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let can_sell = self.can_sell_position(ctx, execution_date, &position.symbol);
|
||||||
|
exit_symbols.insert(position.symbol.clone());
|
||||||
|
order_intents.push(OrderIntent::TargetValue {
|
||||||
|
symbol: position.symbol.clone(),
|
||||||
|
target_value: 0.0,
|
||||||
|
reason: "risk_forced_exit".to_string(),
|
||||||
|
});
|
||||||
|
if can_sell {
|
||||||
|
if self
|
||||||
|
.project_target_zero(
|
||||||
|
ctx,
|
||||||
|
&mut projected,
|
||||||
|
execution_date,
|
||||||
|
&position.symbol,
|
||||||
|
&mut projected_execution_state,
|
||||||
|
)
|
||||||
|
.is_some()
|
||||||
|
&& Self::projected_position_is_flat(&projected, &position.symbol)
|
||||||
|
{
|
||||||
|
same_day_sold_symbols.insert(position.symbol.clone());
|
||||||
|
slot_working_symbols.remove(&position.symbol);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
unresolved_stop_loss_symbols.insert(position.symbol.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if self.config.aiquant_transaction_cost
|
if self.config.aiquant_transaction_cost
|
||||||
&& self.config.rotation_enabled
|
&& self.config.rotation_enabled
|
||||||
&& trading_ratio > 0.0
|
&& trading_ratio > 0.0
|
||||||
@@ -10675,6 +10736,127 @@ mod tests {
|
|||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn platform_aiquant_forced_risk_exit_precedes_weak_market_shrink() {
|
||||||
|
let date = d(2023, 5, 5);
|
||||||
|
let symbol = "300621.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(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: 200_000,
|
||||||
|
tick_volume: 1_000,
|
||||||
|
bid1_volume: 2_000,
|
||||||
|
ask1_volume: 2_000,
|
||||||
|
trading_phase: Some("continuous".to_string()),
|
||||||
|
paused: false,
|
||||||
|
upper_limit: 11.44,
|
||||||
|
lower_limit: 9.36,
|
||||||
|
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: false,
|
||||||
|
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,
|
||||||
|
}],
|
||||||
|
)
|
||||||
|
.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: 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.clear();
|
||||||
|
cfg.take_profit_expr.clear();
|
||||||
|
cfg.weak_market_shrink_overweight_threshold = Some(1.10);
|
||||||
|
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::TargetValue {
|
||||||
|
symbol: intent_symbol,
|
||||||
|
target_value,
|
||||||
|
reason,
|
||||||
|
} if intent_symbol == symbol && *target_value == 0.0 && reason == "risk_forced_exit"
|
||||||
|
)));
|
||||||
|
assert!(!decision.order_intents.iter().any(|intent| matches!(
|
||||||
|
intent,
|
||||||
|
OrderIntent::Shares {
|
||||||
|
symbol: intent_symbol,
|
||||||
|
reason,
|
||||||
|
..
|
||||||
|
} if intent_symbol == symbol && reason == "daily_position_target_adjust"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn platform_aiquant_weak_market_threshold_only_sells_overweight_positions() {
|
fn platform_aiquant_weak_market_threshold_only_sells_overweight_positions() {
|
||||||
let prev_date = d(2023, 5, 4);
|
let prev_date = d(2023, 5, 4);
|
||||||
|
|||||||
Reference in New Issue
Block a user