Add share and percent order intents

This commit is contained in:
boris
2026-04-23 02:03:24 -07:00
parent 23ba74909d
commit 0bc224f703
3 changed files with 482 additions and 0 deletions

View File

@@ -399,6 +399,42 @@ where
report: &mut BrokerExecutionReport,
) -> Result<(), BacktestError> {
match intent {
OrderIntent::Shares {
symbol,
quantity,
reason,
} => self.process_shares(
date,
portfolio,
data,
symbol,
*quantity,
next_order_id,
reason,
intraday_turnover,
execution_cursors,
global_execution_cursor,
commission_state,
report,
),
OrderIntent::Lots {
symbol,
lots,
reason,
} => self.process_lots(
date,
portfolio,
data,
symbol,
*lots,
next_order_id,
reason,
intraday_turnover,
execution_cursors,
global_execution_cursor,
commission_state,
report,
),
OrderIntent::TargetValue {
symbol,
target_value,
@@ -435,6 +471,42 @@ where
commission_state,
report,
),
OrderIntent::Percent {
symbol,
percent,
reason,
} => self.process_percent(
date,
portfolio,
data,
symbol,
*percent,
next_order_id,
reason,
intraday_turnover,
execution_cursors,
global_execution_cursor,
commission_state,
report,
),
OrderIntent::TargetPercent {
symbol,
target_percent,
reason,
} => self.process_target_percent(
date,
portfolio,
data,
symbol,
*target_percent,
next_order_id,
reason,
intraday_turnover,
execution_cursors,
global_execution_cursor,
commission_state,
report,
),
}
}
@@ -1279,6 +1351,38 @@ where
Ok(())
}
fn process_target_percent(
&self,
date: NaiveDate,
portfolio: &mut PortfolioState,
data: &DataSet,
symbol: &str,
target_percent: f64,
next_order_id: &mut u64,
reason: &str,
intraday_turnover: &mut BTreeMap<String, u32>,
execution_cursors: &mut BTreeMap<String, NaiveDateTime>,
global_execution_cursor: &mut Option<NaiveDateTime>,
commission_state: &mut BTreeMap<u64, f64>,
report: &mut BrokerExecutionReport,
) -> Result<(), BacktestError> {
let total_equity = self.rebalance_total_equity_at(date, portfolio, data)?;
self.process_target_value(
date,
portfolio,
data,
symbol,
total_equity * target_percent.max(0.0),
next_order_id,
reason,
intraday_turnover,
execution_cursors,
global_execution_cursor,
commission_state,
report,
)
}
fn process_value(
&self,
date: NaiveDate,
@@ -1365,6 +1469,133 @@ where
}
}
fn process_percent(
&self,
date: NaiveDate,
portfolio: &mut PortfolioState,
data: &DataSet,
symbol: &str,
percent: f64,
next_order_id: &mut u64,
reason: &str,
intraday_turnover: &mut BTreeMap<String, u32>,
execution_cursors: &mut BTreeMap<String, NaiveDateTime>,
global_execution_cursor: &mut Option<NaiveDateTime>,
commission_state: &mut BTreeMap<u64, f64>,
report: &mut BrokerExecutionReport,
) -> Result<(), BacktestError> {
let total_equity = self.rebalance_total_equity_at(date, portfolio, data)?;
self.process_value(
date,
portfolio,
data,
symbol,
total_equity * percent,
next_order_id,
reason,
intraday_turnover,
execution_cursors,
global_execution_cursor,
commission_state,
report,
)
}
fn process_shares(
&self,
date: NaiveDate,
portfolio: &mut PortfolioState,
data: &DataSet,
symbol: &str,
quantity: i32,
next_order_id: &mut u64,
reason: &str,
intraday_turnover: &mut BTreeMap<String, u32>,
execution_cursors: &mut BTreeMap<String, NaiveDateTime>,
global_execution_cursor: &mut Option<NaiveDateTime>,
commission_state: &mut BTreeMap<u64, f64>,
report: &mut BrokerExecutionReport,
) -> Result<(), BacktestError> {
if quantity == 0 {
return Ok(());
}
if quantity > 0 {
let requested_qty = self.round_buy_quantity(
quantity as u32,
self.minimum_order_quantity(data, symbol),
self.order_step_size(data, symbol),
);
self.process_buy(
date,
portfolio,
data,
symbol,
requested_qty,
Self::reserve_order_id(next_order_id),
reason,
intraday_turnover,
execution_cursors,
global_execution_cursor,
commission_state,
None,
report,
)
} else {
self.process_sell(
date,
portfolio,
data,
symbol,
quantity.unsigned_abs(),
Self::reserve_order_id(next_order_id),
reason,
intraday_turnover,
execution_cursors,
global_execution_cursor,
commission_state,
report,
)
}
}
fn process_lots(
&self,
date: NaiveDate,
portfolio: &mut PortfolioState,
data: &DataSet,
symbol: &str,
lots: i32,
next_order_id: &mut u64,
reason: &str,
intraday_turnover: &mut BTreeMap<String, u32>,
execution_cursors: &mut BTreeMap<String, NaiveDateTime>,
global_execution_cursor: &mut Option<NaiveDateTime>,
commission_state: &mut BTreeMap<u64, f64>,
report: &mut BrokerExecutionReport,
) -> Result<(), BacktestError> {
let round_lot = self.round_lot(data, symbol);
let requested_quantity = lots.saturating_abs() as u32 * round_lot;
let signed_quantity = if lots >= 0 {
requested_quantity as i32
} else {
-(requested_quantity as i32)
};
self.process_shares(
date,
portfolio,
data,
symbol,
signed_quantity,
next_order_id,
reason,
intraday_turnover,
execution_cursors,
global_execution_cursor,
commission_state,
report,
)
}
fn maybe_expand_periodic_value_buy_quantity(
&self,
_date: NaiveDate,