Search in sources :

Example 36 with CurrencyConverter

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

the class ClientPerformanceSnapshotTest method testCapitalGainsWithPartialSellDuringReportPeriod.

@Test
public void testCapitalGainsWithPartialSellDuringReportPeriod() {
    Client client = new Client();
    Security security = // 
    new SecurityBuilder().addPrice("2010-01-01", // 
    Values.Quote.factorize(100)).addPrice("2011-06-01", // 
    Values.Quote.factorize(110)).addTo(client);
    Account account = // 
    new AccountBuilder().deposit_("2010-01-01", // 
    1_00).withdraw("2011-01-15", // 
    99_00).addTo(client);
    // 
    new PortfolioBuilder(account).buy(security, "2010-01-01", Values.Share.factorize(10), // 
    1_00).sell(security, "2011-01-15", Values.Share.factorize(1), // 
    99_00).addTo(client);
    CurrencyConverter converter = new TestCurrencyConverter();
    ClientPerformanceSnapshot snapshot = new ClientPerformanceSnapshot(client, converter, startDate, endDate);
    assertThat(snapshot.getValue(CategoryType.INITIAL_VALUE), is(Money.of(CurrencyUnit.EUR, 1000_00)));
    assertThat(snapshot.getValue(CategoryType.EARNINGS), is(Money.of(CurrencyUnit.EUR, 0)));
    assertThat(snapshot.getValue(CategoryType.CAPITAL_GAINS), is(Money.of(CurrencyUnit.EUR, 10_00 * 9 + (99_00 - 100_00))));
    assertThat(snapshot.getValue(CategoryType.FINAL_VALUE), is(Money.of(CurrencyUnit.EUR, 110_00 * 9)));
    assertThat(snapshot.getAbsoluteDelta(), is(snapshot.getValue(CategoryType.FINAL_VALUE).subtract(snapshot.getValue(CategoryType.TRANSFERS)).subtract(snapshot.getValue(CategoryType.INITIAL_VALUE))));
}
Also used : Account(name.abuchen.portfolio.model.Account) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) AccountBuilder(name.abuchen.portfolio.AccountBuilder) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) Client(name.abuchen.portfolio.model.Client) Security(name.abuchen.portfolio.model.Security) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) Test(org.junit.Test)

Example 37 with CurrencyConverter

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

the class ClientPerformanceSnapshotTest method testCapitalGainsWithBuyDuringReportPeriod.

@Test
public void testCapitalGainsWithBuyDuringReportPeriod() {
    Client client = new Client();
    Security security = new Security();
    security.addPrice(new SecurityPrice(LocalDate.of(2010, Month.JANUARY, 1), Values.Quote.factorize(100)));
    security.addPrice(new SecurityPrice(LocalDate.of(2011, Month.JUNE, 1), Values.Quote.factorize(110)));
    client.addSecurity(security);
    Portfolio portfolio = new Portfolio();
    portfolio.setReferenceAccount(new Account());
    portfolio.addTransaction(new PortfolioTransaction(LocalDateTime.of(2010, Month.JANUARY, 1, 0, 0), CurrencyUnit.EUR, 1_00, security, Values.Share.factorize(10), PortfolioTransaction.Type.BUY, 0, 0));
    portfolio.addTransaction(new PortfolioTransaction(LocalDateTime.of(2011, Month.JANUARY, 15, 0, 0), CurrencyUnit.EUR, 99_00, security, Values.Share.factorize(1), PortfolioTransaction.Type.DELIVERY_INBOUND, 0, 0));
    client.addPortfolio(portfolio);
    CurrencyConverter converter = new TestCurrencyConverter();
    ClientPerformanceSnapshot snapshot = new ClientPerformanceSnapshot(client, converter, startDate, endDate);
    assertThat(snapshot.getValue(CategoryType.INITIAL_VALUE), is(Money.of(CurrencyUnit.EUR, 1000_00)));
    assertThat(snapshot.getValue(CategoryType.EARNINGS), is(Money.of(CurrencyUnit.EUR, 0)));
    assertThat(snapshot.getValue(CategoryType.CAPITAL_GAINS), is(Money.of(CurrencyUnit.EUR, 100_00 + (110_00 - 99_00))));
    assertThat(snapshot.getValue(CategoryType.FINAL_VALUE), is(Money.of(CurrencyUnit.EUR, 1210_00)));
    assertThat(snapshot.getAbsoluteDelta(), is(snapshot.getValue(CategoryType.FINAL_VALUE).subtract(snapshot.getValue(CategoryType.TRANSFERS)).subtract(snapshot.getValue(CategoryType.INITIAL_VALUE))));
}
Also used : Account(name.abuchen.portfolio.model.Account) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Portfolio(name.abuchen.portfolio.model.Portfolio) SecurityPrice(name.abuchen.portfolio.model.SecurityPrice) Client(name.abuchen.portfolio.model.Client) Security(name.abuchen.portfolio.model.Security) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) Test(org.junit.Test)

