Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

Added new library to handle fundamentals api. #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public enum ApiFunction
INCOME_STATEMENT,
BALANCE_SHEET,
CASH_FLOW,
EARNINGS,
LISTING_STATUS,

// Forex (FX)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace AlphaVantage.Net.Common.Earnings
{
public abstract class EarningsBase<TQuartleryEarnings, TAnnualEarnings>
where TQuartleryEarnings : EarningsPeriodBase
where TAnnualEarnings : EarningsPeriodBase
{
public string Symbol { get; set; } = String.Empty;

public ICollection<TAnnualEarnings> AnnualEarnings { get; set; } = new List<TAnnualEarnings>();
public ICollection<TQuartleryEarnings> QuarterlyEarnings { get; set; } = new List<TQuartleryEarnings>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using AlphaVantage.Net.Common.Parsing;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;

namespace AlphaVantage.Net.Common.Earnings
{
public abstract class EarningsParserBase<TEarnings, TQuarterlyEarnings, TAnnualEarnings> : IAlphaVantageJsonDocumentParser<TEarnings>
where TQuarterlyEarnings : EarningsPeriodBase
where TAnnualEarnings : EarningsPeriodBase
where TEarnings : EarningsBase<TQuarterlyEarnings, TAnnualEarnings>

{

public abstract TEarnings CreateEarningsInstance();

public abstract TQuarterlyEarnings CreateQuarterlyEarningsInstance();

public abstract TAnnualEarnings CreateAnnualEarningsInstance();

protected abstract Action<TAnnualEarnings, string>? GetAnnualParsingDelegate(string fieldName);
protected abstract Action<TQuarterlyEarnings, string>? GetQuarterlyParsingDelegate(string fieldName);
public TEarnings ParseApiResponse(JsonDocument jsonDocument)
{
var result = CreateEarningsInstance();

try
{
result.Symbol = jsonDocument.RootElement.GetProperty("symbol").GetString();
result.AnnualEarnings = GetAnnualEarningsData(jsonDocument);
result.QuarterlyEarnings = GetQuarterlyEarningsData(jsonDocument);
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}

return result;
}

private List<TAnnualEarnings> GetAnnualEarningsData(JsonDocument jsonDocument)
{
var result = new List<TAnnualEarnings>();
var annualEarningsJson = jsonDocument.RootElement.GetProperty("annualEarnings");

foreach (var annualEarningsDataPoint in annualEarningsJson.EnumerateArray())
{
var annualDataPoint = CreateAnnualEarningsInstance();
EnrichAnnualEarningsDataPoint(annualDataPoint, annualEarningsDataPoint);
result.Add(annualDataPoint);
}

return result;
}
private void EnrichAnnualEarningsDataPoint(TAnnualEarnings annualEarningDataPoint, JsonElement annualEarningDataPointFields)
{
foreach (var fieldJson in annualEarningDataPointFields.EnumerateObject())
{
GetAnnualParsingDelegate(fieldJson.Name)?.Invoke(annualEarningDataPoint, fieldJson.Value.GetString());
}
}

private List<TQuarterlyEarnings> GetQuarterlyEarningsData(JsonDocument jsonDocument)
{
var result = new List<TQuarterlyEarnings>();
var quarterlyEarningsJson = jsonDocument.RootElement.GetProperty("quarterlyEarnings");

foreach (var quarterlyEarningsDataPoint in quarterlyEarningsJson.EnumerateArray())
{

System.Diagnostics.Debug.WriteLine(quarterlyEarningsDataPoint.ToString());
var quarterlyDataPoint = CreateQuarterlyEarningsInstance();
EnrichQuarterlyEarningsDataPoint(quarterlyDataPoint, quarterlyEarningsDataPoint);
result.Add(quarterlyDataPoint);
}

return result;
}
private void EnrichQuarterlyEarningsDataPoint(TQuarterlyEarnings quarterlyEarningDataPoint, JsonElement quarterlyEarningDataPointFields)
{
foreach (var fieldJson in quarterlyEarningDataPointFields.EnumerateObject())
{
GetQuarterlyParsingDelegate(fieldJson.Name)?.Invoke(quarterlyEarningDataPoint, fieldJson.Value.GetString());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace AlphaVantage.Net.Common.Earnings
{
public abstract class EarningsPeriodBase
{
public DateTime FiscalDateEnding { get; set; }
public decimal ReportedEPS { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using AlphaVantage.Net.Common.Currencies;
using System;
using System.Collections.Generic;
using System.Text;

namespace AlphaVantage.Net.Common.Overview
{
public abstract class OverviewBase
{
public string Symbol { get; set; } = String.Empty;
public string AssetType { get; set; } = String.Empty;//probably could become an enum
public string Name { get; set; } = String.Empty;
public string Description { get; set; } = String.Empty;
public string Exchange { get; set; } = String.Empty; //probably could become an enum
public PhysicalCurrency PhysicalCurrency { get; set; }
public string Country { get; set; } = String.Empty;//probably could become an enum

public string Sector { get; set; } = String.Empty;
public string Industry { get; set; } = String.Empty;
public string Address { get; set; } = String.Empty;
public int FullTimeEmployees { get; set; }
public string FiscalYearEnd { get; set; } = String.Empty;
public DateTime LatestQuarter { get; set; }
public long MarketCapitalization { get; set; }
public long EBITDA { get; set; }
public decimal PERatio { get; set; }
public decimal PEGRatio { get; set; }
public decimal BookValue { get; set; }
public decimal DividendPerShare { get; set; }
public decimal DividendYield { get; set; }
public decimal EPS { get; set; }
public decimal RevenuePerShareTTM { get; set; }
public decimal ProfitMargin { get; set; }
public decimal OperatingMarginTTM { get; set; }
public decimal ReturnOnAssetsTTM { get; set; }
public decimal ReturnOnEquityTTM { get; set; }
public long RevenueTTM { get; set; }
public long GrossProfitTTM { get; set; }
public decimal DilutedEPSTTM { get; set; }
public decimal QuarterlyEarningsGrowthYOY { get; set; }
public decimal QuarterlyRevenueGrowthYOY { get; set; }
public decimal AnalystTargetPrice { get; set; }
public decimal TrailingPE { get; set; }
public decimal ForwardPE { get; set; }
public decimal PriceToSalesRatioTTM { get; set; }
public decimal PriceToBookRatio { get; set; }
public decimal EVToRevenue { get; set; }
public decimal EVToEBITDA { get; set; }
public decimal Beta { get; set; }
public decimal FiftyTwoWeekHigh { get; set; }
public decimal FiftyTwoWeekLow { get; set; }
public decimal FiftyDayMovingAverage { get; set; }
public decimal TwoHundredDayMovingAverage { get; set; }
public long SharesOutstanding { get; set; }
public long SharesFloat { get; set; }
public long SharesShort { get; set; }
public long SharesShortPriorMonth { get; set; }
public decimal ShortRatio { get; set; }
public decimal ShortPercentOutstanding { get; set; }
public decimal ShortPercentFloat { get; set; }
public decimal PercentInsiders { get; set; }
public decimal PercentInstitutions { get; set; }
public decimal ForwardAnnualDividendRate { get; set; }
public decimal ForwardAnnualDividendYield { get; set; }
public decimal PayoutRatio { get; set; }
public DateTime DividendDate { get; set; }
public DateTime ExDividendDate { get; set; }
public string LastSplitFactor { get; set; } = String.Empty;
public DateTime LastSplitDate { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using AlphaVantage.Net.Common.Parsing;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;

namespace AlphaVantage.Net.Common.Overview
{
public abstract class OverviewParserBase<TOverview> : IAlphaVantageJsonDocumentParser<TOverview>
where TOverview : OverviewBase
{
public abstract TOverview CreateOverviewInstance();

public abstract Action<TOverview, string>? GetOverviewParsingDelegate(string fieldName);

public TOverview ParseApiResponse(JsonDocument jsonDocument)
{
var result = CreateOverviewInstance();

try
{
EnrichOverviewData(result, jsonDocument);
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
throw;
}

return result;
}

private void EnrichOverviewData(TOverview overview, JsonDocument overviewFields)
{
foreach (var fieldJson in overviewFields.RootElement.EnumerateObject())
{
GetOverviewParsingDelegate(fieldJson.Name)?.Invoke(overview, fieldJson.Value.GetString());
}
}


}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,50 @@
using AlphaVantage.Net.Common.Currencies;
using System;
using System.Globalization;

namespace AlphaVantage.Net.Common.Parsing
{
public static class ParsingExtensions
{
static readonly string EMPTY_VALUE = "None";
public static decimal ParseToDecimal(this string stringToParse)
{
if (stringToParse == EMPTY_VALUE)
return 0;
return decimal.Parse(stringToParse, CultureInfo.InvariantCulture);
}

public static DateTime ParseToDateTime(this string stringToParse)
{
if (stringToParse == EMPTY_VALUE)
return DateTime.MinValue;
return DateTime.Parse(stringToParse, CultureInfo.InvariantCulture);
}

public static long ParseToLong(this string stringToParse)
{
if (stringToParse == EMPTY_VALUE)
return 0;
return long.Parse(stringToParse, CultureInfo.InvariantCulture);
}

public static int ParseToInt(this string stringToParse)
{
if (stringToParse == EMPTY_VALUE)
return 0;
return int.Parse(stringToParse, CultureInfo.InvariantCulture);
}

public static PhysicalCurrency ParseToCurrency(this string stringToParse)
{
if (stringToParse == EMPTY_VALUE)
return 0;
PhysicalCurrency result = PhysicalCurrency.AED;

if (!Enum.TryParse(stringToParse, out result))
throw new FormatException("The currency code did not match a known currency code");

return result;
}
}
}
1 change: 1 addition & 0 deletions AlphaVantage.Net/src/AlphaVantage.Net.Core/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[assembly: InternalsVisibleTo("AlphaVantage.Net.Stocks")]
[assembly: InternalsVisibleTo("AlphaVantage.Net.Forex")]
[assembly: InternalsVisibleTo("AlphaVantage.Net.Fundamentals")]
[assembly: InternalsVisibleTo("AlphaVantage.Net.Crypto")]
[assembly: InternalsVisibleTo("AlphaVantage.Net.TechnicalIndicators")]
[assembly: InternalsVisibleTo("AlphaVantage.Net.Core.Tests")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<PackageId>AlphaVantage.Net.Fundamentals</PackageId>
<Description>
Fully typed client for retrieval fundamental data from Alpha Vantage API. Works with newest and fastest parser System.Text.Json under the hood
</Description>
<Version>2.0.1</Version>
<Authors>LutsenkoKirill</Authors>
<RepositoryUrl>https://github.com/LutsenkoKirill/AlphaVantage.Net</RepositoryUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<LangVersion>8</LangVersion>
<Nullable>enable</Nullable>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageIcon>logo.jpg</PackageIcon>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\AlphaVantage.Net.Core\AlphaVantage.Net.Core.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="NodaTime" Version="3.0.0" />
<PackageReference Include="NodaTime.Serialization.SystemTextJson" Version="1.0.0" />
</ItemGroup>

<ItemGroup>
<None Include="..\logo.jpg" Pack="true" PackagePath="">
<Link>logo.jpg</Link>
</None>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using AlphaVantage.Net.Core.Client;
using System;
using System.Collections.Generic;
using System.Text;

namespace AlphaVantage.Net.Fundamentals.Client
{
public static class CoreClientExtension
{
/// <summary>
/// Return <see cref="FundamentalsClient"/> that simplify work with Fundamentals APIs
/// </summary>
/// <param name="client"></param>
/// <returns></returns>
public static FundamentalsClient Fundamentals(this AlphaVantageClient client)
{
return new FundamentalsClient(client);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using AlphaVantage.Net.Common;
using AlphaVantage.Net.Core.Client;
using AlphaVantage.Net.Fundamentals.Data;
using AlphaVantage.Net.Fundamentals.Parsing;
using JetBrains.Annotations;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace AlphaVantage.Net.Fundamentals.Client
{
public sealed class FundamentalsClient : TypedAlphaVantageClient
{
internal FundamentalsClient(AlphaVantageClient alphaVantageClient) : base(alphaVantageClient)
{
}



}
}
Loading