YahooFinanceApi

repository·master·Indexed 41 days ago

https://github.com/karlwancl/yahoofinanceapi

A .NET Standard 2.0 wrapper for the Yahoo! Finance API. It provides functionality to retrieve stock quotes via QueryAsync, historical price data, dividend history, and stock split information.

Tokens
1.6K
Snippets
7
Records
7
Agent score
2%

What's inside YahooFinanceApi

  1. Install YahooFinanceApi via NuGet

    master

    Install the YahooFinanceApi package using the NuGet Package Manager Console.

    Note for .NET Framework users: If you encounter a System.Runtime.Serialization.Primitives missing exception, you must install that package manually, as NuGet may not automatically resolve this reference for traditional .NET Framework projects.

    PM> Install-Package YahooFinanceApi
  2. Configure Yahoo.IgnoreEmptyRows

    master

    Yahoo sometimes returns broken or empty rows in historical data calls. You can control whether these rows are ignored by setting the Yahoo.IgnoreEmptyRows property.

    // Set to true to ignore invalid/empty rows returned by Yahoo
    Yahoo.IgnoreEmptyRows = true;
  3. Get stock quotes using QueryAsync

    master

    Because Yahoo terminated their CSV quote service, the GetAsync method is obsolete. Use QueryAsync instead. You can query multiple symbols simultaneously and specify which Fields you want to retrieve using a fluent interface.

    To access values, you can use the indexer with a Field or use the typed property directly on the returned object.

    using YahooFinanceApi;
    
    // Query multiple symbols with specific fields
    var securities = await Yahoo.Symbols("AAPL", "GOOG")
        .Fields(Field.Symbol, Field.RegularMarketPrice, Field.FiftyTwoWeekHigh)
        .QueryAsync();
    
    var aapl = securities["AAPL"];
    
    // Access data via indexer or typed property
    var price = aapl[Field.RegularMarketPrice]; 
    // OR
    var priceTyped = aapl.RegularMarketPrice;
  4. Get historical stock data

    master

    Retrieve historical price data (Open, High, Low, Close, Volume, etc.) for a stock. The startTime and endTime parameters default to the EST timezone. You can specify the granularity using the Period enum (e.g., Period.Daily).

    var history = await Yahoo.GetHistoricalAsync("AAPL", new DateTime(2016, 1, 1), new DateTime(2016, 7, 1), Period.Daily);
    
    foreach (var candle in history)
    {
        Console.WriteLine($"DateTime: {candle.DateTime}, Open: {candle.Open}, High: {candle.High}, Low: {candle.Low}, Close: {candle.Close}, Volume: {candle.Volume}, AdjustedClose: {candle.AdjustedClose}");
    }
  5. Get dividend history

    master

    Retrieve the dividend history for a specific stock symbol within a given date range.

    var dividends = await Yahoo.GetDividendsAsync("AAPL", new DateTime(2016, 1, 1), new DateTime(2016, 7, 1));
    foreach (var candle in dividends)
    {
        Console.WriteLine($"DateTime: {candle.DateTime}, Dividend: {candle.Dividend}");
    }
  6. Get stock split history

    master

    Retrieve the history of stock splits for a specific symbol within a given date range.

    var splits = await Yahoo.GetSplitsAsync("AAPL", new DateTime(2014, 6, 8), new DateTime(2014, 6, 10));
    foreach (var s in splits)
    {
        Console.WriteLine($"DateTime: {s.DateTime}, AfterSplit: {s.AfterSplit}, BeforeSplit: {s.BeforeSplit}");
    }
  7. Reference: Supported fields for stock quotes

    master

    When using the .Fields() method with QueryAsync, you can specify any of the following supported fields:

    Language, QuoteType, QuoteSourceName, Currency, MarketState, RegularMarketPrice, RegularMarketTime, RegularMarketChange, RegularMarketOpen, RegularMarketDayHigh, RegularMarketDayLow, RegularMarketVolume, ShortName, FiftyTwoWeekHighChange, FiftyTwoWeekHighChangePercent, FiftyTwoWeekLow, FiftyTwoWeekHigh, DividendDate, EarningsTimestamp, EarningsTimestampStart, EarningsTimestampEnd, TrailingAnnualDividendRate, TrailingPE, TrailingAnnualDividendYield, EpsTrailingTwelveMonths, EpsForward, SharesOutstanding, BookValue, RegularMarketChangePercent, RegularMarketPreviousClose, Bid, Ask, BidSize, AskSize, MessageBoardId, FullExchangeName, LongName, FinancialCurrency, AverageDailyVolume3Month, AverageDailyVolume10Day, FiftyTwoWeekLowChange, FiftyTwoWeekLowChangePercent, TwoHundredDayAverageChangePercent, MarketCap, ForwardPE, PriceToBook, SourceInterval, ExchangeTimezoneName, ExchangeTimezoneShortName, Market, Exchange, ExchangeDataDelayedBy, PriceHint, FiftyDayAverage, FiftyDayAverageChange, FiftyDayAverageChangePercent, TwoHundredDayAverage, TwoHundredDayAverageChange, Tradeable, GmtOffSetMilliseconds, FiftyDayAverage, Symbol