yahoofinance-api

repository·develop·Indexed 20 days ago

https://github.com/sstrickx/yahoofinance-api

A Java library for interacting with the Yahoo Finance API. It provides access to stock quotes, statistics, dividends, FX rates, and historical market data, supporting single and batch requests for stock symbols.

Tokens
1.4K
Snippets
6
Records
6
Agent score
19%

What's inside yahoofinance-api

  1. Retrieve historical stock quotes

    develop

    Historical data can be retrieved in several ways:

    1. During initial fetch: Pass true as the second argument to YahooFinance.get(String symbol, boolean includeHistory).
    2. Using date ranges and intervals: Use YahooFinance.get(String symbol, Calendar from, Calendar to, Interval interval).
    3. Lazy loading: If you call getHistory() on a Stock object that doesn't have history loaded, it will automatically trigger a new request to Yahoo Finance.
    4. 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);
  2. Refresh stock data automatically

    develop

    By default, getQuote() or getStats() might return cached data. To force a refresh of the statistics and dividend data in a single request to Yahoo Finance, pass true to the getter method (e.g., getQuote(true)).

    Warning: Avoid calling getQuote(true), getStats(true), or getDividend(true) too frequently in a short timespan to avoid unnecessary latency and potential rate limiting. For standard usage, use the versions without arguments or with false.

    Stock stock = YahooFinance.get("INTC");
    double price = stock.getQuote(true).getPrice();
  3. Add YahooFinanceAPI as a dependency

    develop

    You can include the YahooFinanceAPI library in your Java project using Maven, Gradle, or Ivy. Replace x.y.z with the desired version.

    ### Maven
    ```xml
    <dependency>
        <groupId>com.yahoofinance-api</groupId>
        <artifactId>YahooFinanceAPI</artifactId>
        <version>x.y.z</version>
    </dependency>

    Gradle

    dependencies {
        compile group: 'com.yahoofinance-api', name: 'YahooFinanceAPI', version: 'x.y.z'
    }

    Ivy

    <dependency org="com.yahoofinance-api" name="YahooFinanceAPI" rev="x.y.z" />
  4. Get FX quotes

    develop

    You can retrieve foreign exchange quotes using YahooFinance.getFx(). You can pass an FxSymbols constant or a raw string symbol (e.g., "USDGBP=X").

    FxQuote usdeur = YahooFinance.getFx(FxSymbols.USDEUR);
    FxQuote usdgbp = YahooFinance.getFx("USDGBP=X");
    System.out.println(usdeur);
    System.out.println(usdgbp);
  5. Fetch multiple stocks in a single request

    develop

    To optimize network usage, you can pass an array of symbols to YahooFinance.get(String[] symbols) to fetch multiple stocks in a single request. This returns a Map<String, Stock> where the key is the symbol.

    String[] symbols = new String[] {"INTC", "BABA", "TSLA", "AIR.PA", "YHOO"};
    Map<String, Stock> stocks = YahooFinance.get(symbols); // single request
    Stock intel = stocks.get("INTC");
    Stock airbus = stocks.get("AIR.PA");
  6. Fetch data for a single stock

    develop

    Use YahooFinance.get(String symbol) to retrieve a Stock object. Once you have the Stock instance, you can access various data points through its sub-objects:

    • getQuote(): Access real-time price, change, etc.
    • getStats(): Access statistics like PE ratio and PEG.
    • getDividend(): Access dividend information like annual yield.

    Example usage:

    Stock stock = YahooFinance.get("INTC");
    
    BigDecimal price = stock.getQuote().getPrice();
    BigDecimal change = stock.getQuote().getChangeInPercent();
    BigDecimal peg = stock.getStats().getPeg();
    BigDecimal dividend = stock.getDividend().getAnnualYieldPercent();
    Stock stock = YahooFinance.get("INTC");
    
    BigDecimal price = stock.getQuote().getPrice();
    BigDecimal change = stock.getQuote().getChangeInPercent();
    BigDecimal peg = stock.getStats().getPeg();
    BigDecimal dividend = stock.getDividend().getAnnualYieldPercent();
    
    stock.print();