Class YahooFinance
- java.lang.Object
-
- yahoofinance.YahooFinance
-
public class YahooFinance extends java.lang.ObjectYahooFinance can be used to retrieve quotes and some extra information on stocks. There is also the possibility to include historical quotes on the requested stocks.When trying to get information on multiple stocks at once, please use the provided methods that accept a
String[]of symbols to get the best performance. To retrieve the basic quote, statistics and dividend data, a single request can be sent to Yahoo Finance for multiple stocks at once. For the historical data however, a separate request has to be sent to Yahoo Finance for each of the requested stocks. The provided methods will retrieve all of the required information in the least amount of requests possible towards Yahoo Finance.You can change the default timeout of 10s for requests to Yahoo Finance by setting the yahoofinance.connection.timeout system property.
Please be aware that the data received from Yahoo Finance is not always complete for every single stock. Stocks on the American stock exchanges usually have a lot more data available than stocks on other exchanges.
This API can also be used to send requests for retrieving FX rates.
Since the data is provided by Yahoo, please check their Terms of Service at https://info.yahoo.com/legal/us/yahoo/
- Version:
- %I%, %G%
- Author:
- Stijn Strickx
-
-
Field Summary
Fields Modifier and Type Field Description static intCONNECTION_TIMEOUTstatic java.lang.StringHISTQUOTES_BASE_URLstatic java.lang.StringHISTQUOTES_QUERY2V8_BASE_URLstatic java.lang.StringHISTQUOTES2_BASE_URLstatic java.lang.StringHISTQUOTES2_COOKIEstatic java.lang.StringHISTQUOTES2_COOKIE_AGREEstatic java.lang.StringHISTQUOTES2_COOKIE_NAMESPACEstatic java.lang.StringHISTQUOTES2_COOKIE_OATH_DONEURLstatic java.lang.StringHISTQUOTES2_COOKIE_OATH_HOSTstatic java.lang.StringHISTQUOTES2_COOKIE_OATH_ORIGINstatic java.lang.StringHISTQUOTES2_COOKIE_OATH_URLstatic java.lang.StringHISTQUOTES2_CRUMBstatic java.lang.StringHISTQUOTES2_CRUMB_URLstatic java.lang.StringHISTQUOTES2_ENABLEDstatic java.lang.StringHISTQUOTES2_SCRAPE_URLstatic java.lang.StringQUOTES_BASE_URLstatic java.lang.StringQUOTES_CSV_DELIMITERstatic java.lang.StringQUOTES_QUERY1V7_BASE_URLstatic java.lang.StringQUOTES_QUERY1V7_ENABLEDstatic java.lang.StringTIMEZONE
-
Constructor Summary
Constructors Constructor Description YahooFinance()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Stockget(java.lang.String symbol)Sends a basic quotes request to Yahoo Finance.static java.util.Map<java.lang.String,Stock>get(java.lang.String[] symbols)Sends a basic quotes request to Yahoo Finance.static java.util.Map<java.lang.String,Stock>get(java.lang.String[] symbols, boolean includeHistorical)Same as theget(String[])method, but with the option to include historical stock quote data.static java.util.Map<java.lang.String,Stock>get(java.lang.String[] symbols, java.util.Calendar from)Sends a request for multiple stocks with the historical quotes included starting from the specifiedCalendardate until today, at the default interval (monthly).static java.util.Map<java.lang.String,Stock>get(java.lang.String[] symbols, java.util.Calendar from, java.util.Calendar to)Sends a request for multiple stocks with the historical quotes included starting from the specifiedCalendardate until the specified Calendar date (to) at the default interval (monthly).static java.util.Map<java.lang.String,Stock>get(java.lang.String[] symbols, java.util.Calendar from, java.util.Calendar to, Interval interval)Sends a request for multiple stocks with the historical quotes included starting from the specifiedCalendardate until the specified Calendar date (to) at the specified interval.static java.util.Map<java.lang.String,Stock>get(java.lang.String[] symbols, java.util.Calendar from, Interval interval)Sends a request for multiple stocks with the historical quotes included starting from the specifiedCalendardate until today, at the specified interval.static java.util.Map<java.lang.String,Stock>get(java.lang.String[] symbols, Interval interval)Sends a request for multiple stocks with the historical quotes included from the past year, at the specified interval.static Stockget(java.lang.String symbol, boolean includeHistorical)Same as theget(String)method, but with the option to include historical stock quote data.static Stockget(java.lang.String symbol, java.util.Calendar from)Sends a request with the historical quotes included starting from the specifiedCalendardate at the default interval (monthly).static Stockget(java.lang.String symbol, java.util.Calendar from, java.util.Calendar to)Sends a request with the historical quotes included starting from the specifiedCalendardate until the specified Calendar date (to) at the default interval (monthly).static Stockget(java.lang.String symbol, java.util.Calendar from, java.util.Calendar to, Interval interval)Sends a request with the historical quotes included starting from the specifiedCalendardate until the specified Calendar date (to) at the specified interval.static Stockget(java.lang.String symbol, java.util.Calendar from, Interval interval)Sends a request with the historical quotes included starting from the specifiedCalendardate at the specified interval.static Stockget(java.lang.String symbol, Interval interval)Sends a request with the historical quotes included at the specified interval (DAILY, WEEKLY, MONTHLY).static FxQuotegetFx(java.lang.String symbol)Sends a request for a single FX rate.static java.util.Map<java.lang.String,FxQuote>getFx(java.lang.String[] symbols)Sends a single request to Yahoo Finance to retrieve a quote for all the requested FX symbols.
-
-
-
Field Detail
-
QUOTES_BASE_URL
public static final java.lang.String QUOTES_BASE_URL
-
QUOTES_QUERY1V7_BASE_URL
public static final java.lang.String QUOTES_QUERY1V7_BASE_URL
-
QUOTES_QUERY1V7_ENABLED
public static final java.lang.String QUOTES_QUERY1V7_ENABLED
-
HISTQUOTES_BASE_URL
public static final java.lang.String HISTQUOTES_BASE_URL
-
HISTQUOTES2_ENABLED
public static final java.lang.String HISTQUOTES2_ENABLED
-
HISTQUOTES2_BASE_URL
public static final java.lang.String HISTQUOTES2_BASE_URL
-
HISTQUOTES_QUERY2V8_BASE_URL
public static final java.lang.String HISTQUOTES_QUERY2V8_BASE_URL
-
HISTQUOTES2_SCRAPE_URL
public static final java.lang.String HISTQUOTES2_SCRAPE_URL
-
HISTQUOTES2_CRUMB_URL
public static final java.lang.String HISTQUOTES2_CRUMB_URL
-
HISTQUOTES2_CRUMB
public static final java.lang.String HISTQUOTES2_CRUMB
-
HISTQUOTES2_COOKIE
public static final java.lang.String HISTQUOTES2_COOKIE
-
HISTQUOTES2_COOKIE_NAMESPACE
public static final java.lang.String HISTQUOTES2_COOKIE_NAMESPACE
-
HISTQUOTES2_COOKIE_AGREE
public static final java.lang.String HISTQUOTES2_COOKIE_AGREE
-
HISTQUOTES2_COOKIE_OATH_URL
public static final java.lang.String HISTQUOTES2_COOKIE_OATH_URL
-
HISTQUOTES2_COOKIE_OATH_HOST
public static final java.lang.String HISTQUOTES2_COOKIE_OATH_HOST
-
HISTQUOTES2_COOKIE_OATH_ORIGIN
public static final java.lang.String HISTQUOTES2_COOKIE_OATH_ORIGIN
-
HISTQUOTES2_COOKIE_OATH_DONEURL
public static final java.lang.String HISTQUOTES2_COOKIE_OATH_DONEURL
-
QUOTES_CSV_DELIMITER
public static final java.lang.String QUOTES_CSV_DELIMITER
- See Also:
- Constant Field Values
-
TIMEZONE
public static final java.lang.String TIMEZONE
- See Also:
- Constant Field Values
-
CONNECTION_TIMEOUT
public static final int CONNECTION_TIMEOUT
-
-
Method Detail
-
get
public static Stock get(java.lang.String symbol) throws java.io.IOException
Sends a basic quotes request to Yahoo Finance. This will return aStockobject with itsStockQuote,StockStatsandStockDividendmember fields filled in with the available data. Returns null if the data can't be retrieved from Yahoo Finance.- Parameters:
symbol- the symbol of the stock for which you want to retrieve information- Returns:
- a
Stockobject containing the requested information - Throws:
java.io.IOException- when there's a connection problem
-
get
public static Stock get(java.lang.String symbol, boolean includeHistorical) throws java.io.IOException
Same as theget(String)method, but with the option to include historical stock quote data. Including historical data will cause theStockobject's member fieldHistoricalQuoteto be filled in with the default past year term at monthly intervals. Returns null if the data can't be retrieved from Yahoo Finance.- Parameters:
symbol- the symbol of the stock for which you want to retrieve informationincludeHistorical- indicates if the historical quotes should be included.- Returns:
- a
Stockobject containing the requested information - Throws:
java.io.IOException- when there's a connection problem
-
get
public static Stock get(java.lang.String symbol, Interval interval) throws java.io.IOException
Sends a request with the historical quotes included at the specified interval (DAILY, WEEKLY, MONTHLY). Returns null if the data can't be retrieved from Yahoo Finance.- Parameters:
symbol- the symbol of the stock for which you want to retrieve informationinterval- the interval of the included historical data- Returns:
- a
Stockobject containing the requested information - Throws:
java.io.IOException- when there's a connection problem
-
get
public static Stock get(java.lang.String symbol, java.util.Calendar from) throws java.io.IOException
Sends a request with the historical quotes included starting from the specifiedCalendardate at the default interval (monthly). Returns null if the data can't be retrieved from Yahoo Finance.- Parameters:
symbol- the symbol of the stock for which you want to retrieve informationfrom- start date of the historical data- Returns:
- a
Stockobject containing the requested information - Throws:
java.io.IOException- when there's a connection problem
-
get
public static Stock get(java.lang.String symbol, java.util.Calendar from, Interval interval) throws java.io.IOException
Sends a request with the historical quotes included starting from the specifiedCalendardate at the specified interval. Returns null if the data can't be retrieved from Yahoo Finance.- Parameters:
symbol- the symbol of the stock for which you want to retrieve informationfrom- start date of the historical datainterval- the interval of the included historical data- Returns:
- a
Stockobject containing the requested information - Throws:
java.io.IOException- when there's a connection problem
-
get
public static Stock get(java.lang.String symbol, java.util.Calendar from, java.util.Calendar to) throws java.io.IOException
Sends a request with the historical quotes included starting from the specifiedCalendardate until the specified Calendar date (to) at the default interval (monthly). Returns null if the data can't be retrieved from Yahoo Finance.- Parameters:
symbol- the symbol of the stock for which you want to retrieve informationfrom- start date of the historical datato- end date of the historical data- Returns:
- a
Stockobject containing the requested information - Throws:
java.io.IOException- when there's a connection problem
-
get
public static Stock get(java.lang.String symbol, java.util.Calendar from, java.util.Calendar to, Interval interval) throws java.io.IOException
Sends a request with the historical quotes included starting from the specifiedCalendardate until the specified Calendar date (to) at the specified interval. Returns null if the data can't be retrieved from Yahoo Finance.- Parameters:
symbol- the symbol of the stock for which you want to retrieve informationfrom- start date of the historical datato- end date of the historical datainterval- the interval of the included historical data- Returns:
- a
Stockobject containing the requested information - Throws:
java.io.IOException- when there's a connection problem
-
get
public static java.util.Map<java.lang.String,Stock> get(java.lang.String[] symbols) throws java.io.IOException
Sends a basic quotes request to Yahoo Finance. This will return aMapobject that links the symbols to their respectiveStockobjects. The Stock objects have theirStockQuote,StockStatsandStockDividendmember fields filled in with the available data.All the information is retrieved in a single request to Yahoo Finance. The returned Map only includes the Stocks that could successfully be retrieved from Yahoo Finance.
- Parameters:
symbols- the symbols of the stocks for which you want to retrieve information- Returns:
- a Map that links the symbols to their respective Stock objects
- Throws:
java.io.IOException- when there's a connection problem
-
get
public static java.util.Map<java.lang.String,Stock> get(java.lang.String[] symbols, boolean includeHistorical) throws java.io.IOException
Same as theget(String[])method, but with the option to include historical stock quote data. Including historical data will cause theStockobjects their member fieldHistoricalQuoteto be filled in with the default past year term at monthly intervals.The latest quotes will be retrieved in a single request to Yahoo Finance. For the historical quotes (if includeHistorical), a separate request will be sent for each requested stock. The returned Map only includes the Stocks that could successfully be retrieved from Yahoo Finance.
- Parameters:
symbols- the symbols of the stocks for which you want to retrieve informationincludeHistorical- indicates if the historical quotes should be included- Returns:
- a Map that links the symbols to their respective Stock objects
- Throws:
java.io.IOException- when there's a connection problem
-
get
public static java.util.Map<java.lang.String,Stock> get(java.lang.String[] symbols, Interval interval) throws java.io.IOException
Sends a request for multiple stocks with the historical quotes included from the past year, at the specified interval. (DAILY, WEEKLY, MONTHLY)The latest quotes will be retrieved in a single request to Yahoo Finance. For the historical quotes, a separate request will be sent for each requested stock. The returned Map only includes the Stocks that could successfully be retrieved from Yahoo Finance.
- Parameters:
symbols- the symbols of the stocks for which you want to retrieve informationinterval- the interval of the included historical data- Returns:
- a Map that links the symbols to their respective Stock objects.
- Throws:
java.io.IOException- when there's a connection problem
-
get
public static java.util.Map<java.lang.String,Stock> get(java.lang.String[] symbols, java.util.Calendar from) throws java.io.IOException
Sends a request for multiple stocks with the historical quotes included starting from the specifiedCalendardate until today, at the default interval (monthly).The latest quotes will be retrieved in a single request to Yahoo Finance. For the historical quotes, a separate request will be sent for each requested stock. The returned Map only includes the Stocks that could successfully be retrieved from Yahoo Finance.
- Parameters:
symbols- the symbols of the stocks for which you want to retrieve informationfrom- start date of the historical data- Returns:
- a Map that links the symbols to their respective Stock objects.
- Throws:
java.io.IOException- when there's a connection problem
-
get
public static java.util.Map<java.lang.String,Stock> get(java.lang.String[] symbols, java.util.Calendar from, Interval interval) throws java.io.IOException
Sends a request for multiple stocks with the historical quotes included starting from the specifiedCalendardate until today, at the specified interval.The latest quotes will be retrieved in a single request to Yahoo Finance. For the historical quotes, a separate request will be sent for each requested stock. The returned Map only includes the Stocks that could successfully be retrieved from Yahoo Finance.
- Parameters:
symbols- the symbols of the stocks for which you want to retrieve informationfrom- start date of the historical datainterval- the interval of the included historical data- Returns:
- a Map that links the symbols to their respective Stock objects.
- Throws:
java.io.IOException- when there's a connection problem
-
get
public static java.util.Map<java.lang.String,Stock> get(java.lang.String[] symbols, java.util.Calendar from, java.util.Calendar to) throws java.io.IOException
Sends a request for multiple stocks with the historical quotes included starting from the specifiedCalendardate until the specified Calendar date (to) at the default interval (monthly).The latest quotes will be retrieved in a single request to Yahoo Finance. For the historical quotes, a separate request will be sent for each requested stock. The returned Map only includes the Stocks that could successfully be retrieved from Yahoo Finance.
- Parameters:
symbols- the symbols of the stocks for which you want to retrieve informationfrom- start date of the historical datato- end date of the historical data- Returns:
- a Map that links the symbols to their respective Stock objects.
- Throws:
java.io.IOException- when there's a connection problem
-
get
public static java.util.Map<java.lang.String,Stock> get(java.lang.String[] symbols, java.util.Calendar from, java.util.Calendar to, Interval interval) throws java.io.IOException
Sends a request for multiple stocks with the historical quotes included starting from the specifiedCalendardate until the specified Calendar date (to) at the specified interval.The latest quotes will be retrieved in a single request to Yahoo Finance. For the historical quotes, a separate request will be sent for each requested stock. The returned Map only includes the Stocks that could successfully be retrieved from Yahoo Finance.
- Parameters:
symbols- the symbols of the stocks for which you want to retrieve informationfrom- start date of the historical datato- end date of the historical datainterval- the interval of the included historical data- Returns:
- a Map that links the symbols to their respective Stock objects.
- Throws:
java.io.IOException- when there's a connection problem
-
getFx
public static FxQuote getFx(java.lang.String symbol) throws java.io.IOException
Sends a request for a single FX rate. Some common symbols can easily be found in the ENUMFxSymbolsSome examples of accepted symbols:- EURUSD=X
- USDEUR=X
- USDGBP=X
- AUDGBP=X
- CADUSD=X
- Parameters:
symbol- symbol for the FX rate you want to request- Returns:
- a quote for the requested FX rate
- Throws:
java.io.IOException- when there's a connection problem
-
getFx
public static java.util.Map<java.lang.String,FxQuote> getFx(java.lang.String[] symbols) throws java.io.IOException
Sends a single request to Yahoo Finance to retrieve a quote for all the requested FX symbols. SeegetFx(String)for more information on the accepted FX symbols.- Parameters:
symbols- an array of FX symbols- Returns:
- the requested FX symbols mapped to their respective quotes
- Throws:
java.io.IOException- when there's a connection problem or the request is incorrect- See Also:
getFx(java.lang.String)
-
-