修正延迟涨停卖出补仓槽位
This commit is contained in:
@@ -8125,7 +8125,6 @@ impl Strategy for PlatformExprStrategy {
|
|||||||
let mut intraday_attempted_buys = BTreeSet::<String>::new();
|
let mut intraday_attempted_buys = BTreeSet::<String>::new();
|
||||||
let mut same_bar_buy_symbols = BTreeSet::<String>::new();
|
let mut same_bar_buy_symbols = BTreeSet::<String>::new();
|
||||||
let mut delayed_sold_symbols = BTreeSet::<String>::new();
|
let mut delayed_sold_symbols = BTreeSet::<String>::new();
|
||||||
let mut delayed_exit_slot_blocking_symbols = BTreeSet::<String>::new();
|
|
||||||
let mut unresolved_stop_loss_symbols = BTreeSet::<String>::new();
|
let mut unresolved_stop_loss_symbols = BTreeSet::<String>::new();
|
||||||
let mut pending_risk_level_forced_exit_symbols = BTreeSet::<String>::new();
|
let mut pending_risk_level_forced_exit_symbols = BTreeSet::<String>::new();
|
||||||
let delayed_limit_exit_time = self
|
let delayed_limit_exit_time = self
|
||||||
@@ -8229,7 +8228,7 @@ impl Strategy for PlatformExprStrategy {
|
|||||||
end_time: Some(delayed_limit_exit_time),
|
end_time: Some(delayed_limit_exit_time),
|
||||||
reason: "delayed_limit_open_sell".to_string(),
|
reason: "delayed_limit_open_sell".to_string(),
|
||||||
});
|
});
|
||||||
delayed_exit_slot_blocking_symbols.insert(symbol.clone());
|
delayed_sold_symbols.insert(symbol.clone());
|
||||||
self.forget_position_entry_date(&symbol);
|
self.forget_position_entry_date(&symbol);
|
||||||
let projected_sold = if ctx
|
let projected_sold = if ctx
|
||||||
.data
|
.data
|
||||||
@@ -8251,7 +8250,6 @@ impl Strategy for PlatformExprStrategy {
|
|||||||
};
|
};
|
||||||
if projected_sold {
|
if projected_sold {
|
||||||
same_day_sold_symbols.insert(symbol.clone());
|
same_day_sold_symbols.insert(symbol.clone());
|
||||||
delayed_sold_symbols.insert(symbol.clone());
|
|
||||||
self.pending_highlimit_holdings.remove(&symbol);
|
self.pending_highlimit_holdings.remove(&symbol);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8334,9 +8332,8 @@ impl Strategy for PlatformExprStrategy {
|
|||||||
let daily_top_up_target_budget = aiquant_total_value * trading_ratio;
|
let daily_top_up_target_budget = aiquant_total_value * trading_ratio;
|
||||||
let mut daily_top_up_pending_buy_value = 0.0_f64;
|
let mut daily_top_up_pending_buy_value = 0.0_f64;
|
||||||
let mut pending_full_close_symbols = BTreeSet::<String>::new();
|
let mut pending_full_close_symbols = BTreeSet::<String>::new();
|
||||||
let mut slot_blocking_symbols = delayed_exit_slot_blocking_symbols.clone();
|
let mut slot_blocking_symbols = BTreeSet::<String>::new();
|
||||||
let should_block_released_slots_after_top_up =
|
let should_block_released_slots_after_top_up = false;
|
||||||
!delayed_exit_slot_blocking_symbols.is_empty();
|
|
||||||
let interleaved_pending_full_close_symbols = BTreeSet::<String>::new();
|
let interleaved_pending_full_close_symbols = BTreeSet::<String>::new();
|
||||||
let risk_level_forced_exit_time = self.risk_level_forced_exit_time();
|
let risk_level_forced_exit_time = self.risk_level_forced_exit_time();
|
||||||
self.pending_full_close_symbols.retain(|symbol| {
|
self.pending_full_close_symbols.retain(|symbol| {
|
||||||
@@ -22368,6 +22365,228 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn platform_delayed_open_partial_exit_releases_top_up_slot() {
|
||||||
|
let prev_date = d(2025, 5, 5);
|
||||||
|
let date = d(2025, 5, 6);
|
||||||
|
let delayed_symbol = "000001.SZ";
|
||||||
|
let held_symbol = "000002.SZ";
|
||||||
|
let buy_symbol = "000003.SZ";
|
||||||
|
let symbols = [delayed_symbol, held_symbol, buy_symbol];
|
||||||
|
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-05-06 10:18:00".to_string()),
|
||||||
|
day_open: 10.0,
|
||||||
|
open: 10.0,
|
||||||
|
high: 11.0,
|
||||||
|
low: 9.0,
|
||||||
|
close: 10.0,
|
||||||
|
last_price: 10.0,
|
||||||
|
bid1: 10.0,
|
||||||
|
ask1: 10.0,
|
||||||
|
prev_close: 10.0,
|
||||||
|
volume: 1_000_000,
|
||||||
|
minute_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: if *symbol == buy_symbol {
|
||||||
|
7.0
|
||||||
|
} else if *symbol == delayed_symbol {
|
||||||
|
8.0
|
||||||
|
} else {
|
||||||
|
9.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(),
|
||||||
|
vec![
|
||||||
|
IntradayExecutionQuote {
|
||||||
|
date,
|
||||||
|
symbol: delayed_symbol.to_string(),
|
||||||
|
timestamp: date.and_hms_opt(9, 31, 0).expect("valid timestamp"),
|
||||||
|
last_price: 10.5,
|
||||||
|
bid1: 10.5,
|
||||||
|
ask1: 10.51,
|
||||||
|
bid1_volume: 10_000,
|
||||||
|
ask1_volume: 10_000,
|
||||||
|
volume_delta: 400,
|
||||||
|
amount_delta: 4_200.0,
|
||||||
|
trading_phase: Some("continuous".to_string()),
|
||||||
|
},
|
||||||
|
IntradayExecutionQuote {
|
||||||
|
date,
|
||||||
|
symbol: held_symbol.to_string(),
|
||||||
|
timestamp: date.and_hms_opt(10, 18, 0).expect("valid timestamp"),
|
||||||
|
last_price: 10.0,
|
||||||
|
bid1: 10.0,
|
||||||
|
ask1: 10.01,
|
||||||
|
bid1_volume: 10_000,
|
||||||
|
ask1_volume: 10_000,
|
||||||
|
volume_delta: 10_000,
|
||||||
|
amount_delta: 100_000.0,
|
||||||
|
trading_phase: Some("continuous".to_string()),
|
||||||
|
},
|
||||||
|
IntradayExecutionQuote {
|
||||||
|
date,
|
||||||
|
symbol: buy_symbol.to_string(),
|
||||||
|
timestamp: date.and_hms_opt(10, 18, 0).expect("valid timestamp"),
|
||||||
|
last_price: 10.0,
|
||||||
|
bid1: 10.0,
|
||||||
|
ask1: 10.01,
|
||||||
|
bid1_volume: 10_000,
|
||||||
|
ask1_volume: 10_000,
|
||||||
|
volume_delta: 10_000,
|
||||||
|
amount_delta: 100_000.0,
|
||||||
|
trading_phase: Some("continuous".to_string()),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
)
|
||||||
|
.expect("dataset");
|
||||||
|
|
||||||
|
let mut portfolio = PortfolioState::new(20_000.0);
|
||||||
|
portfolio
|
||||||
|
.position_mut(delayed_symbol)
|
||||||
|
.buy(prev_date, 1_000, 10.0);
|
||||||
|
portfolio
|
||||||
|
.position_mut(held_symbol)
|
||||||
|
.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 = delayed_symbol.to_string();
|
||||||
|
cfg.refresh_rate = 99;
|
||||||
|
cfg.max_positions = 2;
|
||||||
|
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 = "2".to_string();
|
||||||
|
cfg.stock_filter_expr = "close > 0".to_string();
|
||||||
|
cfg.exposure_expr = "1.0".to_string();
|
||||||
|
cfg.stop_loss_expr = "false".to_string();
|
||||||
|
cfg.take_profit_expr = "false".to_string();
|
||||||
|
cfg.daily_top_up_enabled = true;
|
||||||
|
cfg.release_slot_on_exit_signal = true;
|
||||||
|
cfg.aiquant_transaction_cost = true;
|
||||||
|
cfg.risk_config.trading_constraints.volume_limit_enabled = true;
|
||||||
|
cfg.risk_config.trading_constraints.volume_percent = 0.25;
|
||||||
|
cfg.risk_config.trading_constraints.liquidity_limit_enabled = false;
|
||||||
|
cfg.intraday_execution_time = Some(NaiveTime::from_hms_opt(10, 18, 0).unwrap());
|
||||||
|
cfg.delayed_limit_open_exit_enabled = true;
|
||||||
|
cfg.delayed_limit_open_exit_time = Some(NaiveTime::from_hms_opt(9, 31, 0).unwrap());
|
||||||
|
let mut strategy = PlatformExprStrategy::new(cfg);
|
||||||
|
strategy.rebalance_day_counter = 2;
|
||||||
|
strategy
|
||||||
|
.pending_highlimit_holdings
|
||||||
|
.insert(delayed_symbol.to_string());
|
||||||
|
|
||||||
|
let decision = strategy.on_day(&ctx).expect("platform decision");
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
decision.order_intents.iter().any(|intent| matches!(
|
||||||
|
intent,
|
||||||
|
OrderIntent::AlgoValue {
|
||||||
|
symbol,
|
||||||
|
reason,
|
||||||
|
start_time,
|
||||||
|
..
|
||||||
|
} if symbol == delayed_symbol
|
||||||
|
&& reason == "delayed_limit_open_sell"
|
||||||
|
&& *start_time == Some(NaiveTime::from_hms_opt(9, 31, 0).unwrap())
|
||||||
|
)),
|
||||||
|
"{:?}",
|
||||||
|
decision.order_intents
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
decision.order_intents.iter().any(|intent| matches!(
|
||||||
|
intent,
|
||||||
|
OrderIntent::Value {
|
||||||
|
symbol,
|
||||||
|
reason,
|
||||||
|
..
|
||||||
|
} if symbol == buy_symbol && reason == "daily_top_up_buy"
|
||||||
|
)),
|
||||||
|
"{:?}",
|
||||||
|
decision.order_intents
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
strategy.pending_highlimit_holdings.contains(delayed_symbol),
|
||||||
|
"partial delayed exit must keep residual pending"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn platform_daily_top_up_releases_unsellable_stop_loss_slot() {
|
fn platform_daily_top_up_releases_unsellable_stop_loss_slot() {
|
||||||
let prev_date = d(2026, 3, 31);
|
let prev_date = d(2026, 3, 31);
|
||||||
|
|||||||
Reference in New Issue
Block a user