修正平台策略弱市调仓顺序
This commit is contained in:
@@ -6083,6 +6083,7 @@ impl PlatformExprStrategy {
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn stop_take_action(
|
||||
&self,
|
||||
ctx: &StrategyContext<'_>,
|
||||
@@ -6094,6 +6095,17 @@ impl PlatformExprStrategy {
|
||||
let Some(position) = ctx.portfolio.position(symbol) else {
|
||||
return Ok((false, false));
|
||||
};
|
||||
self.stop_take_action_for_position(ctx, execution_date, day, position)
|
||||
}
|
||||
|
||||
fn stop_take_action_for_position(
|
||||
&self,
|
||||
ctx: &StrategyContext<'_>,
|
||||
execution_date: NaiveDate,
|
||||
day: &DayExpressionState,
|
||||
position: &crate::portfolio::Position,
|
||||
) -> Result<(bool, bool), BacktestError> {
|
||||
let symbol = position.symbol.as_str();
|
||||
if self.config.stop_loss_expr.trim().is_empty()
|
||||
&& self.config.take_profit_expr.trim().is_empty()
|
||||
{
|
||||
@@ -6447,26 +6459,58 @@ 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;
|
||||
}
|
||||
let projected_position = projected.position(&position.symbol).unwrap_or(position);
|
||||
let avg_price = if self.config.aiquant_transaction_cost
|
||||
&& position.average_cost.is_finite()
|
||||
&& position.average_cost > 0.0
|
||||
&& projected_position.average_cost.is_finite()
|
||||
&& projected_position.average_cost > 0.0
|
||||
{
|
||||
position.average_cost
|
||||
projected_position.average_cost
|
||||
} else {
|
||||
position
|
||||
projected_position
|
||||
.average_entry_price()
|
||||
.filter(|value| value.is_finite() && *value > 0.0)
|
||||
.unwrap_or(position.average_cost)
|
||||
.unwrap_or(projected_position.average_cost)
|
||||
};
|
||||
if position.quantity == 0 || avg_price <= 0.0 {
|
||||
if projected_position.quantity == 0 || avg_price <= 0.0 {
|
||||
continue;
|
||||
}
|
||||
let (stop_hit, profit_hit) =
|
||||
self.stop_take_action(ctx, decision_date, execution_date, &day, &position.symbol)?;
|
||||
self.stop_take_action_for_position(ctx, execution_date, &day, projected_position)?;
|
||||
let can_sell = self.can_sell_position(ctx, execution_date, &position.symbol);
|
||||
if stop_hit {
|
||||
exit_symbols.insert(position.symbol.clone());
|
||||
@@ -6547,43 +6591,6 @@ 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
|
||||
@@ -11690,7 +11697,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn platform_stop_loss_skips_weak_market_top_up_for_exiting_position() {
|
||||
fn platform_weak_market_adjusts_before_stop_loss_and_skips_top_up_for_exiting_position() {
|
||||
let prev_date = d(2025, 2, 2);
|
||||
let date = d(2025, 2, 3);
|
||||
let symbols = ["000001.SZ", "000002.SZ"];
|
||||
@@ -11807,23 +11814,42 @@ mod tests {
|
||||
cfg.stop_loss_expr = "0.92".to_string();
|
||||
cfg.take_profit_expr.clear();
|
||||
cfg.daily_top_up_enabled = true;
|
||||
cfg.aiquant_transaction_cost = 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"
|
||||
)));
|
||||
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()
|
||||
.position(|intent| {
|
||||
matches!(
|
||||
intent,
|
||||
OrderIntent::TargetValue {
|
||||
symbol,
|
||||
target_value,
|
||||
reason,
|
||||
} if symbol == "000001.SZ" && *target_value == 0.0 && reason == "stop_loss_exit"
|
||||
)
|
||||
})
|
||||
.expect("stop-loss exit");
|
||||
assert!(target_adjust_index < stop_loss_index);
|
||||
assert!(!decision.order_intents.iter().any(|intent| matches!(
|
||||
intent,
|
||||
OrderIntent::TargetValue { symbol, reason, .. }
|
||||
if symbol == "000001.SZ" && reason == "daily_position_target_adjust"
|
||||
OrderIntent::Value { symbol, reason, .. } | OrderIntent::Shares { symbol, reason, .. }
|
||||
if symbol == "000001.SZ" && reason == "daily_top_up_buy"
|
||||
)));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user