Search in sources :

Example 1 with TestCurrencyConverter

use of name.abuchen.portfolio.TestCurrencyConverter in project portfolio by buchen.

the class CrossEntryCheckTest method testThatAccountTransactionsWithoutSecurity.

@Test
public void testThatAccountTransactionsWithoutSecurity() {
    Portfolio second = new Portfolio();
    client.addPortfolio(second);
    account.addTransaction(new AccountTransaction(LocalDateTime.now(), CurrencyUnit.EUR, 1, null, AccountTransaction.Type.BUY));
    List<Issue> issues = new CrossEntryCheck().execute(client);
    assertThat(issues.size(), is(1));
    assertThat(issues.get(0).getAvailableFixes().get(0), is(instanceOf(DeleteTransactionFix.class)));
    applyFixes(client, issues);
    ClientSnapshot.create(client, new TestCurrencyConverter(), LocalDate.now());
}
Also used : Issue(name.abuchen.portfolio.checks.Issue) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Portfolio(name.abuchen.portfolio.model.Portfolio) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Test(org.junit.Test)

Example 2 with TestCurrencyConverter

use of name.abuchen.portfolio.TestCurrencyConverter in project portfolio by buchen.

the class InvestmentPlanTest method testGenerationOfTransaction.

@Test
public void testGenerationOfTransaction() {
    Client client = new Client();
    Security security = new SecurityBuilder().addTo(client);
    Account account = new AccountBuilder().addTo(client);
    Portfolio portfolio = new PortfolioBuilder(account).addTo(client);
    InvestmentPlan investmentPlan = new InvestmentPlan();
    investmentPlan.setPortfolio(portfolio);
    investmentPlan.setSecurity(security);
    investmentPlan.setAmount(Values.Amount.factorize(100));
    investmentPlan.setInterval(1);
    investmentPlan.setStart(LocalDateTime.parse("2016-01-31T00:00:00"));
    investmentPlan.generateTransactions(new TestCurrencyConverter());
    List<PortfolioTransaction> tx = investmentPlan.getTransactions().stream().filter(t -> t.getDateTime().isBefore(LocalDateTime.parse("2017-04-10T00:00"))).collect(Collectors.toList());
    assertThat(tx.size(), is(15));
    tx = investmentPlan.getTransactions().stream().filter(t -> t.getDateTime().getYear() == 2016 && t.getDateTime().getMonth() == Month.MAY).collect(Collectors.toList());
    // May 2016 should contain two transactions:
    // one "spilled over" from April as 30 April is a Saturday
    // and the regular one from 31 May
    assertThat(tx.size(), is(2));
    assertThat(tx.get(0).getDateTime(), is(LocalDateTime.parse("2016-05-02T00:00")));
    assertThat(tx.get(1).getDateTime(), is(LocalDateTime.parse("2016-05-31T00:00")));
    // check that delta generation of transactions also takes into account
    // the transaction "spilled over" into the next month
    investmentPlan.getTransactions().stream().filter(t -> t.getDateTime().isAfter(LocalDateTime.parse("2016-05-10T00:00"))).collect(Collectors.toList()).forEach(t -> investmentPlan.removeTransaction(t));
    List<PortfolioTransaction> newlyGenerated = investmentPlan.generateTransactions(new TestCurrencyConverter());
    assertThat(newlyGenerated.isEmpty(), is(false));
    assertThat(newlyGenerated.get(0).getDateTime(), is(LocalDateTime.parse("2016-05-31T00:00")));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assert.assertThat(org.junit.Assert.assertThat) AccountBuilder(name.abuchen.portfolio.AccountBuilder) List(java.util.List) Values(name.abuchen.portfolio.money.Values) Month(java.time.Month) LocalDateTime(java.time.LocalDateTime) Test(org.junit.Test) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) Collectors(java.util.stream.Collectors) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) AccountBuilder(name.abuchen.portfolio.AccountBuilder) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) Test(org.junit.Test)

Example 3 with TestCurrencyConverter

use of name.abuchen.portfolio.TestCurrencyConverter in project portfolio by buchen.

the class AccountPerformanceTaxRefundTestCase method testAccountPerformanceTaxRefund.

/**
 * Feature: when calculating the performance of an account, do include taxes
 * and tax refunds but only those that are not paid for a security.
 */
