Expose open order state to strategy runtime
This commit is contained in:
@@ -232,12 +232,14 @@ where
|
||||
.map(|decision_idx| (decision_idx, execution_dates[decision_idx]));
|
||||
let (decision_index, decision_date) =
|
||||
decision_slot.unwrap_or((execution_idx, execution_date));
|
||||
let pre_open_orders = self.broker.open_order_views();
|
||||
let daily_context = StrategyContext {
|
||||
execution_date,
|
||||
decision_date,
|
||||
decision_index,
|
||||
data: &self.data,
|
||||
portfolio: &portfolio,
|
||||
open_orders: &pre_open_orders,
|
||||
};
|
||||
let schedule_rules = self.strategy.schedule_rules();
|
||||
let mut process_events = Vec::new();
|
||||
@@ -304,12 +306,14 @@ where
|
||||
&self.data,
|
||||
&auction_decision,
|
||||
)?;
|
||||
let post_auction_open_orders = self.broker.open_order_views();
|
||||
let post_auction_context = StrategyContext {
|
||||
execution_date,
|
||||
decision_date,
|
||||
decision_index,
|
||||
data: &self.data,
|
||||
portfolio: &portfolio,
|
||||
open_orders: &post_auction_open_orders,
|
||||
};
|
||||
publish_process_events(
|
||||
&mut self.strategy,
|
||||
@@ -337,6 +341,7 @@ where
|
||||
ProcessEventKind::PreOnDay,
|
||||
"on_day:pre",
|
||||
)?;
|
||||
let on_day_open_orders = self.broker.open_order_views();
|
||||
let mut decision = decision_slot
|
||||
.map(|(decision_idx, decision_date)| {
|
||||
self.strategy.on_day(&StrategyContext {
|
||||
@@ -345,6 +350,7 @@ where
|
||||
decision_index: decision_idx,
|
||||
data: &self.data,
|
||||
portfolio: &portfolio,
|
||||
open_orders: &on_day_open_orders,
|
||||
})
|
||||
})
|
||||
.transpose()?
|
||||
@@ -361,6 +367,7 @@ where
|
||||
decision_index,
|
||||
data: &self.data,
|
||||
portfolio: &portfolio,
|
||||
open_orders: &on_day_open_orders,
|
||||
},
|
||||
&mut process_events,
|
||||
&mut self.process_event_bus,
|
||||
@@ -371,6 +378,7 @@ where
|
||||
decision_index,
|
||||
data: &self.data,
|
||||
portfolio: &portfolio,
|
||||
open_orders: &on_day_open_orders,
|
||||
};
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
@@ -385,12 +393,14 @@ where
|
||||
let mut intraday_report =
|
||||
self.broker
|
||||
.execute(execution_date, &mut portfolio, &self.data, &decision)?;
|
||||
let post_intraday_open_orders = self.broker.open_order_views();
|
||||
let post_intraday_context = StrategyContext {
|
||||
execution_date,
|
||||
decision_date,
|
||||
decision_index,
|
||||
data: &self.data,
|
||||
portfolio: &portfolio,
|
||||
open_orders: &post_intraday_open_orders,
|
||||
};
|
||||
publish_process_events(
|
||||
&mut self.strategy,
|
||||
@@ -418,12 +428,14 @@ where
|
||||
|
||||
portfolio.update_prices(execution_date, &self.data, PriceField::Close)?;
|
||||
|
||||
let post_trade_open_orders = self.broker.open_order_views();
|
||||
let post_trade_context = StrategyContext {
|
||||
execution_date,
|
||||
decision_date,
|
||||
decision_index,
|
||||
data: &self.data,
|
||||
portfolio: &portfolio,
|
||||
open_orders: &post_trade_open_orders,
|
||||
};
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
@@ -457,10 +469,19 @@ where
|
||||
report.position_events.extend(close_report.position_events);
|
||||
report.account_events.extend(close_report.account_events);
|
||||
report.diagnostics.extend(close_report.diagnostics);
|
||||
let post_close_open_orders = self.broker.open_order_views();
|
||||
let post_close_context = StrategyContext {
|
||||
execution_date,
|
||||
decision_date,
|
||||
decision_index,
|
||||
data: &self.data,
|
||||
portfolio: &portfolio,
|
||||
open_orders: &post_close_open_orders,
|
||||
};
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&post_trade_context,
|
||||
&post_close_context,
|
||||
&mut process_events,
|
||||
execution_date,
|
||||
ProcessEventKind::PostAfterTrading,
|
||||
@@ -469,17 +490,17 @@ where
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&post_trade_context,
|
||||
&post_close_context,
|
||||
&mut process_events,
|
||||
execution_date,
|
||||
ProcessEventKind::PreSettlement,
|
||||
"settlement:pre",
|
||||
)?;
|
||||
self.strategy.on_settlement(&post_trade_context)?;
|
||||
self.strategy.on_settlement(&post_close_context)?;
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&post_trade_context,
|
||||
&post_close_context,
|
||||
&mut process_events,
|
||||
execution_date,
|
||||
ProcessEventKind::Settlement,
|
||||
@@ -488,7 +509,7 @@ where
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&post_trade_context,
|
||||
&post_close_context,
|
||||
&mut process_events,
|
||||
execution_date,
|
||||
ProcessEventKind::PostSettlement,
|
||||
|
||||
Reference in New Issue
Block a user