修复AiQuant补位买入预算口径

This commit is contained in:
boris
2026-06-17 18:21:35 +08:00
parent 674e4b0b14
commit dae573e318
+194 -11
View File
@@ -6798,6 +6798,11 @@ impl Strategy for PlatformExprStrategy {
&& selection_limit > 0 && selection_limit > 0
{ {
let target_budget = aiquant_total_value * trading_ratio; let target_budget = aiquant_total_value * trading_ratio;
let fixed_slot_buy_cash = if self.config.aiquant_transaction_cost {
target_budget / selection_limit as f64
} else {
0.0
};
let mut working_symbols = slot_working_symbols.clone(); let mut working_symbols = slot_working_symbols.clone();
if self.config.release_slot_on_exit_signal { if self.config.release_slot_on_exit_signal {
for symbol in &same_day_sold_symbols { for symbol in &same_day_sold_symbols {
@@ -6811,16 +6816,20 @@ impl Strategy for PlatformExprStrategy {
if working_symbols.len() >= selection_limit { if working_symbols.len() >= selection_limit {
break; break;
} }
let slot_buy_cash = self.remaining_buy_cash_per_slot( let slot_buy_cash = if self.config.aiquant_transaction_cost {
ctx, fixed_slot_buy_cash
&projected, } else {
execution_date, self.remaining_buy_cash_per_slot(
target_budget, ctx,
selection_limit, &projected,
&working_symbols, execution_date,
&value_symbols, target_budget,
pending_buy_value, selection_limit,
); &working_symbols,
&value_symbols,
pending_buy_value,
)
};
let available_buy_cash = slot_buy_cash.min(aiquant_available_cash); let available_buy_cash = slot_buy_cash.min(aiquant_available_cash);
if slot_buy_cash <= 0.0 || available_buy_cash < slot_buy_cash * 0.5 { if slot_buy_cash <= 0.0 || available_buy_cash < slot_buy_cash * 0.5 {
break; break;
@@ -6886,7 +6895,9 @@ impl Strategy for PlatformExprStrategy {
aiquant_available_cash = (aiquant_available_cash - spent).max(0.0); aiquant_available_cash = (aiquant_available_cash - spent).max(0.0);
working_symbols.insert(symbol.clone()); working_symbols.insert(symbol.clone());
slot_working_symbols.insert(symbol.clone()); slot_working_symbols.insert(symbol.clone());
pending_buy_value += available_buy_cash; if !self.config.aiquant_transaction_cost {
pending_buy_value += available_buy_cash;
}
filled_any = true; filled_any = true;
break; break;
} }
@@ -14061,6 +14072,178 @@ mod tests {
); );
} }
#[test]
fn platform_aiquant_daily_top_up_buys_fixed_slot_after_take_profit_exit() {
let prev_date = d(2025, 2, 25);
let date = d(2025, 2, 26);
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-02-26 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,
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()
.map(|symbol| DailyFactorSnapshot {
date,
symbol: (*symbol).to_string(),
market_cap_bn: match *symbol {
"000003.SZ" => 8.0,
"000002.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_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: 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: 0,
amount_delta: 0.0,
trading_phase: Some("continuous".to_string()),
})
.collect(),
)
.expect("dataset");
let mut portfolio = PortfolioState::new(10_000.0);
portfolio.position_mut("000001.SZ").buy(prev_date, 100, 8.0);
portfolio
.position_mut("000002.SZ")
.buy(prev_date, 3_000, 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 = 99;
cfg.max_positions = 3;
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 = "3".to_string();
cfg.stock_filter_expr = "close > 0".to_string();
cfg.exposure_expr = "0.5".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;
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 == "take_profit_exit"
)),
"{:?}",
decision.order_intents
);
assert!(
decision.order_intents.iter().any(|intent| matches!(
intent,
OrderIntent::Shares {
symbol,
quantity,
reason,
} if symbol == "000003.SZ" && *quantity > 0 && reason == "daily_top_up_buy"
)),
"{:?}",
decision.order_intents
);
}
#[test] #[test]
fn platform_daily_top_up_keeps_unsellable_stop_loss_in_target_count() { fn platform_daily_top_up_keeps_unsellable_stop_loss_in_target_count() {
let prev_date = d(2026, 3, 31); let prev_date = d(2026, 3, 31);