From 71db4ffc33dd10be942fa501add655555dc04140 Mon Sep 17 00:00:00 2001 From: Sai Dharmendra Kanneganti Date: Wed, 17 Feb 2021 16:34:46 -0500 Subject: [PATCH 1/3] Stock advanced fundamentals quartely endpoint fix Fixed an Issue where we were sending quarter instead of quarterly for pulling the data from IEX. Added TimeSeries query params functionality to the Stock Advanced Fundamentals endpoint. --- IEXSharp/Helper/DateTimeExtensions.cs | 7 +++ IEXSharp/IEXSharp.csproj | 3 +- IEXSharp/Model/Shared/Request/TimeSeries.cs | 57 +++++++++++++++++++ .../Model/Shared/Request/TimeSeriesPeriod.cs | 14 +++++ .../Model/Shared/Request/TimeSeriesRange.cs | 44 ++++++++++++++ .../IStockFundamentalsService.cs | 3 +- .../StockFundamentalsService.cs | 14 ++++- .../Cloud/CoreData/StockFundamentalsTest.cs | 27 ++++++--- 8 files changed, 154 insertions(+), 15 deletions(-) create mode 100644 IEXSharp/Model/Shared/Request/TimeSeries.cs create mode 100644 IEXSharp/Model/Shared/Request/TimeSeriesPeriod.cs create mode 100644 IEXSharp/Model/Shared/Request/TimeSeriesRange.cs diff --git a/IEXSharp/Helper/DateTimeExtensions.cs b/IEXSharp/Helper/DateTimeExtensions.cs index e32d8d77..2cae32bf 100644 --- a/IEXSharp/Helper/DateTimeExtensions.cs +++ b/IEXSharp/Helper/DateTimeExtensions.cs @@ -25,6 +25,13 @@ public static TimeSpan GetTimeOfDayInEST(this ITimestampedMinute timestampedObj) /// /// public static DateTime ConvertFromUnixMilliSecToDateTime(this long unixTime) => UnixEpoch.AddMilliseconds(unixTime); + + /// + /// Converts DateTime object to time series endpoint compatible query param value + /// + /// + /// + public static string ToTimeSeriesDate(this DateTime date) => date.ToString("yyyy-MM-dd"); } public interface ITimestampedDateMinute diff --git a/IEXSharp/IEXSharp.csproj b/IEXSharp/IEXSharp.csproj index 6a16beff..a3cd5b30 100644 --- a/IEXSharp/IEXSharp.csproj +++ b/IEXSharp/IEXSharp.csproj @@ -22,6 +22,7 @@ VSLee.IEXSharp true 2019 Victor Lee + latest @@ -38,8 +39,6 @@ - - diff --git a/IEXSharp/Model/Shared/Request/TimeSeries.cs b/IEXSharp/Model/Shared/Request/TimeSeries.cs new file mode 100644 index 00000000..7cd97a02 --- /dev/null +++ b/IEXSharp/Model/Shared/Request/TimeSeries.cs @@ -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 int _Range { get; set; } + private string Range => period == TimeSeriesPeriod.Quarterly ? _Range + "q" : _Range + "y"; + 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 AddRange(int range) + { + _Range = range; + return this; + } + + public TimeSeries AddDateRange(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 (_Range > 0 && string.IsNullOrEmpty(From)) + { + nvc.Add("range", Range); + } + + return nvc; + } + } +} \ No newline at end of file diff --git a/IEXSharp/Model/Shared/Request/TimeSeriesPeriod.cs b/IEXSharp/Model/Shared/Request/TimeSeriesPeriod.cs new file mode 100644 index 00000000..63cd5e11 --- /dev/null +++ b/IEXSharp/Model/Shared/Request/TimeSeriesPeriod.cs @@ -0,0 +1,14 @@ +using System.ComponentModel; + +namespace IEXSharp.Model.Shared.Request +{ + public enum TimeSeriesPeriod + { + [Description("quarterly")] + Quarterly, + [Description("annual")] + Annual, + [Description("ttm")] + Ttm + } +} \ No newline at end of file diff --git a/IEXSharp/Model/Shared/Request/TimeSeriesRange.cs b/IEXSharp/Model/Shared/Request/TimeSeriesRange.cs new file mode 100644 index 00000000..962ba769 --- /dev/null +++ b/IEXSharp/Model/Shared/Request/TimeSeriesRange.cs @@ -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 + } +} \ No newline at end of file diff --git a/IEXSharp/Service/Cloud/CoreData/StockFundamentals/IStockFundamentalsService.cs b/IEXSharp/Service/Cloud/CoreData/StockFundamentals/IStockFundamentalsService.cs index 0805ed77..aedc79f3 100644 --- a/IEXSharp/Service/Cloud/CoreData/StockFundamentals/IStockFundamentalsService.cs +++ b/IEXSharp/Service/Cloud/CoreData/StockFundamentals/IStockFundamentalsService.cs @@ -16,8 +16,9 @@ public interface IStockFundamentalsService /// /// /// + /// /// - Task>> AdvancedFundamentalsAsync(string symbol, Period period = Period.Quarter); + Task>> AdvancedFundamentalsAsync(string symbol, TimeSeriesPeriod period = TimeSeriesPeriod.Quarterly, TimeSeries timeSeries = null); /// /// diff --git a/IEXSharp/Service/Cloud/CoreData/StockFundamentals/StockFundamentalsService.cs b/IEXSharp/Service/Cloud/CoreData/StockFundamentals/StockFundamentalsService.cs index f63d90b6..ab5dc4ed 100644 --- a/IEXSharp/Service/Cloud/CoreData/StockFundamentals/StockFundamentalsService.cs +++ b/IEXSharp/Service/Cloud/CoreData/StockFundamentals/StockFundamentalsService.cs @@ -18,12 +18,21 @@ internal StockFundamentalsService(ExecutorREST executor) this.executor = executor; } - public async Task>> AdvancedFundamentalsAsync(string symbol, Period period = Period.Quarter) + public async Task>> 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}, @@ -33,8 +42,7 @@ public async Task>> Advanc return await executor.ExecuteAsync>(urlPattern, pathNvc, qsb); } - public async Task> BalanceSheetAsync(string symbol, Period period = Period.Quarter, - int last = 1) + public async Task> BalanceSheetAsync(string symbol, Period period = Period.Quarter, int last = 1) { const string urlPattern = "stock/[symbol]/balance-sheet/[last]"; diff --git a/IEXSharpTest/Cloud/CoreData/StockFundamentalsTest.cs b/IEXSharpTest/Cloud/CoreData/StockFundamentalsTest.cs index d9697f8e..663a9323 100644 --- a/IEXSharpTest/Cloud/CoreData/StockFundamentalsTest.cs +++ b/IEXSharpTest/Cloud/CoreData/StockFundamentalsTest.cs @@ -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).AddRange(range).AddDateRange(from, to); + var response = await sandBoxClient.StockFundamentals.AdvancedFundamentalsAsync(symbol, period, timeSeries); Assert.IsNull(response.ErrorMessage); foreach (var data in response.Data) From c8a559bed7dda63ceedb1bea90b9a1020cbe41f5 Mon Sep 17 00:00:00 2001 From: Sai Dharmendra Kanneganti Date: Wed, 17 Feb 2021 16:58:39 -0500 Subject: [PATCH 2/3] Removed Lang version added by visual studio 2017 --- IEXSharp/IEXSharp.csproj | 1 - 1 file changed, 1 deletion(-) diff --git a/IEXSharp/IEXSharp.csproj b/IEXSharp/IEXSharp.csproj index a3cd5b30..3d298c15 100644 --- a/IEXSharp/IEXSharp.csproj +++ b/IEXSharp/IEXSharp.csproj @@ -22,7 +22,6 @@ VSLee.IEXSharp true 2019 Victor Lee - latest From 98cca95b28fc0750f5ac5715e308c50c92c779c5 Mon Sep 17 00:00:00 2001 From: Sai Dharmendra Kanneganti Date: Wed, 17 Feb 2021 17:06:06 -0500 Subject: [PATCH 3/3] Updated TimeSeries class function names to be more meaningful. Removed unnecessary properties --- IEXSharp/Model/Shared/Request/TimeSeries.cs | 12 ++++++------ IEXSharpTest/Cloud/CoreData/StockFundamentalsTest.cs | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/IEXSharp/Model/Shared/Request/TimeSeries.cs b/IEXSharp/Model/Shared/Request/TimeSeries.cs index 7cd97a02..c1777e02 100644 --- a/IEXSharp/Model/Shared/Request/TimeSeries.cs +++ b/IEXSharp/Model/Shared/Request/TimeSeries.cs @@ -13,8 +13,7 @@ public TimeSeries(TimeSeriesPeriod period) this.period = period; } - private int _Range { get; set; } - private string Range => period == TimeSeriesPeriod.Quarterly ? _Range + "q" : _Range + "y"; + private string Range { get; set; } private bool Calendar { get; set; } private int Limit { get; set; } private string From { get; set; } @@ -22,13 +21,14 @@ public TimeSeries(TimeSeriesPeriod period) private int Last { get; set; } private int First { get; set; } - public TimeSeries AddRange(int range) + public TimeSeries SetRange(int range) { - _Range = range; + if (range <= 0) return this; + Range = period == TimeSeriesPeriod.Quarterly ? range + "q" : range + "y"; return this; } - public TimeSeries AddDateRange(DateTime? from, DateTime? to = default) + public TimeSeries SetDateRange(DateTime? from, DateTime? to = default) { if (from == null) return this; From = from?.ToTimeSeriesDate(); @@ -46,7 +46,7 @@ public NameValueCollection TimeSeriesQueryParams() nvc.Add("to", To); } - if (_Range > 0 && string.IsNullOrEmpty(From)) + if (!string.IsNullOrEmpty(Range) && string.IsNullOrEmpty(From)) { nvc.Add("range", Range); } diff --git a/IEXSharpTest/Cloud/CoreData/StockFundamentalsTest.cs b/IEXSharpTest/Cloud/CoreData/StockFundamentalsTest.cs index 663a9323..84e126c9 100644 --- a/IEXSharpTest/Cloud/CoreData/StockFundamentalsTest.cs +++ b/IEXSharpTest/Cloud/CoreData/StockFundamentalsTest.cs @@ -40,7 +40,7 @@ public void Setup() [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).AddRange(range).AddDateRange(from, to); + var timeSeries = new TimeSeries(period).SetRange(range).SetDateRange(from, to); var response = await sandBoxClient.StockFundamentals.AdvancedFundamentalsAsync(symbol, period, timeSeries); Assert.IsNull(response.ErrorMessage);