@Test public void testExcelSampleAggregatedWeekly() { // first day of week is locale dependent Locale locale = Locale.getDefault(); Locale.setDefault(Locale.GERMAN); try { Client client = createClient(); ReportingPeriod.FromXtoY reportInterval = new ReportingPeriod.FromXtoY( // LocalDate.of(2011, Month.DECEMBER, 31), LocalDate.of(2012, Month.JANUARY, 8)); CurrencyConverter converter = new TestCurrencyConverter(); PerformanceIndex index = PerformanceIndex.forClient(client, converter, reportInterval, new ArrayList<Exception>()); index = Aggregation.aggregate(index, Aggregation.Period.WEEKLY); assertNotNull(index); double[] delta = index.getDeltaPercentage(); assertThat(delta.length, is(2)); assertThat(delta[0], IsCloseTo.closeTo(0.023d, PRECISION)); assertThat(delta[1], IsCloseTo.closeTo(-0.0713587d, PRECISION)); double[] accumulated = index.getAccumulatedPercentage(); assertThat(accumulated[0], IsCloseTo.closeTo(0.023d, PRECISION)); assertThat(accumulated[1], IsCloseTo.closeTo(-0.05d, PRECISION)); } finally { Locale.setDefault(locale); } }
@Test public void testThatTransferalsDoNotChangePerformance() { double[] delta = {0d, 0.023d, 0.043d, 0.02d, 0.05d, 0.08d, 0.1d, 0.04d, -0.05d}; long[] transferals = {1000000, 0, 20000, -40000, 0, 0, 540000, -369704, 0}; long[] transferals2 = {1000000, 0, 0, 0, 0, 0, 0, 0, 0}; Client client = createClient(delta, transferals); ReportingPeriod.FromXtoY period = new ReportingPeriod.FromXtoY( LocalDate.of(2012, Month.JANUARY, 1), // LocalDate.of(2012, Month.JANUARY, 9)); CurrencyConverter converter = new TestCurrencyConverter(); ClientIndex index = PerformanceIndex.forClient(client, converter, period, new ArrayList<Exception>()); double[] accumulated = index.getAccumulatedPercentage(); for (int ii = 0; ii < accumulated.length; ii++) assertThat(accumulated[ii], IsCloseTo.closeTo(delta[ii], PRECISION)); Client anotherClient = createClient(delta, transferals2); index = PerformanceIndex.forClient(anotherClient, converter, period, new ArrayList<Exception>()); accumulated = index.getAccumulatedPercentage(); for (int ii = 0; ii < accumulated.length; ii++) assertThat(accumulated[ii], IsCloseTo.closeTo(delta[ii], PRECISION)); }
@Test public void testWhenQuotesAreOnlyAvailableFromTheMiddleOfTheReportInterval() { DateMidnight startDate = new DateMidnight(2012, 12, 31); DateMidnight middleDate = new DateMidnight(2013, 2, 18); DateMidnight endDate = new DateMidnight(2013, 4, 1); // create model Client client = new Client(); new AccountBuilder() // .deposit_(startDate, 100 * Values.Amount.factor()) // .interest(startDate.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.toDate(), endDate.toDate()); ClientIndex clientIndex = PerformanceIndex.forClient(client, reportInterval, warnings); PerformanceIndex securityIndex = PerformanceIndex.forSecurity(clientIndex, security, warnings); // asserts assertTrue(warnings.isEmpty()); Date[] clientDates = clientIndex.getDates(); Date[] securityDates = securityIndex.getDates(); assertThat(securityDates[0], is(middleDate.toDate())); assertThat(securityDates[securityDates.length - 1], is(endDate.toDate())); assertThat( new DateMidnight(clientDates[clientDates.length - 1]), is(new DateMidnight(securityDates[securityDates.length - 1]))); double[] clientAccumulated = clientIndex.getAccumulatedPercentage(); double[] securityAccumulated = securityIndex.getAccumulatedPercentage(); int index = Days.daysBetween(startDate, middleDate).getDays(); assertThat(new DateMidnight(clientDates[index]), is(middleDate)); assertThat(securityAccumulated[0], IsCloseTo.closeTo(clientAccumulated[index], 0.000001d)); long middlePrice = security.getSecurityPrice(middleDate.toDate()).getValue(); long lastPrice = security.getSecurityPrice(endDate.toDate()).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)); }
@Test public void testThatInterstWithoutInvestmentDoesNotCorruptResultAndIsReported() { Client client = new Client(); new AccountBuilder() // .interest("2012-01-02", 100) // .addTo(client); ReportingPeriod.FromXtoY period = new ReportingPeriod.FromXtoY( LocalDate.of(2012, Month.JANUARY, 1), // LocalDate.of(2012, Month.JANUARY, 9)); List<Exception> errors = new ArrayList<Exception>(); CurrencyConverter converter = new TestCurrencyConverter(); ClientIndex index = PerformanceIndex.forClient(client, converter, period, errors); double[] accumulated = index.getAccumulatedPercentage(); for (int ii = 0; ii < accumulated.length; ii++) assertThat(accumulated[ii], IsCloseTo.closeTo(0d, PRECISION)); assertThat(errors.size(), is(1)); assertThat( errors.get(0).getMessage(), startsWith( Messages.MsgDeltaWithoutAssets.substring( 0, Messages.MsgDeltaWithoutAssets.indexOf('{')))); }
@Test public void testThatDepositsOnTheLastDayArePerformanceNeutral() { Client client = new Client(); new AccountBuilder() // .deposit_("2012-01-01", 10000) // .interest("2012-01-02", 1000) // .deposit_("2012-01-10", 10000) // .addTo(client); ReportingPeriod.FromXtoY reportInterval = new ReportingPeriod.FromXtoY( LocalDate.of(2012, Month.JANUARY, 1), // LocalDate.of(2012, Month.JANUARY, 10)); CurrencyConverter converter = new TestCurrencyConverter(); PerformanceIndex index = PerformanceIndex.forClient(client, converter, reportInterval, new ArrayList<Exception>()); double[] accumulated = index.getAccumulatedPercentage(); assertThat(accumulated[accumulated.length - 2], IsCloseTo.closeTo(0.1d, PRECISION)); assertThat(accumulated[accumulated.length - 1], IsCloseTo.closeTo(0.1d, PRECISION)); }
@Test public void testThatNoValuationResultsInZeroPerformance() { Client client = new Client(); ReportingPeriod.FromXtoY period = new ReportingPeriod.FromXtoY( LocalDate.of(2012, Month.JANUARY, 1), // LocalDate.of(2012, Month.JANUARY, 9)); CurrencyConverter converter = new TestCurrencyConverter(); ClientIndex index = PerformanceIndex.forClient(client, converter, period, new ArrayList<Exception>()); double[] accumulated = index.getAccumulatedPercentage(); for (int ii = 0; ii < accumulated.length; ii++) assertThat(accumulated[ii], IsCloseTo.closeTo(0d, PRECISION)); }
@Test public void testErtragsgutschrift2() throws IOException { DeutscheBankPDFExctractor extractor = new DeutscheBankPDFExctractor(new Client()) { @Override String strip(File file) throws IOException { return from(file.getName()); } }; List<Exception> errors = new ArrayList<Exception>(); List<Item> results = extractor.extract(Arrays.asList(new File("DeutscheBankErtragsgutschrift2.txt")), errors); assertThat(errors, empty()); assertThat(results.size(), is(2)); new AssertImportActions().check(results, CurrencyUnit.EUR); // check security Security security = results.stream().filter(i -> i instanceof SecurityItem).findFirst().get().getSecurity(); assertThat(security.getName(), is("ISHS-MSCI N. AMERIC.UCITS ETF BE.SH.(DT.ZT.)")); assertThat(security.getIsin(), is("DE000A0J2060")); assertThat(security.getWkn(), is("A0J206")); assertThat(security.getCurrencyCode(), is("USD")); // check transaction Optional<Item> item = results.stream().filter(i -> i instanceof TransactionItem).findFirst(); assertThat(item.isPresent(), is(true)); assertThat(item.get().getSubject(), instanceOf(AccountTransaction.class)); AccountTransaction transaction = (AccountTransaction) item.get().getSubject(); assertThat(transaction.getType(), is(AccountTransaction.Type.DIVIDENDS)); assertThat(transaction.getSecurity(), is(security)); assertThat(transaction.getDate(), is(LocalDate.parse("2015-03-24"))); assertThat(transaction.getMonetaryAmount(), is(Money.of(CurrencyUnit.EUR, 16_17L))); assertThat(transaction.getShares(), is(Values.Share.factorize(123))); Optional<Unit> grossValue = transaction.getUnit(Unit.Type.GROSS_VALUE); assertThat(grossValue.isPresent(), is(true)); assertThat(grossValue.get().getAmount(), is(Money.of("EUR", 16_17L))); assertThat(grossValue.get().getForex(), is(Money.of("USD", 17_38L))); assertThat( grossValue.get().getExchangeRate().doubleValue(), IsCloseTo.closeTo(0.930578, 0.000001)); }
@Test public void testThatSecurityIndexIsCalculated() { DateMidnight startDate = new DateMidnight(2012, 12, 31); DateMidnight endDate = new DateMidnight(2013, 4, 1); long startPrice = 100 * Values.Amount.factor(); // create model Client client = new Client(); new AccountBuilder() // .deposit_(startDate, 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.toDate(), endDate.toDate()); ClientIndex clientIndex = PerformanceIndex.forClient(client, reportInterval, warnings); PerformanceIndex securityIndex = PerformanceIndex.forSecurity(clientIndex, security, warnings); // asserts assertTrue(warnings.isEmpty()); Date[] dates = securityIndex.getDates(); assertThat(dates[0], is(startDate.toDate())); assertThat(dates[dates.length - 1], is(endDate.toDate())); long lastPrice = security.getSecurityPrice(endDate.toDate()).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)); }
@Test public void testThatPerformanceOfAnInvestmentIntoAnIndexIsIdenticalToIndex() { LocalDate startDate = LocalDate.of(2012, 1, 1); LocalDate endDate = LocalDate.of(2012, 4, 29); // a weekend 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)) { long p = security.getSecurityPrice(date).getValue(); portfolio.inbound_delivery(security, date, 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(); ClientIndex 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(), is(index.getFinalAccumulatedPercentage())); }
@Test public void testWhenQuotesAreOnlyAvailableUntilTheMiddleOfTheReportInterval() { DateMidnight startDate = new DateMidnight(2012, 12, 31); DateMidnight middleDate = new DateMidnight(2013, 2, 18); DateMidnight endDate = new DateMidnight(2013, 3, 31); // create model Client client = new Client(); Account account = new Account(); client.addAccount(account); addT( account, startDate.toCalendar(Locale.getDefault()), Type.DEPOSIT, 100 * Values.Amount.factor()); addT( account, startDate.plusDays(10).toCalendar(Locale.getDefault()), Type.INTEREST, 10 * Values.Amount.factor()); Security security = new Security(); client.addSecurity(security); int startPrice = 50 * Values.Amount.factor(); generatePrices(security, startPrice, startDate, middleDate); // calculate performance indices List<Exception> warnings = new ArrayList<Exception>(); ReportingPeriod reportInterval = new ReportingPeriod.FromXtoY(startDate.toDate(), endDate.toDate()); ClientIndex clientIndex = PerformanceIndex.forClient(client, reportInterval, warnings); PerformanceIndex securityIndex = PerformanceIndex.forSecurity(clientIndex, security, warnings); // asserts assertTrue(warnings.isEmpty()); Date[] clientDates = clientIndex.getDates(); Date[] securityDates = securityIndex.getDates(); assertThat(securityDates[0], is(startDate.toDate())); assertThat(securityDates[securityDates.length - 1], is(middleDate.toDate())); assertThat(new DateMidnight(clientDates[0]), is(new DateMidnight(securityDates[0]))); double[] securityAccumulated = securityIndex.getAccumulatedPercentage(); int index = Days.daysBetween(startDate, middleDate).getDays(); assertThat(new DateMidnight(clientDates[index]), is(middleDate)); assertThat(securityAccumulated[0], IsCloseTo.closeTo(0d, 0.000001d)); long middlePrice = security.getSecurityPrice(middleDate.toDate()).getValue(); double performance = (double) (middlePrice - startPrice) / (double) startPrice; assertThat( securityAccumulated[securityAccumulated.length - 1], IsCloseTo.closeTo(performance, 0.000001d)); }
@Test public void testExcelSample() { Client client = createClient(); ReportingPeriod.FromXtoY period = new ReportingPeriod.FromXtoY( LocalDate.of(2011, Month.DECEMBER, 31), // LocalDate.of(2012, Month.JANUARY, 8)); CurrencyConverter converter = new TestCurrencyConverter(); ClientIndex index = PerformanceIndex.forClient(client, converter, period, new ArrayList<Exception>()); assertNotNull(index); assertThat(period.toInterval(), is(index.getReportInterval().toInterval())); assertThat(client, is(index.getClient())); LocalDate[] dates = index.getDates(); assertThat(dates.length, is(Dates.daysBetween(period.getStartDate(), period.getEndDate()) + 1)); double[] delta = index.getDeltaPercentage(); assertThat(delta[0], is(0d)); assertThat(delta[1], IsCloseTo.closeTo(0.023d, PRECISION)); assertThat(delta[2], IsCloseTo.closeTo(0.0195503d, PRECISION)); assertThat(delta[3], IsCloseTo.closeTo(-0.0220517d, PRECISION)); assertThat(delta[4], IsCloseTo.closeTo(0.0294117647d, PRECISION)); assertThat(delta[5], IsCloseTo.closeTo(0.0285714286d, PRECISION)); assertThat(delta[6], IsCloseTo.closeTo(0.0185185185d, PRECISION)); assertThat(delta[7], IsCloseTo.closeTo(-0.0545454545d, PRECISION)); assertThat(delta[8], IsCloseTo.closeTo(-0.0865384615d, PRECISION)); double[] accumulated = index.getAccumulatedPercentage(); assertThat(accumulated[0], is(0d)); assertThat(accumulated[1], IsCloseTo.closeTo(0.023d, PRECISION)); assertThat(accumulated[2], IsCloseTo.closeTo(0.043d, PRECISION)); assertThat(accumulated[3], IsCloseTo.closeTo(0.02d, PRECISION)); assertThat(accumulated[4], IsCloseTo.closeTo(0.05d, PRECISION)); assertThat(accumulated[5], IsCloseTo.closeTo(0.08d, PRECISION)); assertThat(accumulated[6], IsCloseTo.closeTo(0.10d, PRECISION)); assertThat(accumulated[7], IsCloseTo.closeTo(0.04d, PRECISION)); assertThat(accumulated[8], IsCloseTo.closeTo(-0.05d, PRECISION)); }