Search in sources :

Example 1 with SecurityBuilder

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

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

the class ClassificationIndexTest method testThatTaxesAreNotIncludedInTTWRORCalculation.

@Test
public void testThatTaxesAreNotIncludedInTTWRORCalculation() {
    Client client = new Client();
    Security security = // 
    new SecurityBuilder().addPrice("2015-12-31", // 
    Values.Quote.factorize(100)).addPrice("2016-12-31", // 
    Values.Quote.factorize(110)).addTo(client);
    Account account = // 
    new AccountBuilder().deposit_("2014-01-01", Values.Amount.factorize(1000)).addTo(client);
    AccountTransaction t = new AccountTransaction();
    t.setType(AccountTransaction.Type.DIVIDENDS);
    t.setDateTime(LocalDateTime.parse("2016-06-01T00:00"));
    t.setSecurity(security);
    t.setMonetaryAmount(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(100)));
    t.setShares(Values.Share.factorize(10));
    account.addTransaction(t);
    Portfolio portfolio = // 
    new PortfolioBuilder(account).addTo(client);
    BuySellEntry buy = new BuySellEntry(portfolio, account);
    buy.setType(PortfolioTransaction.Type.BUY);
    buy.setDate(LocalDateTime.parse("2015-12-31T00:00"));
    buy.setMonetaryAmount(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(1000)));
    buy.setShares(Values.Share.factorize(10));
    buy.setSecurity(security);
    buy.insert();
    BuySellEntry sell = new BuySellEntry(portfolio, account);
    sell.setType(PortfolioTransaction.Type.SELL);
    sell.setDate(LocalDateTime.parse("2016-12-31T00:00"));
    sell.setMonetaryAmount(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(1070)));
    sell.setShares(Values.Share.factorize(10));
    sell.setSecurity(security);
    sell.getPortfolioTransaction().addUnit(new Unit(Unit.Type.TAX, Money.of(CurrencyUnit.EUR, Values.Amount.factorize(30))));
    sell.insert();
    Classification classification = new Classification(null, null);
    classification.addAssignment(new Assignment(security));
    List<Exception> warnings = new ArrayList<Exception>();
    PerformanceIndex index = PerformanceIndex.forClassification(client, new TestCurrencyConverter(), classification, new ReportingPeriod.FromXtoY(LocalDate.parse("2015-01-01"), LocalDate.parse("2017-01-01")), warnings);
    assertThat(warnings.isEmpty(), is(true));
    // dividend payment 10% * quote change 10%
    assertThat(index.getFinalAccumulatedPercentage(), IsCloseTo.closeTo((1.1 * 1.1) - 1, 0.000000001d));
    // add taxes to dividend payment
    t.addUnit(new Unit(Unit.Type.TAX, Money.of(CurrencyUnit.EUR, Values.Amount.factorize(50))));
    index = PerformanceIndex.forClassification(client, new TestCurrencyConverter(), classification, new ReportingPeriod.FromXtoY(LocalDate.parse("2015-01-01"), LocalDate.parse("2017-01-01")), warnings);
    // dividend payment 15% * quote change 10%
    assertThat(index.getFinalAccumulatedPercentage(), IsCloseTo.closeTo((1.15 * 1.1) - 1, 0.000000001d));
}
Also used : Account(name.abuchen.portfolio.model.Account) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) Portfolio(name.abuchen.portfolio.model.Portfolio) ArrayList(java.util.ArrayList) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) Security(name.abuchen.portfolio.model.Security) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) Unit(name.abuchen.portfolio.model.Transaction.Unit) Assignment(name.abuchen.portfolio.model.Classification.Assignment) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Classification(name.abuchen.portfolio.model.Classification) AccountBuilder(name.abuchen.portfolio.AccountBuilder) Client(name.abuchen.portfolio.model.Client) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) Test(org.junit.Test)

Example 3 with SecurityBuilder

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

the class ClassificationIndexTest method createClient.

