修复AiQuant策略表达式回测执行语义

This commit is contained in:
boris
2026-06-15 11:16:04 +08:00
parent d3d08276ae
commit 1c31fa80d2
8 changed files with 1227 additions and 292 deletions
+14 -5
View File
@@ -97,7 +97,7 @@ impl DynamicSlippageConfig {
}
}
fn ratio(
pub(crate) fn ratio(
&self,
snapshot: &crate::data::DailyMarketSnapshot,
raw_price: f64,
@@ -4706,8 +4706,12 @@ where
let quote_quantity_limited =
self.quote_quantity_limited_for_window(matching_type, start_cursor, end_cursor);
let lot = round_lot.max(1);
let use_decision_time_quote =
matching_type == MatchingType::NextTickLast && start_cursor.is_some();
let exact_time_order_quote = matching_type != MatchingType::NextTickLast
&& start_cursor.is_some()
&& end_cursor.is_some()
&& start_cursor == end_cursor;
let use_decision_time_quote = start_cursor.is_some()
&& (matching_type == MatchingType::NextTickLast || exact_time_order_quote);
let eligible_quotes: Vec<&IntradayExecutionQuote> = if use_decision_time_quote {
self.latest_known_quote_at_or_before(
quotes,
@@ -5428,7 +5432,7 @@ mod tests {
);
let default_rule = default_broker.buy_rule_check(date, &snapshot, &candidate, None);
assert!(!default_rule.allowed);
assert_eq!(default_rule.reason.as_deref(), Some("trade_disabled"));
assert_eq!(default_rule.reason.as_deref(), Some("buy_disabled"));
let aiquant_broker = BrokerSimulator::new_with_execution_price(
ChinaAShareCostModel::default(),
@@ -5438,12 +5442,17 @@ mod tests {
.with_aiquant_rqalpha_execution_rules(true);
let aiquant_rule = aiquant_broker.buy_rule_check(date, &snapshot, &candidate, None);
assert!(!aiquant_rule.allowed);
assert_eq!(aiquant_rule.reason.as_deref(), Some("trade_disabled"));
assert_eq!(aiquant_rule.reason.as_deref(), Some("buy_disabled"));
let tradable_candidate = limit_test_candidate(true, true);
let aiquant_rule =
aiquant_broker.buy_rule_check(date, &snapshot, &tradable_candidate, None);
assert!(aiquant_rule.allowed);
let lower_limit_buyable_candidate = limit_test_candidate(true, false);
let aiquant_rule =
aiquant_broker.buy_rule_check(date, &snapshot, &lower_limit_buyable_candidate, None);
assert!(aiquant_rule.allowed);
}
#[test]
+7
View File
@@ -1237,6 +1237,13 @@ impl DataSet {
.unwrap_or(&[])
}
pub fn has_execution_quotes_on_date(&self, date: NaiveDate) -> bool {
self.execution_quotes_by_date
.get(&date)
.map(|rows_by_symbol| !rows_by_symbol.is_empty())
.unwrap_or(false)
}
pub fn execution_quote_key_set(&self) -> HashSet<(NaiveDate, String)> {
self.execution_quotes_by_date
.iter()
+113 -3
View File
@@ -1,6 +1,6 @@
use std::collections::{BTreeMap, BTreeSet};
use chrono::{Datelike, NaiveDate};
use chrono::{Datelike, NaiveDate, NaiveTime};
use serde::Serialize;
use thiserror::Error;
@@ -523,8 +523,35 @@ where
return Ok(());
}
let start_time = start_time.or_else(|| self.broker.intraday_execution_start_time());
let caller_start_time = start_time;
let caller_end_time = end_time;
let start_time = caller_start_time.or_else(|| self.broker.intraday_execution_start_time());
let mut symbols = execution_quote_symbols_for_decision(decision, portfolio, open_orders);
self.load_missing_execution_quotes(execution_date, start_time, end_time, &mut symbols)?;
if caller_start_time.is_none() && caller_end_time.is_none() {
for ((intent_start_time, intent_end_time), mut intent_symbols) in
algo_execution_quote_windows_for_decision(decision, portfolio)
{
self.load_missing_execution_quotes(
execution_date,
intent_start_time,
intent_end_time,
&mut intent_symbols,
)?;
}
}
Ok(())
}
fn load_missing_execution_quotes(
&mut self,
execution_date: NaiveDate,
start_time: Option<NaiveTime>,
end_time: Option<NaiveTime>,
symbols: &mut BTreeSet<String>,
) -> Result<(), BacktestError> {
symbols.retain(|symbol| {
!has_execution_quote_in_window(&self.data, execution_date, symbol, start_time, end_time)
});
@@ -536,7 +563,7 @@ where
date: execution_date,
start_time,
end_time,
symbols,
symbols: std::mem::take(symbols),
};
let quotes = self
.execution_quote_loader
@@ -547,6 +574,35 @@ where
Ok(())
}
fn ensure_execution_quotes_for_portfolio_times(
&mut self,
execution_date: NaiveDate,
portfolio: &PortfolioState,
quote_times: &[NaiveTime],
) -> Result<(), BacktestError> {
if self.execution_quote_loader.is_none() || quote_times.is_empty() {
return Ok(());
}
let base_symbols = portfolio
.positions()
.keys()
.cloned()
.collect::<BTreeSet<_>>();
if base_symbols.is_empty() {
return Ok(());
}
for quote_time in quote_times {
let mut symbols = base_symbols.clone();
self.load_missing_execution_quotes(
execution_date,
Some(*quote_time),
Some(*quote_time),
&mut symbols,
)?;
}
Ok(())
}
fn apply_strategy_directives(
&mut self,
execution_date: NaiveDate,
@@ -1875,6 +1931,12 @@ where
"on_day:pre",
)?;
let on_day_open_orders = self.open_order_views();
let decision_quote_times = self.strategy.decision_quote_times();
self.ensure_execution_quotes_for_portfolio_times(
execution_date,
&portfolio,
&decision_quote_times,
)?;
let mut decision = decision_slot
.map(|(decision_idx, decision_date)| {
self.strategy.on_day(&StrategyContext {
@@ -3283,6 +3345,54 @@ fn execution_quote_symbols_for_decision(
symbols
}
fn algo_execution_quote_windows_for_decision(
decision: &StrategyDecision,
portfolio: &PortfolioState,
) -> BTreeMap<(Option<NaiveTime>, Option<NaiveTime>), BTreeSet<String>> {
let mut groups = BTreeMap::<(Option<NaiveTime>, Option<NaiveTime>), BTreeSet<String>>::new();
for intent in &decision.order_intents {
match intent {
OrderIntent::AlgoValue {
symbol,
start_time,
end_time,
..
}
| OrderIntent::AlgoPercent {
symbol,
start_time,
end_time,
..
} => {
if start_time.is_some() || end_time.is_some() {
groups
.entry((*start_time, *end_time))
.or_default()
.insert(symbol.clone());
}
}
OrderIntent::TargetPortfolioSmart {
target_weights,
order_prices:
Some(TargetPortfolioOrderPricing::AlgoOrder {
start_time,
end_time,
..
}),
..
} => {
if start_time.is_some() || end_time.is_some() {
let symbols = groups.entry((*start_time, *end_time)).or_default();
symbols.extend(portfolio.positions().keys().cloned());
symbols.extend(target_weights.keys().cloned());
}
}
_ => {}
}
}
groups
}
fn collect_scheduled_decisions<S: Strategy>(
strategy: &mut S,
scheduler: &Scheduler<'_>,
File diff suppressed because it is too large Load Diff
+73 -1
View File
@@ -295,6 +295,12 @@ pub struct StrategyExpressionTradingConfig {
#[serde(default)]
pub retry_empty_rebalance: Option<bool>,
#[serde(default)]
pub delayed_limit_open_exit: Option<bool>,
#[serde(default)]
pub delayed_limit_open_exit_time: Option<String>,
#[serde(default)]
pub release_slot_on_exit_signal: Option<bool>,
#[serde(default)]
pub subscription_guard_required: Option<bool>,
#[serde(default)]
pub actions: Vec<StrategyExpressionActionConfig>,
@@ -815,6 +821,20 @@ pub fn platform_expr_config_from_spec(
if let Some(enabled) = trading.retry_empty_rebalance {
cfg.retry_empty_rebalance = enabled;
}
if let Some(enabled) = trading.release_slot_on_exit_signal {
cfg.release_slot_on_exit_signal = enabled;
}
if let Some(enabled) = trading.delayed_limit_open_exit {
cfg.delayed_limit_open_exit_enabled = enabled;
if enabled {
cfg.delayed_limit_open_exit_time = trading
.delayed_limit_open_exit_time
.as_deref()
.and_then(|value| parse_schedule_clock_time(Some(value)));
} else {
cfg.delayed_limit_open_exit_time = None;
}
}
if let Some(enabled) = spec
.engine_config
.as_ref()
@@ -945,7 +965,13 @@ pub fn platform_expr_config_from_spec(
if let Some(main_trade_time) = trade_times.last().copied() {
cfg.intraday_execution_time = Some(main_trade_time);
}
if aiquant_compat && trade_times.len() > 1 {
let delayed_limit_open_exit_explicit = spec
.runtime_expressions
.as_ref()
.and_then(|runtime_expr| runtime_expr.trading.as_ref())
.and_then(|trading| trading.delayed_limit_open_exit)
.is_some();
if aiquant_compat && !delayed_limit_open_exit_explicit && trade_times.len() > 1 {
let delayed_time = trade_times[0];
if trade_times
.last()
@@ -1565,4 +1591,50 @@ mod tests {
Some(NaiveTime::from_hms_opt(10, 31, 0).unwrap())
);
}
#[test]
fn parses_explicit_delayed_limit_open_exit() {
let spec = serde_json::json!({
"execution": { "compatibilityProfile": "aiquant_rqalpha" },
"runtimeExpressions": {
"schedule": { "frequency": "daily", "time": "10:40" },
"trading": {
"delayedLimitOpenExit": true,
"delayedLimitOpenExitTime": "10:31"
}
}
});
let cfg = platform_expr_config_from_value("", "", &spec).expect("config");
assert_eq!(
cfg.intraday_execution_time,
Some(NaiveTime::from_hms_opt(10, 40, 0).unwrap())
);
assert!(cfg.delayed_limit_open_exit_enabled);
assert_eq!(
cfg.delayed_limit_open_exit_time,
Some(NaiveTime::from_hms_opt(10, 31, 0).unwrap())
);
}
#[test]
fn explicit_delayed_limit_open_exit_false_overrides_aiquant_trade_times() {
let spec = serde_json::json!({
"execution": { "compatibilityProfile": "aiquant_rqalpha" },
"rebalance": { "tradeTimes": ["10:31", "10:40"] },
"runtimeExpressions": {
"schedule": { "frequency": "daily", "time": "10:40" },
"trading": {
"delayedLimitOpenExit": false,
"delayedLimitOpenExitTime": "10:31"
}
}
});
let cfg = platform_expr_config_from_value("", "", &spec).expect("config");
assert!(!cfg.delayed_limit_open_exit_enabled);
assert_eq!(cfg.delayed_limit_open_exit_time, None);
}
}
+16 -5
View File
@@ -41,6 +41,21 @@ impl ChinaAShareRiskControl {
candidate: &CandidateEligibility,
market: &DailyMarketSnapshot,
instrument: Option<&Instrument>,
) -> Option<&'static str> {
if let Some(reason) = Self::baseline_rejection_reason(date, candidate, market, instrument) {
return Some(reason);
}
if !candidate.allow_buy || !candidate.allow_sell {
return Some("trade_disabled");
}
None
}
pub fn baseline_rejection_reason(
date: NaiveDate,
candidate: &CandidateEligibility,
market: &DailyMarketSnapshot,
instrument: Option<&Instrument>,
) -> Option<&'static str> {
if let Some(reason) = Self::instrument_rejection_reason(instrument, date) {
return Some(reason);
@@ -60,9 +75,6 @@ impl ChinaAShareRiskControl {
if candidate.is_one_yuan || market.day_open <= 1.0 {
return Some("one_yuan");
}
if !candidate.allow_buy || !candidate.allow_sell {
return Some("trade_disabled");
}
None
}
@@ -73,8 +85,7 @@ impl ChinaAShareRiskControl {
instrument: Option<&Instrument>,
check_price: f64,
) -> Option<&'static str> {
if let Some(reason) = Self::selection_rejection_reason(date, candidate, market, instrument)
{
if let Some(reason) = Self::baseline_rejection_reason(date, candidate, market, instrument) {
return Some(reason);
}
if !candidate.allow_buy {
+3
View File
@@ -40,6 +40,9 @@ pub trait Strategy {
fn schedule_rules(&self) -> Vec<ScheduleRule> {
Vec::new()
}
fn decision_quote_times(&self) -> Vec<NaiveTime> {
Vec::new()
}
fn on_scheduled(
&mut self,
_ctx: &StrategyContext<'_>,
@@ -0,0 +1,224 @@
use chrono::{NaiveDate, NaiveTime};
use fidc_core::{
BacktestConfig, BacktestEngine, BenchmarkSnapshot, BrokerSimulator, CandidateEligibility,
ChinaAShareCostModel, ChinaEquityRuleHooks, DailyFactorSnapshot, DailyMarketSnapshot, DataSet,
IntradayExecutionQuote, MatchingType, OrderIntent, PriceField, Strategy, StrategyContext,
StrategyDecision,
};
fn d(year: i32, month: u32, day: u32) -> NaiveDate {
NaiveDate::from_ymd_opt(year, month, day).expect("valid date")
}
fn t(hour: u32, minute: u32, second: u32) -> NaiveTime {
NaiveTime::from_hms_opt(hour, minute, second).expect("valid time")
}
#[derive(Default)]
struct DecisionQuoteReader {
day_count: usize,
}
impl Strategy for DecisionQuoteReader {
fn name(&self) -> &str {
"decision_quote_reader"
}
fn decision_quote_times(&self) -> Vec<NaiveTime> {
vec![t(10, 40, 0)]
}
fn on_day(
&mut self,
ctx: &StrategyContext<'_>,
) -> Result<StrategyDecision, fidc_core::BacktestError> {
self.day_count += 1;
if self.day_count == 1 {
return Ok(StrategyDecision {
order_intents: vec![OrderIntent::Value {
symbol: "000001.SZ".to_string(),
value: 5_000.0,
reason: "seed_position".to_string(),
}],
..StrategyDecision::default()
});
}
assert!(
ctx.portfolio.position("000001.SZ").is_some(),
"second day should carry the first day position"
);
let quote_loaded_before_decision = ctx
.data
.execution_quotes_on(ctx.execution_date, "000001.SZ")
.iter()
.any(|quote| quote.timestamp.time() == t(10, 40, 0) && quote.last_price == 11.0);
assert!(
quote_loaded_before_decision,
"engine must load declared decision quote before strategy.on_day"
);
Ok(StrategyDecision::default())
}
}
#[test]
fn engine_preloads_declared_decision_quotes_for_current_positions() {
let first = d(2026, 1, 5);
let second = d(2026, 1, 6);
let data = DataSet::from_components(
Vec::new(),
vec![
DailyMarketSnapshot {
date: first,
symbol: "000001.SZ".to_string(),
timestamp: Some("2026-01-05 15:00:00".to_string()),
day_open: 10.0,
open: 10.0,
high: 10.2,
low: 9.9,
close: 10.0,
last_price: 10.0,
bid1: 10.0,
ask1: 10.0,
prev_close: 9.8,
volume: 10_000,
tick_volume: 1_000,
bid1_volume: 10_000,
ask1_volume: 10_000,
trading_phase: Some("continuous".to_string()),
paused: false,
upper_limit: 10.78,
lower_limit: 8.82,
price_tick: 0.01,
},
DailyMarketSnapshot {
date: second,
symbol: "000001.SZ".to_string(),
timestamp: Some("2026-01-06 15:00:00".to_string()),
day_open: 10.5,
open: 10.5,
high: 11.2,
low: 10.4,
close: 10.6,
last_price: 10.6,
bid1: 10.6,
ask1: 10.6,
prev_close: 10.0,
volume: 10_000,
tick_volume: 1_000,
bid1_volume: 10_000,
ask1_volume: 10_000,
trading_phase: Some("continuous".to_string()),
paused: false,
upper_limit: 11.0,
lower_limit: 9.0,
price_tick: 0.01,
},
],
vec![
DailyFactorSnapshot {
date: first,
symbol: "000001.SZ".to_string(),
market_cap_bn: 10.0,
free_float_cap_bn: 10.0,
pe_ttm: 10.0,
turnover_ratio: None,
effective_turnover_ratio: None,
extra_factors: Default::default(),
},
DailyFactorSnapshot {
date: second,
symbol: "000001.SZ".to_string(),
market_cap_bn: 10.0,
free_float_cap_bn: 10.0,
pe_ttm: 10.0,
turnover_ratio: None,
effective_turnover_ratio: None,
extra_factors: Default::default(),
},
],
vec![
CandidateEligibility {
date: first,
symbol: "000001.SZ".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,
},
CandidateEligibility {
date: second,
symbol: "000001.SZ".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,
},
],
vec![
BenchmarkSnapshot {
date: first,
benchmark: "000852.SH".to_string(),
open: 1000.0,
close: 1000.0,
prev_close: 990.0,
volume: 1_000_000,
},
BenchmarkSnapshot {
date: second,
benchmark: "000852.SH".to_string(),
open: 1000.0,
close: 1001.0,
prev_close: 1000.0,
volume: 1_000_000,
},
],
)
.expect("dataset");
let broker = BrokerSimulator::new_with_execution_price(
ChinaAShareCostModel::default(),
ChinaEquityRuleHooks,
PriceField::Last,
)
.with_matching_type(MatchingType::NextTickLast)
.with_intraday_execution_start_time(t(10, 40, 0));
let config = BacktestConfig {
initial_cash: 10_000.0,
benchmark_code: "000852.SH".to_string(),
start_date: Some(first),
end_date: Some(second),
decision_lag_trading_days: 0,
execution_price_field: PriceField::Last,
};
let mut engine = BacktestEngine::new(data, DecisionQuoteReader::default(), broker, config)
.with_execution_quote_loader(move |request| {
Ok(request
.symbols
.into_iter()
.map(|symbol| IntradayExecutionQuote {
date: request.date,
symbol,
timestamp: request
.date
.and_time(request.start_time.unwrap_or(t(10, 40, 0))),
last_price: if request.date == second { 11.0 } else { 10.0 },
bid1: if request.date == second { 11.0 } else { 10.0 },
ask1: if request.date == second { 11.0 } else { 10.0 },
bid1_volume: 10_000,
ask1_volume: 10_000,
volume_delta: 10_000,
amount_delta: 100_000.0,
trading_phase: Some("continuous".to_string()),
})
.collect())
});
engine.run().expect("backtest should run");
}