Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/main/java/io/autoinvestor/domain/Asset.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ public class Asset extends AggregateRoot {
private final Date createdAt;
private final Date updatedAt;

private Asset(AssetId id, Mic mic, Ticker ticker, CompanyName name, Date createdAt, Date updatedAt) {
this.id = id;
this.mic = mic;
this.ticker = ticker;
this.name = name;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}

private Asset(Mic mic, Ticker ticker, CompanyName name, Date createdAt, Date updatedAt) {
this.id = AssetId.generate();
this.mic = mic;
Expand All @@ -25,8 +34,8 @@ public static Asset create(String mic, String ticker, String name) {
return new Asset(Mic.from(mic), Ticker.from(ticker), CompanyName.from(name), new Date(), new Date());
}

public static Asset create(String mic, String ticker, String name, Date createdAt, Date updatedAt) {
return new Asset(Mic.from(mic), Ticker.from(ticker), CompanyName.from(name), createdAt, updatedAt);
public static Asset from(String assetId, String mic, String ticker, String name, Date createdAt, Date updatedAt) {
return new Asset(AssetId.of(assetId), Mic.from(mic), Ticker.from(ticker), CompanyName.from(name), createdAt, updatedAt);
}

public String mic() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ public class YFinanceAssetPriceFetcher implements AssetPriceFetcher {

static {
System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");

System.setProperty(
"yahoofinance.baseurl.quotesquery1v7",
"https://query1.finance.yahoo.com/v6/finance/quote"
);

System.setProperty(
"yahoofinance.baseurl.histquotes",
"https://query1.finance.yahoo.com/v7/finance/download"
);
}

@Override
Expand All @@ -43,17 +49,22 @@ public float priceOn(Asset asset, Date date) {
to.add(Calendar.DAY_OF_MONTH, DAYS_LOOKBACK_BUFFER);

try {
Stock stock = YahooFinance.get(asset.ticker(), from, to, Interval.DAILY);
Stock stock = YahooFinance.get(asset.ticker());
if (stock == null) {
throw new PriceNotAvailableException("No data returned for " + asset);
}

List<HistoricalQuote> history = stock.getHistory();
List<HistoricalQuote> history = stock.getHistory(from, to, Interval.DAILY);
if (history == null || history.isEmpty()) {
throw new PriceNotAvailableException(
String.format("No historical data for %s between %s and %s", asset, from.getTime(), to.getTime())
);
}

HistoricalQuote bar = history.stream()
.filter(h -> h.getDate() != null)
.sorted((a, b) -> b.getDate().compareTo(a.getDate()))
.filter(h -> !h.getDate().after(target))
.findFirst()
.max((a, b) -> a.getDate().compareTo(b.getDate()))
.orElseThrow(() -> new PriceNotAvailableException(
String.format("No historical bar found for %s on or before %s", asset, date)
));
Expand All @@ -67,9 +78,9 @@ public float priceOn(Asset asset, Date date) {

return close.floatValue();
} catch (IOException ex) {
logger.error("Error fetching price for {} on {}: {}", asset, date, ex.getMessage(), ex);
logger.error("Error fetching price for {} on {}:", asset, date, ex);
throw new PriceFetchFailedException(
String.format("Unable to fetch price for %s from Yahoo Finance", asset)
String.format("Unable to fetch price for %s from Yahoo Finance %s", asset, ex)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ AssetDocument toDocument(Asset domain) {
}

public Asset toDomain(AssetDocument assetDocument) {
return Asset.create(
return Asset.from(
assetDocument.assetId(),
assetDocument.mic(),
assetDocument.ticker(),
assetDocument.name(),
Expand Down