use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.
the class SecuritiesChart method updateChart.
private void updateChart() {
chart.setRedraw(false);
try {
// delete all line series (quotes + possibly moving average)
ISeries[] series = chart.getSeriesSet().getSeries();
for (ISeries s : series) chart.getSeriesSet().deleteSeries(s.getId());
chart.clearMarkerLines();
customPaintListeners.clear();
customTooltipEvents.clear();
if (security == null || security.getPrices().isEmpty()) {
// $NON-NLS-1$
chart.getTitle().setText(security == null ? "..." : security.getName());
chart.redraw();
return;
}
chart.getTitle().setText(security.getName());
boolean showAreaRelativeToFirstQuote = chartConfig.contains(ChartDetails.CLOSING) || chartConfig.contains(ChartDetails.PURCHASEPRICE);
List<SecurityPrice> prices = security.getPricesIncludingLatest();
int index;
LocalDate[] dates;
double[] values;
double[] valuesRelative;
double[] valuesRelativePositive;
double[] valuesRelativeNegative;
double[] valuesZeroLine;
double firstQuote = 0;
if (chartPeriod == null) {
index = 0;
dates = new LocalDate[prices.size()];
values = new double[prices.size()];
valuesRelative = new double[prices.size()];
valuesRelativePositive = new double[prices.size()];
valuesRelativeNegative = new double[prices.size()];
valuesZeroLine = new double[prices.size()];
} else {
index = Collections.binarySearch(prices, new SecurityPrice(chartPeriod, 0), new SecurityPrice.ByDate());
if (index == -1) {
index = 0;
} else {
index = Math.abs(index);
}
if (index >= prices.size()) {
// no data available
chart.redraw();
return;
}
dates = new LocalDate[prices.size() - index];
values = new double[prices.size() - index];
valuesRelative = new double[prices.size() - index];
valuesRelativePositive = new double[prices.size() - index];
valuesRelativeNegative = new double[prices.size() - index];
valuesZeroLine = new double[prices.size() - index];
}
SecurityPrice p2 = prices.get(index);
if (!chartConfig.contains(ChartDetails.PURCHASEPRICE)) {
firstQuote = (p2.getValue() / Values.Quote.divider());
} else {
Optional<Double> purchasePrice = getLatestPurchasePrice();
if (purchasePrice.isPresent())
firstQuote = purchasePrice.get();
else
showAreaRelativeToFirstQuote = false;
}
addChartMarkerBackground();
for (int ii = 0; index < prices.size(); index++, ii++) {
SecurityPrice p = prices.get(index);
dates[ii] = p.getDate();
values[ii] = p.getValue() / Values.Quote.divider();
if (showAreaRelativeToFirstQuote) {
valuesRelative[ii] = (p.getValue() / Values.Quote.divider()) - firstQuote;
valuesZeroLine[ii] = 0;
if (valuesRelative[ii] >= 0) {
valuesRelativePositive[ii] = valuesRelative[ii];
valuesRelativeNegative[ii] = 0;
} else {
valuesRelativePositive[ii] = 0;
valuesRelativeNegative[ii] = valuesRelative[ii];
}
}
}
Date[] javaDates = TimelineChart.toJavaUtilDate(dates);
if (showAreaRelativeToFirstQuote) {
ILineSeries lineSeries2ndNegative = (ILineSeries) chart.getSeriesSet().createSeries(SeriesType.LINE, // $NON-NLS-1$
Messages.LabelChartDetailClosingIndicator + "Negative");
lineSeries2ndNegative.setSymbolType(PlotSymbolType.NONE);
lineSeries2ndNegative.setYAxisId(1);
configureSeriesPainter(lineSeries2ndNegative, javaDates, valuesRelativeNegative, colorAreaNegative, 1, LineStyle.SOLID, true, false);
ILineSeries lineSeries2ndPositive = (ILineSeries) chart.getSeriesSet().createSeries(SeriesType.LINE, // $NON-NLS-1$
Messages.LabelChartDetailClosingIndicator + "Positive");
lineSeries2ndPositive.setSymbolType(PlotSymbolType.NONE);
lineSeries2ndPositive.setYAxisId(1);
configureSeriesPainter(lineSeries2ndPositive, javaDates, valuesRelativePositive, colorAreaPositive, 1, LineStyle.SOLID, true, false);
}
ILineSeries lineSeries = (ILineSeries) chart.getSeriesSet().createSeries(SeriesType.LINE, Messages.ColumnQuote);
lineSeries.setSymbolType(PlotSymbolType.NONE);
configureSeriesPainter(lineSeries, javaDates, values, colorQuote, 2, LineStyle.SOLID, !showAreaRelativeToFirstQuote, false);
chart.adjustRange();
addChartMarkerForeground();
chart.adjustRange();
IAxis yAxis1st = chart.getAxisSet().getYAxis(0);
IAxis yAxis2nd = chart.getAxisSet().getYAxis(1);
yAxis2nd.setRange(new Range(yAxis1st.getRange().lower - firstQuote, yAxis1st.getRange().upper - firstQuote));
} finally {
chart.setRedraw(true);
chart.redraw();
}
}
use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.
the class SecuritiesTable method addColumnDateOfLatestPrice.
private // NOSONAR
void addColumnDateOfLatestPrice() {
Column column;
// $NON-NLS-1$
column = new Column("9", Messages.ColumnLatestDate, SWT.LEFT, 80);
column.setMenuLabel(Messages.ColumnLatestDate_MenuLabel);
column.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
SecurityPrice latest = ((Security) element).getSecurityPrice(LocalDate.now());
return latest != null ? Values.Date.format(latest.getDate()) : null;
}
@Override
public Color getBackground(Object element) {
Security security = (Security) element;
SecurityPrice latest = security.getSecurityPrice(LocalDate.now());
if (latest == null)
return null;
String feed = security.getLatestFeed() != null ? security.getLatestFeed() : security.getFeed();
if (QuoteFeed.MANUAL.equals(feed))
return null;
LocalDate sevenDaysAgo = LocalDate.now().minusDays(7);
return latest.getDate().isBefore(sevenDaysAgo) ? Colors.WARNING : null;
}
});
column.setSorter(ColumnViewerSorter.create((o1, o2) -> {
SecurityPrice p1 = ((Security) o1).getSecurityPrice(LocalDate.now());
SecurityPrice p2 = ((Security) o2).getSecurityPrice(LocalDate.now());
if (p1 == null)
return p2 == null ? 0 : -1;
if (p2 == null)
return 1;
return p1.getDate().compareTo(p2.getDate());
}));
support.addColumn(column);
}
use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.
the class SecuritiesTable method addColumnLatestPrice.
private // NOSONAR
void addColumnLatestPrice() {
// $NON-NLS-1$
Column column = new Column("4", Messages.ColumnLatest, SWT.RIGHT, 60);
column.setMenuLabel(Messages.ColumnLatest_MenuLabel);
column.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object e) {
Security security = (Security) e;
SecurityPrice latest = security.getSecurityPrice(LocalDate.now());
if (latest == null)
return null;
if (security.getCurrencyCode() == null)
return Values.Quote.format(latest.getValue());
else
return Values.Quote.format(security.getCurrencyCode(), latest.getValue(), getClient().getBaseCurrency());
}
});
column.setSorter(ColumnViewerSorter.create((o1, o2) -> {
SecurityPrice p1 = ((Security) o1).getSecurityPrice(LocalDate.now());
SecurityPrice p2 = ((Security) o2).getSecurityPrice(LocalDate.now());
if (p1 == null)
return p2 == null ? 0 : -1;
if (p2 == null)
return 1;
long v1 = p1.getValue();
long v2 = p2.getValue();
return v1 > v2 ? 1 : v1 == v2 ? 0 : -1;
}));
support.addColumn(column);
}
use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.
the class CSVSecurityPriceExtractor method extract.
private SecurityPrice extract(String[] rawValues, Map<String, Column> field2column) throws ParseException {
LocalDateTime date = getDate(Messages.CSVColumn_Date, rawValues, field2column);
if (date == null)
throw new ParseException(MessageFormat.format(Messages.CSVImportMissingField, Messages.CSVColumn_Date), 0);
Long amount = getQuote(Messages.CSVColumn_Quote, rawValues, field2column);
if (amount == null)
throw new ParseException(MessageFormat.format(Messages.CSVImportMissingField, Messages.CSVColumn_Quote), 0);
return new SecurityPrice(date.toLocalDate(), Math.abs(amount));
}
use of name.abuchen.portfolio.model.SecurityPrice 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++;
}
}
Aggregations