I'm looking for an API that can be accessed via C# implementation where I can get access to free stock market historical information (index and individual companies).
-
possible duplicate of [Stock Trading script/language for .NET?](http://stackoverflow.com/questions/3222357/stock-trading-script-language-for-net) – Shimmy Weitzhandler Aug 22 '10 at 06:03
-
possible duplicate of [Where can I get free real-time stock data?](http://stackoverflow.com/questions/281263/where-can-i-get-free-real-time-stock-data) – monksy Aug 30 '10 at 08:00
-
The easiest way is to use the undocumented Yahoo historical stock functions. I have an example here: http://jarloo.com/code/api-code/get-historical-stock-data/ – Kelly Mar 12 '11 at 03:31
-
Question shows no sign of prior research. What have you tried? What have you researched? – Nov 13 '14 at 11:08
5 Answers
I agree that you can simply parse the data downloaded from Yahoo/Google or similar sites. If you only interested in daily (eod) data, you may download and use data from this historical data provider in your application for free. Documentation and ready-to-use C# and VB.NET examples are available.

- 31
- 1
I have a couple of C# examples on my blog for getting historical data from Yahoo. It's really simple...
Update
Regarding my example... I'm not saving the data to anything, I'm just printing in the console. You'd have to save the data in whatever format or data structure is most reasonable for you.
// A dictionary with tags where the key is the tag
// and the value is the description of the tag
private Dictionary<string, string> _tags = new Dictionary<string, string>();
private void DownloadData(String symbol)
{
string url = String.Format(
"http://finance.yahoo.com/d/quotes.csv?s={0}&f=", symbol);
//Get page showing the table with the chosen indices
HttpWebRequest request = null;
DFDataSet ds = new DFDataSet();
Random rand = new Random(DateTime.Now.Millisecond);
try
{
while (_running)
{
foreach (String key in _tags.Keys)
{
lock (_sync)
{
request = (HttpWebRequest)WebRequest.CreateDefault(
new Uri(url + key));
request.Timeout = 30000;
using (var response = (HttpWebResponse)request.GetResponse())
using (StreamReader input = new StreamReader(
response.GetResponseStream()))
{
Console.WriteLine(String.Format("{0} {1} = {2}",
symbol, _tags[key], input.ReadLine());
}
}
}
Console.WriteLine(Thread.CurrentThread.Name + " running.");
Thread.Sleep(60*1000); // 60 seconds
}
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
}
Note that you can request multiple tags in the same csv file, instead of one tag at a time... to do that, just string all the tags of interest together and add them to the URL just like you add a single tag. The values for the tags will be comma separated.
Update 2.0
Here is how you can get end of day (EOD) historical data from yahoo:
void DownloadDataFromWeb(string symbol)
{
DateTime startDate = DateTime.Parse("1900-01-01");
string baseURL = "http://ichart.finance.yahoo.com/table.csv?";
string queryText = BuildHistoricalDataRequest(symbol, startDate, DateTime.Today);
string url = string.Format("{0}{1}", baseURL, queryText);
//Get page showing the table with the chosen indices
HttpWebRequest request = null;
HttpWebResponse response = null;
StreamReader stReader = null;
//csv content
string docText = string.Empty;
string csvLine = null;
try
{
request = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
request.Timeout = 300000;
response = (HttpWebResponse)request.GetResponse();
stReader = new StreamReader(response.GetResponseStream(), true);
stReader.ReadLine();//skip the first (header row)
while ((csvLine = stReader.ReadLine()) != null)
{
string[] sa = csvLine.Split(new char[] { ',' });
DateTime date = DateTime.Parse(sa[0].Trim('"'));
Double open = double.Parse(sa[1]);
Double high = double.Parse(sa[2]);
Double low = double.Parse(sa[3]);
Double close = double.Parse(sa[4]);
Double volume = double.Parse(sa[5]);
Double adjClose = double.Parse(sa[6]);
// Process the data (e.g. insert into DB)
}
}
catch (Exception e)
{
throw e;
}
}
string BuildHistoricalDataRequest(string symbol, DateTime startDate, DateTime endDate)
{
// We're subtracting 1 from the month because yahoo
// counts the months from 0 to 11 not from 1 to 12.
StringBuilder request = new StringBuilder();
request.AppendFormat("s={0}", symbol);
request.AppendFormat("&a={0}", startDate.Month-1);
request.AppendFormat("&b={0}", startDate.Day);
request.AppendFormat("&c={0}", startDate.Year);
request.AppendFormat("&d={0}", endDate.Month-1);
request.AppendFormat("&e={0}", endDate.Day);
request.AppendFormat("&f={0}", endDate.Year);
request.AppendFormat("&g={0}", "d"); //daily
return request.ToString();
}
The code above will go through each data instance in the CSV file, so you just need to save the data instances to arrays. Calculating the return should be straight forward from then on.
// Create your data lists
List<DateTime> date = new List<DateTime>();
List<Double> open = new List<Double>();
List<Double> high = new List<Double>();
List<Double> low = new List<Double>();
List<Double> close = new List<Double>();
List<Double> volume = new List<Double>();
List<Double> adjClose = new List<Double>();
//
// ...
//
// inside the DownloadDataFromWeb function:
// Add the data points as you're going through the loop
date.Add(DateTime.Parse(sa[0].Trim('"')));
open.Add(double.Parse(sa[1]));
high.Add(double.Parse(sa[2]));
low.Add(double.Parse(sa[3]));
close.Add(double.Parse(sa[4]));
volume.Add(double.Parse(sa[5]));
adjClose.Add(double.Parse(sa[6]));
//
// ...
//
// Calculate the return after you've downloaded all the data...
I hope that's helpful :).

- 39,672
- 31
- 167
- 226
-
-
-
does yahoo have legal restrictions on how you publish the data? also, in your while loop, would you mind explaining how you are saving the data? – locoboy Aug 22 '10 at 16:39
-
@cfarm, I don't think Yahoo has any restrictions on what you do with the data, but don't quote me on it :). If you're not using it for commercial purposes, i.e. not selling the data, then I don't think you'll have a problem. I'll update my answer regarding the while loop. – Kiril Aug 22 '10 at 16:47
-
@Lirik yeah that would be nice. what I'm ultimately trying to do is pull down historical data for any stock, then calculate the daily returns and push those returns to an array. Any clue on how I would be able to implement this using your code? – locoboy Aug 22 '10 at 16:50
-
@cfarm54, yep, you can pull down the historical data for any stock on Yahoo (end of day data), it's part of my first post on EOD data. Calculating the returns and pushing them to an array should be easy after that (I'll update again). – Kiril Aug 22 '10 at 17:01
-
@Lirik thanks. is it the same for pulling down index data? e.g. S&P500, NASDAQ, DOW – locoboy Aug 22 '10 at 18:34
-
@cfarm54, yep, it's the same... you just have to get the right symbol: NASDAQ is ^IXIC, S&P500 is ^GSPC, DOW is ^DJI, etc... – Kiril Aug 22 '10 at 20:56
-
@Lirik thanks. do you know how to make these requests using silverlight? – locoboy Aug 23 '10 at 23:09
-
@cfarm54, not sure about silverlight... I've never used it, but I would assume that the same code should work (silverlight is another framework that uses C#, right?). If not, just look up the basic web request and duplicate the logic. – Kiril Aug 24 '10 at 03:03
-
@Lirik yeah it's not the same because you need to make asynchronous requests. I've never worked with those before have you? – locoboy Aug 24 '10 at 15:36
-
@cfarm54, I've work with asynchronous requests, but never in the context of Silverlight... I imagine it should be pretty straight forward, take a look at this [Silverlight WebRequest Example](http://tiny.cc/5ppxu) and work my logic into the response. – Kiril Aug 24 '10 at 22:20
-
-
Even the questions are not .NET classes, you should simply parse the data and use it with .NET:

- 1
- 1

- 101,809
- 122
- 424
- 632
Take a look at the Mergent Historical Securities Data API at http://www.mergent.com/servius

- 6,443
- 2
- 38
- 59
As a software developer, I would recommend Alpha Vantage. They offer realtime and historical stock quotes (daily, weekly, monthly, etc.) as RESTful JSON APIs.
It’s completely free with unlimited API calls. It’s realtime as long as the stock is listed on major stock exchanges.
Here is an example API call for the MSFT daily prices and volumes, enriched with split/dividend adjustments. The latest data point is the realtime information for the current trading day.
They also offer technical analysis APIs on top of the market data according to their documentation.

- 186
- 1
- 5