Search in sources :

Example 21 with SecurityBuilder

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

the class CostCalculationTest method testWhenSharesHeldGoToZero.

@Test
public void testWhenSharesHeldGoToZero() {
    Client client = new Client();
    Security security = // 
    new SecurityBuilder().addTo(client);
    Portfolio portfolio = // 
    new PortfolioBuilder().buy(security, "2010-01-01", 100 * Values.Share.factor(), // 
    314920).sell(security, "2010-02-01", 100 * Values.Share.factor(), // 
    53150).buy(security, "2010-03-01", 50 * Values.Share.factor(), // 
    168492).sell(security, "2010-04-01", 50 * Values.Share.factor(), // 
    53150).addTo(client);
    CostCalculation cost = new CostCalculation();
    cost.setTermCurrency(CurrencyUnit.EUR);
    cost.visitAll(new TestCurrencyConverter(), portfolio.getTransactions());
    assertThat(cost.getFifoCost(), is(Money.of(CurrencyUnit.EUR, 0L)));
    assertThat(cost.getMovingAverageCost(), is(Money.of(CurrencyUnit.EUR, 0L)));
}
Also used : TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Portfolio(name.abuchen.portfolio.model.Portfolio) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) Client(name.abuchen.portfolio.model.Client) Security(name.abuchen.portfolio.model.Security) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) Test(org.junit.Test)

Example 22 with SecurityBuilder

use of name.abuchen.portfolio.SecurityBuilder 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 SecurityBuilder

use of name.abuchen.portfolio.SecurityBuilder 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 SecurityBuilder

use of name.abuchen.portfolio.SecurityBuilder 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 SecurityBuilder

use of name.abuchen.portfolio.SecurityBuilder 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

SecurityBuilder (name.abuchen.portfolio.SecurityBuilder)25 Client (name.abuchen.portfolio.model.Client)24 Security (name.abuchen.portfolio.model.Security)23 TestCurrencyConverter (name.abuchen.portfolio.TestCurrencyConverter)22 Test (org.junit.Test)22 PortfolioBuilder (name.abuchen.portfolio.PortfolioBuilder)21 AccountBuilder (name.abuchen.portfolio.AccountBuilder)13 Portfolio (name.abuchen.portfolio.model.Portfolio)13 LocalDate (java.time.LocalDate)12 Account (name.abuchen.portfolio.model.Account)9 CurrencyConverter (name.abuchen.portfolio.money.CurrencyConverter)9 TaxonomyBuilder (name.abuchen.portfolio.TaxonomyBuilder)7 Taxonomy (name.abuchen.portfolio.model.Taxonomy)7 ArrayList (java.util.ArrayList)6 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)3 Unit (name.abuchen.portfolio.model.Transaction.Unit)3 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)2 Transaction (name.abuchen.portfolio.model.Transaction)2 Before (org.junit.Before)2 LocalDateTime (java.time.LocalDateTime)1