Add process event bus to backtest engine
This commit is contained in:
@@ -5,6 +5,7 @@ use thiserror::Error;
|
||||
use crate::broker::{BrokerExecutionReport, BrokerSimulator};
|
||||
use crate::cost::CostModel;
|
||||
use crate::data::{BenchmarkSnapshot, DataSet, DataSetError, PriceField};
|
||||
use crate::event_bus::ProcessEventBus;
|
||||
use crate::events::{
|
||||
AccountEvent, FillEvent, OrderEvent, OrderSide, OrderStatus, PositionEvent, ProcessEvent,
|
||||
ProcessEventKind,
|
||||
@@ -95,6 +96,7 @@ pub struct BacktestEngine<S, C, R> {
|
||||
broker: BrokerSimulator<C, R>,
|
||||
config: BacktestConfig,
|
||||
dividend_reinvestment: bool,
|
||||
process_event_bus: ProcessEventBus,
|
||||
}
|
||||
|
||||
impl<S, C, R> BacktestEngine<S, C, R> {
|
||||
@@ -110,6 +112,7 @@ impl<S, C, R> BacktestEngine<S, C, R> {
|
||||
broker,
|
||||
config,
|
||||
dividend_reinvestment: false,
|
||||
process_event_bus: ProcessEventBus::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,6 +120,24 @@ impl<S, C, R> BacktestEngine<S, C, R> {
|
||||
self.dividend_reinvestment = enabled;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn process_event_bus_mut(&mut self) -> &mut ProcessEventBus {
|
||||
&mut self.process_event_bus
|
||||
}
|
||||
|
||||
pub fn add_process_listener<F>(&mut self, kind: ProcessEventKind, listener: F)
|
||||
where
|
||||
F: FnMut(&ProcessEvent) + 'static,
|
||||
{
|
||||
self.process_event_bus.add_listener(kind, listener);
|
||||
}
|
||||
|
||||
pub fn add_any_process_listener<F>(&mut self, listener: F)
|
||||
where
|
||||
F: FnMut(&ProcessEvent) + 'static,
|
||||
{
|
||||
self.process_event_bus.add_any_listener(listener);
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, C, R> BacktestEngine<S, C, R>
|
||||
@@ -222,6 +243,7 @@ where
|
||||
let mut process_events = Vec::new();
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&daily_context,
|
||||
&mut process_events,
|
||||
execution_date,
|
||||
@@ -231,6 +253,7 @@ where
|
||||
self.strategy.before_trading(&daily_context)?;
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&daily_context,
|
||||
&mut process_events,
|
||||
execution_date,
|
||||
@@ -239,6 +262,7 @@ where
|
||||
)?;
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&daily_context,
|
||||
&mut process_events,
|
||||
execution_date,
|
||||
@@ -247,6 +271,7 @@ where
|
||||
)?;
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&daily_context,
|
||||
&mut process_events,
|
||||
execution_date,
|
||||
@@ -261,10 +286,12 @@ where
|
||||
&schedule_rules,
|
||||
&daily_context,
|
||||
&mut process_events,
|
||||
&mut self.process_event_bus,
|
||||
)?;
|
||||
auction_decision.merge_from(self.strategy.open_auction(&daily_context)?);
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&daily_context,
|
||||
&mut process_events,
|
||||
execution_date,
|
||||
@@ -286,12 +313,14 @@ where
|
||||
};
|
||||
publish_process_events(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&post_auction_context,
|
||||
&mut process_events,
|
||||
&mut report.process_events,
|
||||
)?;
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&post_auction_context,
|
||||
&mut process_events,
|
||||
execution_date,
|
||||
@@ -301,6 +330,7 @@ where
|
||||
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&post_auction_context,
|
||||
&mut process_events,
|
||||
execution_date,
|
||||
@@ -333,6 +363,7 @@ where
|
||||
portfolio: &portfolio,
|
||||
},
|
||||
&mut process_events,
|
||||
&mut self.process_event_bus,
|
||||
)?);
|
||||
let on_day_context = StrategyContext {
|
||||
execution_date,
|
||||
@@ -343,6 +374,7 @@ where
|
||||
};
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&on_day_context,
|
||||
&mut process_events,
|
||||
execution_date,
|
||||
@@ -362,6 +394,7 @@ where
|
||||
};
|
||||
publish_process_events(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&post_intraday_context,
|
||||
&mut process_events,
|
||||
&mut intraday_report.process_events,
|
||||
@@ -375,6 +408,7 @@ where
|
||||
report.diagnostics.extend(intraday_report.diagnostics);
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&post_intraday_context,
|
||||
&mut process_events,
|
||||
execution_date,
|
||||
@@ -393,6 +427,7 @@ where
|
||||
};
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&post_trade_context,
|
||||
&mut process_events,
|
||||
execution_date,
|
||||
@@ -402,6 +437,7 @@ where
|
||||
self.strategy.after_trading(&post_trade_context)?;
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&post_trade_context,
|
||||
&mut process_events,
|
||||
execution_date,
|
||||
@@ -411,6 +447,7 @@ where
|
||||
let mut close_report = self.broker.after_trading(execution_date);
|
||||
publish_process_events(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&post_trade_context,
|
||||
&mut process_events,
|
||||
&mut close_report.process_events,
|
||||
@@ -422,6 +459,7 @@ where
|
||||
report.diagnostics.extend(close_report.diagnostics);
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&post_trade_context,
|
||||
&mut process_events,
|
||||
execution_date,
|
||||
@@ -430,6 +468,7 @@ where
|
||||
)?;
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&post_trade_context,
|
||||
&mut process_events,
|
||||
execution_date,
|
||||
@@ -439,6 +478,7 @@ where
|
||||
self.strategy.on_settlement(&post_trade_context)?;
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&post_trade_context,
|
||||
&mut process_events,
|
||||
execution_date,
|
||||
@@ -447,6 +487,7 @@ where
|
||||
)?;
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&post_trade_context,
|
||||
&mut process_events,
|
||||
execution_date,
|
||||
@@ -945,11 +986,13 @@ fn collect_scheduled_decisions<S: Strategy>(
|
||||
rules: &[ScheduleRule],
|
||||
ctx: &StrategyContext<'_>,
|
||||
process_events: &mut Vec<ProcessEvent>,
|
||||
process_event_bus: &mut ProcessEventBus,
|
||||
) -> Result<crate::strategy::StrategyDecision, BacktestError> {
|
||||
let mut combined = crate::strategy::StrategyDecision::default();
|
||||
for rule in scheduler.triggered_rules(execution_date, stage, rules) {
|
||||
publish_phase_event(
|
||||
strategy,
|
||||
process_event_bus,
|
||||
ctx,
|
||||
process_events,
|
||||
execution_date,
|
||||
@@ -959,6 +1002,7 @@ fn collect_scheduled_decisions<S: Strategy>(
|
||||
combined.merge_from(strategy.on_scheduled(ctx, rule)?);
|
||||
publish_phase_event(
|
||||
strategy,
|
||||
process_event_bus,
|
||||
ctx,
|
||||
process_events,
|
||||
execution_date,
|
||||
@@ -971,6 +1015,7 @@ fn collect_scheduled_decisions<S: Strategy>(
|
||||
|
||||
fn publish_phase_event<S: Strategy>(
|
||||
strategy: &mut S,
|
||||
process_event_bus: &mut ProcessEventBus,
|
||||
ctx: &StrategyContext<'_>,
|
||||
events: &mut Vec<ProcessEvent>,
|
||||
date: NaiveDate,
|
||||
@@ -985,6 +1030,7 @@ fn publish_phase_event<S: Strategy>(
|
||||
side: None,
|
||||
detail: detail.into(),
|
||||
};
|
||||
process_event_bus.publish(&event);
|
||||
strategy.on_process_event(ctx, &event)?;
|
||||
events.push(event);
|
||||
Ok(())
|
||||
@@ -992,11 +1038,13 @@ fn publish_phase_event<S: Strategy>(
|
||||
|
||||
fn publish_process_events<S: Strategy>(
|
||||
strategy: &mut S,
|
||||
process_event_bus: &mut ProcessEventBus,
|
||||
ctx: &StrategyContext<'_>,
|
||||
target: &mut Vec<ProcessEvent>,
|
||||
incoming: &mut Vec<ProcessEvent>,
|
||||
) -> Result<(), BacktestError> {
|
||||
for event in incoming.drain(..) {
|
||||
process_event_bus.publish(&event);
|
||||
strategy.on_process_event(ctx, &event)?;
|
||||
target.push(event);
|
||||
}
|
||||
|
||||
55
crates/fidc-core/src/event_bus.rs
Normal file
55
crates/fidc-core/src/event_bus.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use crate::events::{ProcessEvent, ProcessEventKind};
|
||||
|
||||
type ProcessEventListener = Box<dyn FnMut(&ProcessEvent)>;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ProcessEventBus {
|
||||
listeners: BTreeMap<ProcessEventKind, Vec<ProcessEventListener>>,
|
||||
any_listeners: Vec<ProcessEventListener>,
|
||||
}
|
||||
|
||||
impl ProcessEventBus {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn add_listener<F>(&mut self, kind: ProcessEventKind, listener: F)
|
||||
where
|
||||
F: FnMut(&ProcessEvent) + 'static,
|
||||
{
|
||||
self.listeners
|
||||
.entry(kind)
|
||||
.or_default()
|
||||
.push(Box::new(listener));
|
||||
}
|
||||
|
||||
pub fn prepend_listener<F>(&mut self, kind: ProcessEventKind, listener: F)
|
||||
where
|
||||
F: FnMut(&ProcessEvent) + 'static,
|
||||
{
|
||||
self.listeners
|
||||
.entry(kind)
|
||||
.or_default()
|
||||
.insert(0, Box::new(listener));
|
||||
}
|
||||
|
||||
pub fn add_any_listener<F>(&mut self, listener: F)
|
||||
where
|
||||
F: FnMut(&ProcessEvent) + 'static,
|
||||
{
|
||||
self.any_listeners.push(Box::new(listener));
|
||||
}
|
||||
|
||||
pub fn publish(&mut self, event: &ProcessEvent) {
|
||||
if let Some(listeners) = self.listeners.get_mut(&event.kind) {
|
||||
for listener in listeners {
|
||||
listener(event);
|
||||
}
|
||||
}
|
||||
for listener in &mut self.any_listeners {
|
||||
listener(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -91,7 +91,7 @@ pub struct AccountEvent {
|
||||
pub note: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub enum ProcessEventKind {
|
||||
PreBeforeTrading,
|
||||
BeforeTrading,
|
||||
|
||||
@@ -3,6 +3,7 @@ pub mod calendar;
|
||||
pub mod cost;
|
||||
pub mod data;
|
||||
pub mod engine;
|
||||
pub mod event_bus;
|
||||
pub mod events;
|
||||
pub mod instrument;
|
||||
pub mod metrics;
|
||||
@@ -26,6 +27,7 @@ pub use engine::{
|
||||
BacktestConfig, BacktestDayProgress, BacktestEngine, BacktestError, BacktestResult,
|
||||
DailyEquityPoint,
|
||||
};
|
||||
pub use event_bus::ProcessEventBus;
|
||||
pub use events::{
|
||||
AccountEvent, FillEvent, OrderEvent, OrderSide, OrderStatus, PositionEvent, ProcessEvent,
|
||||
ProcessEventKind,
|
||||
|
||||
Reference in New Issue
Block a user