Skip to content

Commit

Permalink
Fixed infinite loop in case of holidays for weekly investment plans
Browse files Browse the repository at this point in the history
Issue: #4374
Issue: #4347
Signed-off-by: mierin12 <[email protected]>
[added test case]
Signed-off-by: Andreas Buchen <[email protected]>
  • Loading branch information
mierin12 authored and buchen committed Nov 30, 2024
1 parent 93051e1 commit fe1f7c3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,25 @@ public void testGenerationOfWeeklyBuyTransaction() throws IOException
assertThat(newlyGenerated.get(2).getTransaction().getDateTime(), is(LocalDateTime.parse("2016-04-08T00:00")));
}

@Test
public void testWeeklyFrom18March2024() throws IOException
{
investmentPlan.setType(InvestmentPlan.Type.PURCHASE_OR_DELIVERY);

investmentPlan.setAccount(account);
investmentPlan.setPortfolio(portfolio);
investmentPlan.setSecurity(security);
investmentPlan.setStart(LocalDateTime.parse("2024-03-18T00:00:00"));

investmentPlan.setInterval(101); // 101 is weekly, 201 bi-weekly

investmentPlan.generateTransactions(new TestCurrencyConverter());

var tx = investmentPlan.getTransactions().stream().toList();
assertThat(tx.get(0).getDateTime(), is(LocalDateTime.parse("2024-03-18T00:00")));
assertThat(tx.get(1).getDateTime(), is(LocalDateTime.parse("2024-03-25T00:00")));
assertThat(tx.get(2).getDateTime(), is(LocalDateTime.parse("2024-04-02T00:00")));
assertThat(tx.get(3).getDateTime(), is(LocalDateTime.parse("2024-04-08T00:00")));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,9 @@ private LocalDate next(LocalDate transactionDate)

if (transactionDate.getDayOfWeek() != start.getDayOfWeek())
{
previousDate = transactionDate.minusDays(
transactionDate.getDayOfWeek().getValue() + 7L - start.getDayOfWeek().getValue());
int offset = transactionDate.getDayOfWeek().getValue() - start.getDayOfWeek().getValue();
offset = offset > 0 ? offset : offset + 7;
previousDate = transactionDate.minusDays(offset);
}

next = previousDate.plusWeeks((long) interval - WEEKS_THRESHOLD);
Expand Down

0 comments on commit fe1f7c3

Please sign in to comment.