Search in sources :

Example 66 with Account

use of name.abuchen.portfolio.model.Account in project portfolio by buchen.

the class ClientSnapshot method getJointPortfolio.

public PortfolioSnapshot getJointPortfolio() {
    if (this.jointPortfolio == null) {
        if (portfolios.isEmpty()) {
            Portfolio portfolio = new Portfolio();
            portfolio.setName(Messages.LabelJointPortfolio);
            portfolio.setReferenceAccount(new Account(Messages.LabelJointPortfolio));
            this.jointPortfolio = PortfolioSnapshot.create(portfolio, converter, date);
        } else if (portfolios.size() == 1) {
            this.jointPortfolio = portfolios.get(0);
        } else {
            this.jointPortfolio = PortfolioSnapshot.merge(portfolios, converter);
        }
    }
    return this.jointPortfolio;
}
Also used : Account(name.abuchen.portfolio.model.Account) ReadOnlyAccount(name.abuchen.portfolio.snapshot.filter.ReadOnlyAccount) Portfolio(name.abuchen.portfolio.model.Portfolio)

Example 67 with Account

use of name.abuchen.portfolio.model.Account 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)

Example 68 with Account

use of name.abuchen.portfolio.model.Account in project portfolio by buchen.

the class PortfolioSnapshot method merge.

public static PortfolioSnapshot merge(List<PortfolioSnapshot> snapshots, CurrencyConverter converter) {
    if (snapshots.isEmpty())
        // $NON-NLS-1$
        throw new IllegalArgumentException("Error: PortfolioSnapshots to be merged must not be empty");
    Portfolio portfolio = new Portfolio() {

        @Override
        public void shallowDeleteTransaction(PortfolioTransaction transaction, Client client) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void deleteTransaction(PortfolioTransaction transaction, Client client) {
            throw new UnsupportedOperationException();
        }
    };
    portfolio.setName(Messages.LabelJointPortfolio);
    Account referenceAccount = new Account(Messages.LabelJointPortfolio);
    referenceAccount.setCurrencyCode(converter.getTermCurrency());
    portfolio.setReferenceAccount(referenceAccount);
    snapshots.forEach(s -> portfolio.addAllTransaction(s.getSource().getTransactions()));
    return create(portfolio, snapshots.get(0).getCurrencyConverter(), snapshots.get(0).getTime());
}
Also used : Account(name.abuchen.portfolio.model.Account) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) ReadOnlyPortfolio(name.abuchen.portfolio.snapshot.filter.ReadOnlyPortfolio) Portfolio(name.abuchen.portfolio.model.Portfolio) Client(name.abuchen.portfolio.model.Client)

Example 69 with Account

use of name.abuchen.portfolio.model.Account in project portfolio by buchen.

the class ClientClassificationFilter method filter.

@Override
public Client filter(Client client) {
    ReadOnlyClient pseudoClient = new ReadOnlyClient(client);
    CalculationState state = new CalculationState(classification);
    for (Account account : client.getAccounts()) {
        ReadOnlyAccount pseudo = new ReadOnlyAccount(account);
        pseudoClient.internalAddAccount(pseudo);
        state.putReadOnly(account, pseudo);
    }
    for (Portfolio portfolio : client.getPortfolios()) {
        ReadOnlyPortfolio pseudoPortfolio = new ReadOnlyPortfolio(portfolio);
        pseudoPortfolio.setReferenceAccount(state.asReadOnly(portfolio.getReferenceAccount()));
        pseudoClient.internalAddPortfolio(pseudoPortfolio);
        state.putReadOnly(portfolio, pseudoPortfolio);
    }
    for (Portfolio portfolio : client.getPortfolios()) adaptPortfolioTransactions(state, portfolio);
    for (Account account : client.getAccounts()) {
        if (state.isCategorized(account))
            adaptAccountTransactions(state, account);
        else
            collectSecurityRelatedTx(state, account);
    }
    for (Security security : state.categorizedSecurities) pseudoClient.internalAddSecurity(security);
    return pseudoClient;
}
Also used : Account(name.abuchen.portfolio.model.Account) Portfolio(name.abuchen.portfolio.model.Portfolio) Security(name.abuchen.portfolio.model.Security)

Example 70 with Account

use of name.abuchen.portfolio.model.Account in project portfolio by buchen.

the class ClientClassificationFilter method adaptAccountTransactions.

