@@ -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 }
0 commit comments