按顺序结算多笔现金应收
This commit is contained in:
@@ -3242,14 +3242,16 @@ where
|
||||
notes: &mut Vec<String>,
|
||||
) -> Result<BrokerExecutionReport, BacktestError> {
|
||||
let mut report = BrokerExecutionReport::default();
|
||||
let settled = portfolio.settle_cash_receivables(date);
|
||||
for receivable in settled {
|
||||
let due = portfolio.take_due_cash_receivables(date);
|
||||
for receivable in due {
|
||||
let cash_before = portfolio.cash();
|
||||
portfolio
|
||||
.settle_cash_receivable(&receivable)
|
||||
.map_err(BacktestError::Execution)?;
|
||||
let mut note = format!(
|
||||
"cash_receivable_settled {} ex_date={} payable_date={} cash={:.2}",
|
||||
receivable.symbol, receivable.ex_date, receivable.payable_date, receivable.amount
|
||||
);
|
||||
let cash_before = portfolio.cash() - receivable.amount;
|
||||
|
||||
if self.dividend_reinvestment
|
||||
&& receivable.reason.starts_with("cash_dividend")
|
||||
&& receivable.amount > 0.0
|
||||
|
||||
@@ -778,25 +778,23 @@ impl PortfolioState {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn settle_cash_receivables(&mut self, date: NaiveDate) -> Vec<CashReceivable> {
|
||||
let mut settled = Vec::new();
|
||||
pub fn take_due_cash_receivables(&mut self, date: NaiveDate) -> Vec<CashReceivable> {
|
||||
let mut due = Vec::new();
|
||||
let mut pending = Vec::new();
|
||||
for receivable in self.cash_receivables.drain(..) {
|
||||
if receivable.payable_date <= date {
|
||||
let amount = Self::fixed_money(receivable.amount, "cash receivable")
|
||||
.expect("cash receivable must be finite fixed-point money");
|
||||
self.cash = self
|
||||
.cash
|
||||
.checked_add(amount)
|
||||
.expect("fixed-point cash overflow while settling receivable");
|
||||
settled.push(receivable);
|
||||
due.push(receivable);
|
||||
} else {
|
||||
pending.push(receivable);
|
||||
}
|
||||
}
|
||||
self.cash_receivables = pending;
|
||||
self.refresh_dividend_receivables();
|
||||
settled
|
||||
due
|
||||
}
|
||||
|
||||
pub fn settle_cash_receivable(&mut self, receivable: &CashReceivable) -> Result<(), String> {
|
||||
self.apply_cash_delta(receivable.amount)
|
||||
}
|
||||
|
||||
pub fn cash_receivables(&self) -> &[CashReceivable] {
|
||||
|
||||
Reference in New Issue
Block a user