How To Get Historical Stock Market Data in C#
HTML-код
- Опубликовано: 8 фев 2025
- In this video I describe how to get historical stock market data with the C# programming language. I utilize the YahooFinanceAPI nuget package to asynchronously fetch the historical stock data. I also show how to handle the return of said asynchronous task and how to continuously get new symbols and timespans for the data from the user. Thank you for watching this video!!!
Link to github repo for this video: github.com/vmi...
Link to my Discord Channel for C# questions, comments, suggestions and discussions: / discord
Thanks for the pointers on how to get started with the stock stuff.
A few things:
1. Keep in mind C# uses PascalCase -> getStockData vs GetStockData
2. You don't need to return an integer, just return the Task. So use Task versus Task. Task by itself means void.
3. Use the await keyword where you are calling GetStockData in lieu of your awaiter.Result == 1, you don't need that (don't do that). So you will have "await stock.GetStockData(...);" instead.
Great video - well communicated and excellent tutorial
Great and simple. Nicely done
Thank you!
@@veremiller2171 I think the next project could be a bit more advanced. What about an analysis program for live stock data that gives alerts when some values is crossed?
@@TheSveum that’s a great suggestion!
@@veremiller2171 I, myself, are about to create a option analysing program through quantitative analysis og the greeks. Fun to use some more advanced math related to finance
@@TheSveum That’s really cool, I would be really interested in learning the details of how you will do this. Did you happen to join my discord server? We could talk more there if you are interested in explaining it lol
Hi,
Great video, same remarks as DyslexicAnaboko
Just another question, is it normal it's so slow on the first call (about 10 seconds), then it's immediate when I call it another time ?
Thanks
I tried this code, but I get an error. Call failed with status code 401 (Unauthorized). How do I fix this?
Hi Vere, Love the program. It was working great until last week, but since then I keep getting "Failed to get symbol..." for any symbol I use. Is it working now when you run it from your desktop? Maybe Yahoo changed something in their API? They've done things like that in the past.
Yeah, unfortunately it looks like it doesn’t work anymore :( I will try to make an update
The developer of the nuget package has fixed the error. The code should work as intended now :)
@@veremiller2171 Hi! Thanks for the video. Are you sure that they fixed the issue? I have tried to build it exacly as you but still get an error that it could not find the symbol.
@@ZliVeN yes, it should work, I ran it about a week ago without any issues. However, it seems like for some people it works fine and for others there’s issues and I’m not sure why. Try following the GitHub repo I have linked in the description.
very helpful video. thank you for making this...
is the source code available ?
Hi
Great video
Any idea how to put it in a loop for all stocks available
You probably don’t want all stocks information, this would take a long time to get all of the responses, and most of this data would be pretty useless to you. I would suggest picking out a handful of stock symbols and storing them in an array and then you could iterate through getting each stocks data with a loop.
Hi, is company's fundamentals also available? Revenue, net income, cash flows, etc...
At min 5:33 I'm getting: error CS0103: The name 'Yahoo' does not exist in the current context
can we use csv file for this??
How do I pull Bitcoin data ?
hello, it is very nice, but i receive always "Failed to get symbol: AAPL" or other stock, what could be ? thanks
Hmmm, it could be a couple of things, I would make sure that you are using the right .Net Framework version with the right nuget package version.
facing error "failed to get symbol"
any solution?
I also got this error. I'm using .NET Framework 4.7.2, with the package saying it supports .NET Framework 4.6.1 and above.
espanol
Hi. Great Video! I am getting the "failed to get symbol" error. All my code is correct, and i am using correct tickers (AAPL, TSLA). I saw something about the developer having an issue but it was resolved so now im struggling. Please get back as soon as possible. Thank you. (I am using the same versions you did)
using System;
using YahooFinanceApi;
using System.Threading.Tasks;
using System.Linq;
namespace stocks_data
{
class Program
{
static void Main(string[] args)
{
char continueStr = 'y';
while (continueStr == 'y')
{
Console.WriteLine("Enter a stock ticker that u want historic data for: ");
string symbol = Console.ReadLine().ToUpper();
Console.WriteLine("Enter the amnount of months of historic data that you want to retrieve: ");
int timespan = Convert.ToInt32(Console.ReadLine());
DateTime endDate = DateTime.Today;
DateTime startDate = DateTime.Today.AddMonths(-timespan);
StockData stock = new StockData();
var awaiter = stock.getStockData(symbol, startDate, endDate);
if (awaiter.Result == 1)
{
Console.WriteLine();
Console.WriteLine("Do you want to get historical data for another ticker? [y/n]");
continueStr = Convert.ToChar(Console.ReadLine());
}
}
Console.WriteLine();
Console.WriteLine("have a nice day");
}
}
class StockData
{
public async Task getStockData(string symbol, DateTime startDate, DateTime endDate)
{
try
{
var historic_data = await Yahoo.GetHistoricalAsync(symbol, startDate, endDate);
var security = await Yahoo.Symbols(symbol).Fields(Field.LongName).QueryAsync();
var ticker = security[symbol];
var companyName = ticker[Field.LongName];
for (int i = 0; i < historic_data.Count; i++)
{
Console.WriteLine(companyName + " Closing price on: " + historic_data.ElementAt(i).DateTime.Month + "/" + historic_data.ElementAt(i).DateTime.Day + "/" + historic_data.ElementAt(i).DateTime.Year + ": $" + Math.Round(historic_data.ElementAt(i).Close, 2));
}
}
catch
{
Console.WriteLine("Failed to get symbol: " + symbol);
}
return 1;
}
}
}
Hi! I have the same issue. Did you find any solution for it?
16k hyper hd monitor btw
Very Nice video, but when i use the same program, i am getting result
Id = 15, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"
don't know whats the reason, cau please correct me where i went wrong
public class GetShareData
{
public void GetData()
{
stockData data = new stockData();
var waiter = data.getStockData("TATAPOWER", DateTime.Now.AddDays(-5), DateTime.Now);
}
}
public class stockData
{
public async Task getStockData(string symbol,DateTime startDate, DateTime endDate)
{
try
{
var historic_data = await Yahoo.GetHistoricalAsync(symbol, startDate, endDate);
var security = await Yahoo.Symbols(symbol).Fields(Field.LongName).QueryAsync();
var ticker = security[symbol];
var companyName = ticker[Field.LongName];
for (int i = 0; i < historic_data.Count; i++)
{
Console.WriteLine(companyName + " Closing price on:" + historic_data.ElementAt(i).DateTime.Month + "/" +
historic_data.ElementAt(i).DateTime.Day + "/" + historic_data.ElementAt(i).DateTime.Year + ": $" +
Math.Round(historic_data.ElementAt(i).Close, 2));
}
}
catch (Exception)
{
throw;
}
return 1;
}
}
}
Hi, ‘TATAPOWER’ is not a stock symbol, you should try something ‘AAPL’ for Apple or ‘TSLA’ for Tesla