use of name.abuchen.portfolio.TestCurrencyConverter in project portfolio by buchen.
the class SecurityPositionTest method testPurchasePriceWithMultipleBuyTransactions.
@Test
public void testPurchasePriceWithMultipleBuyTransactions() {
List<PortfolioTransaction> tx = new ArrayList<PortfolioTransaction>();
tx.add(new PortfolioTransaction(LocalDateTime.now(), CurrencyUnit.EUR, 25000, null, 25 * Values.Share.factor(), Type.BUY, 0, 0));
tx.add(new PortfolioTransaction(LocalDateTime.now(), CurrencyUnit.EUR, 150000, null, 75 * Values.Share.factor(), Type.BUY, 0, 0));
tx.add(new PortfolioTransaction(LocalDateTime.now(), CurrencyUnit.EUR, 100000, null, 50 * Values.Share.factor(), Type.SELL, 0, 0));
SecurityPosition position = new SecurityPosition(new Security(), new TestCurrencyConverter(), new SecurityPrice(), tx);
assertThat(position.getShares(), is(50L * Values.Share.factor()));
assertThat(position.getFIFOPurchasePrice(), is(Money.of(CurrencyUnit.EUR, 20_00)));
// expected: (250 + 1500) * (50/100) / 50 (shares held)
assertThat(position.getMovingAveragePurchasePrice(), is(Money.of(CurrencyUnit.EUR, 17_50)));
}
use of name.abuchen.portfolio.TestCurrencyConverter in project portfolio by buchen.
the class SecurityPositionTest method testThatTransferInCountsIfTransferOutIsMissingPlusBuyTransaction.
@Test
public void testThatTransferInCountsIfTransferOutIsMissingPlusBuyTransaction() {
SecurityPrice price = new SecurityPrice(LocalDate.of(2012, Month.DECEMBER, 2), Values.Quote.factorize(20));
List<PortfolioTransaction> tx = new ArrayList<PortfolioTransaction>();
tx.add(new PortfolioTransaction(LocalDateTime.of(2012, Month.JANUARY, 1, 0, 0), CurrencyUnit.EUR, 50000, null, 50 * Values.Share.factor(), Type.BUY, 0, 0));
tx.add(new PortfolioTransaction(LocalDateTime.of(2012, Month.FEBRUARY, 1, 0, 0), CurrencyUnit.EUR, 55000, null, 50 * Values.Share.factor(), Type.TRANSFER_IN, 0, 0));
SecurityPosition position = new SecurityPosition(new Security(), new TestCurrencyConverter(), price, tx);
assertThat(position.getShares(), is(100L * Values.Share.factor()));
assertThat(position.getFIFOPurchasePrice(), is(Money.of(CurrencyUnit.EUR, 10_50)));
assertThat(position.getFIFOPurchaseValue(), is(Money.of(CurrencyUnit.EUR, 1050_00)));
assertThat(position.getMovingAveragePurchasePrice(), is(Money.of(CurrencyUnit.EUR, 10_50)));
assertThat(position.getMovingAveragePurchaseValue(), is(Money.of(CurrencyUnit.EUR, 1050_00)));
assertThat(position.calculateValue(), is(Money.of(CurrencyUnit.EUR, 2000_00)));
assertThat(position.getProfitLoss(), is(Money.of(CurrencyUnit.EUR, 950_00)));
}
use of name.abuchen.portfolio.TestCurrencyConverter 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))));
}
use of name.abuchen.portfolio.TestCurrencyConverter in project portfolio by buchen.
the class IRRCalculationTest method testDividendPaymentsWithTaxes.
@Test
public void testDividendPaymentsWithTaxes() {
List<Transaction> tx = new ArrayList<>();
Security security = new Security();
tx.add(new //
PortfolioTransaction(//
LocalDateTime.of(2015, Month.DECEMBER, 31, 0, 0), //
CurrencyUnit.EUR, //
Values.Amount.factorize(1000), //
security, //
Values.Share.factorize(10), //
PortfolioTransaction.Type.BUY, Values.Amount.factorize(10), 0));
DividendTransaction t = new DividendTransaction();
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));
t.addUnit(new Unit(Unit.Type.TAX, Money.of(CurrencyUnit.EUR, Values.Amount.factorize(50))));
tx.add(t);
tx.add(new //
PortfolioTransaction(//
LocalDateTime.of(2016, Month.DECEMBER, 31, 0, 0), //
CurrencyUnit.EUR, //
Values.Amount.factorize(1200), //
security, //
Values.Share.factorize(10), //
PortfolioTransaction.Type.SELL, Values.Amount.factorize(10), Values.Amount.factorize(30)));
IRRCalculation calculation = Calculation.perform(IRRCalculation.class, new TestCurrencyConverter(), tx);
// Excel verification
// 31.12.15 -1000
// 01.06.16 150
// 31.12.16 1230
// =XINTZINSFUSS(B1:B3;A1:A3) = 0,412128788
assertThat(calculation.getIRR(), IsCloseTo.closeTo(0.412128788d, 0.00000001d));
}
use of name.abuchen.portfolio.TestCurrencyConverter 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));
}
Aggregations