Search in sources :

Example 1 with MutableMoney

use of name.abuchen.portfolio.money.MutableMoney in project portfolio by buchen.

the class ClientPerformanceSnapshot method determineTransferAmount.

/**
 * Determine the monetary amount when transferring cash between accounts.
 * Because the actual exchange rate of the transferal might differ from the
 * historical rate given by the exchange rate provider (e.g. ECB), we would
 * get rounding differences if we do not take the original amount. If the
 * transferal does not involve the term currency at all, we calculate the
 * average value out of both converted amounts.
 */
private Money determineTransferAmount(AccountTransaction t) {
    if (converter.getTermCurrency().equals(t.getCurrencyCode()))
        return t.getMonetaryAmount();
    Transaction other = t.getCrossEntry().getCrossTransaction(t);
    if (converter.getTermCurrency().equals(other.getCurrencyCode()))
        return other.getMonetaryAmount();
    MutableMoney m = MutableMoney.of(converter.getTermCurrency());
    m.add(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
    m.add(other.getMonetaryAmount().with(converter.at(t.getDateTime())));
    return m.divide(2).toMoney();
}
Also used : MutableMoney(name.abuchen.portfolio.money.MutableMoney) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Transaction(name.abuchen.portfolio.model.Transaction) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction)

Example 2 with MutableMoney

use of name.abuchen.portfolio.money.MutableMoney in project portfolio by buchen.

the class ClientPerformanceSnapshot method addCapitalGains.

private void addCapitalGains() {
    Map<Security, MutableMoney> valuation = new HashMap<>();
    for (Security s : client.getSecurities()) valuation.put(s, MutableMoney.of(converter.getTermCurrency()));
    snapshotStart.getJointPortfolio().getPositions().stream().forEach(p -> valuation.get(p.getInvestmentVehicle()).subtract(p.calculateValue().with(converter.at(snapshotStart.getTime()))));
    for (PortfolioTransaction t : snapshotStart.getJointPortfolio().getSource().getTransactions()) {
        if (!period.containsTransaction().test(t))
            continue;
        switch(t.getType()) {
            case BUY:
            case DELIVERY_INBOUND:
            case TRANSFER_IN:
                valuation.get(t.getSecurity()).subtract(t.getGrossValue().with(converter.at(t.getDateTime())));
                break;
            case SELL:
            case DELIVERY_OUTBOUND:
            case TRANSFER_OUT:
                valuation.get(t.getSecurity()).add(t.getGrossValue().with(converter.at(t.getDateTime())));
                break;
            default:
                throw new UnsupportedOperationException();
        }
    }
    snapshotEnd.getJointPortfolio().getPositions().stream().forEach(p -> valuation.get(p.getInvestmentVehicle()).add(p.calculateValue().with(converter.at(snapshotEnd.getTime()))));
    Category capitalGains = categories.get(CategoryType.CAPITAL_GAINS);
    // add securities w/ capital gains to the positions
    capitalGains.positions = // 
    valuation.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());
    // total capital gains -> sum it up
    capitalGains.valuation = // 
    capitalGains.positions.stream().map(// 
    p -> p.getValuation()).collect(MoneyCollectors.sum(converter.getTermCurrency()));
}
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) MutableMoney(name.abuchen.portfolio.money.MutableMoney) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) HashMap(java.util.HashMap) Security(name.abuchen.portfolio.model.Security)

Example 3 with MutableMoney

use of name.abuchen.portfolio.money.MutableMoney 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 4 with MutableMoney

use of name.abuchen.portfolio.money.MutableMoney in project portfolio by buchen.

the class ClientSnapshot method getMonetaryAssets.

