diff --git a/src/main/java/io/autoinvestor/domain/Asset.java b/src/main/java/io/autoinvestor/domain/Asset.java index ced0a63..c676f21 100644 --- a/src/main/java/io/autoinvestor/domain/Asset.java +++ b/src/main/java/io/autoinvestor/domain/Asset.java @@ -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; @@ -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() { diff --git a/src/main/java/io/autoinvestor/infrastructure/fetchers/YFinanceAssetPriceFetcher.java b/src/main/java/io/autoinvestor/infrastructure/fetchers/YFinanceAssetPriceFetcher.java index ec0235b..10e5f27 100644 --- a/src/main/java/io/autoinvestor/infrastructure/fetchers/YFinanceAssetPriceFetcher.java +++ b/src/main/java/io/autoinvestor/infrastructure/fetchers/YFinanceAssetPriceFetcher.java @@ -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 @@ -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 history = stock.getHistory(); + List 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) )); @@ -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) ); } } diff --git a/src/main/java/io/autoinvestor/infrastructure/repositories/AssetMapper.java b/src/main/java/io/autoinvestor/infrastructure/repositories/AssetMapper.java index 253c88e..67f13f0 100644 --- a/src/main/java/io/autoinvestor/infrastructure/repositories/AssetMapper.java +++ b/src/main/java/io/autoinvestor/infrastructure/repositories/AssetMapper.java @@ -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(),