Expose futures account runtime view

This commit is contained in:
boris
2026-04-23 20:35:32 -07:00
parent 2669350154
commit db4e385308
7 changed files with 191 additions and 9 deletions

View File

@@ -12,6 +12,7 @@ use crate::events::{
AccountEvent, FillEvent, OrderEvent, OrderSide, OrderStatus, PositionEvent, ProcessEvent,
ProcessEventKind,
};
use crate::futures::FuturesAccountState;
use crate::metrics::{BacktestMetrics, compute_backtest_metrics};
use crate::portfolio::{CashReceivable, HoldingSummary, PortfolioState};
use crate::rules::EquityRuleHooks;
@@ -101,6 +102,7 @@ pub struct BacktestEngine<S, C, R> {
process_event_bus: ProcessEventBus,
dynamic_universe: Option<BTreeSet<String>>,
subscriptions: BTreeSet<String>,
futures_account: Option<FuturesAccountState>,
}
impl<S, C, R> BacktestEngine<S, C, R> {
@@ -119,6 +121,7 @@ impl<S, C, R> BacktestEngine<S, C, R> {
process_event_bus: ProcessEventBus::new(),
dynamic_universe: None,
subscriptions: BTreeSet::new(),
futures_account: None,
}
}
@@ -127,6 +130,23 @@ impl<S, C, R> BacktestEngine<S, C, R> {
self
}
pub fn with_futures_account(mut self, account: FuturesAccountState) -> Self {
self.futures_account = Some(account);
self
}
pub fn with_futures_initial_cash(self, initial_cash: f64) -> Self {
self.with_futures_account(FuturesAccountState::new(initial_cash))
}
pub fn futures_account(&self) -> Option<&FuturesAccountState> {
self.futures_account.as_ref()
}
pub fn futures_account_mut(&mut self) -> Option<&mut FuturesAccountState> {
self.futures_account.as_mut()
}
pub fn process_event_bus_mut(&mut self) -> &mut ProcessEventBus {
&mut self.process_event_bus
}
@@ -184,6 +204,7 @@ where
decision_index,
&self.data,
portfolio,
self.futures_account.as_ref(),
open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -224,6 +245,7 @@ where
decision_index,
&self.data,
portfolio,
self.futures_account.as_ref(),
open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -264,6 +286,7 @@ where
decision_index,
&self.data,
portfolio,
self.futures_account.as_ref(),
open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -333,6 +356,7 @@ where
decision_index,
&self.data,
&*portfolio,
self.futures_account.as_ref(),
open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -378,6 +402,7 @@ where
decision_index,
&self.data,
&*portfolio,
self.futures_account.as_ref(),
open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -411,6 +436,7 @@ where
decision_index,
&self.data,
&*portfolio,
self.futures_account.as_ref(),
open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -540,6 +566,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&pre_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -554,6 +581,7 @@ where
decision_index,
data: &self.data,
portfolio: &portfolio,
futures_account: self.futures_account.as_ref(),
open_orders: &pre_open_orders,
dynamic_universe: self.dynamic_universe.as_ref(),
subscriptions: &self.subscriptions,
@@ -574,6 +602,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&pre_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -592,6 +621,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&pre_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -619,6 +649,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&pre_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -635,6 +666,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&pre_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -653,6 +685,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&pre_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -668,6 +701,7 @@ where
decision_index,
data: &self.data,
portfolio: &portfolio,
futures_account: self.futures_account.as_ref(),
open_orders: &pre_open_orders,
dynamic_universe: self.dynamic_universe.as_ref(),
subscriptions: &self.subscriptions,
@@ -688,6 +722,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&pre_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -721,6 +756,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&post_auction_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -735,6 +771,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&post_auction_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -752,6 +789,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&post_auction_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -769,6 +807,7 @@ where
decision_index: decision_idx,
data: &self.data,
portfolio: &portfolio,
futures_account: self.futures_account.as_ref(),
open_orders: &on_day_open_orders,
dynamic_universe: self.dynamic_universe.as_ref(),
subscriptions: &self.subscriptions,
@@ -794,6 +833,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&on_day_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -811,6 +851,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&on_day_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -828,6 +869,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&bar_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -846,6 +888,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&bar_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -861,6 +904,7 @@ where
decision_index,
data: &self.data,
portfolio: &portfolio,
futures_account: self.futures_account.as_ref(),
open_orders: &bar_open_orders,
dynamic_universe: self.dynamic_universe.as_ref(),
subscriptions: &self.subscriptions,
@@ -881,6 +925,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&bar_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -912,6 +957,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&post_intraday_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -933,6 +979,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&post_intraday_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -949,6 +996,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&post_intraday_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -979,6 +1027,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&tick_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -997,6 +1046,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&tick_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -1013,6 +1063,7 @@ where
decision_index,
data: &self.data,
portfolio: &portfolio,
futures_account: self.futures_account.as_ref(),
open_orders: &tick_open_orders,
dynamic_universe: self.dynamic_universe.as_ref(),
subscriptions: &self.subscriptions,
@@ -1032,6 +1083,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&tick_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -1067,6 +1119,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&post_tick_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -1082,6 +1135,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&post_tick_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -1116,6 +1170,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&post_trade_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -1130,6 +1185,7 @@ where
decision_index,
data: &self.data,
portfolio: &portfolio,
futures_account: self.futures_account.as_ref(),
open_orders: &post_trade_open_orders,
dynamic_universe: self.dynamic_universe.as_ref(),
subscriptions: &self.subscriptions,
@@ -1150,6 +1206,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&post_trade_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -1168,6 +1225,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&post_trade_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -1196,6 +1254,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&post_trade_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -1228,6 +1287,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&post_close_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -1244,6 +1304,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&post_close_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -1258,6 +1319,7 @@ where
decision_index,
data: &self.data,
portfolio: &portfolio,
futures_account: self.futures_account.as_ref(),
open_orders: &post_close_open_orders,
dynamic_universe: self.dynamic_universe.as_ref(),
subscriptions: &self.subscriptions,
@@ -1278,6 +1340,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&post_close_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -1296,6 +1359,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&post_close_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -1338,6 +1402,7 @@ where
decision_index,
&self.data,
&portfolio,
self.futures_account.as_ref(),
&post_close_open_orders,
self.dynamic_universe.as_ref(),
&self.subscriptions,
@@ -1769,6 +1834,7 @@ where
decision_index,
data: &self.data,
portfolio,
futures_account: self.futures_account.as_ref(),
open_orders,
dynamic_universe,
subscriptions,
@@ -1808,6 +1874,7 @@ where
decision_index,
&self.data,
&*portfolio,
self.futures_account.as_ref(),
open_orders,
dynamic_universe,
subscriptions,
@@ -1952,6 +2019,7 @@ fn collect_scheduled_decisions<S: Strategy>(
decision_index: usize,
data: &crate::data::DataSet,
portfolio: &PortfolioState,
futures_account: Option<&FuturesAccountState>,
open_orders: &[crate::strategy::OpenOrderView],
dynamic_universe: Option<&BTreeSet<String>>,
subscriptions: &BTreeSet<String>,
@@ -1971,6 +2039,7 @@ fn collect_scheduled_decisions<S: Strategy>(
decision_index,
data,
portfolio,
futures_account,
open_orders,
dynamic_universe,
subscriptions,
@@ -1986,6 +2055,7 @@ fn collect_scheduled_decisions<S: Strategy>(
decision_index,
data,
portfolio,
futures_account,
open_orders,
dynamic_universe,
subscriptions,
@@ -2005,6 +2075,7 @@ fn collect_scheduled_decisions<S: Strategy>(
decision_index,
data,
portfolio,
futures_account,
open_orders,
dynamic_universe,
subscriptions,
@@ -2025,6 +2096,7 @@ fn publish_phase_event<S: Strategy>(
decision_index: usize,
data: &crate::data::DataSet,
portfolio: &PortfolioState,
futures_account: Option<&FuturesAccountState>,
open_orders: &[crate::strategy::OpenOrderView],
dynamic_universe: Option<&BTreeSet<String>>,
subscriptions: &BTreeSet<String>,
@@ -2049,6 +2121,7 @@ fn publish_phase_event<S: Strategy>(
decision_index,
data,
portfolio,
futures_account,
open_orders,
dynamic_universe,
subscriptions,
@@ -2071,6 +2144,7 @@ fn publish_process_events<S: Strategy>(
decision_index: usize,
data: &crate::data::DataSet,
portfolio: &PortfolioState,
futures_account: Option<&FuturesAccountState>,
open_orders: &[crate::strategy::OpenOrderView],
dynamic_universe: Option<&BTreeSet<String>>,
subscriptions: &BTreeSet<String>,
@@ -2086,6 +2160,7 @@ fn publish_process_events<S: Strategy>(
decision_index,
data,
portfolio,
futures_account,
open_orders,
dynamic_universe,
subscriptions,
@@ -2109,6 +2184,7 @@ fn publish_custom_process_event<S: Strategy>(
decision_index: usize,
data: &crate::data::DataSet,
portfolio: &PortfolioState,
futures_account: Option<&FuturesAccountState>,
open_orders: &[crate::strategy::OpenOrderView],
dynamic_universe: Option<&BTreeSet<String>>,
subscriptions: &BTreeSet<String>,
@@ -2123,6 +2199,7 @@ fn publish_custom_process_event<S: Strategy>(
decision_index,
data,
portfolio,
futures_account,
open_orders,
dynamic_universe,
subscriptions,