Search in sources :

Example 1 with Interval

use of name.abuchen.portfolio.util.Interval in project portfolio by buchen.

the class ClientIndex method calculate.

/* package */
void calculate(List<Exception> warnings) {
    Interval interval = getReportInterval().toInterval();
    // the actual interval should not extend into the future
    if (interval.getEnd().isAfter(LocalDate.now())) {
        LocalDate start = interval.getStart();
        LocalDate end = LocalDate.now();
        if (start.isAfter(end))
            start = end;
        interval = Interval.of(start, end);
    }
    // reported via forum: if the user selects as 'since' date something in
    // the future, then #getDays will return something negative. Ensure the
    // 'size' is at least 1 which will create an empty ClientIndex
    int size = Math.max(1, (int) interval.getDays() + 1);
    dates = new LocalDate[size];
    totals = new long[size];
    delta = new double[size];
    accumulated = new double[size];
    inboundTransferals = new long[size];
    outboundTransferals = new long[size];
    taxes = new long[size];
    dividends = new long[size];
    interest = new long[size];
    interestCharge = new long[size];
    collectTransferalsAndTaxes(interval);
    // first value = reference value
    dates[0] = interval.getStart();
    delta[0] = 0;
    accumulated[0] = 0;
    ClientSnapshot snapshot = ClientSnapshot.create(getClient(), getCurrencyConverter(), dates[0]);
    long valuation = totals[0] = snapshot.getMonetaryAssets().getAmount();
    // calculate series
    int index = 1;
    LocalDate date = interval.getStart().plusDays(1);
    while (date.compareTo(interval.getEnd()) <= 0) {
        dates[index] = date;
        snapshot = ClientSnapshot.create(getClient(), getCurrencyConverter(), dates[index]);
        long thisValuation = totals[index] = snapshot.getMonetaryAssets().getAmount();
        if (valuation + inboundTransferals[index] == 0) {
            delta[index] = 0;
            long thisDelta = thisValuation - inboundTransferals[index] + outboundTransferals[index] - valuation;
            if (thisDelta != 0) {
                warnings.add(new RuntimeException(MessageFormat.format(Messages.MsgDeltaWithoutAssets, Values.Amount.format(thisDelta), date.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)))));
            }
        } else {
            delta[index] = (double) (thisValuation + outboundTransferals[index]) / (double) (valuation + inboundTransferals[index]) - 1;
        }
        accumulated[index] = ((accumulated[index - 1] + 1) * (delta[index] + 1)) - 1;
        date = date.plusDays(1);
        valuation = thisValuation;
        index++;
    }
}
Also used : LocalDate(java.time.LocalDate) Interval(name.abuchen.portfolio.util.Interval)

Example 2 with Interval

use of name.abuchen.portfolio.util.Interval in project portfolio by buchen.

the class ClientIndex method collectTransferalsAndTaxes.

