YahooFinanceApi nuget package - "Failure to create client"

Viewed 136

Context

The YahooFinanceApi used to work fine at least as of a few months ago.

Here's a short program which demonstrates the issue:

using YahooFinanceApi;

var data = Yahoo.GetHistoricalAsync("MSFT", new DateTime(2021, 1, 1), DateTime.Now).Result;

Here's the result of running that program:

Unhandled exception. System.AggregateException: One or more errors occurred. (Failure to create client.)
 ---> System.Exception: Failure to create client.
   at YahooFinanceApi.YahooClientFactory.CreateClientAsync(CancellationToken token)
   at YahooFinanceApi.YahooClientFactory.GetClientAndCrumbAsync(Boolean reset, CancellationToken token)
   at YahooFinanceApi.Yahoo.GetResponseStreamAsync(String symbol, Nullable`1 startTime, Nullable`1 endTime, Period period, String events, CancellationToken token)
   at YahooFinanceApi.Yahoo.GetTicksAsync[ITick](String symbol, Nullable`1 startTime, Nullable`1 endTime, Period period, ShowOption showOption, Func`2 instanceFunction, CancellationToken token)
   at YahooFinanceApi.Yahoo.GetHistoricalAsync(String symbol, Nullable`1 startTime, Nullable`1 endTime, Period period, CancellationToken token)
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at System.Threading.Tasks.Task`1.get_Result()
   at Program.<Main>$(String[] args) in C:\Users\dharm\Dropbox\Documents\VisualStudio\YahooFinanceApiSpx\YahooFinanceApiSpx\Program.cs:line 3

Question

Any suggestions for getting the example program to work?

1 Answers

Alternative package

It looks like the YahooQuotesApi nuget package works fine.

Example program

This example retrieves price data and writes it to a CSV file.

using CsvHelper;
using NodaTime;
using System.Globalization;
using YahooQuotesApi;

var quotes = new YahooQuotesBuilder()
    .WithHistoryStartDate(Instant.FromUtc(2002, 12, 1, 0, 0))
    .Build();

var item = await quotes.GetAsync("^GSPC", HistoryFlags.PriceHistory) ?? throw new ArgumentException("Unknown symbol");

using (var writer = new StreamWriter(@"yahoo-api-out.csv"))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
    csv.WriteRecords(item.PriceHistory.Value);
}

Notes

In the following github issue for the project:

https://github.com/dshe/YahooQuotesApi/issues/4

a user had reported an issue with this project. The author then mentioned that the issue should be resolved as of version 5.1.

enter image description here

It seems that perhaps the API service changed in such a way that it caused the various packages to no longer work as expected.

YahooFinanceApi was last updated in 2019. YahooQuotesApi appears to be more regularly updated.

Related