Retrieve historical stock quotes
developHistorical data can be retrieved in several ways:
- During initial fetch: Pass
trueas the second argument toYahooFinance.get(String symbol, boolean includeHistory). - Using date ranges and intervals: Use
YahooFinance.get(String symbol, Calendar from, Calendar to, Interval interval). - Lazy loading: If you call
getHistory()on aStockobject that doesn't have history loaded, it will automatically trigger a new request to Yahoo Finance. - Forcing a refresh: To force a new request for historical data, call
getHistory(Calendar from, Calendar to, Interval interval)with specific parameters.
Available Interval options include DAILY and WEEKLY.
// Option 1: Include history on fetch
Stock tesla = YahooFinance.get("TSLA", true);
System.out.println(tesla.getHistory());
// Option 2: Specific range and interval
Calendar from = Calendar.getInstance();
Calendar to = Calendar.getInstance();
from.add(Calendar.YEAR, -5);
Stock google = YahooFinance.get("GOOG", from, to, Interval.WEEKLY);
// Option 3: Lazy loading / Forcing refresh
Stock googleLazy = YahooFinance.get("GOOG");
List<HistoricalQuote> googleHistQuotes = googleLazy.getHistory(from, to, Interval.DAILY);