C# 中 YahooFinanceAPI NuGet 包的问题
Trouble with YahooFinanceAPI NuGet Package in C#
我在使用 YahooFinanceAPI NuGet 包时遇到问题。
我正在尝试使用此包获取历史数据,目前我有一个 window 用户可以在其中放置一些信息,并且在单击按钮后我想在列表视图中显示历史价格。
点击按钮的过程是:
private async void BtnGetData_Click(object sender, RoutedEventArgs e)
{
DataTable Histo = new DataTable();
foreach (string s in ListCol)
{
DataColumn x = new DataColumn();
x.ColumnName = s;
x.DataType = System.Type.GetType("System.String");
Histo.Columns.Add(x);
}
Symbol = "^FCHI";// Ticker.Text;
StartDate = "21/02/2021";// DD.Text;
EndDate = "21/02/2022"; //DF.Text;
//Histo = DataFromYahoo.GetData(Symbol, StartDate, EndDate);
var awaiter = DataFromYahoo.ReceiveData(Symbol, StartDate, EndDate, Histo);
if (awaiter.Result == 1)
{
foreach (DataRow row in Histo.Rows)
{
foreach (DataColumn col in Histo.Columns)
{
YahooData.Items.Add(row[col].ToString());
}
}
}
}
这部分:
var awaiter = DataFromYahoo.ReceiveData(Symbol, StartDate, EndDate, Histo);
来自我创建的 DLL :
public async Task<int> ReceiveData (string Symbol, string DD, string DF, DataTable Result)
{
DateTime StartDate;
DateTime EndDate;
StartDate = ConvertDate(DD);
EndDate = ConvertDate(DF);
var histo = await Yahoo.GetHistoricalAsync(Symbol, StartDate, EndDate,Period.Daily);
DataRow row;
foreach (var candle in histo)
{
row = Result.NewRow();
row["Date"] = candle.DateTime.Date.ToString();
row["Open"] = candle.Open.ToString();
row["High"] = candle.High.ToString();
row["Low"] = candle.Low.ToString();
row["Close"] = candle.Close.ToString();
row["AdjClos"] = candle.AdjustedClose.ToString();
row["Volume"] = candle.Volume.ToString();
Result.Rows.Add(row);
}
return 1;
}
但是当代码到达部分时:
var histo = await Yahoo.GetHistoricalAsync(Symbol, StartDate, EndDate,Period.Daily);
它停止了,没有任何异常或错误消息。
我从 here
得到这部分
而且我不太确定我做错了什么。我也是 C# 的新手,我并不真正了解异步方法的工作原理,所以如果有人对我有一些答案,我会很高兴。
谢谢!
编辑:ConvertDate() 函数也只是将日期从字符串格式转换为 DateTime 格式,因为它在 link.
中使用
您应该等待您的异步方法:
private async void BtnGetData_Click(object sender, RoutedEventArgs e)
{
DataTable Histo = new DataTable();
foreach (string s in ListCol)
{
DataColumn x = new DataColumn();
x.ColumnName = s;
x.DataType = System.Type.GetType("System.String");
Histo.Columns.Add(x);
}
Symbol = "^FCHI";// Ticker.Text;
StartDate = "21/02/2021";// DD.Text;
EndDate = "21/02/2022"; //DF.Text;
//Histo = DataFromYahoo.GetData(Symbol, StartDate, EndDate);
var awaiter = await DataFromYahoo.ReceiveData(Symbol, StartDate, EndDate, Histo);
if (awaiter == 1)
{
foreach (DataRow row in Histo.Rows)
{
foreach (DataColumn col in Histo.Columns)
{
YahooData.Items.Add(row[col].ToString());
}
}
}
}
“滥用”异步代码,例如访问阻塞 .Result
属性 可能会出现死锁,这可能就是您在这里遇到的情况。
为什么不直接用yahooapi(他们还是支持调用的)。我用这个。请参阅下面的示例函数以获取历史数据
public StringBuilder FetchStockDataOnline(string scriptname, string range, string interval, string indicators = "quote", bool includeTimestamps = true)
{
StringBuilder returnData = null;
string webservice_url = "";
WebResponse wr;
Stream receiveStream = null;
StreamReader reader = null;
try
{
//https://query1.finance.yahoo.com/v7/finance/chart/HDFC.BO?range=2yr&interval=1d&indicators=quote&includeTimestamps=true
webservice_url = string.Format(StockManager.urlGetStockData, scriptname, range, interval, indicators, includeTimestamps);
Uri url = new Uri(webservice_url);
var webRequest = WebRequest.Create(url);
webRequest.Method = "GET";
webRequest.ContentType = "application/json";
wr = webRequest.GetResponseAsync().Result;
receiveStream = wr.GetResponseStream();
reader = new StreamReader(receiveStream);
returnData = new StringBuilder(reader.ReadToEnd());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return returnData;
}
我在使用 YahooFinanceAPI NuGet 包时遇到问题。 我正在尝试使用此包获取历史数据,目前我有一个 window 用户可以在其中放置一些信息,并且在单击按钮后我想在列表视图中显示历史价格。 点击按钮的过程是:
private async void BtnGetData_Click(object sender, RoutedEventArgs e)
{
DataTable Histo = new DataTable();
foreach (string s in ListCol)
{
DataColumn x = new DataColumn();
x.ColumnName = s;
x.DataType = System.Type.GetType("System.String");
Histo.Columns.Add(x);
}
Symbol = "^FCHI";// Ticker.Text;
StartDate = "21/02/2021";// DD.Text;
EndDate = "21/02/2022"; //DF.Text;
//Histo = DataFromYahoo.GetData(Symbol, StartDate, EndDate);
var awaiter = DataFromYahoo.ReceiveData(Symbol, StartDate, EndDate, Histo);
if (awaiter.Result == 1)
{
foreach (DataRow row in Histo.Rows)
{
foreach (DataColumn col in Histo.Columns)
{
YahooData.Items.Add(row[col].ToString());
}
}
}
}
这部分:
var awaiter = DataFromYahoo.ReceiveData(Symbol, StartDate, EndDate, Histo);
来自我创建的 DLL :
public async Task<int> ReceiveData (string Symbol, string DD, string DF, DataTable Result)
{
DateTime StartDate;
DateTime EndDate;
StartDate = ConvertDate(DD);
EndDate = ConvertDate(DF);
var histo = await Yahoo.GetHistoricalAsync(Symbol, StartDate, EndDate,Period.Daily);
DataRow row;
foreach (var candle in histo)
{
row = Result.NewRow();
row["Date"] = candle.DateTime.Date.ToString();
row["Open"] = candle.Open.ToString();
row["High"] = candle.High.ToString();
row["Low"] = candle.Low.ToString();
row["Close"] = candle.Close.ToString();
row["AdjClos"] = candle.AdjustedClose.ToString();
row["Volume"] = candle.Volume.ToString();
Result.Rows.Add(row);
}
return 1;
}
但是当代码到达部分时:
var histo = await Yahoo.GetHistoricalAsync(Symbol, StartDate, EndDate,Period.Daily);
它停止了,没有任何异常或错误消息。
我从 here
得到这部分而且我不太确定我做错了什么。我也是 C# 的新手,我并不真正了解异步方法的工作原理,所以如果有人对我有一些答案,我会很高兴。 谢谢!
编辑:ConvertDate() 函数也只是将日期从字符串格式转换为 DateTime 格式,因为它在 link.
中使用您应该等待您的异步方法:
private async void BtnGetData_Click(object sender, RoutedEventArgs e)
{
DataTable Histo = new DataTable();
foreach (string s in ListCol)
{
DataColumn x = new DataColumn();
x.ColumnName = s;
x.DataType = System.Type.GetType("System.String");
Histo.Columns.Add(x);
}
Symbol = "^FCHI";// Ticker.Text;
StartDate = "21/02/2021";// DD.Text;
EndDate = "21/02/2022"; //DF.Text;
//Histo = DataFromYahoo.GetData(Symbol, StartDate, EndDate);
var awaiter = await DataFromYahoo.ReceiveData(Symbol, StartDate, EndDate, Histo);
if (awaiter == 1)
{
foreach (DataRow row in Histo.Rows)
{
foreach (DataColumn col in Histo.Columns)
{
YahooData.Items.Add(row[col].ToString());
}
}
}
}
“滥用”异步代码,例如访问阻塞 .Result
属性 可能会出现死锁,这可能就是您在这里遇到的情况。
为什么不直接用yahooapi(他们还是支持调用的)。我用这个。请参阅下面的示例函数以获取历史数据
public StringBuilder FetchStockDataOnline(string scriptname, string range, string interval, string indicators = "quote", bool includeTimestamps = true)
{
StringBuilder returnData = null;
string webservice_url = "";
WebResponse wr;
Stream receiveStream = null;
StreamReader reader = null;
try
{
//https://query1.finance.yahoo.com/v7/finance/chart/HDFC.BO?range=2yr&interval=1d&indicators=quote&includeTimestamps=true
webservice_url = string.Format(StockManager.urlGetStockData, scriptname, range, interval, indicators, includeTimestamps);
Uri url = new Uri(webservice_url);
var webRequest = WebRequest.Create(url);
webRequest.Method = "GET";
webRequest.ContentType = "application/json";
wr = webRequest.GetResponseAsync().Result;
receiveStream = wr.GetResponseStream();
reader = new StreamReader(receiveStream);
returnData = new StringBuilder(reader.ReadToEnd());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return returnData;
}