Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advanced fundamentals fixes cum upgrades #101

Merged
merged 3 commits into from
Feb 19, 2021
Merged
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
7 changes: 7 additions & 0 deletions IEXSharp/Helper/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public static TimeSpan GetTimeOfDayInEST(this ITimestampedMinute timestampedObj)
/// <param name="unixTime"></param>
/// <returns></returns>
public static DateTime ConvertFromUnixMilliSecToDateTime(this long unixTime) => UnixEpoch.AddMilliseconds(unixTime);

/// <summary>
/// Converts DateTime object to time series endpoint compatible query param value
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static string ToTimeSeriesDate(this DateTime date) => date.ToString("yyyy-MM-dd");
}

public interface ITimestampedDateMinute
Expand Down
2 changes: 0 additions & 2 deletions IEXSharp/IEXSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
</PropertyGroup>

<ItemGroup>
<Folder Include="Model\CoreData" />
<Folder Include="Model\Shared\Request\" />
<Folder Include="Service\Cloud\CoreData" />
</ItemGroup>

Expand Down
57 changes: 57 additions & 0 deletions IEXSharp/Model/Shared/Request/TimeSeries.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using Common.Logging.Configuration;
using IEXSharp.Helper;

namespace IEXSharp.Model.Shared.Request
{
public class TimeSeries
{
private readonly TimeSeriesPeriod period;

public TimeSeries(TimeSeriesPeriod period)
{
this.period = period;
}

private string Range { get; set; }
private bool Calendar { get; set; }
private int Limit { get; set; }
private string From { get; set; }
private string To { get; set; }
private int Last { get; set; }
private int First { get; set; }

public TimeSeries SetRange(int range)
{
if (range <= 0) return this;
Range = period == TimeSeriesPeriod.Quarterly ? range + "q" : range + "y";
return this;
}

public TimeSeries SetDateRange(DateTime? from, DateTime? to = default)
{
if (from == null) return this;
From = from?.ToTimeSeriesDate();
To = to?.ToTimeSeriesDate() ?? DateTime.Today.ToTimeSeriesDate();
return this;
}

public NameValueCollection TimeSeriesQueryParams()
{
var nvc = new NameValueCollection();

if (From != null)
{
nvc.Add("from", From);
nvc.Add("to", To);
}

if (!string.IsNullOrEmpty(Range) && string.IsNullOrEmpty(From))
{
nvc.Add("range", Range);
}

return nvc;
}
}
}
14 changes: 14 additions & 0 deletions IEXSharp/Model/Shared/Request/TimeSeriesPeriod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.ComponentModel;

namespace IEXSharp.Model.Shared.Request
{
public enum TimeSeriesPeriod
{
[Description("quarterly")]
Quarterly,
[Description("annual")]
Annual,
[Description("ttm")]
Ttm
}
}
44 changes: 44 additions & 0 deletions IEXSharp/Model/Shared/Request/TimeSeriesRange.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.ComponentModel;