@Test
public void testAccountPerformanceTaxRefund() throws IOException {
    Client client = ClientFactory.load(SecurityTestCase.class.getResourceAsStream("account_performance_tax_refund.xml"));
    Account account = client.getAccounts().get(0);
    ReportingPeriod period = new ReportingPeriod.FromXtoY(LocalDate.parse("2013-12-06"), LocalDate.parse("2014-12-06"));
    AccountTransaction deposit = account.getTransactions().get(0);
    // no changes in holdings, ttwror must be:
    double startValue = deposit.getAmount();
    double endValue = account.getCurrentAmount(LocalDateTime.of(2016, 1, 1, 10, 00));
    double ttwror = (endValue / startValue) - 1;
    List<Exception> warnings = new ArrayList<>();
    CurrencyConverter converter = new TestCurrencyConverter();
    PerformanceIndex accountPerformance = PerformanceIndex.forAccount(client, converter, account, period, warnings);
    assertThat(warnings, empty());
    double calculatedTtwror = accountPerformance.getFinalAccumulatedPercentage();
    assertThat(calculatedTtwror, closeTo(ttwror, 0.0001));
    // if the tax_refund is for a security, it must not be included in the
    // performance of the account
    AccountTransaction taxRefund = account.getTransactions().get(1);
    assertThat(taxRefund.getType(), is(AccountTransaction.Type.TAX_REFUND));
    taxRefund.setSecurity(new Security());
    accountPerformance = PerformanceIndex.forAccount(client, converter, account, period, warnings);
    assertThat(warnings, empty());
    assertThat(accountPerformance.getFinalAccumulatedPercentage(), lessThan(calculatedTtwror));
}
Also used : Account(name.abuchen.portfolio.model.Account) ArrayList(java.util.ArrayList) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Security(name.abuchen.portfolio.model.Security) IOException(java.io.IOException) PerformanceIndex(name.abuchen.portfolio.snapshot.PerformanceIndex) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) ReportingPeriod(name.abuchen.portfolio.snapshot.ReportingPeriod) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Client(name.abuchen.portfolio.model.Client) Test(org.junit.Test)

Example 4 with TestCurrencyConverter

use of name.abuchen.portfolio.TestCurrencyConverter in project portfolio by buchen.

the class ClassificationTestCase method testCase11_ForexSecurity100_ForexAccountTransfers.

