Add bar and tick strategy lifecycle

This commit is contained in:
boris
2026-04-23 19:17:04 -07:00
parent 86e4db6272
commit 1760fc6cd1
9 changed files with 525 additions and 10 deletions

View File

@@ -111,6 +111,8 @@ pub struct BrokerSimulator<C, R> {
inactive_limit: bool,
liquidity_limit: bool,
intraday_execution_start_time: Option<NaiveTime>,
runtime_intraday_start_time: Cell<Option<NaiveTime>>,
runtime_intraday_end_time: Cell<Option<NaiveTime>>,
next_order_id: Cell<u64>,
open_orders: RefCell<Vec<OpenOrder>>,
}
@@ -129,6 +131,8 @@ impl<C, R> BrokerSimulator<C, R> {
inactive_limit: true,
liquidity_limit: true,
intraday_execution_start_time: None,
runtime_intraday_start_time: Cell::new(None),
runtime_intraday_end_time: Cell::new(None),
next_order_id: Cell::new(1),
open_orders: RefCell::new(Vec::new()),
}
@@ -151,6 +155,8 @@ impl<C, R> BrokerSimulator<C, R> {
inactive_limit: true,
liquidity_limit: true,
intraday_execution_start_time: None,
runtime_intraday_start_time: Cell::new(None),
runtime_intraday_end_time: Cell::new(None),
next_order_id: Cell::new(1),
open_orders: RefCell::new(Vec::new()),
}
@@ -521,6 +527,25 @@ where
Ok(report)
}
pub fn execute_between(
&self,
date: NaiveDate,
portfolio: &mut PortfolioState,
data: &DataSet,
decision: &StrategyDecision,
start_time: Option<NaiveTime>,
end_time: Option<NaiveTime>,
) -> Result<BrokerExecutionReport, BacktestError> {
let previous_start_time = self.runtime_intraday_start_time.get();
let previous_end_time = self.runtime_intraday_end_time.get();
self.runtime_intraday_start_time.set(start_time);
self.runtime_intraday_end_time.set(end_time);
let result = self.execute(date, portfolio, data, decision);
self.runtime_intraday_start_time.set(previous_start_time);
self.runtime_intraday_end_time.set(previous_end_time);
result
}
fn process_order_intent(
&self,
date: NaiveDate,
@@ -4128,12 +4153,16 @@ where
return None;
}
let runtime_start_time = self.runtime_intraday_start_time.get();
let runtime_end_time = self.runtime_intraday_end_time.get();
let start_cursor = algo_request
.and_then(|request| request.start_time)
.or(runtime_start_time)
.or(self.intraday_execution_start_time)
.map(|start_time| date.and_time(start_time));
let end_cursor = algo_request
.and_then(|request| request.end_time)
.or(runtime_end_time)
.map(|end_time| date.and_time(end_time));
let quotes = data.execution_quotes_on(date, symbol);