修复AiQuant转换调度和不可卖调仓语义
This commit is contained in:
@@ -7854,6 +7854,7 @@ impl Strategy for PlatformExprStrategy {
|
|||||||
let mut intraday_attempted_buys = BTreeSet::<String>::new();
|
let mut intraday_attempted_buys = BTreeSet::<String>::new();
|
||||||
let mut delayed_sold_symbols = BTreeSet::<String>::new();
|
let mut delayed_sold_symbols = BTreeSet::<String>::new();
|
||||||
let mut unresolved_stop_loss_symbols = BTreeSet::<String>::new();
|
let mut unresolved_stop_loss_symbols = BTreeSet::<String>::new();
|
||||||
|
let mut unsellable_stop_take_attempted_symbols = BTreeSet::<String>::new();
|
||||||
let mut pre_detected_stop_take_exit_symbols = BTreeSet::<String>::new();
|
let mut pre_detected_stop_take_exit_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
|
||||||
@@ -8164,16 +8165,29 @@ impl Strategy for PlatformExprStrategy {
|
|||||||
}
|
}
|
||||||
let (stop_hit, profit_hit) =
|
let (stop_hit, profit_hit) =
|
||||||
self.stop_take_action_for_position(ctx, signal_date, &day, position)?;
|
self.stop_take_action_for_position(ctx, signal_date, &day, position)?;
|
||||||
if stop_hit {
|
|
||||||
pre_detected_stop_take_exit_symbols.insert(position.symbol.clone());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if profit_hit {
|
|
||||||
pre_detected_stop_take_exit_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);
|
||||||
|
if stop_hit || profit_hit {
|
||||||
|
if can_sell {
|
||||||
|
pre_detected_stop_take_exit_symbols.insert(position.symbol.clone());
|
||||||
|
} else {
|
||||||
|
let reason = if stop_hit {
|
||||||
|
"stop_loss_exit"
|
||||||
|
} else {
|
||||||
|
"take_profit_exit"
|
||||||
|
};
|
||||||
|
order_intents.push(OrderIntent::TargetValue {
|
||||||
|
symbol: position.symbol.clone(),
|
||||||
|
target_value: 0.0,
|
||||||
|
reason: reason.to_string(),
|
||||||
|
});
|
||||||
|
unsellable_stop_take_attempted_symbols.insert(position.symbol.clone());
|
||||||
|
if stop_hit {
|
||||||
|
unresolved_stop_loss_symbols.insert(position.symbol.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if !can_sell {
|
if !can_sell {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -8340,6 +8354,9 @@ impl Strategy for PlatformExprStrategy {
|
|||||||
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);
|
||||||
if stop_hit {
|
if stop_hit {
|
||||||
|
if unsellable_stop_take_attempted_symbols.contains(&position.symbol) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
self.pending_full_close_symbols
|
self.pending_full_close_symbols
|
||||||
.insert(position.symbol.clone());
|
.insert(position.symbol.clone());
|
||||||
exit_symbols.insert(position.symbol.clone());
|
exit_symbols.insert(position.symbol.clone());
|
||||||
@@ -8372,6 +8389,9 @@ impl Strategy for PlatformExprStrategy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if profit_hit {
|
if profit_hit {
|
||||||
|
if unsellable_stop_take_attempted_symbols.contains(&position.symbol) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
self.pending_full_close_symbols
|
self.pending_full_close_symbols
|
||||||
.insert(position.symbol.clone());
|
.insert(position.symbol.clone());
|
||||||
exit_symbols.insert(position.symbol.clone());
|
exit_symbols.insert(position.symbol.clone());
|
||||||
@@ -15697,6 +15717,150 @@ mod tests {
|
|||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn platform_aiquant_allows_target_adjust_when_stop_loss_sell_is_lower_limit_blocked() {
|
||||||
|
let prev_date = d(2025, 1, 10);
|
||||||
|
let date = d(2025, 1, 13);
|
||||||
|
let symbol = "603269.SH";
|
||||||
|
let data = DataSet::from_components_with_actions_and_quotes(
|
||||||
|
vec![Instrument {
|
||||||
|
symbol: symbol.to_string(),
|
||||||
|
name: symbol.to_string(),
|
||||||
|
board: "SH".to_string(),
|
||||||
|
round_lot: 100,
|
||||||
|
listed_at: Some(d(2010, 1, 1)),
|
||||||
|
delisted_at: None,
|
||||||
|
status: "active".to_string(),
|
||||||
|
}],
|
||||||
|
vec![DailyMarketSnapshot {
|
||||||
|
date,
|
||||||
|
symbol: symbol.to_string(),
|
||||||
|
timestamp: Some("2025-01-13 10:15:00".to_string()),
|
||||||
|
day_open: 12.5,
|
||||||
|
open: 12.5,
|
||||||
|
high: 12.5,
|
||||||
|
low: 12.5,
|
||||||
|
close: 12.5,
|
||||||
|
last_price: 12.5,
|
||||||
|
bid1: 12.5,
|
||||||
|
ask1: 12.5,
|
||||||
|
prev_close: 13.89,
|
||||||
|
volume: 600,
|
||||||
|
minute_volume: 600,
|
||||||
|
bid1_volume: 0,
|
||||||
|
ask1_volume: 0,
|
||||||
|
trading_phase: Some("continuous".to_string()),
|
||||||
|
paused: false,
|
||||||
|
upper_limit: 15.28,
|
||||||
|
lower_limit: 12.5,
|
||||||
|
price_tick: 0.01,
|
||||||
|
}],
|
||||||
|
vec![DailyFactorSnapshot {
|
||||||
|
date,
|
||||||
|
symbol: symbol.to_string(),
|
||||||
|
market_cap_bn: 2.7567,
|
||||||
|
free_float_cap_bn: 2.7567,
|
||||||
|
pe_ttm: 8.0,
|
||||||
|
turnover_ratio: Some(1.0),
|
||||||
|
effective_turnover_ratio: Some(1.0),
|
||||||
|
extra_factors: BTreeMap::new(),
|
||||||
|
}],
|
||||||
|
vec![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,
|
||||||
|
}],
|
||||||
|
vec![BenchmarkSnapshot {
|
||||||
|
date,
|
||||||
|
benchmark: "000852.SH".to_string(),
|
||||||
|
open: 1000.0,
|
||||||
|
close: 1000.0,
|
||||||
|
prev_close: 1000.0,
|
||||||
|
volume: 1_000_000,
|
||||||
|
}],
|
||||||
|
Vec::new(),
|
||||||
|
vec![IntradayExecutionQuote {
|
||||||
|
date,
|
||||||
|
symbol: symbol.to_string(),
|
||||||
|
timestamp: date.and_hms_opt(10, 15, 0).expect("timestamp"),
|
||||||
|
last_price: 12.5,
|
||||||
|
bid1: 12.5,
|
||||||
|
ask1: 12.5,
|
||||||
|
bid1_volume: 0,
|
||||||
|
ask1_volume: 0,
|
||||||
|
volume_delta: 600,
|
||||||
|
amount_delta: 7_500.0,
|
||||||
|
trading_phase: Some("continuous".to_string()),
|
||||||
|
}],
|
||||||
|
)
|
||||||
|
.expect("dataset");
|
||||||
|
let mut portfolio = PortfolioState::new(3_950_000.0);
|
||||||
|
portfolio
|
||||||
|
.position_mut(symbol)
|
||||||
|
.buy_with_mark_price(prev_date, 3_500, 13.9039, 12.5);
|
||||||
|
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.matching_type = MatchingType::MinuteLast;
|
||||||
|
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 = true;
|
||||||
|
cfg.signal_symbol = symbol.to_string();
|
||||||
|
cfg.exposure_expr = "0.5".to_string();
|
||||||
|
cfg.selection_limit_expr = "40".to_string();
|
||||||
|
cfg.stock_filter_expr = "false".to_string();
|
||||||
|
cfg.stop_loss_expr = "0.92".to_string();
|
||||||
|
cfg.take_profit_expr.clear();
|
||||||
|
cfg.intraday_execution_time = Some(NaiveTime::from_hms_opt(10, 15, 0).expect("time"));
|
||||||
|
let mut strategy = PlatformExprStrategy::new(cfg);
|
||||||
|
|
||||||
|
let decision = strategy.on_day(&ctx).expect("platform decision");
|
||||||
|
|
||||||
|
assert!(decision.order_intents.iter().any(|intent| matches!(
|
||||||
|
intent,
|
||||||
|
OrderIntent::TargetValue {
|
||||||
|
symbol: intent_symbol,
|
||||||
|
target_value,
|
||||||
|
reason,
|
||||||
|
} if intent_symbol == symbol && *target_value == 0.0 && reason == "stop_loss_exit"
|
||||||
|
)));
|
||||||
|
assert!(decision.order_intents.iter().any(|intent| matches!(
|
||||||
|
intent,
|
||||||
|
OrderIntent::Shares {
|
||||||
|
symbol: intent_symbol,
|
||||||
|
quantity,
|
||||||
|
reason,
|
||||||
|
} if intent_symbol == symbol
|
||||||
|
&& *quantity > 0
|
||||||
|
&& reason == "daily_position_target_adjust"
|
||||||
|
)));
|
||||||
|
assert!(!strategy.pending_full_close_symbols.contains(symbol));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn platform_aiquant_stock_expr_uses_scheduled_minute_price_for_limit_check() {
|
fn platform_aiquant_stock_expr_uses_scheduled_minute_price_for_limit_check() {
|
||||||
let date = d(2024, 1, 30);
|
let date = d(2024, 1, 30);
|
||||||
|
|||||||
@@ -114,6 +114,8 @@ pub struct StrategyEngineConfig {
|
|||||||
pub template_id: Option<String>,
|
pub template_id: Option<String>,
|
||||||
#[serde(default, alias = "profile_name")]
|
#[serde(default, alias = "profile_name")]
|
||||||
pub profile_name: Option<String>,
|
pub profile_name: Option<String>,
|
||||||
|
#[serde(default, alias = "compatibility_profile")]
|
||||||
|
pub compatibility_profile: Option<String>,
|
||||||
#[serde(default, alias = "benchmark_symbol")]
|
#[serde(default, alias = "benchmark_symbol")]
|
||||||
pub benchmark_symbol: Option<String>,
|
pub benchmark_symbol: Option<String>,
|
||||||
#[serde(default, alias = "signal_symbol")]
|
#[serde(default, alias = "signal_symbol")]
|
||||||
@@ -1689,11 +1691,10 @@ pub fn platform_expr_config_from_spec(
|
|||||||
if !cfg.benchmark_symbol.trim().is_empty() {
|
if !cfg.benchmark_symbol.trim().is_empty() {
|
||||||
cfg.benchmark_symbol = normalize_symbol(&cfg.benchmark_symbol, None);
|
cfg.benchmark_symbol = normalize_symbol(&cfg.benchmark_symbol, None);
|
||||||
}
|
}
|
||||||
let aiquant_profile = is_aiquant_profile(
|
let aiquant_profile = spec.engine_config.as_ref().is_some_and(|engine| {
|
||||||
spec.engine_config
|
is_aiquant_profile(engine.profile_name.as_deref())
|
||||||
.as_ref()
|
|| is_aiquant_profile(engine.compatibility_profile.as_deref())
|
||||||
.and_then(|engine| engine.profile_name.as_deref()),
|
});
|
||||||
);
|
|
||||||
if aiquant_profile {
|
if aiquant_profile {
|
||||||
cfg.aiquant_transaction_cost = true;
|
cfg.aiquant_transaction_cost = true;
|
||||||
cfg.strict_value_budget = true;
|
cfg.strict_value_budget = true;
|
||||||
@@ -2881,6 +2882,32 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parses_aiquant_compatibility_profile_for_delayed_limit_exit() {
|
||||||
|
let spec = serde_json::json!({
|
||||||
|
"engineConfig": {
|
||||||
|
"profileName": "cn_a_microcap_v1",
|
||||||
|
"compatibilityProfile": "aiquant_rqalpha"
|
||||||
|
},
|
||||||
|
"rebalance": { "tradeTimes": ["09:31", "10:15"] },
|
||||||
|
"runtimeExpressions": {
|
||||||
|
"schedule": { "frequency": "daily", "time": "10:15" }
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let cfg = platform_expr_config_from_value("", "", &spec).expect("config");
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
cfg.intraday_execution_time,
|
||||||
|
Some(NaiveTime::from_hms_opt(10, 15, 0).unwrap())
|
||||||
|
);
|
||||||
|
assert!(cfg.delayed_limit_open_exit_enabled);
|
||||||
|
assert_eq!(
|
||||||
|
cfg.delayed_limit_open_exit_time,
|
||||||
|
Some(NaiveTime::from_hms_opt(9, 31, 0).unwrap())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parses_explicit_delayed_limit_open_exit() {
|
fn parses_explicit_delayed_limit_open_exit() {
|
||||||
let spec = serde_json::json!({
|
let spec = serde_json::json!({
|
||||||
|
|||||||
Reference in New Issue
Block a user