Skip to content

Commit

Permalink
Merge pull request #63 from ooples/bugfix
Browse files Browse the repository at this point in the history
Fixed a null reference issue on certain conditions for GetChartInfoAs…
  • Loading branch information
ooples authored Mar 11, 2024
2 parents f4e31e7 + 4c31268 commit 1d988bd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
20 changes: 13 additions & 7 deletions src/Helpers/ChartHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ internal override IEnumerable<T> ParseYahooJsonData<T>(string jsonData)
{
var chartNodes = JsonNode.Parse(jsonData)!;
var root = chartNodes["chart"]!["result"]![0];
var dates = root!["timestamp"]!.AsArray().Select(x => x!.GetValue<long>().FromUnixTimeStamp());
var dates = root!["timestamp"]?.AsArray().Select(x => x!.GetValue<long>().FromUnixTimeStamp());
var indicatorRoot = root["indicators"]!["quote"]![0];

if (dates == null || indicatorRoot == null || indicatorRoot.AsArray().Count == 0)
{
throw new InvalidOperationException("Requested Information Not Available On Yahoo Finance");
}

var closePrices = indicatorRoot!["close"]!.AsArray().Select(x => x != null ? Math.Round(x.GetValue<double>(), 4) : 0);
var openPrices = indicatorRoot!["open"]!.AsArray().Select(x => x != null ? Math.Round(x.GetValue<double>(), 4) : 0);
var lowPrices = indicatorRoot!["low"]!.AsArray().Select(x => x != null ? Math.Round(x.GetValue<double>(), 4) : 0);
Expand All @@ -22,12 +28,12 @@ internal override IEnumerable<T> ParseYahooJsonData<T>(string jsonData)

var result = new ChartData
{
DateList = dates.ToList(),
CloseList = closePrices.ToList(),
OpenList = openPrices.ToList(),
HighList = highPrices.ToList(),
VolumeList = volumes.ToList(),
LowList = lowPrices.ToList()
DateList = new List<DateTime>(dates),
CloseList = new List<double>(closePrices),
OpenList = new List<double>(openPrices),
HighList = new List<double>(highPrices),
VolumeList = new List<double>(volumes),
LowList = new List<double>(lowPrices)
};

return new[] { result }.Cast<T>();
Expand Down
2 changes: 1 addition & 1 deletion src/OoplesFinance.YahooFinanceAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<AutoGenerateBindingRedirects>True</AutoGenerateBindingRedirects>
<Title>Ooples Finance Yahoo Finance API</Title>
<Version>1.6</Version>
<Version>1.6.1</Version>
<Authors>ooples</Authors>
<Company>Ooples Finance</Company>
<Copyright>Ooples Finance LLC 2022-2023</Copyright>
Expand Down
4 changes: 2 additions & 2 deletions tests/TestConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
var symbols = new string[] { symbol, "MSFT", "NFLX", "TSLA", "YHOO", "SPY", "A", "AA", "GOOG", "F", "UBER", "LYFT" };

var yahooClient = new YahooClient();
var historicalDataList = await yahooClient.GetHistoricalDataAsync(symbol, DataFrequency.Daily, startDate);
//var historicalDataList = await yahooClient.GetHistoricalDataAsync(symbol, DataFrequency.Daily, startDate);
//var capitalGainList = await yahooClient.GetCapitalGainDataAsync(symbol, DataFrequency.Monthly, startDate);
//var dividendList = await yahooClient.GetDividendDataAsync(symbol, DataFrequency.Weekly, startDate);
//var stockSplitList = await yahooClient.GetStockSplitDataAsync(symbol, DataFrequency.Monthly, startDate);
Expand Down Expand Up @@ -46,7 +46,7 @@
//var incomeStatementHistoryQuarterlyList = await yahooClient.GetIncomeStatementHistoryQuarterlyAsync(symbol);
//var cashflowStatementHistoryQuarterlyList = await yahooClient.GetCashflowStatementHistoryQuarterlyAsync(symbol);
//var balanceSheetHistoryQuarterlyList = await yahooClient.GetBalanceSheetHistoryQuarterlyAsync(symbol);
//var chartInfoList = await yahooClient.GetChartInfoAsync("GOOG", TimeRange._1Year, TimeInterval._1Day);
var chartInfoList = await yahooClient.GetChartInfoAsync("BIO.B", TimeRange._1Year, TimeInterval._1Day);
//var sparkChartInfoList = await yahooClient.GetSparkChartInfoAsync(symbols, TimeRange._1Month, TimeInterval._1Day);
//var realTimeQuoteList = await yahooClient.GetRealTimeQuotesAsync(symbols);
//var marketSummaryList = await yahooClient.GetMarketSummaryAsync();
Expand Down

0 comments on commit 1d988bd

Please sign in to comment.