private void collectTransferalsAndTaxes(Interval interval) {
    for (Account account : getClient().getAccounts()) {
        // 
        account.getTransactions().stream().filter(t -> !t.getDateTime().toLocalDate().isBefore(interval.getStart()) && !t.getDateTime().toLocalDate().isAfter(interval.getEnd())).forEach(t -> {
            // NOSONAR
            LocalDate d = t.getDateTime().toLocalDate();
            switch(t.getType()) {
                case DEPOSIT:
                    addValue(inboundTransferals, t.getCurrencyCode(), t.getAmount(), interval, d);
                    break;
                case REMOVAL:
                    addValue(outboundTransferals, t.getCurrencyCode(), t.getAmount(), interval, d);
                    break;
                case TAXES:
                    addValue(taxes, t.getCurrencyCode(), t.getAmount(), interval, d);
                    break;
                case TAX_REFUND:
                    addValue(taxes, t.getCurrencyCode(), -t.getAmount(), interval, d);
                    break;
                case DIVIDENDS:
                    addValue(taxes, t.getCurrencyCode(), t.getUnitSum(Unit.Type.TAX).getAmount(), interval, d);
                    addValue(dividends, t.getCurrencyCode(), t.getAmount(), interval, d);
                    break;
                case INTEREST:
                    addValue(interest, t.getCurrencyCode(), t.getAmount(), interval, d);
                    break;
                case INTEREST_CHARGE:
                    addValue(interest, t.getCurrencyCode(), -t.getAmount(), interval, d);
                    addValue(interestCharge, t.getCurrencyCode(), t.getAmount(), interval, d);
                    break;
                default:
                    // do nothing
                    break;
            }
        });
    }
    for (Portfolio portfolio : getClient().getPortfolios()) {
        // 
        portfolio.getTransactions().stream().filter(t -> !t.getDateTime().toLocalDate().isBefore(interval.getStart()) && !t.getDateTime().toLocalDate().isAfter(interval.getEnd())).forEach(t -> {
            LocalDate d = t.getDateTime().toLocalDate();
            // collect taxes
            addValue(// 
            taxes, // 
            t.getCurrencyCode(), // 
            t.getUnitSum(Unit.Type.TAX).getAmount(), interval, d);
            // collect transferals
            switch(t.getType()) {
                case DELIVERY_INBOUND:
                    addValue(inboundTransferals, t.getCurrencyCode(), t.getAmount(), interval, d);
                    break;
                case DELIVERY_OUTBOUND:
                    addValue(outboundTransferals, t.getCurrencyCode(), t.getAmount(), interval, d);
                    break;
                default:
                    break;
            }
        });
    }
}
Also used : Money(name.abuchen.portfolio.money.Money) Values(name.abuchen.portfolio.money.Values) Client(name.abuchen.portfolio.model.Client) Account(name.abuchen.portfolio.model.Account) FormatStyle(java.time.format.FormatStyle) Messages(name.abuchen.portfolio.Messages) MessageFormat(java.text.MessageFormat) List(java.util.List) Dates(name.abuchen.portfolio.util.Dates) Unit(name.abuchen.portfolio.model.Transaction.Unit) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) Interval(name.abuchen.portfolio.util.Interval) Portfolio(name.abuchen.portfolio.model.Portfolio) Account(name.abuchen.portfolio.model.Account) Portfolio(name.abuchen.portfolio.model.Portfolio) LocalDate(java.time.LocalDate)

Example 3 with Interval

use of name.abuchen.portfolio.util.Interval in project portfolio by buchen.

the class SecuritiesChart method setupTooltip.

