use of name.abuchen.portfolio.money.CurrencyConverter in project portfolio by buchen.
the class ClientIRRYield method collectDatesAndValues.
private static void collectDatesAndValues(Interval interval, ClientSnapshot snapshotStart, ClientSnapshot snapshotEnd, List<Transaction> transactions, List<LocalDate> dates, List<Double> values) {
CurrencyConverter converter = snapshotStart.getCurrencyConverter();
dates.add(interval.getStart());
// snapshots are always in target currency, no conversion needed
values.add(-snapshotStart.getMonetaryAssets().getAmount() / Values.Amount.divider());
for (Transaction t : transactions) {
dates.add(t.getDateTime().toLocalDate());
if (t instanceof AccountTransaction) {
AccountTransaction at = (AccountTransaction) t;
long amount = converter.convert(t.getDateTime(), t.getMonetaryAmount()).getAmount();
if (at.getType() == Type.DEPOSIT || at.getType() == Type.TRANSFER_IN)
amount = -amount;
values.add(amount / Values.Amount.divider());
} else if (t instanceof PortfolioTransaction) {
PortfolioTransaction pt = (PortfolioTransaction) t;
long amount = converter.convert(t.getDateTime(), t.getMonetaryAmount()).getAmount();
if (pt.getType() == PortfolioTransaction.Type.DELIVERY_INBOUND || pt.getType() == PortfolioTransaction.Type.TRANSFER_IN)
amount = -amount;
values.add(amount / Values.Amount.divider());
} else {
throw new UnsupportedOperationException();
}
}
dates.add(interval.getEnd());
values.add(snapshotEnd.getMonetaryAssets().getAmount() / Values.Amount.divider());
}
use of name.abuchen.portfolio.money.CurrencyConverter in project portfolio by buchen.
the class SecurityIndex method calculate.
/* package */
void calculate(PerformanceIndex clientIndex, Security security) {
List<SecurityPrice> prices = security.getPrices();
if (prices.isEmpty()) {
initEmpty(clientIndex);
return;
}
// prices only include historical quotes, not the latest quote. Merge
// the latest quote into the list if necessary
prices = security.getPricesIncludingLatest();
Interval actualInterval = clientIndex.getActualInterval();
LocalDate firstPricePoint = prices.get(0).getDate();
if (firstPricePoint.isAfter(actualInterval.getEnd())) {
initEmpty(clientIndex);
return;
}
LocalDate startDate = clientIndex.getFirstDataPoint().orElse(actualInterval.getEnd());
if (firstPricePoint.isAfter(startDate))
startDate = firstPricePoint;
LocalDate endDate = actualInterval.getEnd();
LocalDate lastPricePoint = prices.get(prices.size() - 1).getDate();
if (lastPricePoint.isBefore(endDate))
endDate = lastPricePoint;
int size = (int) ChronoUnit.DAYS.between(startDate, endDate) + 1;
if (size <= 0) {
initEmpty(clientIndex);
return;
}
// needs currency conversion if
// a) the currency of the security is not null
// (otherwise it is an index)
// b) the term currency differs from the currency of the security
CurrencyConverter converter = security.getCurrencyCode() != null && !security.getCurrencyCode().equals(clientIndex.getCurrencyConverter().getTermCurrency()) ? clientIndex.getCurrencyConverter() : null;
dates = new LocalDate[size];
delta = new double[size];
accumulated = new double[size];
inboundTransferals = new long[size];
outboundTransferals = new long[size];
totals = new long[size];
final double adjustment = clientIndex.getAccumulatedPercentage()[Dates.daysBetween(actualInterval.getStart(), startDate)];
// first value = reference value
dates[0] = startDate;
delta[0] = 0;
accumulated[0] = adjustment;
long valuation = totals[0] = convert(converter, security, startDate);
// calculate series
int index = 1;
LocalDate date = startDate.plusDays(1);
while (date.compareTo(endDate) <= 0) {
dates[index] = date;
long thisValuation = totals[index] = convert(converter, security, date);
long thisDelta = thisValuation - valuation;
delta[index] = (double) thisDelta / (double) valuation;
accumulated[index] = ((accumulated[index - 1] + 1 - adjustment) * (delta[index] + 1)) - 1 + adjustment;
date = date.plusDays(1);
valuation = thisValuation;
index++;
}
}
use of name.abuchen.portfolio.money.CurrencyConverter in project portfolio by buchen.
the class AbstractNodeTreeViewer method addAdditionalColumns.
protected // NOSONAR
void addAdditionalColumns(// NOSONAR
ShowHideColumnHelper support) {
// $NON-NLS-1$
Column column = new Column("exchangeRate", Messages.ColumnExchangeRate, SWT.RIGHT, 80);
column.setGroupLabel(Messages.ColumnForeignCurrencies);
column.setLabelProvider(new // NOSONAR
ColumnLabelProvider() {
@Override
public String getText(Object element) {
TaxonomyNode node = (TaxonomyNode) element;
if (!node.isAssignment())
return null;
String baseCurrency = node.getAssignment().getInvestmentVehicle().getCurrencyCode();
if (baseCurrency == null)
return null;
CurrencyConverter converter = getModel().getCurrencyConverter();
ExchangeRate rate = converter.getRate(LocalDate.now(), baseCurrency);
if (useIndirectQuotation)
rate = rate.inverse();
return Values.ExchangeRate.format(rate.getValue());
}
@Override
public String getToolTipText(Object e) {
String text = getText(e);
if (text == null)
return null;
String term = getModel().getCurrencyConverter().getTermCurrency();
String base = ((TaxonomyNode) e).getAssignment().getInvestmentVehicle().getCurrencyCode();
return text + ' ' + (useIndirectQuotation ? base + '/' + term : term + '/' + base);
}
});
column.setVisible(false);
support.addColumn(column);
// $NON-NLS-1$
column = new Column("actBaseCurrency", Messages.ColumnActualValue + Messages.BaseCurrencyCue, SWT.RIGHT, 100);
column.setDescription(Messages.ColumnActualValueBaseCurrency);
column.setGroupLabel(Messages.ColumnForeignCurrencies);
column.setLabelProvider(new // NOSONAR
ColumnLabelProvider() {
@Override
public String getText(Object element) {
TaxonomyNode node = (TaxonomyNode) element;
if (node.isClassification() || getModel().getCurrencyCode().equals(node.getAssignment().getInvestmentVehicle().getCurrencyCode())) {
return Values.Money.format(node.getActual(), getModel().getCurrencyCode());
} else if (node.getAssignment().getInvestmentVehicle().getCurrencyCode() != null) {
// currency code (e.g. is not an stock market index)
return Values.Money.format(getModel().getCurrencyConverter().with(node.getAssignment().getInvestmentVehicle().getCurrencyCode()).convert(LocalDate.now(), node.getActual()), getModel().getCurrencyCode());
} else {
return null;
}
}
});
column.setVisible(false);
support.addColumn(column);
//
getModel().getClient().getSettings().getAttributeTypes().filter(//
a -> a.supports(Security.class)).forEach(attribute -> {
Column col = new AttributeColumn(attribute);
col.setVisible(false);
col.setSorter(null);
col.getEditingSupport().addListener(this);
support.addColumn(col);
});
}
Aggregations