private void adaptAccountTransactions(CalculationState state, Account account) {
    int accountWeight = state.getWeight(account);
    for (AccountTransaction t : account.getTransactions()) {
        long amount = value(t.getAmount(), accountWeight);
        switch(t.getType()) {
            case SELL:
                // removal in the account
                if (!state.isCategorized(t.getSecurity()))
                    state.asReadOnly(account).internalAddTransaction(new AccountTransaction(t.getDateTime(), t.getCurrencyCode(), amount, null, AccountTransaction.Type.DEPOSIT));
                break;
            case BUY:
                if (!state.isCategorized(t.getSecurity()))
                    state.asReadOnly(account).internalAddTransaction(new AccountTransaction(t.getDateTime(), t.getCurrencyCode(), amount, null, AccountTransaction.Type.REMOVAL));
                break;
            case DIVIDENDS:
                if (!state.isCategorized(t.getSecurity()))
                    state.asReadOnly(account).internalAddTransaction(new AccountTransaction(t.getDateTime(), t.getCurrencyCode(), amount, null, AccountTransaction.Type.DEPOSIT));
                else
                    addSecurityRelatedAccountT(state, account, t);
                break;
            case FEES_REFUND:
                if (t.getSecurity() != null && state.isCategorized(t.getSecurity()))
                    addSecurityRelatedAccountT(state, account, t);
                else if (t.getSecurity() != null)
                    state.asReadOnly(account).internalAddTransaction(new AccountTransaction(t.getDateTime(), t.getCurrencyCode(), amount, null, AccountTransaction.Type.DEPOSIT));
                else
                    state.asReadOnly(account).internalAddTransaction(new AccountTransaction(t.getDateTime(), t.getCurrencyCode(), amount, null, t.getType()));
                break;
            case FEES:
                if (t.getSecurity() != null && state.isCategorized(t.getSecurity()))
                    addSecurityRelatedAccountT(state, account, t);
                else if (t.getSecurity() != null)
                    state.asReadOnly(account).internalAddTransaction(new AccountTransaction(t.getDateTime(), t.getCurrencyCode(), amount, null, AccountTransaction.Type.REMOVAL));
                else
                    state.asReadOnly(account).internalAddTransaction(new AccountTransaction(t.getDateTime(), t.getCurrencyCode(), amount, null, t.getType()));
                break;
            case TRANSFER_IN:
                // is created.
                if (!state.isCategorized((Account) t.getCrossEntry().getCrossOwner(t)))
                    state.asReadOnly(account).internalAddTransaction(new AccountTransaction(t.getDateTime(), t.getCurrencyCode(), amount, null, AccountTransaction.Type.DEPOSIT));
                else
                    addTransferT(state, account, t);
                break;
            case TRANSFER_OUT:
                // then create a removal transaction
                if (!state.isCategorized((Account) t.getCrossEntry().getCrossOwner(t)))
                    state.asReadOnly(account).internalAddTransaction(new AccountTransaction(t.getDateTime(), t.getCurrencyCode(), amount, null, AccountTransaction.Type.REMOVAL));
                break;
            case TAX_REFUND:
                // taxes are never included
                state.asReadOnly(account).internalAddTransaction(new AccountTransaction(t.getDateTime(), t.getCurrencyCode(), amount, null, AccountTransaction.Type.DEPOSIT));
                break;
            case TAXES:
                // taxes are never included
                state.asReadOnly(account).internalAddTransaction(new AccountTransaction(t.getDateTime(), t.getCurrencyCode(), amount, null, AccountTransaction.Type.REMOVAL));
                break;
            case DEPOSIT:
            case REMOVAL:
            case INTEREST:
            case INTEREST_CHARGE:
                state.asReadOnly(account).internalAddTransaction(new AccountTransaction(t.getDateTime(), t.getCurrencyCode(), amount, null, t.getType()));
                break;
            default:
                throw new UnsupportedOperationException();
        }
    }
}
Also used : Account(name.abuchen.portfolio.model.Account) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction)

Aggregations

Account (name.abuchen.portfolio.model.Account)75 Security (name.abuchen.portfolio.model.Security)39 Client (name.abuchen.portfolio.model.Client)38 Portfolio (name.abuchen.portfolio.model.Portfolio)37 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)35 Test (org.junit.Test)31 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)25 CurrencyConverter (name.abuchen.portfolio.money.CurrencyConverter)22 TestCurrencyConverter (name.abuchen.portfolio.TestCurrencyConverter)21 ArrayList (java.util.ArrayList)17 LocalDate (java.time.LocalDate)16 AccountBuilder (name.abuchen.portfolio.AccountBuilder)14 Unit (name.abuchen.portfolio.model.Transaction.Unit)14 SecurityBuilder (name.abuchen.portfolio.SecurityBuilder)13 PortfolioBuilder (name.abuchen.portfolio.PortfolioBuilder)12 Money (name.abuchen.portfolio.money.Money)12 Collections (java.util.Collections)11 List (java.util.List)11 AccountTransferEntry (name.abuchen.portfolio.model.AccountTransferEntry)11 BuySellEntry (name.abuchen.portfolio.model.BuySellEntry)11