Example 38 with CurrencyConverter

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

the class ClientPerformanceSnapshotTest method testDepositPlusInterest.

@Test
public void testDepositPlusInterest() {
    Client client = new Client();
    Account account = new Account();
    account.addTransaction(new AccountTransaction(LocalDateTime.of(2010, Month.JANUARY, 1, 0, 0), CurrencyUnit.EUR, 1000_00, null, AccountTransaction.Type.DEPOSIT));
    account.addTransaction(new AccountTransaction(LocalDateTime.of(2011, Month.JUNE, 1, 0, 0), CurrencyUnit.EUR, 50_00, null, AccountTransaction.Type.INTEREST));
    client.addAccount(account);
    CurrencyConverter converter = new TestCurrencyConverter();
    ClientPerformanceSnapshot snapshot = new ClientPerformanceSnapshot(client, converter, startDate, endDate);
    assertNotNull(snapshot);
    assertNotNull(snapshot.getStartClientSnapshot());
    assertEquals(startDate, snapshot.getStartClientSnapshot().getTime());
    assertNotNull(snapshot.getEndClientSnapshot());
    assertEquals(endDate, snapshot.getEndClientSnapshot().getTime());
    List<Category> categories = snapshot.getCategories();
    assertNotNull(categories);
    assertThat(categories.size(), is(ClientPerformanceSnapshot.CategoryType.values().length));
    assertThat(snapshot.getValue(CategoryType.INITIAL_VALUE), is(Money.of(CurrencyUnit.EUR, 1000_00)));
    assertThat(snapshot.getValue(CategoryType.EARNINGS), is(Money.of(CurrencyUnit.EUR, 50_00)));
    assertThat(snapshot.getValue(CategoryType.FINAL_VALUE), is(Money.of(CurrencyUnit.EUR, 1050_00)));
    assertThat(snapshot.getAbsoluteDelta(), is(snapshot.getValue(CategoryType.FINAL_VALUE).subtract(snapshot.getValue(CategoryType.TRANSFERS)).subtract(snapshot.getValue(CategoryType.INITIAL_VALUE))));
}
Also used : Account(name.abuchen.portfolio.model.Account) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Category(name.abuchen.portfolio.snapshot.ClientPerformanceSnapshot.Category) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Client(name.abuchen.portfolio.model.Client) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) Test(org.junit.Test)

Example 39 with CurrencyConverter

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

the class AccountListView method updateChart.

