Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IRR calculation for securities with portfolio transfer #1898

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -3,6 +3,7 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -77,6 +78,13 @@ public Trade(Security security, Portfolio portfolio, long shares)
.sum() / (double) shares);
}

// let's sort again because the list might not be sorted anymore due to transfers
Collections.sort(transactions,
(p1, p2) -> p1.getTransaction().getDateTime().compareTo(p2.getTransaction().getDateTime()));

// re-set start date from first entry after sorting
this.setStart(transactions.get(0).getTransaction().getDateTime());

calculateIRR(converter);
}

Expand Down
Expand Up @@ -113,6 +113,10 @@ private Trade createNewTradeFromSell(Map<Portfolio, List<TransactionPair<Portfol
pair.getTransaction().getSecurity(), pair.getOwner(), pair));

long sharesToDistribute = pair.getTransaction().getShares();

// sort open to get fifo
Collections.sort(open,
(p1, p2) -> p1.getTransaction().getDateTime().compareTo(p2.getTransaction().getDateTime()));

for (TransactionPair<PortfolioTransaction> candidate : new ArrayList<>(open))
{
Expand Down