Step 1: Building the stock names we want to download
The program requests the user to enter the stock-tickers and date range they would like to download, or it will use its default values. The stock-tickers must be separated by a comma and listed on the yahoo finance website - finance.yahoo.com. When the default parameters are used the program will download the entire S&P500 stock-tickers, from the date range of 1st January 2000 to 1st January 2014. The list of S&P500 stock-tickers is pulled from a CSV file in the program folder and was originally sourced from Wikipedia. If you have a different set of symbols you would like to download you should modify this file with your own stock-tickers. Pseudo code:Console.WriteLine("Enter stock names. If blank, all SP500 Stocks will be downloaded"); string userStocks = Console.ReadLine(); if (userStocks.Length == 0) { // Download all S&P500 stocks } else { // Read user input and download those stocks }
Step 2: Downloading from Yahoo Finance API
For downloading the CSV files with historical prices from Yahoo Finance’s API, we need the following URL structure:Every url starts as:
https://ichart.yahoo.com/table.csv?s=
Then, it needs a stock name (e.g.: Microsoft):
https://ichart.yahoo.com/table.csv?s=MSFT
A From Date (e.g.: 01/01/2000):
https://ichart.yahoo.com/table.csv?s=MSFT&a=0&b=1&c=2000
A To Date (e.g.: 24/12/2014):
https://ichart.yahoo.com/table.csv?s=MSFT&a=0&b=1&c=2000&d=11&e=24&f=2014
Resolution of the data (daily, weekly, etc):
https://ichart.yahoo.com/table.csv?s=MSFT&a=0&b=1&c=2000&d=11&e=24&f=2014&g=w
File format:
https://ichart.yahoo.com/table.csv?s=MSFT&a=0&b=1&c=2000&d=11&e=24&f=2014&g=w&ignore=.csv
We construct this url-address from variables inside the program, so that it changes for each value of the stock-ticker and the dates required (that were built in Step 1). Months are written indexed from zero: so 0 is January, 11 is December. We will save all the files to separate directories. The first time the program runs these directories do not exist, so we must create them. The following code checks if the directory exists, and creates it if it doesn't:// Create the directories in case they don't exist if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); Console.WriteLine("Directory created."); }We don’t want to waste time in downloading files that we may already have downloaded. Therefore, we build a condition that checks if the file already exists in the directory. We use the .NET WebClient class to process the download request and save it directly to a file.
// If the file has not been downloaded yet: if (!File.Exists(dayFile)) { webClient.DownloadFile(url, dayFile); Console.WriteLine(stock + " Data downloaded successfully!"); } else { //If the file already exists in the directory Console.WriteLine( stock + " file already exists"); }Its important to wrap the WebClient in a "Try-Catch" loop to handle 404 or web time out exceptions. This code downloads the files separated in folders according to its data resolution (daily or weekly bars), for each stock. It takes a while if you are downloading all the S&P500 stocks, but it is great fun! Hope you like the app, I really encourage beginners in C# to try code it yourself, it took me a while but the results are great. Feel free to post your questions, and goodluck learning C#! Feel free to download the source code here: C# Yahoo Finance Update: Thanks to Jonathan Evans for his suggested updates to the code, they are now included in the download file.
Raul
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!