Home > database >  Trouble with YahooFinanceAPI NuGet Package in C#
Trouble with YahooFinanceAPI NuGet Package in C#

Time:03-03

I'm having trouble using YahooFinanceAPI NuGet Package. I'm trying to get historical Data using this package, for now I have a window where the user can put some information, and after clicking on a button I would like to display the historical price in a List View. The procedur when cliking the button is :

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());
                    }
                }
            }
        }

This part :

var awaiter = DataFromYahoo.ReceiveData(Symbol, StartDate, EndDate, Histo);

comes from a DLL that I created :

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;
        }

But when the code reach the part :

var histo = await Yahoo.GetHistoricalAsync(Symbol, StartDate, EndDate,Period.Daily);

It stop without any exception or error message.

I get this part from here

And I'm not really sure what I'm doing wrong. I'm also new in C# and I'm didn't really understood how works the async method, so if someone has some answer for me I will be glad. Thanks !

EDIT : Also the ConvertDate() function will just convert date from a string format to a DateTime format as it's use in the link.

CodePudding user response:

You should await your async method:

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());
            }
        }
    }
}

"Abusing" asynchronous code by for example accessing the blocking .Result property may deadlock which is probably what you have encountered here.

  • Related