use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.
the class CSVSecurityPriceExtractor method extract.
@Override
public List<Item> extract(int skipLines, List<String[]> rawValues, Map<String, Column> field2column, List<Exception> errors) {
Security dummy = new Security();
for (String[] line : rawValues) {
try {
SecurityPrice p = extract(line, field2column);
if (p != null)
dummy.addPrice(p);
} catch (ParseException e) {
errors.add(e);
}
}
List<Item> result = new ArrayList<>();
if (!dummy.getPrices().isEmpty())
result.add(new SecurityItem(dummy));
return result;
}
use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.
the class ReBalancingViewer method addColumns.
@Override
protected void addColumns(ShowHideColumnHelper support) {
addDimensionColumn(support);
addDesiredAllocationColumn(support);
// $NON-NLS-1$
Column column = new Column("targetvalue", Messages.ColumnTargetValue, SWT.RIGHT, 100);
column.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
TaxonomyNode node = (TaxonomyNode) element;
return node.isClassification() ? Values.Money.format(node.getTarget(), getModel().getCurrencyCode()) : null;
}
});
support.addColumn(column);
addActualColumns(support);
// $NON-NLS-1$
column = new Column("delta%", Messages.ColumnDeltaPercent, SWT.RIGHT, 60);
column.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
TaxonomyNode node = (TaxonomyNode) element;
if (node.getTarget() == null)
return null;
return Values.Percent.format(((double) node.getActual().getAmount() / (double) node.getTarget().getAmount()) - 1);
}
@Override
public Color getForeground(Object element) {
TaxonomyNode node = (TaxonomyNode) element;
if (node.getTarget() == null)
return null;
return Display.getCurrent().getSystemColor(node.getActual().isGreaterOrEqualThan(node.getTarget()) ? SWT.COLOR_DARK_GREEN : SWT.COLOR_DARK_RED);
}
});
support.addColumn(column);
// $NON-NLS-1$
column = new Column("delta", Messages.ColumnDeltaValue, SWT.RIGHT, 100);
column.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
TaxonomyNode node = (TaxonomyNode) element;
if (node.getTarget() == null)
return null;
return Values.Money.format(node.getActual().subtract(node.getTarget()), getModel().getCurrencyCode());
}
@Override
public Color getForeground(Object element) {
TaxonomyNode node = (TaxonomyNode) element;
if (node.getTarget() == null)
return null;
return Display.getCurrent().getSystemColor(node.getActual().isGreaterOrEqualThan(node.getTarget()) ? SWT.COLOR_DARK_GREEN : SWT.COLOR_DARK_RED);
}
});
support.addColumn(column);
// $NON-NLS-1$
column = new Column("quote", Messages.ColumnQuote, SWT.RIGHT, 60);
column.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
TaxonomyNode node = (TaxonomyNode) element;
Security security = node.getBackingSecurity();
if (security == null || security.getCurrencyCode() == null)
return null;
SecurityPrice price = security.getSecurityPrice(LocalDate.now());
return Values.Quote.format(security.getCurrencyCode(), price.getValue(), getModel().getCurrencyCode());
}
});
support.addColumn(column);
// $NON-NLS-1$
column = new Column("shares", Messages.ColumnSharesOwned, SWT.RIGHT, 60);
column.setLabelProvider(new SharesLabelProvider() {
@Override
public Long getValue(Object element) {
TaxonomyNode node = (TaxonomyNode) element;
Security security = node.getBackingSecurity();
if (security == null || security.getCurrencyCode() == null)
return null;
AssetPosition position = getModel().getClientSnapshot().getPositionsByVehicle().get(security);
if (position == null)
return null;
return Math.round(position.getPosition().getShares() * node.getWeight() / (double) Classification.ONE_HUNDRED_PERCENT);
}
});
column.setVisible(false);
support.addColumn(column);
// $NON-NLS-1$
column = new Column("deltashares", Messages.ColumnDeltaShares, SWT.RIGHT, 100);
column.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
TaxonomyNode node = (TaxonomyNode) element;
// no delta shares for unassigned securities
if (node.getParent() != null && node.getParent().isUnassignedCategory())
return null;
Security security = node.getBackingSecurity();
if (security == null || security.getCurrencyCode() == null)
return null;
String priceCurrency = security.getCurrencyCode();
long price = security.getSecurityPrice(LocalDate.now()).getValue();
long weightedPrice = Math.round(node.getWeight() * price / (double) Classification.ONE_HUNDRED_PERCENT);
if (weightedPrice == 0L)
return Values.Share.format(0L);
String deltaCurrency = node.getActual().getCurrencyCode();
long delta = node.getParent().getTarget().getAmount() - node.getParent().getActual().getAmount();
// delta in order to know how many shares need to bought or sold
if (!deltaCurrency.equals(priceCurrency)) {
delta = getModel().getCurrencyConverter().with(priceCurrency).convert(LocalDate.now(), Money.of(deltaCurrency, delta)).getAmount();
}
long shares = Math.round(delta * Values.Share.divider() * Values.Quote.dividerToMoney() / weightedPrice);
return Values.Share.format(shares);
}
});
support.addColumn(column);
addAdditionalColumns(support);
}
use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.
the class CSVSecurityPriceExtractorTest method testSecurityCreation.
@Test
public void testSecurityCreation() throws ParseException {
Client client = new Client();
CSVExtractor extractor = new CSVSecurityPriceExtractor(client);
List<Exception> errors = new ArrayList<Exception>();
List<Item> results = extractor.extract(0, //
Arrays.<String[]>asList(new String[] { "2015-01-01", "14,20" }, new String[] { "2015-01-02", "15,20" }), buildField2Column(extractor), errors);
assertThat(errors, empty());
assertThat(results.size(), is(1));
Security security = results.stream().filter(i -> i instanceof SecurityItem).findAny().get().getSecurity();
List<SecurityPrice> prices = security.getPrices();
assertThat(prices.size(), is(2));
assertThat(prices.get(0), is(new SecurityPrice(LocalDate.parse("2015-01-01"), Values.Quote.factorize(14.20))));
assertThat(prices.get(1), is(new SecurityPrice(LocalDate.parse("2015-01-02"), Values.Quote.factorize(15.20))));
}
use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.
the class SecurityPositionTest method testFIFOPurchasePriceWithForex.
@Test
public void testFIFOPurchasePriceWithForex() {
CurrencyConverter currencyConverter = new TestCurrencyConverter().with(CurrencyUnit.USD);
Security security = new Security("", CurrencyUnit.USD);
PortfolioTransaction t = new PortfolioTransaction();
t.setType(PortfolioTransaction.Type.DELIVERY_INBOUND);
t.setDateTime(LocalDateTime.parse("2017-01-25T00:00"));
t.setShares(Values.Share.factorize(13));
t.setSecurity(security);
t.setMonetaryAmount(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(9659.24)));
t.addUnit(new Unit(Unit.Type.GROSS_VALUE, Money.of(CurrencyUnit.EUR, Values.Amount.factorize(9644.24)), Money.of(CurrencyUnit.USD, Values.Amount.factorize(10287.13)), BigDecimal.valueOf(0.937470704040499)));
t.addUnit(new Unit(Unit.Type.FEE, Money.of(CurrencyUnit.EUR, Values.Amount.factorize(15)), Money.of(CurrencyUnit.USD, Values.Amount.factorize(16)), BigDecimal.valueOf(0.937470704040499)));
List<PortfolioTransaction> tx = new ArrayList<>();
tx.add(t);
SecurityPosition position = new SecurityPosition(security, currencyConverter, new SecurityPrice(), tx);
assertThat(position.getShares(), is(13L * Values.Share.factor()));
// 10287.13 / 13 = 791.32
assertThat(position.getFIFOPurchasePrice(), is(Money.of(CurrencyUnit.USD, Values.Amount.factorize(791.32))));
assertThat(position.getMovingAveragePurchasePrice(), is(Money.of(CurrencyUnit.USD, Values.Amount.factorize(791.32))));
// 9659.24 EUR x ( 1 / 0.937470704040499) = 10303,51
assertThat(position.getFIFOPurchaseValue(), is(Money.of(CurrencyUnit.USD, Values.Amount.factorize(9659.24 * (1 / 0.937470704040499)))));
Client client = new Client();
client.addSecurity(security);
Account a = new Account();
client.addAccount(a);
Portfolio p = new Portfolio();
p.setReferenceAccount(a);
p.addTransaction(t);
client.addPortfolio(p);
SecurityPerformanceSnapshot snapshot = SecurityPerformanceSnapshot.create(client, currencyConverter, new ReportingPeriod.FromXtoY(LocalDate.parse("2016-12-31"), LocalDate.parse("2017-02-01")));
assertThat(snapshot.getRecords().size(), is(1));
SecurityPerformanceRecord record = snapshot.getRecords().get(0);
assertThat(record.getSecurity(), is(security));
assertThat(record.getFifoCost(), is(position.getFIFOPurchaseValue()));
assertThat(record.getFifoCostPerSharesHeld().toMoney(), is(position.getFIFOPurchasePrice()));
}
use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.
the class SecurityPositionTest method testFIFOPurchasePrice.
@Test
public void testFIFOPurchasePrice() {
List<PortfolioTransaction> tx = new ArrayList<PortfolioTransaction>();
tx.add(new PortfolioTransaction(LocalDateTime.now(), CurrencyUnit.EUR, 100000, null, 100 * Values.Share.factor(), Type.BUY, 0, 0));
tx.add(new PortfolioTransaction(LocalDateTime.now(), CurrencyUnit.EUR, 50000, 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, 10_00)));
assertThat(position.getMovingAveragePurchasePrice(), is(Money.of(CurrencyUnit.EUR, 10_00)));
}
Aggregations