private void setupTooltip() {
    TimelineChartToolTip toolTip = chart.getToolTip();
    toolTip.setValueFormat(new DecimalFormat(Values.Quote.pattern()));
    // $NON-NLS-1$
    toolTip.addSeriesExclude(Messages.LabelChartDetailClosingIndicator + "Positive");
    // $NON-NLS-1$
    toolTip.addSeriesExclude(Messages.LabelChartDetailClosingIndicator + "Negative");
    // $NON-NLS-1$
    toolTip.addSeriesExclude(Messages.LabelChartDetailClosingIndicator + "Zero");
    toolTip.addSeriesExclude(Messages.SecurityMenuBuy);
    // $NON-NLS-1$
    toolTip.addSeriesExclude(Messages.SecurityMenuBuy + "1");
    // $NON-NLS-1$
    toolTip.addSeriesExclude(Messages.SecurityMenuBuy + "2");
    toolTip.addSeriesExclude(Messages.SecurityMenuSell);
    // $NON-NLS-1$
    toolTip.addSeriesExclude(Messages.SecurityMenuSell + "1");
    // $NON-NLS-1$
    toolTip.addSeriesExclude(Messages.SecurityMenuSell + "2");
    toolTip.addSeriesExclude(Messages.LabelChartDetailDividends);
    // $NON-NLS-1$
    toolTip.addSeriesExclude(Messages.LabelChartDetailDividends + "1");
    // $NON-NLS-1$
    toolTip.addSeriesExclude(Messages.LabelChartDetailDividends + "2");
    toolTip.addSeriesExclude(Messages.LabelChartDetailBollingerBands);
    toolTip.addExtraInfo((composite, focus) -> {
        if (focus instanceof Date) {
            Instant instant = ((Date) focus).toInstant();
            ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
            LocalDate date = zdt.toLocalDate();
            Interval displayInterval = Interval.of(date.minusDays(5), date.plusDays(5));
            // 
            customTooltipEvents.stream().filter(// 
            t -> displayInterval.contains(t.getDateTime())).forEach(t -> {
                if (t instanceof AccountTransaction)
                    addDividendTooltip(composite, (AccountTransaction) t);
                else if (t instanceof PortfolioTransaction)
                    addInvestmentTooltip(composite, (PortfolioTransaction) t);
            });
        }
    });
}
Also used : Arrays(java.util.Arrays) Client(name.abuchen.portfolio.model.Client) Transaction(name.abuchen.portfolio.model.Transaction) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) LineStyle(org.swtchart.LineStyle) Point(org.eclipse.swt.graphics.Point) Composite(org.eclipse.swt.widgets.Composite) ILineSeries(org.swtchart.ILineSeries) Interval(name.abuchen.portfolio.util.Interval) EnumSet(java.util.EnumSet) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Separator(org.eclipse.jface.action.Separator) Button(org.eclipse.swt.widgets.Button) MenuManager(org.eclipse.jface.action.MenuManager) Security(name.abuchen.portfolio.model.Security) Display(org.eclipse.swt.widgets.Display) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) List(java.util.List) Colors(name.abuchen.portfolio.ui.util.Colors) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) LocalDate(java.time.LocalDate) RowLayoutFactory(org.eclipse.jface.layout.RowLayoutFactory) SWT(org.eclipse.swt.SWT) ClientSecurityFilter(name.abuchen.portfolio.snapshot.filter.ClientSecurityFilter) SeriesType(org.swtchart.ISeries.SeriesType) Optional(java.util.Optional) Label(org.eclipse.swt.widgets.Label) ClientSnapshot(name.abuchen.portfolio.snapshot.ClientSnapshot) SelectionListener(org.eclipse.swt.events.SelectionListener) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) PaintListener(org.eclipse.swt.events.PaintListener) Money(name.abuchen.portfolio.money.Money) Values(name.abuchen.portfolio.money.Values) IAxis(org.swtchart.IAxis) Images(name.abuchen.portfolio.ui.Images) AssetPosition(name.abuchen.portfolio.snapshot.AssetPosition) ArrayUtils(org.apache.commons.lang3.ArrayUtils) ISeries(org.swtchart.ISeries) TimelineChartToolTip(name.abuchen.portfolio.ui.util.chart.TimelineChartToolTip) Range(org.swtchart.Range) ArrayList(java.util.ArrayList) MessageFormat(com.ibm.icu.text.MessageFormat) Messages(name.abuchen.portfolio.ui.Messages) TemporalAmount(java.time.temporal.TemporalAmount) Period(java.time.Period) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) GridDataFactory(org.eclipse.jface.layout.GridDataFactory) DecimalFormat(java.text.DecimalFormat) SimpleAction(name.abuchen.portfolio.ui.util.SimpleAction) ILegend(org.swtchart.ILegend) Action(org.eclipse.jface.action.Action) TimelineChart(name.abuchen.portfolio.ui.util.chart.TimelineChart) PlotSymbolType(org.swtchart.ILineSeries.PlotSymbolType) PortfolioPlugin(name.abuchen.portfolio.ui.PortfolioPlugin) Color(org.eclipse.swt.graphics.Color) Unit(name.abuchen.portfolio.model.Transaction.Unit) IMenuManager(org.eclipse.jface.action.IMenuManager) DateTimeFormatter(java.time.format.DateTimeFormatter) SecurityPrice(name.abuchen.portfolio.model.SecurityPrice) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Menu(org.eclipse.swt.widgets.Menu) Collections(java.util.Collections) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) TimelineChartToolTip(name.abuchen.portfolio.ui.util.chart.TimelineChartToolTip) ZonedDateTime(java.time.ZonedDateTime) DecimalFormat(java.text.DecimalFormat) Instant(java.time.Instant) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) LocalDate(java.time.LocalDate) Date(java.util.Date) LocalDate(java.time.LocalDate) Interval(name.abuchen.portfolio.util.Interval)

Example 4 with Interval

use of name.abuchen.portfolio.util.Interval in project portfolio by buchen.

the class MaxDrawdownDurationWidget method update.

