Search in sources :

Example 1 with SecurityPerformanceSnapshot

use of name.abuchen.portfolio.snapshot.security.SecurityPerformanceSnapshot in project portfolio by buchen.

the class CurrencyTestCase method testFIFOPurchasePriceWithForex.

@Test
public void testFIFOPurchasePriceWithForex() {
    ClientSnapshot snapshot = ClientSnapshot.create(client, converter, LocalDate.parse("2015-08-09"));
    // 1.1. ........ -> 454.60 EUR
    // 1.1. 571.90 $ -> 471.05 EUR (exchange rate: 1.2141)
    // 3.8. 577.60 $ -> 498.45 EUR (exchange rate: 1.1588)
    AssetPosition position = snapshot.getPositionsByVehicle().get(securityUSD);
    assertThat(position.getPosition().getShares(), is(Values.Share.factorize(15)));
    assertThat(position.getFIFOPurchaseValue(), is(Money.of(CurrencyUnit.EUR, 454_60 + 471_05 + 498_45)));
    ReportingPeriod period = new ReportingPeriod.FromXtoY(LocalDate.parse("2014-12-31"), LocalDate.parse("2015-08-10"));
    SecurityPerformanceSnapshot performance = SecurityPerformanceSnapshot.create(client, converter, period);
    SecurityPerformanceRecord record = performance.getRecords().stream().filter(r -> r.getSecurity() == securityUSD).findAny().get();
    assertThat(record.getSharesHeld(), is(Values.Share.factorize(15)));
    assertThat(record.getFifoCost(), is(Money.of(CurrencyUnit.EUR, 454_60 + 471_05 + 498_45)));
}
Also used : ClientSnapshot(name.abuchen.portfolio.snapshot.ClientSnapshot) ReportingPeriod(name.abuchen.portfolio.snapshot.ReportingPeriod) SecurityPerformanceRecord(name.abuchen.portfolio.snapshot.security.SecurityPerformanceRecord) SecurityPerformanceSnapshot(name.abuchen.portfolio.snapshot.security.SecurityPerformanceSnapshot) AssetPosition(name.abuchen.portfolio.snapshot.AssetPosition) Test(org.junit.Test)

Example 2 with SecurityPerformanceSnapshot

use of name.abuchen.portfolio.snapshot.security.SecurityPerformanceSnapshot 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)

Example 3 with SecurityPerformanceSnapshot

use of name.abuchen.portfolio.snapshot.security.SecurityPerformanceSnapshot in project portfolio by buchen.

the class SecurityPerformanceTaxRefundTestCase method testSecurityPerformanceTaxRefund.

/**
 * Feature: when calculating the performance of a security, do not include
 * taxes and tax refunds. Include taxes and tax refunds only when
 * calculating a performance of a porfolio and/or client.
 */
