Hilres.Yahoo.ApiClient 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Hilres.Yahoo.ApiClient --version 1.0.1                
NuGet\Install-Package Hilres.Yahoo.ApiClient -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Hilres.Yahoo.ApiClient" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Hilres.Yahoo.ApiClient --version 1.0.1                
#r "nuget: Hilres.Yahoo.ApiClient, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Hilres.Yahoo.ApiClient as a Cake Addin
#addin nuget:?package=Hilres.Yahoo.ApiClient&version=1.0.1

// Install Hilres.Yahoo.ApiClient as a Cake Tool
#tool nuget:?package=Hilres.Yahoo.ApiClient&version=1.0.1                

Get stock data from the unofficial Yahoo web API

NuGet

This will access the unofficial Yahoo web API. There API may not be available in the future.

Using Hilres.Yahoo.ApiClient

There are two function that will retrieve the historical stock prices.

YahooClient class

This YahooClient class should be created only once when the application is started. The logger is optional.

public YahooClient(ILogger? logger = null)

Example using logging in a console appliction

using Hilres.Yahoo.ApiClient;

private static class Program
{
    private static async Task Main()
    {
        using var loggerFactory = LoggerFactory.Create(builder =>
        {
            builder
                .AddFilter("Microsoft", LogLevel.Warning)
                .AddFilter("System", LogLevel.Warning)
                .AddConsole();
        });
        ILogger logger = loggerFactory.CreateLogger<Program>();
        logger.LogInformation("Start");

        var client = new YahooClient(loggerFactory.CreateLogger<YahooClient>());

        /* more code */

        logger.LogInformation("End");
    }
}

GetPricesAsync function

This will retrieve a IEnumerable<YahooPrice> price list.

public async Task<YahooPricesResult> GetPricesAsync(
    string symbol,
    DateOnly? firstDate = null,
    DateOnly? lastDate = null,
    YahooInterval interval = YahooInterval.Daily,
    CancellationToken cancellationToken = default)

Example:

var client = new YahooClient();
var result = await client.GetPricesAsync("msft", new(2022, 1, 3), new(2022, 1, 8));

if (result.IsSuccessful)
{
    foreach (var item in result.Prices)
    {
        Console.WriteLine(item);
    }
}

GetPricesParserAsync function

This will retrive a IAsyncEnumerable<YahooPriceParser> price list. YahooPriceParser is used to convert the data and is not an object to use. The IAsyncEnumerable can only be used once and then the connection is closed.

public async Task<YahooPricesParserResult> GetPricesParserAsync(
    string symbol,
    DateOnly? firstDate = null,
    DateOnly? lastDate = null,
    YahooInterval interval = YahooInterval.Daily,
    CancellationToken cancellationToken = default)

Example:

var parser = await client.GetPricesParserAsync("msft", new(2022, 1, 3), new(2022, 1, 8));
if (parser.IsSuccessful)
{
    var prices = await parser.Prices
            .Select(p => new MyPrice(p.Date, p.AdjClose, p.Volume))
            .ToDictionaryAsync(p => p.Date)
            .ConfigureAwait(false);

    Console.WriteLine(prices[new(2022, 1, 4)]);
    Console.WriteLine(prices[new(2022, 1, 6)]);
}

public record MyPrice(DateOnly Date, double? AdjClose, long? Volume);

The cool thing about GetPricesParserAsync is that it minimizes creating extra objects. In this case, the values are going directly into the dictionary. GetPricesAsync is a warper that calls GetPricesParserAsync

Notes

Based on https://github.com/danh955/z016

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 372 11/7/2022
1.0.1 440 3/17/2022