public Money getMonetaryAssets() {
    if (this.assets == null) {
        MutableMoney sum = MutableMoney.of(getCurrencyCode());
        for (AccountSnapshot account : accounts) sum.add(account.getFunds());
        // use joint portfolio to reduce rounding errors if a security is
        // split across multiple portfolio
        sum.add(getJointPortfolio().getValue());
        this.assets = sum.toMoney();
    }
    return this.assets;
}
Also used : MutableMoney(name.abuchen.portfolio.money.MutableMoney)

Example 5 with MutableMoney

use of name.abuchen.portfolio.money.MutableMoney in project portfolio by buchen.

the class ClientPerformanceSnapshotTest method testCurrencyGainsWithTransferalInOtherCurrencies.

@Test
public void testCurrencyGainsWithTransferalInOtherCurrencies() {
    Client client = new Client();
    Account usd = // 
    new AccountBuilder("USD").deposit_("2015-01-01", // 
    1000_00).addTo(client);
    Account cad = // 
    new AccountBuilder("CAD").deposit_("2015-01-01", // 
    1000_00).addTo(client);
    // insert account transfer
    AccountTransferEntry entry = new AccountTransferEntry(usd, cad);
    entry.setDate(LocalDateTime.parse("2015-01-10T00:00"));
    AccountTransaction source = entry.getSourceTransaction();
    AccountTransaction target = entry.getTargetTransaction();
    source.setMonetaryAmount(Money.of("USD", 500_00));
    target.setMonetaryAmount(Money.of("CAD", 1000_00));
    source.addUnit(new Unit(Unit.Type.GROSS_VALUE, source.getMonetaryAmount(), target.getMonetaryAmount(), BigDecimal.valueOf(.5)));
    entry.insert();
    // check currency gain calculation of client performance snapshot
    CurrencyConverter converter = new TestCurrencyConverter();
    ClientPerformanceSnapshot snapshot = new // 
    ClientPerformanceSnapshot(// 
    client, // 
    converter, LocalDate.parse("2015-01-01"), LocalDate.parse("2015-01-15"));
    MutableMoney currencyGains = MutableMoney.of(converter.getTermCurrency());
    currencyGains.subtract(snapshot.getValue(CategoryType.INITIAL_VALUE));
    currencyGains.subtract(snapshot.getValue(CategoryType.CAPITAL_GAINS));
    currencyGains.subtract(snapshot.getValue(CategoryType.EARNINGS));
    currencyGains.add(snapshot.getValue(CategoryType.FEES));
    currencyGains.add(snapshot.getValue(CategoryType.TAXES));
    currencyGains.add(snapshot.getValue(CategoryType.TRANSFERS));
    currencyGains.add(snapshot.getValue(CategoryType.FINAL_VALUE));
    assertThat(snapshot.getCategoryByType(CategoryType.CURRENCY_GAINS).getValuation(), is(currencyGains.toMoney()));
}
Also used : Account(name.abuchen.portfolio.model.Account) MutableMoney(name.abuchen.portfolio.money.MutableMoney) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) AccountBuilder(name.abuchen.portfolio.AccountBuilder) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Client(name.abuchen.portfolio.model.Client) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) Unit(name.abuchen.portfolio.model.Transaction.Unit) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) Test(org.junit.Test)

Aggregations

MutableMoney (name.abuchen.portfolio.money.MutableMoney)14 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)9 Client (name.abuchen.portfolio.model.Client)7 Money (name.abuchen.portfolio.money.Money)7 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)6 Unit (name.abuchen.portfolio.model.Transaction.Unit)6 Messages (name.abuchen.portfolio.Messages)5 Transaction (name.abuchen.portfolio.model.Transaction)5 CurrencyConverter (name.abuchen.portfolio.money.CurrencyConverter)5 ArrayList (java.util.ArrayList)4 Account (name.abuchen.portfolio.model.Account)4 LocalDate (java.time.LocalDate)3 Collections (java.util.Collections)3 EnumMap (java.util.EnumMap)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 Portfolio (name.abuchen.portfolio.model.Portfolio)3 Security (name.abuchen.portfolio.model.Security)3