private void updateChart(Account account) {
    try {
        accountBalanceChart.suspendUpdate(true);
        for (ISeries s : accountBalanceChart.getSeriesSet().getSeries()) accountBalanceChart.getSeriesSet().deleteSeries(s.getId());
        if (account == null)
            return;
        List<AccountTransaction> tx = account.getTransactions();
        if (tx.isEmpty())
            return;
        CurrencyConverter converter = new CurrencyConverterImpl(factory, account.getCurrencyCode());
        Collections.sort(tx, new Transaction.ByDate());
        LocalDate now = LocalDate.now();
        LocalDate start = tx.get(0).getDateTime().toLocalDate();
        LocalDate end = tx.get(tx.size() - 1).getDateTime().toLocalDate();
        if (now.isAfter(end))
            end = now;
        if (now.isBefore(start))
            start = now;
        int days = (int) ChronoUnit.DAYS.between(start, end) + 2;
        LocalDate[] dates = new LocalDate[days];
        double[] values = new double[days];
        dates[0] = start.minusDays(1);
        values[0] = 0d;
        for (int ii = 1; ii < dates.length; ii++) {
            values[ii] = // 
            AccountSnapshot.create(account, converter, start).getFunds().getAmount() / Values.Amount.divider();
            dates[ii] = start;
            start = start.plusDays(1);
        }
        accountBalanceChart.addDateSeries(dates, values, Colors.CASH, account.getName());
        accountBalanceChart.adjustRange();
    } finally {
        accountBalanceChart.suspendUpdate(false);
    }
}
Also used : Transaction(name.abuchen.portfolio.model.Transaction) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) ISeries(org.swtchart.ISeries) LocalDate(java.time.LocalDate) CurrencyConverterImpl(name.abuchen.portfolio.money.CurrencyConverterImpl) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter)

Example 40 with CurrencyConverter

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

the class AbstractSecurityTransactionModel method updateSharesAndQuote.

protected void updateSharesAndQuote() {
    // do not auto-suggest shares and quote when editing an existing transaction
    if (hasSource())
        return;
    if (type == PortfolioTransaction.Type.SELL || type == PortfolioTransaction.Type.DELIVERY_OUTBOUND) {
        boolean hasPosition = false;
        if (portfolio != null) {
            // since the security position has always the currency of the
            // investment vehicle, actually no conversion is needed. Hence
            // we can use an arbitrary converter.
            CurrencyConverter converter = new CurrencyConverterImpl(getExchangeRateProviderFactory(), CurrencyUnit.EUR);
            PortfolioSnapshot snapshot = PortfolioSnapshot.create(portfolio, converter, date);
            SecurityPosition position = snapshot.getPositionsBySecurity().get(security);
            if (position != null) {
                setShares(position.getShares());
                setTotal(position.calculateValue().getAmount());
                hasPosition = true;
            }
        }
        if (!hasPosition) {
            setShares(0);
            setQuote(new BigDecimal(security.getSecurityPrice(date).getValue() / Values.Quote.divider()));
        }
    } else {
        setQuote(new BigDecimal(security.getSecurityPrice(date).getValue() / Values.Quote.divider()));
    }
}
Also used : SecurityPosition(name.abuchen.portfolio.snapshot.SecurityPosition) PortfolioSnapshot(name.abuchen.portfolio.snapshot.PortfolioSnapshot) CurrencyConverterImpl(name.abuchen.portfolio.money.CurrencyConverterImpl) BigDecimal(java.math.BigDecimal) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter)

Aggregations

CurrencyConverter (name.abuchen.portfolio.money.CurrencyConverter)53 Client (name.abuchen.portfolio.model.Client)41 TestCurrencyConverter (name.abuchen.portfolio.TestCurrencyConverter)38 Test (org.junit.Test)36 Security (name.abuchen.portfolio.model.Security)19 ArrayList (java.util.ArrayList)18 AccountBuilder (name.abuchen.portfolio.AccountBuilder)15 Account (name.abuchen.portfolio.model.Account)15 LocalDate (java.time.LocalDate)13 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)12 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)11 SecurityBuilder (name.abuchen.portfolio.SecurityBuilder)9 CurrencyConverterImpl (name.abuchen.portfolio.money.CurrencyConverterImpl)9 Portfolio (name.abuchen.portfolio.model.Portfolio)7 SecurityPrice (name.abuchen.portfolio.model.SecurityPrice)6 Transaction (name.abuchen.portfolio.model.Transaction)6 Unit (name.abuchen.portfolio.model.Transaction.Unit)6 ClientSnapshot (name.abuchen.portfolio.snapshot.ClientSnapshot)6 ReportingPeriod (name.abuchen.portfolio.snapshot.ReportingPeriod)6 Classification (name.abuchen.portfolio.model.Classification)5