@Override
public void update() {
    super.update();
    PerformanceIndex index = getDashboardData().getDataSeriesCache().lookup(get(DataSeriesConfig.class).getDataSeries(), get(ReportingPeriodConfig.class).getReportingPeriod());
    Drawdown drawdown = index.getDrawdown();
    Interval maxDDDuration = drawdown.getMaxDrawdownDuration();
    indicator.setText(MessageFormat.format(Messages.LabelXDays, maxDDDuration.getDays()));
    boolean isUntilEndOfPeriod = maxDDDuration.getEnd().equals(index.getReportInterval().getEndDate());
    String maxDDSupplement = isUntilEndOfPeriod ? Messages.TooltipMaxDrawdownDurationEndOfPeriod : Messages.TooltipMaxDrawdownDurationFromXtoY;
    // recovery time
    Interval recoveryTime = drawdown.getLongestRecoveryTime();
    isUntilEndOfPeriod = recoveryTime.getEnd().equals(index.getReportInterval().getEndDate());
    String recoveryTimeSupplement = isUntilEndOfPeriod ? Messages.TooltipMaxDrawdownDurationEndOfPeriod : Messages.TooltipMaxDrawdownDurationFromXtoY;
    InfoToolTip.attach(indicator, // $NON-NLS-1$
    Messages.TooltipMaxDrawdownDuration + "\n\n" + MessageFormat.format(maxDDSupplement, formatter.format(maxDDDuration.getStart()), formatter.format(maxDDDuration.getEnd())) + // $NON-NLS-1$
    "\n\n" + MessageFormat.format(Messages.TooltipMaxDurationLowToHigh, recoveryTime.getDays()) + MessageFormat.format(recoveryTimeSupplement, formatter.format(recoveryTime.getStart()), formatter.format(recoveryTime.getEnd())));
}
Also used : PerformanceIndex(name.abuchen.portfolio.snapshot.PerformanceIndex) Drawdown(name.abuchen.portfolio.math.Risk.Drawdown) Interval(name.abuchen.portfolio.util.Interval)

Example 5 with Interval

use of name.abuchen.portfolio.util.Interval in project portfolio by buchen.

the class CPIIndex method calculate.

/* package */
void calculate(PerformanceIndex clientIndex) {
    Interval actualInterval = clientIndex.getActualInterval();
    LocalDate firstDataPoint = clientIndex.getFirstDataPoint().orElse(actualInterval.getStart());
    Interval interval = Interval.of(firstDataPoint, actualInterval.getEnd());
    List<ConsumerPriceIndex> cpiSeries = new ArrayList<ConsumerPriceIndex>(clientIndex.getClient().getConsumerPriceIndices());
    Collections.sort(cpiSeries, new ConsumerPriceIndex.ByDate());
    List<LocalDate> dates = new ArrayList<LocalDate>();
    List<Double> accumulated = new ArrayList<Double>();
    int baseline = -1;
    for (ConsumerPriceIndex cpi : cpiSeries) {
        LocalDate date = LocalDate.of(cpi.getYear(), cpi.getMonth(), 1);
        date = date.with(ChronoField.DAY_OF_MONTH, date.lengthOfMonth());
        if (interval.contains(date)) {
            if (baseline == -1)
                baseline = cpi.getIndex();
            dates.add(date);
            accumulated.add(((double) cpi.getIndex() / (double) baseline) - 1);
        }
    }
    this.dates = dates.toArray(new LocalDate[0]);
    this.accumulated = new double[accumulated.size()];
    for (int ii = 0; ii < this.accumulated.length; ii++) this.accumulated[ii] = accumulated.get(ii);
}
Also used : ConsumerPriceIndex(name.abuchen.portfolio.model.ConsumerPriceIndex) ArrayList(java.util.ArrayList) LocalDate(java.time.LocalDate) Interval(name.abuchen.portfolio.util.Interval)

Aggregations

Interval (name.abuchen.portfolio.util.Interval)9 LocalDate (java.time.LocalDate)8 CurrencyConverter (name.abuchen.portfolio.money.CurrencyConverter)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)3 Client (name.abuchen.portfolio.model.Client)3 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)3 Money (name.abuchen.portfolio.money.Money)3 Values (name.abuchen.portfolio.money.Values)3 DateTimeFormatter (java.time.format.DateTimeFormatter)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Optional (java.util.Optional)2 Messages (name.abuchen.portfolio.Messages)2 Drawdown (name.abuchen.portfolio.math.Risk.Drawdown)2 Account (name.abuchen.portfolio.model.Account)2 Portfolio (name.abuchen.portfolio.model.Portfolio)2 Security (name.abuchen.portfolio.model.Security)2 SecurityPrice (name.abuchen.portfolio.model.SecurityPrice)2