@Test
public void testSecurityPerformanceTaxRefund() throws IOException {
    Client client = ClientFactory.load(SecurityTestCase.class.getResourceAsStream("security_performance_tax_refund.xml"));
    Security security = client.getSecurities().get(0);
    Portfolio portfolio = client.getPortfolios().get(0);
    PortfolioTransaction delivery = portfolio.getTransactions().get(0);
    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"));
    // no changes in holdings, ttwror must (without taxes and tax refunds):
    double startValue = delivery.getAmount() - delivery.getUnitSum(Unit.Type.TAX).getAmount();
    double endValue = delivery.getShares() * security.getSecurityPrice(LocalDate.parse("2014-12-06")).getValue() / Values.Share.divider() / Values.Quote.dividerToMoney();
    double ttwror = (endValue / startValue) - 1;
    assertThat(record.getTrueTimeWeightedRateOfReturn(), closeTo(ttwror, 0.0001));
    // accrued taxes must be 5 (paid 10 on delivery + 5 tax refund):
    assertThat(record.getTaxes(), is(Money.of(CurrencyUnit.EUR, 5_00L)));
    // accrued fees must be 10 (paid 10 on delivery)
    assertThat(record.getFees(), is(Money.of(CurrencyUnit.EUR, 10_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
    // in this sample file, the valuation of the security drops by 971
    // euros. However, the client has total valuation of 8406 while the
    // portfolio has valuation of only 8401 as it does not include the tax
    // refund. Therefore the performances of the porfolio is worse than that
    // of the client.
    assertThatTTWROROfPortfolioIsLessThan(client, clientIndex, ttwror);
    // the irr must not include taxes as well (compared with Excel):
    assertThat(record.getIrr(), closeTo(-0.030745789, 0.0001));
    // ensure the performance of the account is zero
    List<Exception> warnings = new ArrayList<Exception>();
    PerformanceIndex accountIndex = PerformanceIndex.forAccount(client, converter, client.getAccounts().get(0), period, warnings);
    assertThat(warnings, empty());
    assertThat(accountIndex.getFinalAccumulatedPercentage(), is(0d));
}
Also used : Portfolio(name.abuchen.portfolio.model.Portfolio) ArrayList(java.util.ArrayList) SecurityPerformanceRecord(name.abuchen.portfolio.snapshot.security.SecurityPerformanceRecord) Security(name.abuchen.portfolio.model.Security) SecurityPerformanceSnapshot(name.abuchen.portfolio.snapshot.security.SecurityPerformanceSnapshot) PerformanceIndex(name.abuchen.portfolio.snapshot.PerformanceIndex) IOException(java.io.IOException) ReportingPeriod(name.abuchen.portfolio.snapshot.ReportingPeriod) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Client(name.abuchen.portfolio.model.Client) Test(org.junit.Test)

Example 4 with SecurityPerformanceSnapshot

use of name.abuchen.portfolio.snapshot.security.SecurityPerformanceSnapshot in project portfolio by buchen.

the class SecurityTaxAndFeeAccountTransactionsTestCase method testAdidas.

@Test
public void testAdidas() {
    SecurityPerformanceSnapshot snapshot = SecurityPerformanceSnapshot.create(client, converter, interval);
    SecurityPerformanceRecord record = snapshot.getRecords().stream().filter(r -> r.getSecurity().equals(adidas)).findAny().orElseThrow(IllegalArgumentException::new);
    assertThat(record.getTransactions().size(), is(6));
    // fees and taxes from the buy transaction + separate transactions
    assertThat(record.getFees(), is(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(10d + 10d - 5d))));
    assertThat(record.getTaxes(), is(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(10d + 25d - 5d))));
    // delta is end value - starting value including all fees and taxes
    assertThat(record.getDelta(), is(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(1456.5 - 1533 - 10 + 5 - 25 + 5))));
    // IRR as calculated in Excel
    // @formatter:off
    // date          w/ tax       w/o tax
    // 02.01.17     -1533         -1523
    // 09.01.17       -10           -10
    // 10.01.17         5             5
    // 11.01.17       -25             0
    // 12.01.17         5             0
    // 31.01.17      1456,5        1456,5
    // XIRR    -0,573350433  -0,453151944
    // @formatter:on
    assertThat(record.getIrr(), closeTo(-0.453151944d, 1.0e-8));
    // check filtering - ClientSecurityFilter
    Client filteredClient = new ClientSecurityFilter(adidas).filter(client);
    checkFilteredClientAdidas(filteredClient, 1.0);
    // check filtering - ClientClassificationFilter
    Taxonomy case1 = client.getTaxonomies().stream().filter(t -> "case_full_classification_adidas".equals(t.getName())).findAny().orElseThrow(IllegalArgumentException::new);
    Classification classification = case1.getAllClassifications().stream().filter(c -> "category_security".equals(c.getName())).findAny().orElseThrow(IllegalArgumentException::new);
    filteredClient = new ClientClassificationFilter(classification).filter(client);
    checkFilteredClientAdidas(filteredClient, 1.0);
    // TTWROR must be identical to one calculated via ClientSecurityFilter
    // (implicitly used by the SecurityPeformanceSnapshot)
    PerformanceIndex index = PerformanceIndex.forClassification(client, converter, classification, interval, new ArrayList<>());
    assertThat(index.getFinalAccumulatedPercentage(), is(record.getTrueTimeWeightedRateOfReturn()));
    // a partial assignment of the security should be identical to the full
    // assignment - only the weight differs
    Taxonomy case2 = client.getTaxonomies().stream().filter(t -> "case_partial_classification_adidas".equals(t.getName())).findAny().orElseThrow(IllegalArgumentException::new);
    classification = case2.getAllClassifications().stream().filter(c -> "category_security".equals(c.getName())).findAny().orElseThrow(IllegalArgumentException::new);
    filteredClient = new ClientClassificationFilter(classification).filter(client);
    checkFilteredClientAdidas(filteredClient, 0.5);
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) 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) SecurityPerformanceSnapshot(name.abuchen.portfolio.snapshot.security.SecurityPerformanceSnapshot) 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) PortfolioClientFilter(name.abuchen.portfolio.snapshot.filter.PortfolioClientFilter) SecurityPerformanceRecord(name.abuchen.portfolio.snapshot.security.SecurityPerformanceRecord) Collectors(java.util.stream.Collectors) PerformanceIndex(name.abuchen.portfolio.snapshot.PerformanceIndex) List(java.util.List) LocalDate(java.time.LocalDate) ClientSecurityFilter(name.abuchen.portfolio.snapshot.filter.ClientSecurityFilter) IsCloseTo.closeTo(org.hamcrest.number.IsCloseTo.closeTo) ClientSnapshot(name.abuchen.portfolio.snapshot.ClientSnapshot) ClientSecurityFilter(name.abuchen.portfolio.snapshot.filter.ClientSecurityFilter) Taxonomy(name.abuchen.portfolio.model.Taxonomy) ClientClassificationFilter(name.abuchen.portfolio.snapshot.filter.ClientClassificationFilter) Classification(name.abuchen.portfolio.model.Classification) SecurityPerformanceRecord(name.abuchen.portfolio.snapshot.security.SecurityPerformanceRecord) SecurityPerformanceSnapshot(name.abuchen.portfolio.snapshot.security.SecurityPerformanceSnapshot) Client(name.abuchen.portfolio.model.Client) PerformanceIndex(name.abuchen.portfolio.snapshot.PerformanceIndex) Test(org.junit.Test)

