Search in sources :

Example 21 with PortfolioBuilder

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

the class TransactionComparatorTest method testBuyIsPreferredOverSell.

@Test
public void testBuyIsPreferredOverSell() {
    Portfolio portfolio = // 
    new PortfolioBuilder().sell(security, "2010-01-01", 100, // 
    100).buy(security, "2010-01-01", 100, // 
    100).addTo(client);
    List<PortfolioTransaction> list = portfolio.getTransactions();
    Collections.sort(list, new TransactionComparator());
    assertThat(list.get(0).getType(), is(Type.BUY));
    assertThat(list.get(1).getType(), is(Type.SELL));
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Portfolio(name.abuchen.portfolio.model.Portfolio) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) Test(org.junit.Test)

Example 22 with PortfolioBuilder

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

the class ClientIndexTest method testThatPerformanceOfAnInvestmentIntoAnIndexIsIdenticalToIndex.

@Test
public void testThatPerformanceOfAnInvestmentIntoAnIndexIsIdenticalToIndex() {
    LocalDate startDate = LocalDate.of(2012, 1, 1);
    // a weekend
    LocalDate endDate = LocalDate.of(2012, 4, 29);
    long startPrice = Values.Quote.factorize(100);
    Client client = new Client();
    Security security = // 
    new SecurityBuilder().generatePrices(startPrice, startDate, // 
    endDate).addTo(client);
    PortfolioBuilder portfolio = new PortfolioBuilder(new Account());
    // add some buy transactions
    LocalDate date = startDate;
    while (date.isBefore(endDate)) {
        // performance is only identical if the capital invested
        // additionally has the identical performance on its first day -->
        // use the closing quote of the previous day
        long p = security.getSecurityPrice(date.minusDays(1)).getValue();
        portfolio.inbound_delivery(security, date.atStartOfDay(), Values.Share.factorize(100), p);
        date = date.plusDays(20);
    }
    portfolio.addTo(client);
    ReportingPeriod.FromXtoY period = new ReportingPeriod.FromXtoY(startDate, endDate);
    List<Exception> warnings = new ArrayList<Exception>();
    CurrencyConverter converter = new TestCurrencyConverter();
    PerformanceIndex index = PerformanceIndex.forClient(client, converter, period, warnings);
    assertTrue(warnings.isEmpty());
    double[] accumulated = index.getAccumulatedPercentage();
    long lastPrice = security.getSecurityPrice(endDate).getValue();
    assertThat((double) (lastPrice - startPrice) / (double) startPrice, IsCloseTo.closeTo(accumulated[accumulated.length - 1], PRECISION));
    PerformanceIndex benchmark = PerformanceIndex.forSecurity(index, security);
    assertThat(benchmark.getFinalAccumulatedPercentage(), IsCloseTo.closeTo(index.getFinalAccumulatedPercentage(), PRECISION));
}
Also used : Account(name.abuchen.portfolio.model.Account) ArrayList(java.util.ArrayList) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) Security(name.abuchen.portfolio.model.Security) LocalDate(java.time.LocalDate) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Client(name.abuchen.portfolio.model.Client) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) Test(org.junit.Test)

Example 23 with PortfolioBuilder

use of name.abuchen.portfolio.PortfolioBuilder 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 24 with PortfolioBuilder

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

the class GroupByTaxonomyTest method testSecuritiesWithPartialAssignment.

