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++;
}
}
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;
}
});
}
}
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);
});
}
});
}
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())));
}
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);
}
Aggregations