Search in sources :

Example 51 with CurrencyConverter

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());
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Transaction(name.abuchen.portfolio.model.Transaction) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter)

Example 52 with CurrencyConverter

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++;
    }
}
Also used : SecurityPrice(name.abuchen.portfolio.model.SecurityPrice) LocalDate(java.time.LocalDate) Interval(name.abuchen.portfolio.util.Interval) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter)

Example 53 with CurrencyConverter

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);
    });
}
Also used : NameColumn(name.abuchen.portfolio.ui.views.columns.NameColumn) Arrays(java.util.Arrays) DND(org.eclipse.swt.dnd.DND) Classification(name.abuchen.portfolio.model.Classification) ModificationListener(name.abuchen.portfolio.ui.util.viewers.ColumnEditingSupport.ModificationListener) ESelectionService(org.eclipse.e4.ui.workbench.modeling.ESelectionService) Composite(org.eclipse.swt.widgets.Composite) ColumnViewerToolTipSupport(org.eclipse.jface.viewers.ColumnViewerToolTipSupport) ITreeContentProvider(org.eclipse.jface.viewers.ITreeContentProvider) UIConstants(name.abuchen.portfolio.ui.UIConstants) Separator(org.eclipse.jface.action.Separator) Predicate(java.util.function.Predicate) MenuManager(org.eclipse.jface.action.MenuManager) Set(java.util.Set) Security(name.abuchen.portfolio.model.Security) UUID(java.util.UUID) Display(org.eclipse.swt.widgets.Display) ColumnLabelProvider(org.eclipse.jface.viewers.ColumnLabelProvider) ExchangeRate(name.abuchen.portfolio.money.ExchangeRate) PortfolioPart(name.abuchen.portfolio.ui.PortfolioPart) ColumnEditingSupport(name.abuchen.portfolio.ui.util.viewers.ColumnEditingSupport) Transfer(org.eclipse.swt.dnd.Transfer) ShowHideColumnHelper(name.abuchen.portfolio.ui.util.viewers.ShowHideColumnHelper) List(java.util.List) Colors(name.abuchen.portfolio.ui.util.Colors) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) AttributeColumn(name.abuchen.portfolio.ui.views.columns.AttributeColumn) LocalDate(java.time.LocalDate) SWT(org.eclipse.swt.SWT) TreeViewer(org.eclipse.jface.viewers.TreeViewer) SecurityTransfer(name.abuchen.portfolio.ui.dnd.SecurityTransfer) SecuritySelection(name.abuchen.portfolio.ui.selection.SecuritySelection) DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) TransferData(org.eclipse.swt.dnd.TransferData) DragSourceAdapter(org.eclipse.swt.dnd.DragSourceAdapter) ContextMenu(name.abuchen.portfolio.ui.util.ContextMenu) Values(name.abuchen.portfolio.money.Values) NoteColumn(name.abuchen.portfolio.ui.views.columns.NoteColumn) InvestmentVehicle(name.abuchen.portfolio.model.InvestmentVehicle) BookmarkMenu(name.abuchen.portfolio.ui.util.BookmarkMenu) NameColumnLabelProvider(name.abuchen.portfolio.ui.views.columns.NameColumn.NameColumnLabelProvider) Images(name.abuchen.portfolio.ui.Images) Image(org.eclipse.swt.graphics.Image) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) ViewerDropAdapter(org.eclipse.jface.viewers.ViewerDropAdapter) IsinColumn(name.abuchen.portfolio.ui.views.columns.IsinColumn) Messages(name.abuchen.portfolio.ui.Messages) TreeSelection(org.eclipse.jface.viewers.TreeSelection) StringEditingSupport(name.abuchen.portfolio.ui.util.viewers.StringEditingSupport) LinkedList(java.util.LinkedList) ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) ValueEditingSupport(name.abuchen.portfolio.ui.util.viewers.ValueEditingSupport) Viewer(org.eclipse.jface.viewers.Viewer) SimpleAction(name.abuchen.portfolio.ui.util.SimpleAction) Column(name.abuchen.portfolio.ui.util.viewers.Column) Action(org.eclipse.jface.action.Action) TreeViewerCSVExporter(name.abuchen.portfolio.ui.util.TreeViewerCSVExporter) Preference(org.eclipse.e4.core.di.extensions.Preference) Color(org.eclipse.swt.graphics.Color) IMenuManager(org.eclipse.jface.action.IMenuManager) Named(name.abuchen.portfolio.model.Named) StringJoiner(java.util.StringJoiner) TreeColumnLayout(org.eclipse.jface.layout.TreeColumnLayout) ToolTip(org.eclipse.jface.window.ToolTip) Collections(java.util.Collections) Control(org.eclipse.swt.widgets.Control) Assignment(name.abuchen.portfolio.model.Classification.Assignment) AttributeColumn(name.abuchen.portfolio.ui.views.columns.AttributeColumn) ExchangeRate(name.abuchen.portfolio.money.ExchangeRate) NameColumn(name.abuchen.portfolio.ui.views.columns.NameColumn) AttributeColumn(name.abuchen.portfolio.ui.views.columns.AttributeColumn) NoteColumn(name.abuchen.portfolio.ui.views.columns.NoteColumn) IsinColumn(name.abuchen.portfolio.ui.views.columns.IsinColumn) Column(name.abuchen.portfolio.ui.util.viewers.Column) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter)

Aggregations

CurrencyConverter (name.abuchen.portfolio.money.CurrencyConverter)53 Client (name.abuchen.portfolio.model.Client)41 TestCurrencyConverter (name.abuchen.portfolio.TestCurrencyConverter)38 Test (org.junit.Test)36 Security (name.abuchen.portfolio.model.Security)19 ArrayList (java.util.ArrayList)18 AccountBuilder (name.abuchen.portfolio.AccountBuilder)15 Account (name.abuchen.portfolio.model.Account)15 LocalDate (java.time.LocalDate)13 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)12 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)11 SecurityBuilder (name.abuchen.portfolio.SecurityBuilder)9 CurrencyConverterImpl (name.abuchen.portfolio.money.CurrencyConverterImpl)9 Portfolio (name.abuchen.portfolio.model.Portfolio)7 SecurityPrice (name.abuchen.portfolio.model.SecurityPrice)6 Transaction (name.abuchen.portfolio.model.Transaction)6 Unit (name.abuchen.portfolio.model.Transaction.Unit)6 ClientSnapshot (name.abuchen.portfolio.snapshot.ClientSnapshot)6 ReportingPeriod (name.abuchen.portfolio.snapshot.ReportingPeriod)6 Classification (name.abuchen.portfolio.model.Classification)5