Package yahoofinance

Class YahooFinance


  • public class YahooFinance
    extends java.lang.Object
    YahooFinance 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
    • Constructor Summary

      Constructors 
      Constructor Description
      YahooFinance()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Stock get​(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 the get(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 specified Calendar date 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 specified Calendar date 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 specified Calendar date 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 specified Calendar date 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 Stock get​(java.lang.String symbol, boolean includeHistorical)
      Same as the get(String) method, but with the option to include historical stock quote data.
      static Stock get​(java.lang.String symbol, java.util.Calendar from)
      Sends a request with the historical quotes included starting from the specified Calendar date at the default interval (monthly).
      static Stock get​(java.lang.String symbol, java.util.Calendar from, java.util.Calendar to)
      Sends a request with the historical quotes included starting from the specified Calendar date until the specified Calendar date (to) at the default interval (monthly).
      static Stock get​(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 specified Calendar date until the specified Calendar date (to) at the specified interval.
      static Stock get​(java.lang.String symbol, java.util.Calendar from, Interval interval)
      Sends a request with the historical quotes included starting from the specified Calendar date at the specified interval.
      static Stock get​(java.lang.String symbol, Interval interval)
      Sends a request with the historical quotes included at the specified interval (DAILY, WEEKLY, MONTHLY).
      static FxQuote getFx​(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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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
      • CONNECTION_TIMEOUT

        public static final int CONNECTION_TIMEOUT
    • Constructor Detail

      • YahooFinance

        public YahooFinance()
    • 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 a Stock object with its StockQuote, StockStats and StockDividend member 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 Stock object 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 the get(String) method, but with the option to include historical stock quote data. Including historical data will cause the Stock object's member field HistoricalQuote to 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 information
        includeHistorical - indicates if the historical quotes should be included.
        Returns:
        a Stock object 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 information
        interval - the interval of the included historical data
        Returns:
        a Stock object 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 specified Calendar date 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 information
        from - start date of the historical data
        Returns:
        a Stock object 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 specified Calendar date 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 information
        from - start date of the historical data
        interval - the interval of the included historical data
        Returns:
        a Stock object 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 specified Calendar date 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 information
        from - start date of the historical data
        to - end date of the historical data
        Returns:
        a Stock object 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 specified Calendar date 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 information
        from - start date of the historical data
        to - end date of the historical data
        interval - the interval of the included historical data
        Returns:
        a Stock object 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 a Map object that links the symbols to their respective Stock objects. The Stock objects have their StockQuote, StockStats and StockDividend member 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 the get(String[]) method, but with the option to include historical stock quote data. Including historical data will cause the Stock objects their member field HistoricalQuote to 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 information
        includeHistorical - 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 information
        interval - 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 specified Calendar date 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 information
        from - 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 specified Calendar date 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 information
        from - start date of the historical data
        interval - 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 specified Calendar date 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 information
        from - start date of the historical data
        to - 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 specified Calendar date 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 information
        from - start date of the historical data
        to - end date of the historical data
        interval - 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 ENUM FxSymbols Some 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. See getFx(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)