use of name.abuchen.portfolio.money.Values in project portfolio by buchen.
the class SecuritiesChart method addInvestmentMarkers.
private void addInvestmentMarkers(List<PortfolioTransaction> transactions, String seriesLabel, Color color) {
if (transactions.isEmpty())
return;
customTooltipEvents.addAll(transactions);
if (chartConfig.contains(ChartDetails.SHOW_MARKER_LINES)) {
transactions.forEach(t -> {
String label = Values.Share.format(t.getType().isPurchase() ? t.getShares() : -t.getShares());
double value = t.getGrossPricePerShare(converter.with(t.getSecurity().getCurrencyCode())).getAmount() / Values.Quote.divider();
chart.addMarkerLine(t.getDateTime().toLocalDate(), color, label, value);
});
} else {
Date[] dates = transactions.stream().map(PortfolioTransaction::getDateTime).map(d -> Date.from(d.atZone(ZoneId.systemDefault()).toInstant())).collect(Collectors.toList()).toArray(new Date[0]);
double[] values = transactions.stream().mapToDouble(t -> t.getGrossPricePerShare(converter.with(t.getSecurity().getCurrencyCode())).getAmount() / Values.Quote.divider()).toArray();
// $NON-NLS-1$
ILineSeries border = (ILineSeries) chart.getSeriesSet().createSeries(SeriesType.LINE, seriesLabel + "2");
border.setYAxisId(0);
border.setSymbolColor(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
border.setSymbolType(PlotSymbolType.DIAMOND);
border.setSymbolSize(7);
configureSeriesPainter(border, dates, values, null, 0, LineStyle.NONE, false, false);
ILineSeries background = (ILineSeries) chart.getSeriesSet().createSeries(SeriesType.LINE, // $NON-NLS-1$
seriesLabel + "1");
background.setYAxisId(0);
background.setSymbolType(PlotSymbolType.DIAMOND);
background.setSymbolSize(6);
background.setSymbolColor(Display.getDefault().getSystemColor(SWT.COLOR_WHITE));
configureSeriesPainter(background, dates, values, null, 0, LineStyle.NONE, false, false);
ILineSeries inner = (ILineSeries) chart.getSeriesSet().createSeries(SeriesType.LINE, seriesLabel);
inner.setYAxisId(0);
inner.setSymbolType(PlotSymbolType.DIAMOND);
inner.setSymbolSize(4);
inner.setSymbolColor(color);
configureSeriesPainter(inner, dates, values, color, 0, LineStyle.NONE, false, true);
if (chartConfig.contains(ChartDetails.SHOW_DATA_LABELS)) {
customPaintListeners.add(event -> {
IAxis xAxis = chart.getAxisSet().getXAxis(0);
IAxis yAxis = chart.getAxisSet().getYAxis(0);
for (int index = 0; index < dates.length; index++) {
int x = xAxis.getPixelCoordinate(dates[index].getTime());
int y = yAxis.getPixelCoordinate(values[index]);
PortfolioTransaction t = transactions.get(index);
String label = Values.Share.format(t.getType().isPurchase() ? t.getShares() : -t.getShares());
Point textExtent = event.gc.textExtent(label);
event.gc.setForeground(Colors.BLACK);
event.gc.drawText(label, x - (textExtent.x / 2), y + 10, true);
}
});
}
}
}
use of name.abuchen.portfolio.money.Values in project portfolio by buchen.
the class SecuritiesChart method addDividendMarkerLines.
private void addDividendMarkerLines() {
List<AccountTransaction> dividends = client.getAccounts().stream().flatMap(a -> a.getTransactions().stream()).filter(t -> t.getSecurity() == security).filter(t -> t.getType() == AccountTransaction.Type.DIVIDENDS).filter(t -> chartPeriod == null || chartPeriod.isBefore(t.getDateTime().toLocalDate())).sorted(new Transaction.ByDate()).collect(Collectors.toList());
if (dividends.isEmpty())
return;
customTooltipEvents.addAll(dividends);
if (chartConfig.contains(ChartDetails.SHOW_MARKER_LINES)) {
dividends.forEach(t -> chart.addMarkerLine(t.getDateTime().toLocalDate(), colorEventDividend, getDividendLabel(t)));
} else {
Date[] dates = dividends.stream().map(AccountTransaction::getDateTime).map(d -> Date.from(d.atZone(ZoneId.systemDefault()).toInstant())).collect(Collectors.toList()).toArray(new Date[0]);
IAxis yAxis1st = chart.getAxisSet().getYAxis(0);
double yAxis1stAxisPrice = yAxis1st.getRange().lower;
double[] values = new double[dates.length];
Arrays.fill(values, yAxis1stAxisPrice);
ILineSeries border = (ILineSeries) chart.getSeriesSet().createSeries(SeriesType.LINE, // $NON-NLS-1$
Messages.LabelChartDetailDividends + "2");
border.setYAxisId(0);
border.setSymbolType(PlotSymbolType.SQUARE);
border.setSymbolSize(6);
border.setSymbolColor(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
configureSeriesPainter(border, dates, values, null, 0, LineStyle.NONE, false, false);
ILineSeries background = (ILineSeries) chart.getSeriesSet().createSeries(SeriesType.LINE, // $NON-NLS-1$
Messages.LabelChartDetailDividends + "1");
background.setYAxisId(0);
background.setSymbolType(PlotSymbolType.SQUARE);
background.setSymbolSize(5);
background.setSymbolColor(Display.getDefault().getSystemColor(SWT.COLOR_WHITE));
configureSeriesPainter(background, dates, values, null, 0, LineStyle.NONE, false, false);
ILineSeries inner = (ILineSeries) chart.getSeriesSet().createSeries(SeriesType.LINE, Messages.LabelChartDetailDividends);
inner.setYAxisId(0);
inner.setSymbolType(PlotSymbolType.SQUARE);
inner.setSymbolSize(3);
inner.setSymbolColor(colorEventDividend);
configureSeriesPainter(inner, dates, values, null, 0, LineStyle.NONE, false, true);
if (chartConfig.contains(ChartDetails.SHOW_DATA_LABELS)) {
customPaintListeners.add(event -> {
IAxis xAxis = chart.getAxisSet().getXAxis(0);
IAxis yAxis = chart.getAxisSet().getYAxis(0);
for (int index = 0; index < dates.length; index++) {
int x = xAxis.getPixelCoordinate(dates[index].getTime());
int y = yAxis.getPixelCoordinate(values[index]);
String label = getDividendLabel(dividends.get(index));
Point textExtent = event.gc.textExtent(label);
event.gc.setForeground(Colors.BLACK);
event.gc.drawText(label, x - (textExtent.x / 2), y - 22, true);
}
});
}
}
}
use of name.abuchen.portfolio.money.Values in project portfolio by buchen.
the class SecuritiesChart method addMovingAveragePurchasePrice.
private void addMovingAveragePurchasePrice() {
// no purchase price
if (security.getCurrencyCode() == null)
return;
// create a list of dates that are relevant for floating avg purchase price
// changes (i.e. all purchase and sell events)
Client filteredClient = new ClientSecurityFilter(security).filter(client);
CurrencyConverter securityCurrency = converter.with(security.getCurrencyCode());
LocalDate today = LocalDate.now();
List<LocalDate> candidates = //
client.getPortfolios().stream().flatMap(//
p -> p.getTransactions().stream()).filter(t -> t.getSecurity().equals(security)).filter(t -> !(t.getType() == PortfolioTransaction.Type.TRANSFER_IN || t.getType() == PortfolioTransaction.Type.TRANSFER_OUT)).filter(t -> t.getDateTime().toLocalDate().isBefore(today)).map(t -> (chartPeriod == null || t.getDateTime().toLocalDate().isAfter(chartPeriod)) ? t.getDateTime().toLocalDate() : chartPeriod).distinct().sorted().collect(Collectors.toList());
// calculate floating avg purchase price for each event - separate lineSeries
// per holding period
List<Double> values = new ArrayList<>();
List<LocalDate> dates = new ArrayList<>();
int seriesCounter = 0;
for (LocalDate eventDate : candidates) {
Optional<Double> purchasePrice = getMovingAveragePurchasePrice(filteredClient, securityCurrency, eventDate);
if (purchasePrice.isPresent()) {
dates.add(eventDate);
values.add(purchasePrice.get());
} else {
if (!dates.isEmpty()) {
// add previous value if the data series ends here (no more
// future events)
dates.add(eventDate);
values.add(values.get(values.size() - 1));
createMovingAveragePurchaseLineSeries(values, dates, seriesCounter++);
values.clear();
dates.clear();
} else if (dates.isEmpty()) {
// if no holding period exists, then do not add the event at
// all
}
}
}
// add today if needed
getMovingAveragePurchasePrice(filteredClient, securityCurrency, today).ifPresent(price -> {
dates.add(today);
values.add(price);
});
if (!dates.isEmpty())
createMovingAveragePurchaseLineSeries(values, dates, seriesCounter);
}
use of name.abuchen.portfolio.money.Values in project portfolio by buchen.
the class SecuritiesChart method addFIFOPurchasePrice.
private void addFIFOPurchasePrice() {
// no purchase price
if (security.getCurrencyCode() == null)
return;
// create a list of dates that are relevant for FIFO purchase price
// changes (i.e. all purchase and sell events)
Client filteredClient = new ClientSecurityFilter(security).filter(client);
CurrencyConverter securityCurrency = converter.with(security.getCurrencyCode());
LocalDate today = LocalDate.now();
List<LocalDate> candidates = //
client.getPortfolios().stream().flatMap(//
p -> p.getTransactions().stream()).filter(t -> t.getSecurity().equals(security)).filter(t -> !(t.getType() == PortfolioTransaction.Type.TRANSFER_IN || t.getType() == PortfolioTransaction.Type.TRANSFER_OUT)).filter(t -> t.getDateTime().toLocalDate().isBefore(today)).map(t -> (chartPeriod == null || t.getDateTime().toLocalDate().isAfter(chartPeriod)) ? t.getDateTime().toLocalDate() : chartPeriod).distinct().sorted().collect(Collectors.toList());
// calculate FIFO purchase price for each event - separate lineSeries
// per holding period
List<Double> values = new ArrayList<>();
List<LocalDate> dates = new ArrayList<>();
int seriesCounter = 0;
for (LocalDate eventDate : candidates) {
Optional<Double> purchasePrice = getPurchasePrice(filteredClient, securityCurrency, eventDate);
if (purchasePrice.isPresent()) {
dates.add(eventDate);
values.add(purchasePrice.get());
} else {
if (!dates.isEmpty()) {
// add previous value if the data series ends here (no more
// future events)
dates.add(eventDate);
values.add(values.get(values.size() - 1));
createFIFOPurchaseLineSeries(values, dates, seriesCounter++);
values.clear();
dates.clear();
} else if (dates.isEmpty()) {
// if no holding period exists, then do not add the event at
// all
}
}
}
// add today if needed
getPurchasePrice(filteredClient, securityCurrency, today).ifPresent(price -> {
dates.add(today);
values.add(price);
});
if (!dates.isEmpty())
createFIFOPurchaseLineSeries(values, dates, seriesCounter);
}
Aggregations