任何人都知道如何在ASP.NET中免费获取历史股票数据?

时间:2010-08-22 05:53:31

标签: asp.net api stocks

我正在寻找可以通过C#实现访问的API,我可以访问免费的股票市场历史信息(指数和个别公司)。

5 个答案:

答案 0 :(得分:1)

我同意您可以简单地解析从Yahoo / Google或类似网站下载的数据。如果您只对每日(eod)数据感兴趣,可以免费下载并使用应用程序中此historical data provider的数据。可以使用文档和即用型C#和VB.NET示例。

答案 1 :(得分:0)

答案 2 :(得分:0)

我有几个C# examples on my blog用于从雅虎获取历史数据。这真的很简单......

更新

关于我的例子......我没有将数据保存到任何东西,我只是在控制台中打印。您必须以适合您的任何格式或数据结构保存数据。

// 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);
    }
}

请注意,您可以在同一个csv文件中请求多个标记,而不是一次请求一个标记...为此,只需将所有感兴趣的标记串在一起并将其添加到URL,就像添加单个标记一样标签。标签的值将以逗号分隔。

更新2.0

以下是如何从雅虎获取结束日期(EOD)历史数据:

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();
}

上面的代码将遍历CSV文件中的每个数据实例,因此您只需将数据实例保存到数组中。从那时起,计算回报应该是直接的。

// 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...

我希望这有用:)。

答案 3 :(得分:0)

http://www.mergent.com/servius

查看Mergent Historical Securities Data API

答案 4 :(得分:0)

作为软件开发人员,我建议Alpha Vantage。它们以 RESTful JSON API 提供实时和历史股票报价(每日,每周,每月等)。

无限制的API调用完全免费。只要股票在主要证券交易所上市,它就是实时的。

Here是MSFT每日价格和交易量的示例API调用,丰富了拆分/股息调整。最新数据点是当前交易日的实时信息。

他们还根据他们的文档在市场数据之上提供技术分析API。