Use prev close for open auction rebalances
This commit is contained in:
@@ -447,18 +447,12 @@ where
|
||||
data: &DataSet,
|
||||
target_weights: &BTreeMap<String, f64>,
|
||||
) -> Result<(BTreeMap<String, u32>, Vec<String>), BacktestError> {
|
||||
let equity = self.total_equity_at(date, portfolio, data, self.execution_price_field)?;
|
||||
let equity = self.rebalance_total_equity_at(date, portfolio, data)?;
|
||||
let target_weight_sum = target_weights.values().copied().sum::<f64>();
|
||||
let mut desired_targets = BTreeMap::new();
|
||||
let mut diagnostics = Vec::new();
|
||||
for (symbol, weight) in target_weights {
|
||||
let price = data
|
||||
.price(date, symbol, self.execution_price_field)
|
||||
.ok_or_else(|| BacktestError::MissingPrice {
|
||||
date,
|
||||
symbol: symbol.clone(),
|
||||
field: price_field_name(self.execution_price_field),
|
||||
})?;
|
||||
let price = self.rebalance_valuation_price(date, symbol, data)?;
|
||||
let raw_qty = ((equity * weight) / price).floor() as u32;
|
||||
desired_targets.insert(
|
||||
symbol.clone(),
|
||||
@@ -482,13 +476,7 @@ where
|
||||
.map(|pos| pos.quantity)
|
||||
.unwrap_or(0);
|
||||
let desired_qty = *desired_targets.get(&symbol).unwrap_or(&0);
|
||||
let price = data
|
||||
.price(date, &symbol, self.execution_price_field)
|
||||
.ok_or_else(|| BacktestError::MissingPrice {
|
||||
date,
|
||||
symbol: symbol.clone(),
|
||||
field: price_field_name(self.execution_price_field),
|
||||
})?;
|
||||
let price = self.rebalance_valuation_price(date, &symbol, data)?;
|
||||
let minimum_order_quantity = self.minimum_order_quantity(data, &symbol);
|
||||
let order_step_size = self.order_step_size(data, &symbol);
|
||||
let min_target_qty = self.minimum_target_quantity(
|
||||
@@ -1585,6 +1573,78 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
fn rebalance_valuation_price_field_name(&self) -> &'static str {
|
||||
if self.is_open_auction_matching() {
|
||||
"prev_close"
|
||||
} else {
|
||||
price_field_name(self.execution_price_field)
|
||||
}
|
||||
}
|
||||
|
||||
fn rebalance_valuation_price_for_snapshot(
|
||||
&self,
|
||||
snapshot: &crate::data::DailyMarketSnapshot,
|
||||
) -> Option<f64> {
|
||||
let price = if self.is_open_auction_matching() {
|
||||
snapshot.prev_close
|
||||
} else {
|
||||
snapshot.price(self.execution_price_field)
|
||||
};
|
||||
if price.is_finite() && price > 0.0 {
|
||||
Some(price)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn rebalance_valuation_price(
|
||||
&self,
|
||||
date: NaiveDate,
|
||||
symbol: &str,
|
||||
data: &DataSet,
|
||||
) -> Result<f64, BacktestError> {
|
||||
let snapshot = data
|
||||
.market(date, symbol)
|
||||
.ok_or_else(|| BacktestError::MissingPrice {
|
||||
date,
|
||||
symbol: symbol.to_string(),
|
||||
field: self.rebalance_valuation_price_field_name(),
|
||||
})?;
|
||||
self.rebalance_valuation_price_for_snapshot(snapshot)
|
||||
.ok_or_else(|| BacktestError::MissingPrice {
|
||||
date,
|
||||
symbol: symbol.to_string(),
|
||||
field: self.rebalance_valuation_price_field_name(),
|
||||
})
|
||||
}
|
||||
|
||||
fn rebalance_total_equity_at(
|
||||
&self,
|
||||
date: NaiveDate,
|
||||
portfolio: &PortfolioState,
|
||||
data: &DataSet,
|
||||
) -> Result<f64, BacktestError> {
|
||||
let mut market_value = 0.0;
|
||||
for position in portfolio.positions().values() {
|
||||
let snapshot =
|
||||
data.market(date, &position.symbol)
|
||||
.ok_or_else(|| BacktestError::MissingPrice {
|
||||
date,
|
||||
symbol: position.symbol.clone(),
|
||||
field: self.rebalance_valuation_price_field_name(),
|
||||
})?;
|
||||
let price = self
|
||||
.rebalance_valuation_price_for_snapshot(snapshot)
|
||||
.ok_or_else(|| BacktestError::MissingPrice {
|
||||
date,
|
||||
symbol: position.symbol.clone(),
|
||||
field: self.rebalance_valuation_price_field_name(),
|
||||
})?;
|
||||
market_value += price * position.quantity as f64;
|
||||
}
|
||||
Ok(portfolio.cash() + market_value)
|
||||
}
|
||||
|
||||
fn round_buy_quantity(
|
||||
&self,
|
||||
quantity: u32,
|
||||
|
||||
Reference in New Issue
Block a user