Jarloo

Menu

Get Historical Stock Data From Yahoo

Need to download historical stock data? Need a large amount ie: since 1962? Well look no further, I got you covered!

How to Download Historical Stock Information from Yahoo

Yahoo has a secret API they use on there own pages that lets you display historical stock data. The API is REST based and very easy to use.

You need to specify each stock ticker your looking for and the start date you want the history from. (The oldest date appears to be around 1962.)

An example of an historical query on Apple (AAPL) looks like so:
http://ichart.finance.yahoo.com/table.csv?s=AAPL&c=1962

C# Historical Stock Downloading Code

To use the undocumented Yahoo! API to download historical stock data you will need to define a stock like so:

HistoricalStock.cs

using System;

namespace Jarloo.YahooHistoricalLoader
{
    public class HistoricalStock
    {
        public DateTime Date { get; set; }
        public double Open { get; set; }
        public double High { get; set; }
        public double Low { get; set; }
        public double Close { get; set; }
        public double Volume { get; set; }
        public double AdjClose { get; set; }
    }
}

The code to actually download and parse the results of the historical stock data, you will need something similiar to the C# code below:

HistoricalStockDownloader.cs

using System;
using System.Collections.Generic;
using System.Net;

namespace Jarloo.YahooHistoricalLoader
{
    public class HistoricalStockDownloader
    {
        public static List DownloadData(string ticker, int yearToStartFrom)
        {
            List retval = new List();

            using (WebClient web = new WebClient())
            {
                string data = web.DownloadString(string.Format("http://ichart.finance.yahoo.com/table.csv?s={0}&c={1}", ticker, yearToStartFrom));

                data =  data.Replace("r","");

                string[] rows = data.Split('n');

                //First row is headers so Ignore it
                for (int i = 1; i < rows.Length; i++)
                {
                    if (rows[i].Replace("n","").Trim() == "") continue;

                    string[] cols = rows[i].Split(',');

                    HistoricalStock hs = new HistoricalStock();
                    hs.Date = Convert.ToDateTime(cols[0]);
                    hs.Open = Convert.ToDouble(cols[1]);
                    hs.High = Convert.ToDouble(cols[2]);
                    hs.Low = Convert.ToDouble(cols[3]);
                    hs.Close = Convert.ToDouble(cols[4]);
                    hs.Volume = Convert.ToDouble(cols[5]);
                    hs.AdjClose = Convert.ToDouble(cols[6]);

                    retval.Add(hs);
                }

                return retval;
            }
        }
    }
}

Example of using the C# Historical Stock Downloader

Create a simple C# console application then add this code:

using System;
using System.Collections.Generic;

namespace Jarloo.YahooHistoricalLoader
{
    class Program
    {
        static void Main(string[] args)
        {
            List data = HistoricalStockDownloader.DownloadData("AAPL", 1962);

            foreach (HistoricalStock stock in data)
            {
                Console.WriteLine(string.Format("Date={0} High={1} Low={2} Open={3} Close{4}",stock.Date,stock.High,stock.Low,stock.Open,stock.Close));
            }

            Console.Read();
        }
    }
}

 

Categories:   Code, Finance

Comments

Sorry, comments are closed for this item.