修正历史清仓残量补仓语义
This commit is contained in:
@@ -8708,8 +8708,49 @@ impl Strategy for PlatformExprStrategy {
|
|||||||
}
|
}
|
||||||
let (stop_hit, profit_hit) =
|
let (stop_hit, profit_hit) =
|
||||||
self.stop_take_action_for_position(ctx, signal_date, &day, projected_position)?;
|
self.stop_take_action_for_position(ctx, signal_date, &day, projected_position)?;
|
||||||
if carried_full_close_symbols.contains(&position.symbol) && !stop_hit && !profit_hit {
|
let is_carried_full_close = carried_full_close_symbols.contains(&position.symbol);
|
||||||
|
if is_carried_full_close {
|
||||||
|
if !stop_hit && !profit_hit {
|
||||||
self.pending_full_close_symbols.remove(&position.symbol);
|
self.pending_full_close_symbols.remove(&position.symbol);
|
||||||
|
} else {
|
||||||
|
exit_symbols.insert(position.symbol.clone());
|
||||||
|
slot_blocking_symbols.insert(position.symbol.clone());
|
||||||
|
order_intents.push(OrderIntent::TargetValue {
|
||||||
|
symbol: position.symbol.clone(),
|
||||||
|
target_value: 0.0,
|
||||||
|
reason: "pending_full_close_exit".to_string(),
|
||||||
|
});
|
||||||
|
self.forget_position_entry_date(&position.symbol);
|
||||||
|
let can_sell = defer_execution_risk
|
||||||
|
|| self.can_sell_position(ctx, execution_date, &position.symbol);
|
||||||
|
if can_sell {
|
||||||
|
let close_submitted = self
|
||||||
|
.project_target_zero(
|
||||||
|
ctx,
|
||||||
|
&mut projected,
|
||||||
|
projection_date,
|
||||||
|
&position.symbol,
|
||||||
|
&mut projected_execution_state,
|
||||||
|
)
|
||||||
|
.is_some();
|
||||||
|
if close_submitted {
|
||||||
|
slot_working_symbols.remove(&position.symbol);
|
||||||
|
if Self::projected_position_is_flat(&projected, &position.symbol) {
|
||||||
|
same_day_sold_symbols.insert(position.symbol.clone());
|
||||||
|
pending_full_close_symbols.remove(&position.symbol);
|
||||||
|
self.pending_full_close_symbols.remove(&position.symbol);
|
||||||
|
} else {
|
||||||
|
pending_full_close_symbols.insert(position.symbol.clone());
|
||||||
|
slot_blocking_symbols.insert(position.symbol.clone());
|
||||||
|
self.pending_full_close_symbols
|
||||||
|
.insert(position.symbol.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if stop_hit {
|
||||||
|
unresolved_stop_loss_symbols.insert(position.symbol.clone());
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
let can_sell = defer_execution_risk
|
let can_sell = defer_execution_risk
|
||||||
|| self.can_sell_position(ctx, execution_date, &position.symbol);
|
|| self.can_sell_position(ctx, execution_date, &position.symbol);
|
||||||
@@ -21739,6 +21780,202 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn platform_aiquant_carried_full_close_does_not_release_new_top_up_slot() {
|
||||||
|
let prev_date = d(2025, 4, 29);
|
||||||
|
let date = d(2025, 4, 30);
|
||||||
|
let buy_first = "000001.SZ";
|
||||||
|
let buy_second = "000002.SZ";
|
||||||
|
let keep_first = "000101.SZ";
|
||||||
|
let take_profit = "000102.SZ";
|
||||||
|
let keep_second = "000103.SZ";
|
||||||
|
let symbols = [buy_first, buy_second, keep_first, take_profit, keep_second];
|
||||||
|
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-04-30 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: 20_000,
|
||||||
|
bid1_volume: 20_000,
|
||||||
|
ask1_volume: 20_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 {
|
||||||
|
s if s == buy_first => 1.0,
|
||||||
|
s if s == buy_second => 2.0,
|
||||||
|
s if s == keep_first => 3.0,
|
||||||
|
s if s == keep_second => 4.0,
|
||||||
|
_ => 100.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: true,
|
||||||
|
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: 20_000,
|
||||||
|
ask1_volume: 20_000,
|
||||||
|
volume_delta: 20_000,
|
||||||
|
amount_delta: 200_000.0,
|
||||||
|
trading_phase: Some("continuous".to_string()),
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
)
|
||||||
|
.expect("dataset");
|
||||||
|
|
||||||
|
let mut portfolio = PortfolioState::new(5_000.0);
|
||||||
|
portfolio.position_mut(keep_first).buy(prev_date, 100, 10.0);
|
||||||
|
portfolio.position_mut(take_profit).buy(prev_date, 100, 8.0);
|
||||||
|
portfolio
|
||||||
|
.position_mut(keep_second)
|
||||||
|
.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 = keep_first.to_string();
|
||||||
|
cfg.refresh_rate = 99;
|
||||||
|
cfg.max_positions = 4;
|
||||||
|
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 = "4".to_string();
|
||||||
|
cfg.stock_filter_expr = "close > 0".to_string();
|
||||||
|
cfg.exposure_expr = "1.0".to_string();
|
||||||
|
cfg.take_profit_expr = "1.1".to_string();
|
||||||
|
cfg.daily_top_up_enabled = true;
|
||||||
|
cfg.release_slot_on_exit_signal = 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 = 2;
|
||||||
|
strategy
|
||||||
|
.pending_full_close_symbols
|
||||||
|
.insert(take_profit.to_string());
|
||||||
|
|
||||||
|
let decision = strategy.on_day(&ctx).expect("platform decision");
|
||||||
|
let top_up_symbols = decision
|
||||||
|
.order_intents
|
||||||
|
.iter()
|
||||||
|
.filter_map(|intent| match intent {
|
||||||
|
OrderIntent::Value { symbol, reason, .. } if reason == "daily_top_up_buy" => {
|
||||||
|
Some(symbol.as_str())
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
decision.order_intents.iter().any(|intent| matches!(
|
||||||
|
intent,
|
||||||
|
OrderIntent::TargetValue {
|
||||||
|
symbol,
|
||||||
|
target_value,
|
||||||
|
reason,
|
||||||
|
} if symbol == take_profit
|
||||||
|
&& *target_value == 0.0
|
||||||
|
&& reason == "pending_full_close_exit"
|
||||||
|
)),
|
||||||
|
"{:?}",
|
||||||
|
decision.order_intents
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
top_up_symbols,
|
||||||
|
vec![buy_first],
|
||||||
|
"{:?}",
|
||||||
|
decision.order_intents
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
!top_up_symbols.contains(&buy_second),
|
||||||
|
"{:?}",
|
||||||
|
decision.order_intents
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn platform_max_holding_days_exit_preempts_take_profit_exit() {
|
fn platform_max_holding_days_exit_preempts_take_profit_exit() {
|
||||||
let date = d(2025, 2, 26);
|
let date = d(2025, 2, 26);
|
||||||
|
|||||||
Reference in New Issue
Block a user