private Client createClient(int weight) {
    Client client = new Client();
    Taxonomy taxonomy = // 
    new TaxonomyBuilder().addClassification(// 
    "one").addTo(client);
    Security security = // 
    new SecurityBuilder().addPrice("2011-12-31", // 
    100 * Values.Quote.factor()).addPrice("2012-01-03", // 
    106 * Values.Quote.factor()).addPrice("2012-01-08", // 
    112 * Values.Quote.factor()).assign(taxonomy, "one", // 
    weight).addTo(client);
    Account account = // 
    new AccountBuilder().deposit_("2011-12-31", // 
    10000 * Values.Amount.factor()).interest("2012-01-01", // 
    230 * Values.Amount.factor()).deposit_("2012-01-02", // 
    200 * Values.Amount.factor()).interest("2012-01-02", // 
    200 * Values.Amount.factor()).withdraw("2012-01-03", // 
    400 * Values.Amount.factor()).fees____("2012-01-03", // 
    234 * Values.Amount.factor()).interest("2012-01-04", // 
    293 * Values.Amount.factor()).interest("2012-01-05", // 
    293 * Values.Amount.factor()).deposit_("2012-01-06", // 
    5400 * Values.Amount.factor()).interest("2012-01-06", // 
    195 * Values.Amount.factor()).withdraw("2012-01-07", // 
    3697 * Values.Amount.factor()).fees____("2012-01-07", // 
    882 * Values.Amount.factor()).fees____("2012-01-08", // 
    1003 * Values.Amount.factor()).dividend("2012-01-08", 100 * Values.Amount.factor(), // 
    security).assign(taxonomy, "one", // 
    weight).addTo(client);
    // 
    new PortfolioBuilder(account).buy(security, "2012-01-01", 50 * Values.Share.factor(), // 
    50 * 101 * Values.Amount.factor()).inbound_delivery(security, "2012-01-01", 100 * Values.Share.factor(), // 
    100 * 100 * Values.Amount.factor()).sell(security, "2012-01-05", 50 * Values.Share.factor(), // 
    50 * 105 * Values.Amount.factor()).addTo(client);
    return client;
}
Also used : Account(name.abuchen.portfolio.model.Account) Taxonomy(name.abuchen.portfolio.model.Taxonomy) AccountBuilder(name.abuchen.portfolio.AccountBuilder) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) Client(name.abuchen.portfolio.model.Client) Security(name.abuchen.portfolio.model.Security) TaxonomyBuilder(name.abuchen.portfolio.TaxonomyBuilder) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder)

Example 4 with SecurityBuilder

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

the class ClientIndexTest method testThatPerformanceOfInvestmentAndIndexIsIdendicalWhenInForeignCurrency.

@Test
public void testThatPerformanceOfInvestmentAndIndexIsIdendicalWhenInForeignCurrency() {
    LocalDate startDate = LocalDate.of(2015, 1, 1);
    LocalDate endDate = LocalDate.of(2015, 8, 1);
    long startPrice = Values.Quote.factorize(100);
    Client client = new Client();
    Security security = // 
    new SecurityBuilder("USD").generatePrices(startPrice, startDate, // 
    endDate).addTo(client);
    Account account = new AccountBuilder().addTo(client);
    // 
    new PortfolioBuilder(account).inbound_delivery(security, LocalDateTime.of(2014, 01, 01, 0, 0), Values.Share.factorize(100), // 
    100).addTo(client);
    ReportingPeriod.FromXtoY period = new ReportingPeriod.FromXtoY(startDate, endDate);
    List<Exception> warnings = new ArrayList<Exception>();
    CurrencyConverter converter = new TestCurrencyConverter().with(CurrencyUnit.EUR);
    PerformanceIndex index = PerformanceIndex.forClient(client, converter, period, warnings);
    assertTrue(warnings.isEmpty());
    PerformanceIndex benchmark = PerformanceIndex.forSecurity(index, security);
    assertTrue(warnings.isEmpty());
    assertThat(benchmark.getFinalAccumulatedPercentage(), IsCloseTo.closeTo(index.getFinalAccumulatedPercentage(), PRECISION));
    PerformanceIndex investment = PerformanceIndex.forInvestment(client, converter, security, period, warnings);
    assertTrue(warnings.isEmpty());
    assertThat(investment.getFinalAccumulatedPercentage(), is(index.getFinalAccumulatedPercentage()));
}
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) AccountBuilder(name.abuchen.portfolio.AccountBuilder) Client(name.abuchen.portfolio.model.Client) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) Test(org.junit.Test)

Example 5 with SecurityBuilder

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

the class ClientPerformanceSnapshotTest method testCapitalGainsWithPartialSellDuringReportPeriodWithFees.

@Test
public void testCapitalGainsWithPartialSellDuringReportPeriodWithFees() {
    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, // 
    1).addTo(client);
    CurrencyConverter converter = new TestCurrencyConverter();
    ClientPerformanceSnapshot snapshot = new ClientPerformanceSnapshot(client, converter, startDate, endDate);
    assertThat(snapshot.getValue(CategoryType.CAPITAL_GAINS), is(Money.of(CurrencyUnit.EUR, 1000 * 9 + (9900 - 10000) + 1)));
    assertThat(snapshot.getValue(CategoryType.FEES), is(Money.of(CurrencyUnit.EUR, 1)));
    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)

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