use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.
the class CSVExporter method exportMergedSecurityPrices.
public void exportMergedSecurityPrices(File file, List<Security> securities) throws IOException {
// prepare: (a) find earliest date (b) ignore securities w/o quotes
LocalDate earliestDate = null;
List<Security> export = new ArrayList<Security>(securities.size());
for (Security s : securities) {
List<SecurityPrice> prices = s.getPrices();
if (!prices.isEmpty()) {
export.add(s);
LocalDate quoteDate = prices.get(0).getDate();
if (earliestDate == null)
earliestDate = quoteDate;
else
earliestDate = earliestDate.isAfter(quoteDate) ? quoteDate : earliestDate;
}
}
try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
CSVPrinter printer = new CSVPrinter(writer);
printer.setStrategy(STRATEGY);
// write header
printer.print(Messages.CSVColumn_Date);
for (Security security : export) printer.print(security.getExternalIdentifier());
printer.println();
// stop if no securities exist
if (earliestDate == null)
return;
// write quotes
LocalDate pointer = earliestDate;
LocalDate today = LocalDate.now();
while (pointer.compareTo(today) <= 0) {
// check if any quotes exist for that day at all
int[] indices = new int[export.size()];
int ii = 0;
for (Security security : export) {
SecurityPrice p = new SecurityPrice(pointer, 0);
indices[ii] = Collections.binarySearch(security.getPrices(), p);
ii++;
}
boolean hasValues = false;
for (ii = 0; ii < indices.length && !hasValues; ii++) hasValues = indices[ii] >= 0;
if (hasValues) {
printer.print(pointer.toString());
for (ii = 0; ii < indices.length; ii++) {
if (indices[ii] < 0)
// $NON-NLS-1$
printer.print("");
else
printer.print(Values.Quote.format(export.get(ii).getPrices().get(indices[ii]).getValue()));
}
printer.println();
}
pointer = pointer.plusDays(1);
}
}
}
use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.
the class StockSplitModel method applyChanges.
@Override
public void applyChanges() {
// $NON-NLS-1$
SecurityEvent event = new SecurityEvent(exDate, SecurityEvent.Type.STOCK_SPLIT, newShares + ":" + oldShares);
security.addEvent(event);
if (isChangeTransactions()) {
List<TransactionPair<?>> transactions = security.getTransactions(getClient());
for (TransactionPair<?> pair : transactions) {
Transaction t = pair.getTransaction();
if (t.getDateTime().toLocalDate().isBefore(exDate))
t.setShares(t.getShares() * newShares / oldShares);
}
}
if (isChangeHistoricalQuotes()) {
List<SecurityPrice> quotes = security.getPrices();
for (SecurityPrice p : quotes) {
if (p.getDate().isBefore(exDate))
p.setValue(p.getValue() * oldShares / newShares);
}
}
}
Aggregations