Download Historical Stock Data Using Yahoo API In ASP.NET

Download Historical Stock Data Using Yahoo API In ASP.NET


 

Download Source Code - Download Stock Data

historical_stock_data
The demo ASP.NET C# project downloads historical stock data from Yahoo Finance for any valid specified date range and stock symbol. The provided C Sharp solution retrieves a CSV file from Yahoo then displays historical market data in a GridView control that’s been styled with CSS.

The Default.aspx page includes two controls that that allow you to entered a Start Date and End Date for the historical period of interest. The projects validates that the dates entered by the user are valid calendar dates. In addition, the Start Date must be a in the past. Also, the End Date must be grater than the Start Date. Finally, the issue symbol field on the main page cannot be blank or null. All field validation is done in the code-behind page for the project.

Once validation is complete, the Yahoo API URL string is built based on the following:

//Build URL string
string baseURL = “http://ichart.yahoo.com/table.csv?”;
string queryText = BuildHistoricalDataRequest(symbol, dtStart, dtEnd);
string url = string.Format(“{0}{1}”, baseURL, queryText);

An HttpWebRequest object is used to call the Yahoo API then an HttpWebResponse object is used to grab the CSV file returned from Yahoo Finance. This particular request returns Daily Historical Quotes, which include Open, High, Low, Close, Volume, and the Adjusted Close. The data is sorted by trade date in descending order.

The market data is retrieved from the CSV file then stored in a DataTable. The DataTable is bound to a GridView control on the Default.aspx page so that the data can be viewed in a user-friendly format.

This C# ASP.NET solution processes the historical market data without storing it in a database. However, you can easily change the provided demo to store the downloaded data into a database.

If you’re only interested in getting the latest quote for a stock, reference our Get Stock Quotes solution.

You can get the full ASP.NET source code for this project by selecting the download link below.

Download Source Code - Download Stock Data