use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.
the class SecurityBuilder method addPrice.
public SecurityBuilder addPrice(String date, long price) {
SecurityPrice p = new SecurityPrice(LocalDate.parse(date), price);
security.addPrice(p);
return this;
}
use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.
the class SecurityBuilder method generatePrices.
public SecurityBuilder generatePrices(long startPrice, LocalDate start, LocalDate end) {
security.addPrice(new SecurityPrice(start, startPrice));
Random random = new Random();
LocalDate date = start;
long price = startPrice;
while (date.compareTo(end) < 0) {
date = date.plusDays(1);
if (date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY)
continue;
price = (long) ((double) price * ((random.nextDouble() * 0.2 - 0.1d) + 1));
security.addPrice(new SecurityPrice(date, price));
}
return this;
}
use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.
the class YahooFinanceQuoteFeed method updateHistoricalQuotes.
@Override
public final boolean updateHistoricalQuotes(Security security, List<Exception> errors) {
LocalDate start = caculateStart(security);
List<SecurityPrice> quotes = internalGetQuotes(SecurityPrice.class, security, start, errors);
boolean isUpdated = false;
if (quotes != null) {
for (SecurityPrice p : quotes) {
boolean isAdded = security.addPrice(p);
isUpdated = isUpdated || isAdded;
}
}
return isUpdated;
}
use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.
the class QuoteFromTransactionExtractor method extractQuotes.
/**
* Extracts the quotes for the given {@link Security}.
*
* @param security
* {@link Security}
* @return true if quotes were found, else false
*/
public boolean extractQuotes(Security security) {
boolean bChanges = false;
SecurityPrice pLatest = null;
// walk through all all transactions for securiy
for (TransactionPair<?> p : security.getTransactions(client)) {
Transaction t = p.getTransaction();
// check the type of the transaction
if (t instanceof PortfolioTransaction) {
PortfolioTransaction pt = (PortfolioTransaction) t;
// get date and quote and build a price from it
Quote q = pt.getGrossPricePerShare();
LocalDate d = pt.getDateTime().toLocalDate();
SecurityPrice price = new SecurityPrice(d, q.getAmount());
bChanges |= security.addPrice(price);
// remember the lates price
if ((pLatest == null) || d.isAfter(pLatest.getDate())) {
pLatest = price;
}
}
}
// set the latest price (if at leas one price was found)
if (pLatest != null) {
LatestSecurityPrice lsp = new LatestSecurityPrice(pLatest.getDate(), pLatest.getValue());
bChanges |= security.setLatest(lsp);
}
return bChanges;
}
use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.
the class ClientPerformanceSnapshotTest method testCapitalGains.
@Test
public void testCapitalGains() {
Client client = new Client();
Security security = new Security();
security.addPrice(new SecurityPrice(LocalDate.of(2010, Month.JANUARY, 1), Values.Quote.factorize(100)));
security.addPrice(new SecurityPrice(LocalDate.of(2011, Month.JUNE, 1), Values.Quote.factorize(110)));
client.addSecurity(security);
Portfolio portfolio = new Portfolio();
portfolio.setReferenceAccount(new Account());
portfolio.addTransaction(new PortfolioTransaction(LocalDateTime.of(2010, Month.JANUARY, 1, 0, 0), CurrencyUnit.EUR, 1_00, security, Values.Share.factorize(10), PortfolioTransaction.Type.BUY, 0, 0));
client.addPortfolio(portfolio);
CurrencyConverter converter = new TestCurrencyConverter();
ClientPerformanceSnapshot snapshot = new ClientPerformanceSnapshot(client, converter, startDate, endDate);
assertThat(snapshot.getValue(CategoryType.INITIAL_VALUE), is(Money.of(CurrencyUnit.EUR, 1000_00)));
assertThat(snapshot.getValue(CategoryType.EARNINGS), is(Money.of(CurrencyUnit.EUR, 0)));
assertThat(snapshot.getValue(CategoryType.CAPITAL_GAINS), is(Money.of(CurrencyUnit.EUR, 100_00)));
assertThat(snapshot.getValue(CategoryType.FINAL_VALUE), is(Money.of(CurrencyUnit.EUR, 1100_00)));
assertThat(snapshot.getAbsoluteDelta(), is(snapshot.getValue(CategoryType.FINAL_VALUE).subtract(snapshot.getValue(CategoryType.TRANSFERS)).subtract(snapshot.getValue(CategoryType.INITIAL_VALUE))));
}
Aggregations