【问题标题】:Anyone know how to get historical stock data for free in ASP.NET? [closed]有人知道如何在 ASP.NET 中免费获取历史股票数据吗? [关闭]
【发布时间】:2010-08-22 05:53:30
【问题描述】:

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

【问题讨论】:

标签: asp.net api stocks


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我有几个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...
    

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

    【讨论】:

    • 您知道这些数据是否有任何发布限制?
    • @cfarm54,“发布限制”是什么意思?
    • 雅虎对您如何发布数据有法律限制吗?另外,在您的 while 循环中,您介意解释一下您是如何保存数据的吗?
    • @cfarm,我认为雅虎对您对数据的处理方式没有任何限制,但请不要引用我的话 :)。如果您不将其用于商业目的,即不出售数据,那么我认为您不会有问题。我将更新关于 while 循环的答案。
    • @Lirik 是的,那会很好。我最终要做的是拉下任何股票的历史数据,然后计算每日收益并将这些收益推送到数组中。关于如何使用您的代码实现这一点的任何线索?
    【解决方案3】:
    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

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

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-25
        • 2010-10-19
        • 2011-09-14
        • 2015-03-03
        • 2016-08-13
        • 2011-03-23
        • 1970-01-01
        • 2023-03-10
        相关资源
        最近更新 更多