use of name.abuchen.portfolio.money.ExchangeRate in project portfolio by buchen.
the class InverseExchangeRateTimeSeriesTest method testInverseLookupOfExchangeRate.
@Test
public void testInverseLookupOfExchangeRate() {
ExchangeRateTimeSeriesImpl source = new ExchangeRateTimeSeriesImpl(null, "EUR", "USD");
source.addRate(new ExchangeRate(LocalDate.parse("2014-12-01"), BigDecimal.valueOf(1)));
source.addRate(new ExchangeRate(LocalDate.parse("2014-12-02"), BigDecimal.valueOf(2)));
source.addRate(new ExchangeRate(LocalDate.parse("2014-12-03"), BigDecimal.valueOf(3)));
InverseExchangeRateTimeSeries inverse = new InverseExchangeRateTimeSeries(source);
assertThat(inverse.lookupRate(LocalDate.parse("2014-11-30")).get().getValue(), is(new BigDecimal(1).setScale(10)));
assertThat(inverse.lookupRate(LocalDate.parse("2014-12-01")).get().getValue(), is(new BigDecimal(1).setScale(10)));
assertThat(inverse.lookupRate(LocalDate.parse("2014-12-02")).get().getValue(), is(new BigDecimal(0.500).setScale(10)));
assertThat(inverse.lookupRate(LocalDate.parse("2014-12-03")).get().getValue(), is(BigDecimal.valueOf(0.3333333333)));
assertThat(inverse.lookupRate(LocalDate.parse("2014-12-04")).get().getValue(), is(BigDecimal.valueOf(0.3333333333)));
assertThat(inverse.getBaseCurrency(), is(source.getTermCurrency()));
assertThat(inverse.getTermCurrency(), is(source.getBaseCurrency()));
}
use of name.abuchen.portfolio.money.ExchangeRate in project portfolio by buchen.
the class AccountTransactionModel method updateExchangeRate.
private void updateExchangeRate() {
// transaction
if (sourceTransaction != null)
return;
if (getAccountCurrencyCode().equals(getSecurityCurrencyCode())) {
setExchangeRate(BigDecimal.ONE);
} else if (!getSecurityCurrencyCode().isEmpty()) {
ExchangeRateTimeSeries series = //
getExchangeRateProviderFactory().getTimeSeries(getSecurityCurrencyCode(), getAccountCurrencyCode());
if (series != null)
setExchangeRate(series.lookupRate(date).orElse(new ExchangeRate(date, BigDecimal.ONE)).getValue());
else
setExchangeRate(BigDecimal.ONE);
}
}
use of name.abuchen.portfolio.money.ExchangeRate in project portfolio by buchen.
the class TestCurrencyConverter method convert.
@Override
public Money convert(LocalDate date, Money amount) {
if (termCurrency.equals(amount.getCurrencyCode()))
return amount;
if (amount.isZero())
return Money.of(termCurrency, 0);
// testing: any other currency will be converted 1:1
if (!amount.getCurrencyCode().equals(series.getBaseCurrency()))
return Money.of(termCurrency, amount.getAmount());
ExchangeRate rate = getRate(date, amount.getCurrencyCode());
BigDecimal converted = rate.getValue().multiply(BigDecimal.valueOf(amount.getAmount()));
return Money.of(termCurrency, Math.round(converted.doubleValue()));
}
use of name.abuchen.portfolio.money.ExchangeRate in project portfolio by buchen.
the class ExchangeRatesListView method createTopTable.
@Override
protected void createTopTable(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
TableColumnLayout layout = new TableColumnLayout();
container.setLayout(layout);
TableViewer indeces = new TableViewer(container, SWT.FULL_SELECTION);
ShowHideColumnHelper support = new // $NON-NLS-1$
ShowHideColumnHelper(// $NON-NLS-1$
ExchangeRatesListView.class.getSimpleName() + "@top2", getPreferenceStore(), indeces, layout);
Column column = new Column(Messages.ColumnBaseCurrency, SWT.None, 80);
column.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
return ((ExchangeRateTimeSeries) element).getBaseCurrency();
}
});
// $NON-NLS-1$ //$NON-NLS-2$
ColumnViewerSorter.create(ExchangeRateTimeSeries.class, "baseCurrency", "termCurrency").attachTo(column, SWT.DOWN);
support.addColumn(column);
column = new Column(Messages.ColumnTermCurrency, SWT.None, 80);
column.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
return ((ExchangeRateTimeSeries) element).getTermCurrency();
}
});
// $NON-NLS-1$ //$NON-NLS-2$
ColumnViewerSorter.create(ExchangeRateTimeSeries.class, "termCurrency", "baseCurrency").attachTo(column);
support.addColumn(column);
column = new Column(Messages.ColumnCurrencyProvider, SWT.None, 150);
column.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
return ((ExchangeRateTimeSeries) element).getProvider().getName();
}
});
// $NON-NLS-1$
ColumnViewerSorter.create(ExchangeRateTimeSeries.class, "provider").attachTo(column);
support.addColumn(column);
column = new Column(Messages.ColumnDateLatestExchangeRate, SWT.None, 150);
column.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
ExchangeRateTimeSeries series = (ExchangeRateTimeSeries) element;
List<ExchangeRate> rates = series.getRates();
return rates.isEmpty() ? null : Values.Date.format(rates.get(rates.size() - 1).getTime());
}
});
ColumnViewerSorter.create(element -> {
ExchangeRateTimeSeries series = (ExchangeRateTimeSeries) element;
List<ExchangeRate> rates = series.getRates();
return rates.isEmpty() ? null : rates.get(rates.size() - 1).getTime();
}).attachTo(column);
support.addColumn(column);
support.createColumns();
indeces.getTable().setHeaderVisible(true);
indeces.getTable().setLinesVisible(true);
indeces.setContentProvider(ArrayContentProvider.getInstance());
indeces.setInput(providerFactory.getAvailableTimeSeries());
indeces.refresh();
indeces.addSelectionChangedListener(event -> refreshChart((ExchangeRateTimeSeries) ((IStructuredSelection) event.getSelection()).getFirstElement()));
}
use of name.abuchen.portfolio.money.ExchangeRate in project portfolio by buchen.
the class ECBExchangeRateProvider method save.
@Override
public synchronized void save(IProgressMonitor monitor) throws IOException {
if (!data.isDirty())
return;
// store latest exchange rate separately -> faster to load upon startup
// of the application
File file = getStorageFile(FILE_SUMMARY);
Map<String, ExchangeRate> summary = new HashMap<>();
for (ExchangeRateTimeSeriesImpl s : data.getSeries()) s.getLatest().ifPresent(rate -> summary.put(s.getTermCurrency(), rate));
write(summary, file);
// write the full history data
file = getStorageFile(FILE_STORAGE);
write(data, file);
}
Aggregations