Search in sources :

Example 16 with SecurityBuilder

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

the class SecurityIndexTest method testThatSecurityIndexIsCalculated.

@Test
public void testThatSecurityIndexIsCalculated() {
    LocalDate startDate = LocalDate.of(2012, 12, 31);
    LocalDate endDate = LocalDate.of(2013, 4, 1);
    long startPrice = 100 * Values.Amount.factor();
    // create model
    Client client = new Client();
    // 
    new AccountBuilder().deposit_(startDate.atStartOfDay(), // 
    startPrice).addTo(client);
    Security security = // 
    new SecurityBuilder().generatePrices(startPrice, startDate, // 
    endDate).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[] dates = securityIndex.getDates();
    assertThat(dates[0], is(startDate));
    assertThat(dates[dates.length - 1], is(endDate));
    long lastPrice = security.getSecurityPrice(endDate).getValue();
    double performance = (double) (lastPrice - startPrice) / (double) startPrice;
    double[] accumulated = securityIndex.getAccumulatedPercentage();
    assertThat(accumulated[0], is(0d));
    assertThat(accumulated[accumulated.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 17 with SecurityBuilder

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

the class SecurityIndexTest method testWhenQuotesAreOnlyAvailableFromTheMiddleOfTheReportInterval.

@Test
public void testWhenQuotesAreOnlyAvailableFromTheMiddleOfTheReportInterval() {
    LocalDate startDate = LocalDate.of(2012, 12, 31);
    LocalDate middleDate = LocalDate.of(2013, 2, 18);
    LocalDate endDate = LocalDate.of(2013, 4, 1);
    // 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);
    Security security = // 
    new SecurityBuilder().generatePrices(50 * Values.Amount.factor(), middleDate, // 
    endDate).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(middleDate));
    assertThat(securityDates[securityDates.length - 1], is(endDate));
    assertThat(clientDates[clientDates.length - 1], is(securityDates[securityDates.length - 1]));
    double[] clientAccumulated = clientIndex.getAccumulatedPercentage();
    double[] securityAccumulated = securityIndex.getAccumulatedPercentage();
    int index = Dates.daysBetween(startDate, middleDate);
    assertThat(clientDates[index], is(middleDate));
    assertThat(securityAccumulated[0], IsCloseTo.closeTo(clientAccumulated[index], 0.000001d));
    long middlePrice = security.getSecurityPrice(middleDate).getValue();
    long lastPrice = security.getSecurityPrice(endDate).getValue();
    // 10% is interest of the deposit
    double performance = (double) (lastPrice - middlePrice) / (double) middlePrice + 0.1d;
    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 18 with SecurityBuilder

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

the class PortfolioClientFilterTest method setupClient.

/**
 * Creates three portfolios (A-C) with a reference account each.
 */
@Before
public void setupClient() {
    client = new Client();
    Security security = new SecurityBuilder().addTo(client);
    // 2016-01-01 deposit
    // 2016-02-01 buy
    // 2016-03-01 dividend
    Arrays.asList("A", "B", "C").forEach(index -> {
        Account a = // 
        new AccountBuilder().deposit_("2016-01-01", Values.Amount.factorize(100)).dividend("2016-03-01", Values.Amount.factorize(10), // 
        security).addTo(client);
        a.setName(index);
        Portfolio p = // 
        new PortfolioBuilder(a).buy(security, "2016-02-01", Values.Share.factorize(1), Values.Amount.factorize(100)).addTo(client);
        p.setName(index);
    });
}
Also used : Account(name.abuchen.portfolio.model.Account) Portfolio(name.abuchen.portfolio.model.Portfolio) 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) Before(org.junit.Before)

Example 19 with SecurityBuilder

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

the class CostCalculationTest method testThatRoundingDifferencesAreRemovedIfZeroSharesHeld.

@Test
public void testThatRoundingDifferencesAreRemovedIfZeroSharesHeld() {
    Client client = new Client();
    Security security = // 
    new SecurityBuilder().addTo(client);
    Portfolio portfolio = // 
    new PortfolioBuilder().buy(security, "2010-01-01", 10 * Values.Share.factor(), // 
    1).sell(security, "2010-02-01", 3 * Values.Share.factor(), // 
    1).sell(security, "2010-03-01", 3 * Values.Share.factor(), // 
    1).sell(security, "2010-03-01", 4 * Values.Share.factor(), // 
    1).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 20 with SecurityBuilder

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

the class CostCalculationTest method testFifoBuySellTransactions.

@Test
public void testFifoBuySellTransactions() {
    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)).sell(security, "2010-02-01", 15 * Values.Share.factor(), // 
    Values.Amount.factorize(531.50)).buy(security, "2010-03-01", 52 * Values.Share.factor(), // 
    Values.Amount.factorize(1684.92)).buy(security, "2010-03-01", 32 * Values.Share.factor(), // 
    Values.Amount.factorize(959.30)).addTo(client);
    CostCalculation cost = new CostCalculation();
    cost.setTermCurrency(CurrencyUnit.EUR);
    cost.visitAll(new TestCurrencyConverter(), portfolio.getTransactions());
    // expected:
    // 3149,20 - round(3149,20 * 15/109) + 1684,92 + 959,30 = 5360,04385
    assertThat(cost.getFifoCost(), is(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(5360.04))));
    // expected moving average is identical because it is only one buy
    // transaction
    // 3149,20 * 94/109 + 1684.92 + 959.30 = 5360,04385
    assertThat(cost.getMovingAverageCost(), is(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(5360.04))));
}
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