1

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

4

5 回答 5

1

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

于 2012-04-09T06:00:55.650 回答
0

我的博客上有几个C# 示例,用于从 Yahoo 获取历史数据。这真的很简单...

更新

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

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

我希望这会有所帮助:)。

于 2010-08-22T14:37:00.863 回答
0

查看http://www.mergent.com/servius上的 Mergent 历史证券数据 API

于 2010-10-19T06:21:24.980 回答
0

作为一名软件开发人员,我会推荐Alpha Vantage他们以RESTful JSON API的形式提供实时和历史股票报价(每日、每周、每月等)。

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

以下是 MSFT 每日价格和交易量的示例 API 调用,其中包含拆分/股息调整。最新数据点是当前交易日的实时信息。

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

于 2017-03-13T22:35:40.657 回答