Search in sources :

Example 1 with ToLongBiFunction

use of java.util.function.ToLongBiFunction in project portfolio by buchen.

the class PerformanceIndex method calculateAbsoluteInvestedCapital.

/**
 * Calculates the absolute invested capital, i.e. starting with the first
 * transaction recorded for the client.
 */
public long[] calculateAbsoluteInvestedCapital() {
    ToLongBiFunction<Money, LocalDateTime> convertIfNecessary = (amount, date) -> {
        if (amount.getCurrencyCode().equals(getCurrencyConverter().getTermCurrency()))
            return amount.getAmount();
        else
            return getCurrencyConverter().convert(date, amount).getAmount();
    };
    long startValue = 0;
    Interval interval = getActualInterval();
    LocalDateTime intervalStart = interval.getStart().atStartOfDay();
    for (Account account : getClient().getAccounts()) startValue += // 
    account.getTransactions().stream().filter(t -> t.getType() == AccountTransaction.Type.DEPOSIT || t.getType() == AccountTransaction.Type.REMOVAL).filter(// 
    t -> t.getDateTime().isBefore(intervalStart)).mapToLong(t -> {
        if (t.getType() == AccountTransaction.Type.DEPOSIT)
            return convertIfNecessary.applyAsLong(t.getMonetaryAmount(), t.getDateTime());
        else if (t.getType() == AccountTransaction.Type.REMOVAL)
            return -convertIfNecessary.applyAsLong(t.getMonetaryAmount(), t.getDateTime());
        else
            return 0;
    }).sum();
    for (Portfolio portfolio : getClient().getPortfolios()) startValue += // 
    portfolio.getTransactions().stream().filter(t -> t.getType() == PortfolioTransaction.Type.DELIVERY_INBOUND || t.getType() == PortfolioTransaction.Type.DELIVERY_OUTBOUND).filter(// 
    t -> t.getDateTime().isBefore(intervalStart)).mapToLong(t -> {
        if (t.getType() == PortfolioTransaction.Type.DELIVERY_INBOUND)
            return convertIfNecessary.applyAsLong(t.getMonetaryAmount(), t.getDateTime());
        else if (t.getType() == PortfolioTransaction.Type.DELIVERY_OUTBOUND)
            return -convertIfNecessary.applyAsLong(t.getMonetaryAmount(), t.getDateTime());
        else
            return 0;
    }).sum();
    return calculateInvestedCapital(startValue);
}
Also used : LocalDateTime(java.time.LocalDateTime) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Arrays(java.util.Arrays) Money(name.abuchen.portfolio.money.Money) Values(name.abuchen.portfolio.money.Values) Client(name.abuchen.portfolio.model.Client) LocalDateTime(java.time.LocalDateTime) Messages(name.abuchen.portfolio.Messages) Classification(name.abuchen.portfolio.model.Classification) ToLongBiFunction(java.util.function.ToLongBiFunction) ClientClassificationFilter(name.abuchen.portfolio.snapshot.filter.ClientClassificationFilter) OutputStreamWriter(java.io.OutputStreamWriter) Interval(name.abuchen.portfolio.util.Interval) TradeCalendar(name.abuchen.portfolio.util.TradeCalendar) Portfolio(name.abuchen.portfolio.model.Portfolio) Account(name.abuchen.portfolio.model.Account) Predicate(java.util.function.Predicate) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Volatility(name.abuchen.portfolio.math.Risk.Volatility) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Drawdown(name.abuchen.portfolio.math.Risk.Drawdown) Security(name.abuchen.portfolio.model.Security) PortfolioClientFilter(name.abuchen.portfolio.snapshot.filter.PortfolioClientFilter) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) LocalDate(java.time.LocalDate) ClientSecurityFilter(name.abuchen.portfolio.snapshot.filter.ClientSecurityFilter) Writer(java.io.Writer) Optional(java.util.Optional) Collections(java.util.Collections) CSVStrategy(org.apache.commons.csv.CSVStrategy) CSVPrinter(org.apache.commons.csv.CSVPrinter) Money(name.abuchen.portfolio.money.Money) Account(name.abuchen.portfolio.model.Account) Portfolio(name.abuchen.portfolio.model.Portfolio) Interval(name.abuchen.portfolio.util.Interval)

Aggregations

File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 StandardCharsets (java.nio.charset.StandardCharsets)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 Predicate (java.util.function.Predicate)1 ToLongBiFunction (java.util.function.ToLongBiFunction)1 Messages (name.abuchen.portfolio.Messages)1 Drawdown (name.abuchen.portfolio.math.Risk.Drawdown)1 Volatility (name.abuchen.portfolio.math.Risk.Volatility)1 Account (name.abuchen.portfolio.model.Account)1 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)1 Classification (name.abuchen.portfolio.model.Classification)1