修正弱市缩仓补买预算
This commit is contained in:
@@ -6820,7 +6820,13 @@ impl Strategy for PlatformExprStrategy {
|
||||
&& selection_limit > 0
|
||||
{
|
||||
let target_budget = aiquant_total_value * trading_ratio;
|
||||
let fixed_slot_buy_cash = if self.config.aiquant_transaction_cost {
|
||||
let use_fixed_slot_buy_cash = self.config.aiquant_transaction_cost
|
||||
&& !(weak_market_shrink_due
|
||||
&& self
|
||||
.config
|
||||
.weak_market_shrink_overweight_threshold
|
||||
.is_some());
|
||||
let fixed_slot_buy_cash = if use_fixed_slot_buy_cash {
|
||||
target_budget / selection_limit as f64
|
||||
} else {
|
||||
0.0
|
||||
@@ -6838,7 +6844,7 @@ impl Strategy for PlatformExprStrategy {
|
||||
if working_symbols.len() >= selection_limit {
|
||||
break;
|
||||
}
|
||||
let slot_buy_cash = if self.config.aiquant_transaction_cost {
|
||||
let slot_buy_cash = if use_fixed_slot_buy_cash {
|
||||
fixed_slot_buy_cash
|
||||
} else {
|
||||
self.remaining_buy_cash_per_slot(
|
||||
@@ -9802,6 +9808,167 @@ mod tests {
|
||||
)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn platform_aiquant_weak_market_threshold_top_up_uses_remaining_budget() {
|
||||
let prev_date = d(2023, 5, 4);
|
||||
let date = d(2023, 5, 5);
|
||||
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(format!("{date} 10:18:00")),
|
||||
day_open: 10.0,
|
||||
open: 10.0,
|
||||
high: 10.2,
|
||||
low: 9.8,
|
||||
close: 10.0,
|
||||
last_price: 10.0,
|
||||
bid1: 10.0,
|
||||
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: 999.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("timestamp"),
|
||||
last_price: 10.0,
|
||||
bid1: 10.0,
|
||||
ask1: 10.01,
|
||||
bid1_volume: 10,
|
||||
ask1_volume: 10,
|
||||
volume_delta: 10_000,
|
||||
amount_delta: 100_000.0,
|
||||
trading_phase: Some("continuous".to_string()),
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
.expect("dataset");
|
||||
let mut portfolio = PortfolioState::new(100_000.0);
|
||||
portfolio
|
||||
.position_mut("000001.SZ")
|
||||
.buy(prev_date, 3_000, 10.0);
|
||||
portfolio
|
||||
.position_mut("000002.SZ")
|
||||
.buy(prev_date, 2_400, 10.0);
|
||||
portfolio.apply_cash_delta(-54_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 = "000001.SZ".to_string();
|
||||
cfg.max_positions = 3;
|
||||
cfg.selection_limit_expr = "3".to_string();
|
||||
cfg.market_cap_lower_expr = "0".to_string();
|
||||
cfg.market_cap_upper_expr = "1000".to_string();
|
||||
cfg.stock_filter_expr = "close > 0".to_string();
|
||||
cfg.exposure_expr = "0.5".to_string();
|
||||
cfg.stop_loss_expr.clear();
|
||||
cfg.take_profit_expr.clear();
|
||||
cfg.daily_top_up_enabled = true;
|
||||
cfg.weak_market_shrink_overweight_threshold = Some(1.10);
|
||||
cfg.intraday_execution_time = Some(NaiveTime::from_hms_opt(10, 18, 0).expect("time"));
|
||||
let mut strategy = PlatformExprStrategy::new(cfg);
|
||||
strategy.rebalance_day_counter = 1;
|
||||
|
||||
let decision = strategy.on_day(&ctx).expect("platform decision");
|
||||
|
||||
assert!(
|
||||
decision.order_intents.iter().any(|intent| matches!(
|
||||
intent,
|
||||
OrderIntent::Shares {
|
||||
reason,
|
||||
quantity,
|
||||
..
|
||||
} if reason == "daily_position_target_adjust" && *quantity < 0
|
||||
)),
|
||||
"{:?}",
|
||||
decision.order_intents
|
||||
);
|
||||
assert!(!decision.order_intents.iter().any(|intent| matches!(
|
||||
intent,
|
||||
OrderIntent::Value { reason, .. } if reason == "daily_top_up_buy"
|
||||
)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn platform_aiquant_weak_market_does_not_retarget_same_ratio_every_day() {
|
||||
let first_date = d(2023, 5, 4);
|
||||
|
||||
Reference in New Issue
Block a user