修复AiQuant策略表达式回测执行语义
This commit is contained in:
@@ -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]
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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<'_>,
|
||||
|
||||
Reference in New Issue
Block a user