@Test
public void testCase11_ForexSecurity100_ForexAccountTransfers() {
    Taxonomy case11 = client.getTaxonomies().stream().filter(t -> "case_11".equals(t.getName())).findAny().orElseThrow(IllegalArgumentException::new);
    // check performance of 'subcategory'
    Classification ccategory = case11.getAllClassifications().stream().filter(c -> "category".equals(c.getName())).findAny().orElseThrow(IllegalArgumentException::new);
    Client categoryClient = new ClientClassificationFilter(ccategory).filter(client);
    List<PortfolioTransaction> txp = categoryClient.getPortfolios().stream().flatMap(p -> p.getTransactions().stream()).collect(Collectors.toList());
    assertThat(txp, hasItem(allOf(// 
    hasProperty("dateTime", is(LocalDateTime.parse("2012-01-16T00:00"))), // 
    hasProperty("type", is(PortfolioTransaction.Type.BUY)), hasProperty("monetaryAmount", is(Money.of(CurrencyUnit.USD, Values.Amount.factorize(476.48 * 0.7d)))))));
    assertThat(txp, hasItem(allOf(// 
    hasProperty("dateTime", is(LocalDateTime.parse("2012-01-16T00:00"))), // 
    hasProperty("type", is(PortfolioTransaction.Type.DELIVERY_INBOUND)), hasProperty("monetaryAmount", is(Money.of(CurrencyUnit.USD, Values.Amount.factorize((476.48 - 10) - (476.48 * 0.7d))))))));
    // check that shares of the split transaction add up to the expected
    // value, e.g. the total value of the original transaction
    assertThat(txp.stream().filter(t -> t.getDateTime().equals(LocalDateTime.parse("2012-01-16T00:00"))).mapToLong(PortfolioTransaction::getShares).sum(), is(Values.Share.factorize(1)));
    List<AccountTransaction> txa = categoryClient.getAccounts().stream().filter(a -> "Account Forex".equals(a.getName())).flatMap(a -> a.getTransactions().stream()).collect(Collectors.toList());
    assertThat(txa, hasItem(allOf(// 
    hasProperty("dateTime", is(LocalDateTime.parse("2012-01-16T00:00"))), // 
    hasProperty("type", is(AccountTransaction.Type.BUY)), hasProperty("monetaryAmount", is(Money.of(CurrencyUnit.USD, Values.Amount.factorize(476.48 * 0.7d)))))));
    // check cash transfers
    assertThat(txa, hasItem(allOf(// 
    hasProperty("dateTime", is(LocalDateTime.parse("2012-01-17T00:00"))), // 
    hasProperty("type", is(AccountTransaction.Type.TRANSFER_OUT)), hasProperty("monetaryAmount", is(Money.of(CurrencyUnit.USD, Values.Amount.factorize(100 * 0.7d)))))));
    txa = categoryClient.getAccounts().stream().filter(a -> "Konto 3".equals(a.getName())).flatMap(a -> a.getTransactions().stream()).collect(Collectors.toList());
    assertThat(txa, hasItem(allOf(// 
    hasProperty("dateTime", is(LocalDateTime.parse("2012-01-17T00:00"))), // 
    hasProperty("type", is(AccountTransaction.Type.TRANSFER_IN)), hasProperty("monetaryAmount", is(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(78.19 * 0.7d)))))));
    assertThat(txa, hasItem(allOf(// 
    hasProperty("dateTime", is(LocalDateTime.parse("2012-01-17T00:00"))), // 
    hasProperty("type", is(AccountTransaction.Type.DEPOSIT)), hasProperty("monetaryAmount", is(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(78.19 * 0.3d)))))));
    // create index
    List<Exception> warnings = new ArrayList<>();
    PerformanceIndex index_subcategory = PerformanceIndex.forClassification(client, converter, ccategory, interval, warnings);
    assertThat(warnings.isEmpty(), is(true));
    // 686.84 EUR (Konto 3 EUR)
    // 296.46 USD * 1.2141 (exchange rate in TestCurrencyConverter)
    // 2 * Apple 456.4799 / 1.2141 (quote for Apple)
    assertThat(index_subcategory.getTotals()[index_subcategory.getTotals().length - 1], is(// 
    Values.Amount.factorize(686.84) + Values.Amount.factorize(296.46 / 1.2141) + Values.Amount.factorize(2 * 456.4799 / 1.2141)));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) Money(name.abuchen.portfolio.money.Money) Values(name.abuchen.portfolio.money.Values) Client(name.abuchen.portfolio.model.Client) BeforeClass(org.junit.BeforeClass) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) LocalDateTime(java.time.LocalDateTime) Classification(name.abuchen.portfolio.model.Classification) ArrayList(java.util.ArrayList) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Matchers.hasProperty(org.hamcrest.Matchers.hasProperty) Assert.assertThat(org.junit.Assert.assertThat) ClientClassificationFilter(name.abuchen.portfolio.snapshot.filter.ClientClassificationFilter) CoreMatchers.allOf(org.hamcrest.CoreMatchers.allOf) ClientFactory(name.abuchen.portfolio.model.ClientFactory) ReportingPeriod(name.abuchen.portfolio.snapshot.ReportingPeriod) Taxonomy(name.abuchen.portfolio.model.Taxonomy) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) IOException(java.io.IOException) Test(org.junit.Test) Security(name.abuchen.portfolio.model.Security) Collectors(java.util.stream.Collectors) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) PerformanceIndex(name.abuchen.portfolio.snapshot.PerformanceIndex) List(java.util.List) LocalDate(java.time.LocalDate) Taxonomy(name.abuchen.portfolio.model.Taxonomy) ArrayList(java.util.ArrayList) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) IOException(java.io.IOException) PerformanceIndex(name.abuchen.portfolio.snapshot.PerformanceIndex) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) ClientClassificationFilter(name.abuchen.portfolio.snapshot.filter.ClientClassificationFilter) Classification(name.abuchen.portfolio.model.Classification) Client(name.abuchen.portfolio.model.Client) Test(org.junit.Test)

Example 5 with TestCurrencyConverter

use of name.abuchen.portfolio.TestCurrencyConverter in project portfolio by buchen.

the class SecurityPerformanceTaxRefundTestCase method testSecurityPerformanceTaxRefundAllSold.

/**
 * Feature: Same as {@link #testSecurityPerformanceTaxRefunds} except that
 * now the security has been sold. Taxes paid when selling the security must
 * be ignored.
 */
