Search in sources :

Example 1 with TradeCalendar

use of name.abuchen.portfolio.util.TradeCalendar in project portfolio by buchen.

the class InvestmentPlan method next.

/**
 * Returns the date for the next transaction to be generated based on the
 * interval
 */
private LocalDate next(LocalDate transactionDate) {
    LocalDate previousDate = transactionDate;
    if (transactionDate.getDayOfMonth() != start.getDayOfMonth()) {
        int daysBetween = Integer.MAX_VALUE;
        LocalDate testDate = transactionDate.minusMonths(1);
        testDate = testDate.withDayOfMonth(Math.min(testDate.lengthOfMonth(), start.getDayOfMonth()));
        for (int ii = 0; ii < 3; ii++) {
            int d = Dates.daysBetween(transactionDate, testDate);
            if (d < daysBetween) {
                daysBetween = d;
                previousDate = testDate;
            }
            testDate = testDate.plusMonths(1);
            testDate = testDate.withDayOfMonth(Math.min(testDate.lengthOfMonth(), start.getDayOfMonth()));
        }
    }
    LocalDate next = previousDate.plusMonths(interval);
    // correct day of month (say the transactions are to be generated on the
    // 31st, but the month has only 30 days)
    next = next.withDayOfMonth(Math.min(next.lengthOfMonth(), start.getDayOfMonth()));
    // do not generate a investment plan transaction on a public holiday
    TradeCalendar tradeCalendar = new TradeCalendar();
    while (tradeCalendar.isHoliday(next)) next = next.plusDays(1);
    return next;
}
Also used : TradeCalendar(name.abuchen.portfolio.util.TradeCalendar) LocalDate(java.time.LocalDate)

Aggregations

LocalDate (java.time.LocalDate)1 TradeCalendar (name.abuchen.portfolio.util.TradeCalendar)1