@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 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 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 testChangesOnFirstDayOfInvestment() { Client client = new Client(); new AccountBuilder() // .deposit_("2012-01-01", 10000) // .interest("2012-01-02", 1000) // .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 - 1], IsCloseTo.closeTo(0.1d, PRECISION)); }
@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)); }