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

Fixed a null reference issue on certain conditions for GetChartInfoAs… #63

Merged
merged 1 commit into from
Mar 11, 2024
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
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 @@ -5,11 +5,11 @@
{
var startDate = DateTime.Now.AddYears(-1);
var symbol = "AAPL";
var fundSymbol = "VSMPX";

Check warning on line 8 in tests/TestConsoleApp/Program.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The variable 'fundSymbol' is assigned but its value is never used

Check warning on line 8 in tests/TestConsoleApp/Program.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The variable 'fundSymbol' is assigned but its value is never used

Check warning on line 8 in tests/TestConsoleApp/Program.cs

View workflow job for this annotation

GitHub Actions / Analyze (javascript)

The variable 'fundSymbol' is assigned but its value is never used

Check warning on line 8 in tests/TestConsoleApp/Program.cs

View workflow job for this annotation

GitHub Actions / Analyze (javascript)

The variable 'fundSymbol' is assigned but its value is never used
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 Expand Up @@ -89,7 +89,7 @@
//var topUpsideBreakoutStocksList = await yahooClient.GetTopUpsideBreakoutStocksAsync(10);

var r = await yahooClient.GetRealTimeQuotesAsync("TSLA");
Console.WriteLine($"{r.Symbol} {r.RegularMarketPrice} {r.Bid} {r.BidSize} {r.Ask} {r.AskSize}");

Check warning on line 92 in tests/TestConsoleApp/Program.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Dereference of a possibly null reference.

Check warning on line 92 in tests/TestConsoleApp/Program.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Dereference of a possibly null reference.

Check warning on line 92 in tests/TestConsoleApp/Program.cs

View workflow job for this annotation

GitHub Actions / Analyze (javascript)

Dereference of a possibly null reference.

Check warning on line 92 in tests/TestConsoleApp/Program.cs

View workflow job for this annotation

GitHub Actions / Analyze (javascript)

Dereference of a possibly null reference.

Console.WriteLine();
}
Expand Down
Loading