@Test
public void testSecurityPerformanceTaxRefundAllSold() throws IOException {
    Client client = ClientFactory.load(SecurityTestCase.class.getResourceAsStream("security_performance_tax_refund_all_sold.xml"));
    Portfolio portfolio = client.getPortfolios().get(0);
    PortfolioTransaction delivery = portfolio.getTransactions().get(0);
    PortfolioTransaction sell = portfolio.getTransactions().get(1);
    ReportingPeriod period = new ReportingPeriod.FromXtoY(LocalDate.parse("2013-12-06"), LocalDate.parse("2014-12-06"));
    TestCurrencyConverter converter = new TestCurrencyConverter();
    SecurityPerformanceSnapshot snapshot = SecurityPerformanceSnapshot.create(client, converter, period);
    SecurityPerformanceRecord record = snapshot.getRecords().get(0);
    assertThat(record.getSecurity().getName(), is("Basf SE"));
    assertThat(record.getSharesHeld(), is(0L));
    // no changes in holdings, ttwror must (without taxes and tax refunds):
    double startValue = delivery.getAmount() - delivery.getUnitSum(Unit.Type.TAX).getAmount();
    double endValue = sell.getAmount() + sell.getUnitSum(Unit.Type.TAX).getAmount();
    double ttwror = (endValue / startValue) - 1;
    assertThat(record.getTrueTimeWeightedRateOfReturn(), closeTo(ttwror, 0.0001));
    // accrued taxes must be 0 (paid 10 on delivery + 5 tax refund + 10
    // taxes on sell):
    assertThat(record.getTaxes(), is(Money.of(CurrencyUnit.EUR, 15_00L)));
    // accrued fees must be 20 (paid 10 on delivery + 10 on sell)
    assertThat(record.getFees(), is(Money.of(CurrencyUnit.EUR, 20_00L)));
    // make sure that tax refund is included in transactions
    assertThat(record.getTransactions(), hasItem(isA(AccountTransaction.class)));
    // ttwror of classification must be identical to ttwror of security
    assertThatTTWROROfClassificationWithSecurityIsIdentical(client, period, ttwror);
    // check client performance + performance of portfolio + account
    PerformanceIndex clientIndex = assertPerformanceOfClient(client, period, ttwror);
    // the performance of the portfolio (w/o account) includes taxes
    assertThatTTWROROfPortfolioIsLessThan(client, clientIndex, ttwror);
    // the irr must not include taxes as well (compared with Excel):
    assertThat(record.getIrr(), closeTo(-0.032248297, 0.0001));
}
Also used : ReportingPeriod(name.abuchen.portfolio.snapshot.ReportingPeriod) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Portfolio(name.abuchen.portfolio.model.Portfolio) SecurityPerformanceRecord(name.abuchen.portfolio.snapshot.security.SecurityPerformanceRecord) Client(name.abuchen.portfolio.model.Client) SecurityPerformanceSnapshot(name.abuchen.portfolio.snapshot.security.SecurityPerformanceSnapshot) PerformanceIndex(name.abuchen.portfolio.snapshot.PerformanceIndex) Test(org.junit.Test)

Aggregations

TestCurrencyConverter (name.abuchen.portfolio.TestCurrencyConverter)74 Test (org.junit.Test)72 Client (name.abuchen.portfolio.model.Client)55 Security (name.abuchen.portfolio.model.Security)46 CurrencyConverter (name.abuchen.portfolio.money.CurrencyConverter)38 ArrayList (java.util.ArrayList)31 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)26 SecurityBuilder (name.abuchen.portfolio.SecurityBuilder)23 Portfolio (name.abuchen.portfolio.model.Portfolio)21 AccountBuilder (name.abuchen.portfolio.AccountBuilder)19 PortfolioBuilder (name.abuchen.portfolio.PortfolioBuilder)19 LocalDate (java.time.LocalDate)18 Account (name.abuchen.portfolio.model.Account)18 SecurityPrice (name.abuchen.portfolio.model.SecurityPrice)15 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)13 ReportingPeriod (name.abuchen.portfolio.snapshot.ReportingPeriod)10 Taxonomy (name.abuchen.portfolio.model.Taxonomy)8 CurrencyUnit (name.abuchen.portfolio.money.CurrencyUnit)8 PerformanceIndex (name.abuchen.portfolio.snapshot.PerformanceIndex)8 IOException (java.io.IOException)7