Skip to content

Commit

Permalink
WIP first sample of annualized quote change column
Browse files Browse the repository at this point in the history
  • Loading branch information
OnkelDok committed Jan 3, 2025
1 parent 5cddf16 commit b0ddf35
Showing 1 changed file with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ public SecuritiesTable(Composite parent, AbstractFinanceView view)
addColumnDateOfLatestPrice();
addColumnDateOfLatestHistoricalPrice();
addQuoteDeltaColumn();
addQuoteDeltaColumnAnnualized();
support.addColumn(new DistanceFromMovingAverageColumn(LocalDate::now));
support.addColumn(new DistanceFromAllTimeHighColumn(LocalDate::now,
view.getPart().getReportingPeriods().stream().collect(toMutableList())));
Expand Down Expand Up @@ -588,7 +589,7 @@ private void addQuoteDeltaColumn() // NOSONAR
if (previous.getDate().isAfter(interval.getStart()))
return null;

return Double.valueOf((latest.getValue() - previous.getValue()) / (double) previous.getValue());
return Double.valueOf((latest.getValue() / (double) previous.getValue()) - 1);
};

Column column = new Column("delta-w-period", Messages.ColumnQuoteChange, SWT.RIGHT, 80); //$NON-NLS-1$
Expand All @@ -614,6 +615,59 @@ else if (v2 == null)
support.addColumn(column);
}

private void addQuoteDeltaColumnAnnualized() // NOSONAR
{
// create a modifiable copy as all menus share the same list of
// reporting periods
List<ReportingPeriod> options = view.getPart().getReportingPeriods().stream().collect(toMutableList());

BiFunction<Object, ReportingPeriod, Double> valueProvider = (element, option) -> {

Interval interval = option.toInterval(LocalDate.now());

Security security = (Security) element;

SecurityPrice latest = security.getSecurityPrice(interval.getEnd());
SecurityPrice previous = security.getSecurityPrice(interval.getStart());

if (latest == null || previous == null)
return null;

if (previous.getValue() == 0)
return null;

if (previous.getDate().isAfter(interval.getStart()))
return null;

var totalDays = (double) java.time.temporal.ChronoUnit.DAYS.between(previous.getDate(), latest.getDate());

var totalGain = latest.getValue() / (double) previous.getValue();
return Double.valueOf(Math.pow(totalGain, 365 / totalDays)) - 1;
};

Column column = new Column("delta-w-period-annualized", Messages.ColumnQuoteChange + " (annualisiert)", SWT.RIGHT, 80); //$NON-NLS-1$
column.setOptions(new ReportingPeriodColumnOptions(Messages.ColumnQuoteChange_Option + " (p.a.)", options));
column.setDescription(Messages.ColumnQuoteChange_Description);
column.setLabelProvider(new QuoteReportingPeriodLabelProvider(valueProvider));
column.setVisible(false);
column.setSorter(ColumnViewerSorter.create((o1, o2) -> {
ReportingPeriod option = (ReportingPeriod) ColumnViewerSorter.SortingContext.getColumnOption();

Double v1 = valueProvider.apply(o1, option);
Double v2 = valueProvider.apply(o2, option);

if (v1 == null && v2 == null)
return 0;
else if (v1 == null)
return -1;
else if (v2 == null)
return 1;

return Double.compare(v1.doubleValue(), v2.doubleValue());
}));
support.addColumn(column);
}

private void addAttributeColumns()
{
AttributeColumn.createFor(getClient(), Security.class) //
Expand Down

0 comments on commit b0ddf35

Please sign in to comment.