namespace IEXSharp.Model.Shared.Request
{
public enum TimeSeriesRange
{
[Description("today")]
Today,
[Description("yesterday")]
Yesterday,
[Description("ytd")]
Ytd,
[Description("last-week")]
LastWeek,
[Description("last-month")]
LastMonth,
[Description("last-quarter")]
LastQuarter,
[Description("d")]
Days,
[Description("w")]
Weeks,
[Description("m")]
Months,
[Description("q")]
Quarters,
[Description("y")]
Years,
[Description("tomorrow")]
Tomorrow,
[Description("this-week")]
ThisWeek,
[Description("this-month")]
ThisMonth,
[Description("this-quarter")]
ThisQuarter,
[Description("next-week")]
NextWeek,
[Description("next-month")]
NextMonth,
[Description("next-quarter")]
NextQuarter
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public interface IStockFundamentalsService
/// </summary>
/// <param name="symbol"></param>
/// <param name="period"></param>
/// <param name="timeSeries"></param>
/// <returns></returns>
Task<IEXResponse<IEnumerable<AdvancedFundamentalsResponse>>> AdvancedFundamentalsAsync(string symbol, Period period = Period.Quarter);
Task<IEXResponse<IEnumerable<AdvancedFundamentalsResponse>>> AdvancedFundamentalsAsync(string symbol, TimeSeriesPeriod period = TimeSeriesPeriod.Quarterly, TimeSeries timeSeries = null);

/// <summary>
/// <see cref="https://iexcloud.io/docs/api/#balance-sheet"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@ internal StockFundamentalsService(ExecutorREST executor)
this.executor = executor;
}

public async Task<IEXResponse<IEnumerable<AdvancedFundamentalsResponse>>> AdvancedFundamentalsAsync(string symbol, Period period = Period.Quarter)
public async Task<IEXResponse<IEnumerable<AdvancedFundamentalsResponse>>> AdvancedFundamentalsAsync(string symbol, TimeSeriesPeriod period = TimeSeriesPeriod.Quarterly, TimeSeries timeSeries = null)
{
const string urlPattern = "time-series/fundamentals/[symbol]/[period]";

var qsb = new QueryStringBuilder();

if (timeSeries != null)
{
var queryParams = timeSeries.TimeSeriesQueryParams();
foreach (var nameValue in queryParams)
{
qsb.Add(nameValue.Key, nameValue.Value);
}
}

var pathNvc = new NameValueCollection
{
{"symbol", symbol},
Expand All @@ -33,8 +42,7 @@ public async Task<IEXResponse<IEnumerable<AdvancedFundamentalsResponse>>> Advanc
return await executor.ExecuteAsync<IEnumerable<AdvancedFundamentalsResponse>>(urlPattern, pathNvc, qsb);
}

public async Task<IEXResponse<BalanceSheetResponse>> BalanceSheetAsync(string symbol, Period period = Period.Quarter,
int last = 1)
public async Task<IEXResponse<BalanceSheetResponse>> BalanceSheetAsync(string symbol, Period period = Period.Quarter, int last = 1)
{
const string urlPattern = "stock/[symbol]/balance-sheet/[last]";

Expand Down
27 changes: 18 additions & 9 deletions IEXSharpTest/Cloud/CoreData/StockFundamentalsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,24 @@ public void Setup()
}

[Test]
[TestCase("BEDU", Period.Annual)]
[TestCase("BEDU", Period.Quarter)]
[TestCase("F", Period.Annual)]
[TestCase("CCM", Period.Quarter)]
[TestCase("AAPL", Period.Quarter)]
[TestCase("FB", Period.Quarter)]
public async Task AdvancedFundamentalsAsyncTest(string symbol, Period period = Period.Quarter)
{
var response = await sandBoxClient.StockFundamentals.AdvancedFundamentalsAsync(symbol, period);
[TestCase("BEDU", TimeSeriesPeriod.Annual)]
[TestCase("BEDU", TimeSeriesPeriod.Quarterly)]
[TestCase("F", TimeSeriesPeriod.Annual)]
[TestCase("CCM", TimeSeriesPeriod.Quarterly)]
[TestCase("AAPL", TimeSeriesPeriod.Annual)]
[TestCase("AAPL", TimeSeriesPeriod.Quarterly)]
[TestCase("FB", TimeSeriesPeriod.Quarterly)]
[TestCase("BEDU", TimeSeriesPeriod.Annual, 2)]
[TestCase("BEDU", TimeSeriesPeriod.Quarterly, 2)]
[TestCase("F", TimeSeriesPeriod.Annual, 3)]
[TestCase("CCM", TimeSeriesPeriod.Quarterly, 4)]
[TestCase("AAPL", TimeSeriesPeriod.Quarterly, 3)]
[TestCase("FB", TimeSeriesPeriod.Quarterly, 5)]
[TestCase("AAPL", TimeSeriesPeriod.Quarterly, null, "2008-1-1", "2010-1-1")]
public async Task AdvancedFundamentalsAsyncTest(string symbol, TimeSeriesPeriod period = TimeSeriesPeriod.Quarterly, int range = 1, DateTime? from = null, DateTime? to = null)
{
var timeSeries = new TimeSeries(period).SetRange(range).SetDateRange(from, to);
var response = await sandBoxClient.StockFundamentals.AdvancedFundamentalsAsync(symbol, period, timeSeries);

Assert.IsNull(response.ErrorMessage);
foreach (var data in response.Data)
Expand Down