Example 5 with SecurityPerformanceSnapshot

use of name.abuchen.portfolio.snapshot.security.SecurityPerformanceSnapshot in project portfolio by buchen.

the class SecurityTestCase method testSecurityPerformanceWithMissingHistoricalQuotes.

/**
 * Issue: If historical quotes start only after the purchase (or delivery)
 * of a security, the security is valued at 0 (no quote available) and the
 * performance would go crazy to -100% (as reported in the forum). This
 * scenario makes sure that earliest available historical quote is used.
 */
@Test
public void testSecurityPerformanceWithMissingHistoricalQuotes() throws IOException {
    Client client = ClientFactory.load(SecurityTestCase.class.getResourceAsStream("security_performance_with_missing_historical_quotes.xml"));
    Security security = client.getSecurities().get(0);
    PortfolioTransaction delivery = client.getPortfolios().get(0).getTransactions().get(0);
    assertThat("delivery transaction must be before earliest historical quote", delivery.getDateTime().toLocalDate(), lessThan(security.getPrices().get(0).getDate()));
    ReportingPeriod period = new ReportingPeriod.FromXtoY(LocalDate.parse("2013-12-04"), LocalDate.parse("2014-12-04"));
    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.getTrueTimeWeightedRateOfReturn(), closeTo(-0.0594, 0.0001));
    assertThat(record.getIrr(), closeTo(-0.0643, 0.0001));
    // actually, in this simple scenario (no cash transfers involved), the
    // ttwror is easy to calculate:
    double endvalue = delivery.getShares() * security.getSecurityPrice(LocalDate.parse("2014-12-04")).getValue() / Values.Share.divider() / Values.Quote.dividerToMoney();
    assertThat(record.getTrueTimeWeightedRateOfReturn(), closeTo((endvalue / delivery.getAmount()) - 1, 0.0001));
}
Also used : ReportingPeriod(name.abuchen.portfolio.snapshot.ReportingPeriod) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) SecurityPerformanceRecord(name.abuchen.portfolio.snapshot.security.SecurityPerformanceRecord) Client(name.abuchen.portfolio.model.Client) Security(name.abuchen.portfolio.model.Security) SecurityPerformanceSnapshot(name.abuchen.portfolio.snapshot.security.SecurityPerformanceSnapshot) Test(org.junit.Test)

Aggregations

SecurityPerformanceRecord (name.abuchen.portfolio.snapshot.security.SecurityPerformanceRecord)8 SecurityPerformanceSnapshot (name.abuchen.portfolio.snapshot.security.SecurityPerformanceSnapshot)8 Test (org.junit.Test)8 TestCurrencyConverter (name.abuchen.portfolio.TestCurrencyConverter)7 Client (name.abuchen.portfolio.model.Client)7 ReportingPeriod (name.abuchen.portfolio.snapshot.ReportingPeriod)7 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)5 Security (name.abuchen.portfolio.model.Security)5 Portfolio (name.abuchen.portfolio.model.Portfolio)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 CurrencyConverter (name.abuchen.portfolio.money.CurrencyConverter)3 ClientSnapshot (name.abuchen.portfolio.snapshot.ClientSnapshot)3 PerformanceIndex (name.abuchen.portfolio.snapshot.PerformanceIndex)3 LocalDate (java.time.LocalDate)2 ClientFactory (name.abuchen.portfolio.model.ClientFactory)2 CurrencyUnit (name.abuchen.portfolio.money.CurrencyUnit)2 CoreMatchers.is (org.hamcrest.CoreMatchers.is)2 Assert.assertThat (org.junit.Assert.assertThat)2 LocalDateTime (java.time.LocalDateTime)1