跳过无业务分钟回调
This commit is contained in:
@@ -384,6 +384,10 @@ impl<C, R> BrokerSimulator<C, R> {
|
|||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn has_open_orders(&self) -> bool {
|
||||||
|
!self.open_orders.borrow().is_empty()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<C, R> BrokerSimulator<C, R>
|
impl<C, R> BrokerSimulator<C, R>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use std::collections::{BTreeMap, BTreeSet};
|
use std::collections::{BTreeMap, BTreeSet};
|
||||||
|
|
||||||
use chrono::{Datelike, Duration, NaiveDate, NaiveTime};
|
use chrono::{Datelike, Duration, NaiveDate, NaiveTime, Timelike};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
@@ -1086,6 +1086,10 @@ where
|
|||||||
views
|
views
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn has_open_orders(&self) -> bool {
|
||||||
|
self.broker.has_open_orders() || !self.futures_open_orders.is_empty()
|
||||||
|
}
|
||||||
|
|
||||||
fn aggregate_initial_cash(&self) -> f64 {
|
fn aggregate_initial_cash(&self) -> f64 {
|
||||||
self.config.initial_cash
|
self.config.initial_cash
|
||||||
+ self
|
+ self
|
||||||
@@ -2486,6 +2490,20 @@ where
|
|||||||
!filter_by_subscription || self.subscriptions.contains("e.symbol)
|
!filter_by_subscription || self.subscriptions.contains("e.symbol)
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
let requires_minute_callbacks = self.strategy.requires_minute_callbacks();
|
||||||
|
let has_minute_process_listeners = self.process_event_bus.has_listeners_for(&[
|
||||||
|
ProcessEventKind::PreMinute,
|
||||||
|
ProcessEventKind::Minute,
|
||||||
|
ProcessEventKind::PostMinute,
|
||||||
|
]);
|
||||||
|
let minute_schedule_all_times = schedule_rules
|
||||||
|
.iter()
|
||||||
|
.any(|rule| rule.stage == ScheduleStage::Minute && rule.time_rule.is_none());
|
||||||
|
let minute_schedule_minutes = schedule_rules
|
||||||
|
.iter()
|
||||||
|
.filter(|rule| rule.stage == ScheduleStage::Minute)
|
||||||
|
.filter_map(|rule| rule.time_rule.as_ref()?.minute_of_day())
|
||||||
|
.collect::<BTreeSet<_>>();
|
||||||
let mut minute_cursor = 0usize;
|
let mut minute_cursor = 0usize;
|
||||||
while minute_cursor < minute_quotes.len() {
|
while minute_cursor < minute_quotes.len() {
|
||||||
let minute_timestamp = minute_quotes[minute_cursor].timestamp;
|
let minute_timestamp = minute_quotes[minute_cursor].timestamp;
|
||||||
@@ -2497,6 +2515,17 @@ where
|
|||||||
minute_end += 1;
|
minute_end += 1;
|
||||||
}
|
}
|
||||||
let minute_group = &minute_quotes[minute_cursor..minute_end];
|
let minute_group = &minute_quotes[minute_cursor..minute_end];
|
||||||
|
let schedule_candidate = minute_schedule_all_times
|
||||||
|
|| minute_schedule_minutes
|
||||||
|
.contains(&(minute_time.hour() * 60 + minute_time.minute()));
|
||||||
|
if !requires_minute_callbacks
|
||||||
|
&& !has_minute_process_listeners
|
||||||
|
&& !schedule_candidate
|
||||||
|
&& !self.has_open_orders()
|
||||||
|
{
|
||||||
|
minute_cursor = minute_end;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
let minute_open_orders = self.open_order_views();
|
let minute_open_orders = self.open_order_views();
|
||||||
publish_phase_event(
|
publish_phase_event(
|
||||||
&mut self.strategy,
|
&mut self.strategy,
|
||||||
@@ -2535,6 +2564,7 @@ where
|
|||||||
result.order_events.as_slice(),
|
result.order_events.as_slice(),
|
||||||
result.fills.as_slice(),
|
result.fills.as_slice(),
|
||||||
)?;
|
)?;
|
||||||
|
if requires_minute_callbacks {
|
||||||
for quote in minute_group {
|
for quote in minute_group {
|
||||||
minute_decision.merge_from(self.strategy.on_minute(
|
minute_decision.merge_from(self.strategy.on_minute(
|
||||||
&StrategyContext {
|
&StrategyContext {
|
||||||
@@ -2556,6 +2586,7 @@ where
|
|||||||
quote,
|
quote,
|
||||||
)?);
|
)?);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
publish_phase_event(
|
publish_phase_event(
|
||||||
&mut self.strategy,
|
&mut self.strategy,
|
||||||
&mut self.process_event_bus,
|
&mut self.process_event_bus,
|
||||||
|
|||||||
@@ -125,6 +125,15 @@ impl ProcessEventBus {
|
|||||||
loader.install_enabled(self, enabled_names)
|
loader.install_enabled(self, enabled_names)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn has_listeners_for(&self, kinds: &[ProcessEventKind]) -> bool {
|
||||||
|
!self.any_listeners.is_empty()
|
||||||
|
|| kinds.iter().any(|kind| {
|
||||||
|
self.listeners
|
||||||
|
.get(kind)
|
||||||
|
.is_some_and(|listeners| !listeners.is_empty())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
pub fn publish(&mut self, event: &ProcessEvent) {
|
pub fn publish(&mut self, event: &ProcessEvent) {
|
||||||
if let Some(listeners) = self.listeners.get_mut(&event.kind) {
|
if let Some(listeners) = self.listeners.get_mut(&event.kind) {
|
||||||
for listener in listeners {
|
for listener in listeners {
|
||||||
|
|||||||
@@ -10057,6 +10057,10 @@ impl Strategy for PlatformExprStrategy {
|
|||||||
self.config.initial_subscriptions.clone()
|
self.config.initial_subscriptions.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn requires_minute_callbacks(&self) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
fn schedule_rules(&self) -> Vec<ScheduleRule> {
|
fn schedule_rules(&self) -> Vec<ScheduleRule> {
|
||||||
if self.config.explicit_action_stage != PlatformExplicitActionStage::Minute {
|
if self.config.explicit_action_stage != PlatformExplicitActionStage::Minute {
|
||||||
return Vec::new();
|
return Vec::new();
|
||||||
|
|||||||
@@ -22,6 +22,9 @@ pub trait Strategy {
|
|||||||
fn initial_subscriptions(&self) -> BTreeSet<String> {
|
fn initial_subscriptions(&self) -> BTreeSet<String> {
|
||||||
BTreeSet::new()
|
BTreeSet::new()
|
||||||
}
|
}
|
||||||
|
fn requires_minute_callbacks(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
fn management_fee(
|
fn management_fee(
|
||||||
&mut self,
|
&mut self,
|
||||||
_ctx: &StrategyContext<'_>,
|
_ctx: &StrategyContext<'_>,
|
||||||
|
|||||||
@@ -637,6 +637,7 @@ struct MinuteProbeStrategy {
|
|||||||
seen_ticks: Rc<RefCell<Vec<String>>>,
|
seen_ticks: Rc<RefCell<Vec<String>>>,
|
||||||
scheduled_count: Rc<RefCell<usize>>,
|
scheduled_count: Rc<RefCell<usize>>,
|
||||||
subscribe_symbols: BTreeSet<String>,
|
subscribe_symbols: BTreeSet<String>,
|
||||||
|
minute_callbacks: bool,
|
||||||
ordered: bool,
|
ordered: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -812,6 +813,10 @@ impl Strategy for MinuteProbeStrategy {
|
|||||||
"minute-probe"
|
"minute-probe"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn requires_minute_callbacks(&self) -> bool {
|
||||||
|
self.minute_callbacks
|
||||||
|
}
|
||||||
|
|
||||||
fn schedule_rules(&self) -> Vec<ScheduleRule> {
|
fn schedule_rules(&self) -> Vec<ScheduleRule> {
|
||||||
vec![
|
vec![
|
||||||
ScheduleRule::daily("minute_barrier", ScheduleStage::Minute)
|
ScheduleRule::daily("minute_barrier", ScheduleStage::Minute)
|
||||||
@@ -2066,6 +2071,7 @@ fn engine_runs_minute_hooks_and_executes_minute_orders() {
|
|||||||
seen_ticks: seen_ticks.clone(),
|
seen_ticks: seen_ticks.clone(),
|
||||||
scheduled_count: scheduled_count.clone(),
|
scheduled_count: scheduled_count.clone(),
|
||||||
subscribe_symbols: BTreeSet::from(["000001.SZ".to_string(), "000002.SZ".to_string()]),
|
subscribe_symbols: BTreeSet::from(["000001.SZ".to_string(), "000002.SZ".to_string()]),
|
||||||
|
minute_callbacks: true,
|
||||||
ordered: false,
|
ordered: false,
|
||||||
};
|
};
|
||||||
let broker = BrokerSimulator::new_with_execution_price(
|
let broker = BrokerSimulator::new_with_execution_price(
|
||||||
@@ -2143,6 +2149,82 @@ fn engine_runs_minute_hooks_and_executes_minute_orders() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn engine_skips_empty_platform_style_minute_callbacks_between_schedule_times() {
|
||||||
|
let date = d(2025, 1, 2);
|
||||||
|
let mut data = single_day_anchor_data(date);
|
||||||
|
data.add_execution_quotes(vec![
|
||||||
|
IntradayExecutionQuote {
|
||||||
|
date,
|
||||||
|
symbol: "000001.SZ".to_string(),
|
||||||
|
timestamp: dt(2025, 1, 2, 10, 18, 0),
|
||||||
|
last_price: 10.2,
|
||||||
|
bid1: 10.1,
|
||||||
|
ask1: 10.2,
|
||||||
|
bid1_volume: 1_000,
|
||||||
|
ask1_volume: 1_000,
|
||||||
|
volume_delta: 1_000,
|
||||||
|
amount_delta: 10_200.0,
|
||||||
|
trading_phase: Some("continuous".to_string()),
|
||||||
|
},
|
||||||
|
IntradayExecutionQuote {
|
||||||
|
date,
|
||||||
|
symbol: "000001.SZ".to_string(),
|
||||||
|
timestamp: dt(2025, 1, 2, 10, 19, 0),
|
||||||
|
last_price: 10.3,
|
||||||
|
bid1: 10.2,
|
||||||
|
ask1: 10.3,
|
||||||
|
bid1_volume: 1_000,
|
||||||
|
ask1_volume: 1_000,
|
||||||
|
volume_delta: 1_000,
|
||||||
|
amount_delta: 10_300.0,
|
||||||
|
trading_phase: Some("continuous".to_string()),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
let seen_ticks = Rc::new(RefCell::new(Vec::new()));
|
||||||
|
let scheduled_count = Rc::new(RefCell::new(0usize));
|
||||||
|
let strategy = MinuteProbeStrategy {
|
||||||
|
seen_ticks: seen_ticks.clone(),
|
||||||
|
scheduled_count: scheduled_count.clone(),
|
||||||
|
subscribe_symbols: BTreeSet::from(["000001.SZ".to_string()]),
|
||||||
|
minute_callbacks: false,
|
||||||
|
ordered: false,
|
||||||
|
};
|
||||||
|
let broker = BrokerSimulator::new_with_execution_price(
|
||||||
|
ChinaAShareCostModel::default(),
|
||||||
|
ChinaEquityRuleHooks::default(),
|
||||||
|
PriceField::Last,
|
||||||
|
);
|
||||||
|
let mut engine = BacktestEngine::new(
|
||||||
|
data,
|
||||||
|
strategy,
|
||||||
|
broker,
|
||||||
|
BacktestConfig {
|
||||||
|
initial_cash: 10_000.0,
|
||||||
|
benchmark_code: "000300.SH".to_string(),
|
||||||
|
start_date: Some(date),
|
||||||
|
end_date: Some(date),
|
||||||
|
decision_lag_trading_days: 0,
|
||||||
|
execution_price_field: PriceField::Last,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.with_execution_quote_loader(|_| Ok(Vec::new()));
|
||||||
|
|
||||||
|
let result = engine.run().expect("scheduled-only minute run");
|
||||||
|
|
||||||
|
assert!(seen_ticks.borrow().is_empty());
|
||||||
|
assert_eq!(*scheduled_count.borrow(), 1);
|
||||||
|
assert!(result.fills.is_empty());
|
||||||
|
assert_eq!(
|
||||||
|
result
|
||||||
|
.process_events
|
||||||
|
.iter()
|
||||||
|
.filter(|event| event.kind == ProcessEventKind::PreMinute)
|
||||||
|
.count(),
|
||||||
|
1
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn strategy_context_exposes_engine_native_data_helpers() {
|
fn strategy_context_exposes_engine_native_data_helpers() {
|
||||||
let date1 = d(2025, 1, 2);
|
let date1 = d(2025, 1, 2);
|
||||||
|
|||||||
Reference in New Issue
Block a user