Search in sources :

Example 11 with SecurityBuilder

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

the class GroupByTaxonomyTest method testThatPartialAssignmentsInSubClassificationsAreMerged.

@Test
public void testThatPartialAssignmentsInSubClassificationsAreMerged() {
    Client client = new Client();
    Taxonomy taxonomy = // 
    new TaxonomyBuilder().addClassification(// 
    "debt").addClassification("debt", // 
    "cat1").addClassification("debt", // 
    "cat2").addTo(client);
    Security a = // 
    new SecurityBuilder().addPrice("2010-01-01", // 
    Values.Quote.factorize(10)).assign(taxonomy, "cat1", // 
    Classification.ONE_HUNDRED_PERCENT / 2).assign(taxonomy, "cat2", // 
    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(1));
    AssetCategory debt = grouping.byClassification(taxonomy.getClassificationById("debt"));
    assertThat(debt.getPositions().size(), is(1));
    assertThat(debt.getValuation(), is(Money.of(CurrencyUnit.EUR, 100_00)));
}
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 12 with SecurityBuilder

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

the class PortfolioSnapshotTest method testBuyAndSellLeavesNoEntryInSnapshot.

@Test
public void testBuyAndSellLeavesNoEntryInSnapshot() {
    Client client = new Client();
    Security a = // 
    new SecurityBuilder().addPrice("2010-01-01", // 
    1000).addTo(client);
    Portfolio portfolio = // 
    new PortfolioBuilder().buy(a, "2010-01-01", 1000000, // 
    10000).sell(a, "2010-01-02", 700000, // 
    12000).sell(a, "2010-01-03", 300000, // 
    12000).addTo(client);
    LocalDate date = LocalDate.parse("2010-01-31");
    PortfolioSnapshot snapshot = PortfolioSnapshot.create(portfolio, new TestCurrencyConverter(), date);
    assertTrue(snapshot.getPositions().isEmpty());
}
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) LocalDate(java.time.LocalDate) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) Test(org.junit.Test)

Example 13 with SecurityBuilder

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

the class SecurityIndexTest method testWhenQuotesAreOnlyAvailableUntilTheMiddleOfTheReportInterval.

@Test
public void testWhenQuotesAreOnlyAvailableUntilTheMiddleOfTheReportInterval() {
    LocalDate startDate = LocalDate.of(2012, 12, 31);
    LocalDate middleDate = LocalDate.of(2013, 2, 18);
    LocalDate endDate = LocalDate.of(2013, 3, 31);
    // create model
    Client client = new Client();
    // 
    new AccountBuilder().deposit_(startDate.atStartOfDay(), // 
    100 * Values.Amount.factor()).interest(startDate.atStartOfDay().plusDays(10), // 
    10 * Values.Amount.factor()).addTo(client);
    int startPrice = 50 * Values.Amount.factor();
    Security security = // 
    new SecurityBuilder().generatePrices(startPrice, startDate, // 
    middleDate).addTo(client);
    // calculate performance indices
    List<Exception> warnings = new ArrayList<Exception>();
    ReportingPeriod reportInterval = new ReportingPeriod.FromXtoY(startDate, endDate);
    CurrencyConverter converter = new TestCurrencyConverter();
    PerformanceIndex clientIndex = PerformanceIndex.forClient(client, converter, reportInterval, warnings);
    PerformanceIndex securityIndex = PerformanceIndex.forSecurity(clientIndex, security);
    // asserts
    assertTrue(warnings.isEmpty());
    LocalDate[] clientDates = clientIndex.getDates();
    LocalDate[] securityDates = securityIndex.getDates();
    assertThat(securityDates[0], is(startDate));
    assertThat(securityDates[securityDates.length - 1], is(middleDate));
    assertThat(clientDates[0], is(securityDates[0]));
    double[] securityAccumulated = securityIndex.getAccumulatedPercentage();
    int index = Dates.daysBetween(startDate, middleDate);
    assertThat(clientDates[index], is(middleDate));
    assertThat(securityAccumulated[0], IsCloseTo.closeTo(0d, 0.000001d));
    long middlePrice = security.getSecurityPrice(middleDate).getValue();
    double performance = (double) (middlePrice - startPrice) / (double) startPrice;
    assertThat(securityAccumulated[securityAccumulated.length - 1], IsCloseTo.closeTo(performance, 0.000001d));
}
Also used : ArrayList(java.util.ArrayList) 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) AccountBuilder(name.abuchen.portfolio.AccountBuilder) Client(name.abuchen.portfolio.model.Client) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) Test(org.junit.Test)

Example 14 with SecurityBuilder

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

the class ClientSecurityFilterTest method setupClient.

@Before
public void setupClient() {
    client = new Client();
    securityUSD = // 
    new SecurityBuilder().addPrice("2016-06-01", // 
    Values.Quote.factorize(100)).addTo(client);
    securityEUR = // 
    new SecurityBuilder(CurrencyUnit.USD).addPrice("2016-06-01", // 
    Values.Quote.factorize(100)).addTo(client);
    accountEUR = // 
    new AccountBuilder(CurrencyUnit.EUR).dividend("2017-01-01", Values.Amount.factorize(20), // 
    securityEUR).addTo(client);
    accountUSD = // 
    new AccountBuilder(CurrencyUnit.USD).dividend("2017-01-01", Values.Amount.factorize(20), // 
    securityUSD).addTo(client);
    // 
    new PortfolioBuilder(accountEUR).inbound_delivery(securityEUR, "2016-06-01", Values.Share.factorize(20), // 
    Values.Amount.factorize(2000)).inbound_delivery(securityUSD, "2016-06-01", Values.Share.factorize(20), // 
    Values.Amount.factorize(2000)).addTo(client);
}
Also used : AccountBuilder(name.abuchen.portfolio.AccountBuilder) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) Client(name.abuchen.portfolio.model.Client) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) Before(org.junit.Before)

Example 15 with SecurityBuilder

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

the class CostCalculationTest method testFifoBuySellTransactions2.

@Test
public void testFifoBuySellTransactions2() {
    Client client = new Client();
    Security security = // 
    new SecurityBuilder().addTo(client);
    Portfolio portfolio = // 
    new PortfolioBuilder().buy(security, "2010-01-01", 109 * Values.Share.factor(), // 
    Values.Amount.factorize(3149.20)).buy(security, "2010-02-01", 52 * Values.Share.factor(), // 
    Values.Amount.factorize(1684.92)).sell(security, "2010-03-01", 15 * Values.Share.factor(), // 
    Values.Amount.factorize(531.50)).addTo(client);
    CostCalculation cost = new CostCalculation();
    cost.setTermCurrency(CurrencyUnit.EUR);
    cost.visitAll(new TestCurrencyConverter(), portfolio.getTransactions());
    // expected:
    // 3149,20 + 1684,92 - round(3149,20 * 15/109) = 4400,743853211009174
    assertThat(cost.getFifoCost(), is(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(4400.74))));
    // expected moving average is identical because it is only one buy
    // transaction
    // (3149,20 + 1684.92) * 146/161 = 4383,736149068322981
    assertThat(cost.getMovingAverageCost(), is(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(4383.74))));
}
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)

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