Intro:
From yahoo you can get EOD (end of day) historical prices, or real-time prices. The EOD prices are amazingly simple to download. See my blog for explanations on how to get the data and for C# code examples.
I'm in the process of writing a real-time data feed "engine" that downloads and stores the real-time prices in a database. The engine will initially be able to download historical prices from Yahoo and Interactive Brokers and it will be able to store the data in a database of your choice: MS SQL, MySQL, SQLite, etc. It's open source, but I'll post more information on my blog when I get closer to releasing it (within a couple of days).
Another option is eclipse trader... it allows you to record the historical data with granularity as low as 1 minute and stores the prices locally in a text file. It basically downloads the real-time data from Yahoo with a 15 minute delay. Since I wanted a more robust solution and I'm working on a big school project for which we need data, I decided to write my own data feed engine (which I mentioned above).
Sample Code:
Here is sample C# code that demonstrates how to download real-time data:
public void Start()
{
string url = "http://finance.yahoo.com/d/quotes.csv?s=MSFT+GOOG&f=snl1d1t1ohgdr";
//Get page showing the table with the chosen indices
HttpWebRequest request = null;
IDatabase database =
DatabaseFactory.CreateDatabase(
DatabaseFactory.DatabaseType.SQLite);
//csv content
try
{
while (true)
{
using (Stream file = File.Create("quotes.csv"))
{
request = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
request.Timeout = 30000;
using (var response = (HttpWebResponse)request.GetResponse())
using (Stream input = response.GetResponseStream())
{
CopyStream(input, file);
}
}
Console.WriteLine("------------------------------------------------");
database.InsertData(Directory.GetCurrentDirectory() + "/quotes.csv");
File.Delete("quotes.csv");
Thread.Sleep(10000); // 10 seconds
}
}
catch (Exception exc)
{
Console.WriteLine(exc.ToString());
Console.ReadKey();
}
}
Database:
On the database side I use an OleDb
connection to the CSV file to populate a DataSet
and then I update my actual database via the DataSet
, it basically makes it possible to match all of the columns from the CSV file returned from Yahoo directly to your database (if your database does not support batch inserts of CSV data, like SQLite). Otherwise, inserting the data is a one-liner... just batch insert the CSV into your database.
You can read more about the formatting of the url here: http://www.gummy-stuff.org/Yahoo-data.htm