Search in sources :

Example 26 with PortfolioTransaction

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

the class ClientPerformanceSnapshot method addEarnings.

private void addEarnings() {
    MutableMoney mEarnings = MutableMoney.of(converter.getTermCurrency());
    MutableMoney mOtherEarnings = MutableMoney.of(converter.getTermCurrency());
    MutableMoney mFees = MutableMoney.of(converter.getTermCurrency());
    MutableMoney mTaxes = MutableMoney.of(converter.getTermCurrency());
    MutableMoney mDeposits = MutableMoney.of(converter.getTermCurrency());
    MutableMoney mRemovals = MutableMoney.of(converter.getTermCurrency());
    Map<Security, MutableMoney> earningsBySecurity = new HashMap<>();
    for (Account account : client.getAccounts()) {
        for (AccountTransaction t : account.getTransactions()) {
            if (!period.containsTransaction().test(t))
                continue;
            switch(t.getType()) {
                case DIVIDENDS:
                case INTEREST:
                    addEarningTransaction(account, t, mEarnings, mOtherEarnings, mTaxes, earningsBySecurity);
                    break;
                case INTEREST_CHARGE:
                    Money charged = t.getMonetaryAmount().with(converter.at(t.getDateTime()));
                    mEarnings.subtract(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
                    earnings.add(new TransactionPair<AccountTransaction>(account, t));
                    mOtherEarnings.subtract(charged);
                    break;
                case DEPOSIT:
                    mDeposits.add(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
                    break;
                case REMOVAL:
                    mRemovals.add(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
                    break;
                case FEES:
                    mFees.add(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
                    fees.add(new TransactionPair<AccountTransaction>(account, t));
                    break;
                case FEES_REFUND:
                    mFees.subtract(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
                    fees.add(new TransactionPair<AccountTransaction>(account, t));
                    break;
                case TAXES:
                    mTaxes.add(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
                    taxes.add(new TransactionPair<AccountTransaction>(account, t));
                    break;
                case TAX_REFUND:
                    mTaxes.subtract(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
                    taxes.add(new TransactionPair<AccountTransaction>(account, t));
                    break;
                case BUY:
                case SELL:
                case TRANSFER_IN:
                case TRANSFER_OUT:
                    // no operation
                    break;
                default:
                    throw new UnsupportedOperationException();
            }
        }
    }
    for (Portfolio portfolio : client.getPortfolios()) {
        for (PortfolioTransaction t : portfolio.getTransactions()) {
            if (!period.containsTransaction().test(t))
                continue;
            Money unit = t.getUnitSum(Unit.Type.FEE, converter);
            if (!unit.isZero()) {
                mFees.add(unit);
                fees.add(new TransactionPair<PortfolioTransaction>(portfolio, t));
            }
            unit = t.getUnitSum(Unit.Type.TAX, converter);
            if (!unit.isZero()) {
                mTaxes.add(unit);
                taxes.add(new TransactionPair<PortfolioTransaction>(portfolio, t));
            }
            switch(t.getType()) {
                case DELIVERY_INBOUND:
                    mDeposits.add(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
                    break;
                case DELIVERY_OUTBOUND:
                    mRemovals.add(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
                    break;
                case BUY:
                case SELL:
                case TRANSFER_IN:
                case TRANSFER_OUT:
                    break;
                default:
                    throw new UnsupportedOperationException();
            }
        }
    }
    Category earningsCategory = categories.get(CategoryType.EARNINGS);
    earningsCategory.valuation = mEarnings.toMoney();
    earningsCategory.positions = earningsBySecurity.entrySet().stream().filter(entry -> !entry.getValue().isZero()).map(entry -> new Position(entry.getKey(), entry.getValue().toMoney())).sorted(// 
    (p1, p2) -> p1.getLabel().compareToIgnoreCase(p2.getLabel())).collect(Collectors.toList());
    if (!mOtherEarnings.isZero())
        earningsCategory.positions.add(new Position(Messages.LabelInterest, mOtherEarnings.toMoney()));
    categories.get(CategoryType.FEES).valuation = mFees.toMoney();
    categories.get(CategoryType.TAXES).valuation = mTaxes.toMoney();
    categories.get(CategoryType.TRANSFERS).valuation = mDeposits.toMoney().subtract(mRemovals.toMoney());
    categories.get(CategoryType.TRANSFERS).positions.add(new Position(Messages.LabelDeposits, mDeposits.toMoney()));
    categories.get(CategoryType.TRANSFERS).positions.add(new Position(Messages.LabelRemovals, mRemovals.toMoney()));
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Money(name.abuchen.portfolio.money.Money) Client(name.abuchen.portfolio.model.Client) Transaction(name.abuchen.portfolio.model.Transaction) Account(name.abuchen.portfolio.model.Account) EnumMap(java.util.EnumMap) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) TransactionPair(name.abuchen.portfolio.model.TransactionPair) HashMap(java.util.HashMap) Security(name.abuchen.portfolio.model.Security) Messages(name.abuchen.portfolio.Messages) MutableMoney(name.abuchen.portfolio.money.MutableMoney) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) Unit(name.abuchen.portfolio.model.Transaction.Unit) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) MoneyCollectors(name.abuchen.portfolio.money.MoneyCollectors) LocalDate(java.time.LocalDate) Map(java.util.Map) Collections(java.util.Collections) Portfolio(name.abuchen.portfolio.model.Portfolio) Account(name.abuchen.portfolio.model.Account) HashMap(java.util.HashMap) Portfolio(name.abuchen.portfolio.model.Portfolio) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Security(name.abuchen.portfolio.model.Security) Money(name.abuchen.portfolio.money.Money) MutableMoney(name.abuchen.portfolio.money.MutableMoney) MutableMoney(name.abuchen.portfolio.money.MutableMoney) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction)

Example 27 with PortfolioTransaction

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

the class SecurityPosition method split.

public static SecurityPosition split(SecurityPosition position, int weight) {
    List<PortfolioTransaction> splitTransactions = new ArrayList<>(position.transactions.size());
    for (PortfolioTransaction t : position.transactions) {
        PortfolioTransaction t2 = new PortfolioTransaction();
        t2.setDateTime(t.getDateTime());
        t2.setSecurity(t.getSecurity());
        t2.setType(t.getType());
        t2.setCurrencyCode(t.getCurrencyCode());
        t2.setAmount(Math.round(t.getAmount() * weight / (double) Classification.ONE_HUNDRED_PERCENT));
        t2.setShares(Math.round(t.getShares() * weight / (double) Classification.ONE_HUNDRED_PERCENT));
        t.getUnits().forEach(u -> {
            long splitAmount = Math.round(u.getAmount().getAmount() * weight / (double) Classification.ONE_HUNDRED_PERCENT);
            if (u.getForex() == null) {
                t2.addUnit(new // 
                Unit(// 
                u.getType(), Money.of(u.getAmount().getCurrencyCode(), splitAmount)));
            } else {
                long splitForex = Math.round(u.getForex().getAmount() * weight / (double) Classification.ONE_HUNDRED_PERCENT);
                t2.addUnit(new // 
                Unit(// 
                u.getType(), Money.of(u.getAmount().getCurrencyCode(), splitAmount), // 
                Money.of(u.getForex().getCurrencyCode(), splitForex), u.getExchangeRate()));
            }
        });
        splitTransactions.add(t2);
    }
    return new SecurityPosition(position.investment, position.converter, position.price, Math.round(position.shares * weight / (double) Classification.ONE_HUNDRED_PERCENT), splitTransactions);
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) ArrayList(java.util.ArrayList)

Example 28 with PortfolioTransaction

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

the class ClientClassificationFilter method addDeliveryT.

private void addDeliveryT(CalculationState state, Portfolio portfolio, PortfolioTransaction t, PortfolioTransaction.Type targetType, int weight) {
    PortfolioTransaction copy = new PortfolioTransaction();
    copy.setDateTime(t.getDateTime());
    copy.setCurrencyCode(t.getCurrencyCode());
    copy.setSecurity(t.getSecurity());
    copy.setShares(value(t.getShares(), weight));
    copy.setType(targetType);
    // calculation is without taxes -> remove any taxes & adapt total
    // accordingly
    long taxes = value(t.getUnitSum(Unit.Type.TAX).getAmount(), weight);
    long amount = value(t.getAmount(), weight);
    copy.setAmount(copy.getType() == PortfolioTransaction.Type.DELIVERY_INBOUND ? amount - taxes : amount + taxes);
    // copy all units (except for taxes) over to the new transaction
    t.getUnits().filter(u -> u.getType() != Unit.Type.TAX).forEach(u -> copy.addUnit(value(u, weight)));
    state.asReadOnly(portfolio).internalAddTransaction(copy);
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Money(name.abuchen.portfolio.money.Money) Client(name.abuchen.portfolio.model.Client) Account(name.abuchen.portfolio.model.Account) InvestmentVehicle(name.abuchen.portfolio.model.InvestmentVehicle) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Set(java.util.Set) HashMap(java.util.HashMap) Security(name.abuchen.portfolio.model.Security) Classification(name.abuchen.portfolio.model.Classification) Visitor(name.abuchen.portfolio.model.Taxonomy.Visitor) HashSet(java.util.HashSet) Unit(name.abuchen.portfolio.model.Transaction.Unit) Map(java.util.Map) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) Assignment(name.abuchen.portfolio.model.Classification.Assignment) Portfolio(name.abuchen.portfolio.model.Portfolio) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction)

Example 29 with PortfolioTransaction

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

the class ClientClassificationFilter method addBuySellT.

private void addBuySellT(CalculationState state, Portfolio portfolio, PortfolioTransaction t) {
    int securityWeight = state.getWeight(t.getSecurity());
    long taxes = value(t.getUnitSum(Unit.Type.TAX).getAmount(), securityWeight);
    long securityAmount = value(t.getAmount(), securityWeight);
    securityAmount = t.getType() == PortfolioTransaction.Type.BUY ? securityAmount - taxes : securityAmount + taxes;
    Account account = (Account) t.getCrossEntry().getCrossOwner(t);
    int accountWeight = state.getWeight(account);
    long accountAmount = value(t.getAmount(), accountWeight);
    long commonAmount = Math.min(securityAmount, accountAmount);
    int commonWeight = (int) Math.round(((double) commonAmount / (double) securityAmount) * securityWeight);
    // create a buy/sell transactions with the amount shared by the account
    // assignment and the security assignment
    BuySellEntry copy = new BuySellEntry(state.asReadOnly(portfolio), state.account2readonly.get(account));
    copy.setDate(t.getDateTime());
    copy.setCurrencyCode(t.getCurrencyCode());
    copy.setSecurity(t.getSecurity());
    copy.setType(t.getType());
    copy.setNote(t.getNote());
    copy.setShares(value(t.getShares(), commonWeight));
    copy.setAmount(commonAmount);
    // copy all units (except for taxes) over to the new transaction
    t.getUnits().filter(u -> u.getType() != Unit.Type.TAX).forEach(u -> copy.getPortfolioTransaction().addUnit(value(u, commonWeight)));
    state.asReadOnly(portfolio).internalAddTransaction(copy.getPortfolioTransaction());
    state.asReadOnly(account).internalAddTransaction(copy.getAccountTransaction());
    if (accountAmount - commonAmount > 0) {
        AccountTransaction ta = new AccountTransaction(t.getDateTime(), t.getCurrencyCode(), accountAmount - commonAmount, null, t.getType() == PortfolioTransaction.Type.BUY ? AccountTransaction.Type.REMOVAL : AccountTransaction.Type.DEPOSIT);
        state.asReadOnly(account).internalAddTransaction(ta);
    }
    if (securityAmount - commonAmount > 0) {
        PortfolioTransaction tp = new PortfolioTransaction();
        tp.setDateTime(t.getDateTime());
        tp.setCurrencyCode(t.getCurrencyCode());
        tp.setSecurity(t.getSecurity());
        tp.setShares(value(t.getShares(), securityWeight - commonWeight));
        tp.setType(t.getType() == PortfolioTransaction.Type.BUY ? PortfolioTransaction.Type.DELIVERY_INBOUND : PortfolioTransaction.Type.DELIVERY_OUTBOUND);
        tp.setAmount(securityAmount - commonAmount);
        t.getUnits().filter(u -> u.getType() != Unit.Type.TAX).forEach(u -> tp.addUnit(value(u, securityWeight - commonWeight)));
        state.asReadOnly(portfolio).internalAddTransaction(tp);
    }
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Money(name.abuchen.portfolio.money.Money) Client(name.abuchen.portfolio.model.Client) Account(name.abuchen.portfolio.model.Account) InvestmentVehicle(name.abuchen.portfolio.model.InvestmentVehicle) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Set(java.util.Set) HashMap(java.util.HashMap) Security(name.abuchen.portfolio.model.Security) Classification(name.abuchen.portfolio.model.Classification) Visitor(name.abuchen.portfolio.model.Taxonomy.Visitor) HashSet(java.util.HashSet) Unit(name.abuchen.portfolio.model.Transaction.Unit) Map(java.util.Map) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) Assignment(name.abuchen.portfolio.model.Classification.Assignment) Portfolio(name.abuchen.portfolio.model.Portfolio) Account(name.abuchen.portfolio.model.Account) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction)

Example 30 with PortfolioTransaction

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

the class QuoteFromTransactionExtractor method extractQuotes.

/**
 * Extracts the quotes for the given {@link Security}.
 *
 * @param security
 *            {@link Security}
 * @return true if quotes were found, else false
 */
public boolean extractQuotes(Security security) {
    boolean bChanges = false;
    SecurityPrice pLatest = null;
    // walk through all all transactions for securiy
    for (TransactionPair<?> p : security.getTransactions(client)) {
        Transaction t = p.getTransaction();
        // check the type of the transaction
        if (t instanceof PortfolioTransaction) {
            PortfolioTransaction pt = (PortfolioTransaction) t;
            // get date and quote and build a price from it
            Quote q = pt.getGrossPricePerShare();
            LocalDate d = pt.getDateTime().toLocalDate();
            SecurityPrice price = new SecurityPrice(d, q.getAmount());
            bChanges |= security.addPrice(price);
            // remember the lates price
            if ((pLatest == null) || d.isAfter(pLatest.getDate())) {
                pLatest = price;
            }
        }
    }
    // set the latest price (if at leas one price was found)
    if (pLatest != null) {
        LatestSecurityPrice lsp = new LatestSecurityPrice(pLatest.getDate(), pLatest.getValue());
        bChanges |= security.setLatest(lsp);
    }
    return bChanges;
}
Also used : Quote(name.abuchen.portfolio.money.Quote) LatestSecurityPrice(name.abuchen.portfolio.model.LatestSecurityPrice) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Transaction(name.abuchen.portfolio.model.Transaction) LatestSecurityPrice(name.abuchen.portfolio.model.LatestSecurityPrice) SecurityPrice(name.abuchen.portfolio.model.SecurityPrice) LocalDate(java.time.LocalDate)

Aggregations

PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)131 Security (name.abuchen.portfolio.model.Security)85 Test (org.junit.Test)84 Client (name.abuchen.portfolio.model.Client)70 ArrayList (java.util.ArrayList)64 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)64 Unit (name.abuchen.portfolio.model.Transaction.Unit)56 Money (name.abuchen.portfolio.money.Money)56 IOException (java.io.IOException)47 BuySellEntry (name.abuchen.portfolio.model.BuySellEntry)44 List (java.util.List)43 CurrencyUnit (name.abuchen.portfolio.money.CurrencyUnit)42 Values (name.abuchen.portfolio.money.Values)41 LocalDateTime (java.time.LocalDateTime)39 Portfolio (name.abuchen.portfolio.model.Portfolio)39 CoreMatchers.is (org.hamcrest.CoreMatchers.is)35 Assert.assertThat (org.junit.Assert.assertThat)35 TestCurrencyConverter (name.abuchen.portfolio.TestCurrencyConverter)33 Collectors (java.util.stream.Collectors)30 BuySellEntryItem (name.abuchen.portfolio.datatransfer.Extractor.BuySellEntryItem)28