@SuppressWarnings("null")
@Test
public void testSecuritiesWithPartialAssignment() {
    Client client = new Client();
    Taxonomy taxonomy = // 
    new TaxonomyBuilder().addClassification(// 
    "debt").addTo(client);
    Security a = // 
    new SecurityBuilder().addPrice("2010-01-01", // 
    Values.Quote.factorize(10)).assign(taxonomy, "debt", // 
    Classification.ONE_HUNDRED_PERCENT / 2).addTo(client);
    Portfolio portfolio = // 
    new PortfolioBuilder().inbound_delivery(a, "2010-01-01", Values.Share.factorize(10), // 
    10000).addTo(client);
    LocalDate date = LocalDate.parse("2010-01-01");
    PortfolioSnapshot snapshot = PortfolioSnapshot.create(portfolio, new TestCurrencyConverter(), date);
    assertNotNull(snapshot);
    GroupByTaxonomy grouping = snapshot.groupByTaxonomy(taxonomy);
    assertThat(grouping.asList().size(), is(2));
    AssetCategory debt = grouping.byClassification(taxonomy.getClassificationById("debt"));
    assertThat(debt.getValuation(), is(Money.of(CurrencyUnit.EUR, 50_00)));
    assertThat(debt.getPositions().size(), is(1));
    AssetCategory unassigned = null;
    for (AssetCategory category : grouping.asList()) if (category.getClassification().getId().equals(Classification.UNASSIGNED_ID))
        unassigned = category;
    assertThat(unassigned, notNullValue());
    assertThat(unassigned.getValuation(), is(Money.of(CurrencyUnit.EUR, 50_00)));
    assertThat(unassigned.getPositions().size(), is(1));
}
Also used : TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Taxonomy(name.abuchen.portfolio.model.Taxonomy) Portfolio(name.abuchen.portfolio.model.Portfolio) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) Client(name.abuchen.portfolio.model.Client) Security(name.abuchen.portfolio.model.Security) LocalDate(java.time.LocalDate) TaxonomyBuilder(name.abuchen.portfolio.TaxonomyBuilder) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) Test(org.junit.Test)

Example 25 with PortfolioBuilder

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

the class GroupByTaxonomyTest method testThatPartialAssignmentsAreSeparated.

@Test
public void testThatPartialAssignmentsAreSeparated() {
    Client client = new Client();
    Taxonomy taxonomy = // 
    new TaxonomyBuilder().addClassification(// 
    "debt").addClassification(// 
    "equity").addTo(client);
    Security a = // 
    new SecurityBuilder().addPrice("2010-01-01", // 
    Values.Quote.factorize(10)).assign(taxonomy, "debt", // 
    Classification.ONE_HUNDRED_PERCENT / 2).assign(taxonomy, "equity", // 
    Classification.ONE_HUNDRED_PERCENT / 2).addTo(client);
    Portfolio portfolio = // 
    new PortfolioBuilder().inbound_delivery(a, "2010-01-01", Values.Share.factorize(10), // 
    10000).addTo(client);
    LocalDate date = LocalDate.parse("2010-01-01");
    PortfolioSnapshot snapshot = PortfolioSnapshot.create(portfolio, new TestCurrencyConverter(), date);
    assertNotNull(snapshot);
    GroupByTaxonomy grouping = snapshot.groupByTaxonomy(taxonomy);
    AssetCategory debt = grouping.byClassification(taxonomy.getClassificationById("debt"));
    assertThat(debt.getValuation(), is(Money.of(CurrencyUnit.EUR, 50_00)));
    assertThat(debt.getPositions().size(), is(1));
    AssetCategory equity = grouping.byClassification(taxonomy.getClassificationById("equity"));
    assertThat(equity.getValuation(), is(Money.of(CurrencyUnit.EUR, 50_00)));
    assertThat(equity.getPositions().size(), is(1));
}
Also used : TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Taxonomy(name.abuchen.portfolio.model.Taxonomy) Portfolio(name.abuchen.portfolio.model.Portfolio) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) Client(name.abuchen.portfolio.model.Client) Security(name.abuchen.portfolio.model.Security) LocalDate(java.time.LocalDate) TaxonomyBuilder(name.abuchen.portfolio.TaxonomyBuilder) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) Test(org.junit.Test)

Aggregations

PortfolioBuilder (name.abuchen.portfolio.PortfolioBuilder)25 Test (org.junit.Test)22 SecurityBuilder (name.abuchen.portfolio.SecurityBuilder)21 Client (name.abuchen.portfolio.model.Client)20 Security (name.abuchen.portfolio.model.Security)19 TestCurrencyConverter (name.abuchen.portfolio.TestCurrencyConverter)18 Portfolio (name.abuchen.portfolio.model.Portfolio)17 LocalDate (java.time.LocalDate)9 AccountBuilder (name.abuchen.portfolio.AccountBuilder)9 Account (name.abuchen.portfolio.model.Account)8 TaxonomyBuilder (name.abuchen.portfolio.TaxonomyBuilder)7 Taxonomy (name.abuchen.portfolio.model.Taxonomy)7 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)5 CurrencyConverter (name.abuchen.portfolio.money.CurrencyConverter)5 ArrayList (java.util.ArrayList)3 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)2 Unit (name.abuchen.portfolio.model.Transaction.Unit)2 Before (org.junit.Before)2 LocalDateTime (java.time.LocalDateTime)1 Month (java.time.Month)1