Skip to content

Commit a46e236

Browse files
authored
fix: update yfinance (#15)
1 parent c3b58d2 commit a46e236

3 files changed

Lines changed: 30 additions & 9 deletions

File tree

src/main/java/io/autoinvestor/domain/Asset.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ public class Asset extends AggregateRoot {
1010
private final Date createdAt;
1111
private final Date updatedAt;
1212

13+
private Asset(AssetId id, Mic mic, Ticker ticker, CompanyName name, Date createdAt, Date updatedAt) {
14+
this.id = id;
15+
this.mic = mic;
16+
this.ticker = ticker;
17+
this.name = name;
18+
this.createdAt = createdAt;
19+
this.updatedAt = updatedAt;
20+
}
21+
1322
private Asset(Mic mic, Ticker ticker, CompanyName name, Date createdAt, Date updatedAt) {
1423
this.id = AssetId.generate();
1524
this.mic = mic;
@@ -25,8 +34,8 @@ public static Asset create(String mic, String ticker, String name) {
2534
return new Asset(Mic.from(mic), Ticker.from(ticker), CompanyName.from(name), new Date(), new Date());
2635
}
2736

28-
public static Asset create(String mic, String ticker, String name, Date createdAt, Date updatedAt) {
29-
return new Asset(Mic.from(mic), Ticker.from(ticker), CompanyName.from(name), createdAt, updatedAt);
37+
public static Asset from(String assetId, String mic, String ticker, String name, Date createdAt, Date updatedAt) {
38+
return new Asset(AssetId.of(assetId), Mic.from(mic), Ticker.from(ticker), CompanyName.from(name), createdAt, updatedAt);
3039
}
3140

3241
public String mic() {

src/main/java/io/autoinvestor/infrastructure/fetchers/YFinanceAssetPriceFetcher.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@ public class YFinanceAssetPriceFetcher implements AssetPriceFetcher {
2525

2626
static {
2727
System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
28+
2829
System.setProperty(
2930
"yahoofinance.baseurl.quotesquery1v7",
3031
"https://query1.finance.yahoo.com/v6/finance/quote"
3132
);
33+
34+
System.setProperty(
35+
"yahoofinance.baseurl.histquotes",
36+
"https://query1.finance.yahoo.com/v7/finance/download"
37+
);
3238
}
3339

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

4551
try {
46-
Stock stock = YahooFinance.get(asset.ticker(), from, to, Interval.DAILY);
52+
Stock stock = YahooFinance.get(asset.ticker());
4753
if (stock == null) {
4854
throw new PriceNotAvailableException("No data returned for " + asset);
4955
}
5056

51-
List<HistoricalQuote> history = stock.getHistory();
57+
List<HistoricalQuote> history = stock.getHistory(from, to, Interval.DAILY);
58+
if (history == null || history.isEmpty()) {
59+
throw new PriceNotAvailableException(
60+
String.format("No historical data for %s between %s and %s", asset, from.getTime(), to.getTime())
61+
);
62+
}
63+
5264
HistoricalQuote bar = history.stream()
5365
.filter(h -> h.getDate() != null)
54-
.sorted((a, b) -> b.getDate().compareTo(a.getDate()))
5566
.filter(h -> !h.getDate().after(target))
56-
.findFirst()
67+
.max((a, b) -> a.getDate().compareTo(b.getDate()))
5768
.orElseThrow(() -> new PriceNotAvailableException(
5869
String.format("No historical bar found for %s on or before %s", asset, date)
5970
));
@@ -67,9 +78,9 @@ public float priceOn(Asset asset, Date date) {
6778

6879
return close.floatValue();
6980
} catch (IOException ex) {
70-
logger.error("Error fetching price for {} on {}: {}", asset, date, ex.getMessage(), ex);
81+
logger.error("Error fetching price for {} on {}:", asset, date, ex);
7182
throw new PriceFetchFailedException(
72-
String.format("Unable to fetch price for %s from Yahoo Finance", asset)
83+
String.format("Unable to fetch price for %s from Yahoo Finance %s", asset, ex)
7384
);
7485
}
7586
}

src/main/java/io/autoinvestor/infrastructure/repositories/AssetMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ AssetDocument toDocument(Asset domain) {
1919
}
2020

2121
public Asset toDomain(AssetDocument assetDocument) {
22-
return Asset.create(
22+
return Asset.from(
23+
assetDocument.assetId(),
2324
assetDocument.mic(),
2425
assetDocument.ticker(),
2526
assetDocument.name(),

0 commit comments

Comments
 (0)