从 API 获取 python 项目的库存历史数据

Getting Stock Historical Data from API for a python project

我正在制作股票分析程序,但为此,我需要 NYSE 股票历史数据 API。

我到处搜索,但找不到有用的解决方案(即 Yahoo/Google 财务 API 已关闭)。

我尝试了 Alpha Vantage API,但它似乎没有输出每只股票的 close/open 值。

有没有其他方法可以让大众访问历史数据,而无需直接从雅虎财经下载每只股票的 .csv?

您可以从准备好的数据集中获取股票数据或从其他网站抓取。

您可以在此处找到它: https://github.com/eliangcs/pystock-data

https://www.kaggle.com/borismarjanovic/price-volume-data-for-all-us-stocks-etfs https://www.kaggle.com/camnugent/sandp500

尝试Quandl。它非常简单易用,但您必须注册并获得某些库的 API 密钥。

In [11]: mydata = quandl.get('WFE/INDEXES_NYSE')

In [12]: mydata.head(5)
Out[12]: 
               Value
Date                
2016-01-31   9632.70
2016-02-29   9559.53
2016-03-31  10207.38
2016-04-30  10436.92
2016-05-31  10441.00

如果要指定日期范围,请使用以下内容

In [37]: quandl.get("WIKI/AMZN", start_date="2017-7-10", end_date="2018-7-10")
Out[37]: 
                Open       High      Low     ...       Adj. Low  Adj. Close  Adj. Volume
Date                                         ...                                        
2017-07-10   985.000   999.4392   983.50     ...         983.50     996.470    3462884.0
2017-07-11   993.000   995.9900   983.72     ...         983.72     994.130    2947479.0
2017-07-12  1000.650  1008.5500   998.10     ...         998.10    1006.510    3491988.0
2017-07-13  1004.620  1006.8800   995.90     ...         995.90     999.855    2864533.0
2017-07-14  1002.400  1004.4500   996.89     ...         996.89    1001.810    2066362.0
2017-07-17  1004.690  1014.7500  1003.81     ...        1003.81    1010.040    3636801.0
2017-07-18  1006.000  1026.0300  1004.00     ...        1004.00    1024.380    3957892.0
2017-07-19  1025.000  1031.5900  1022.50     ...        1022.50    1026.870    2936902.0
2017-07-20  1031.590  1034.9700  1022.52     ...        1022.52    1028.700    2964341.0
2017-07-21  1021.280  1026.1000  1011.00     ...        1011.00    1025.670    2677517.0
2017-07-24  1028.340  1043.0100  1027.43     ...        1027.43    1038.950    3212499.0
2017-07-25  1038.050  1043.3300  1032.48     ...        1032.48    1039.870    2432328.0
2017-07-26  1043.200  1053.2000  1043.20     ...        1043.20    1052.800    2828980.0
2017-07-27  1069.550  1083.3100  1040.18     ...        1040.18    1046.000    9905158.0
2017-07-28  1012.140  1032.8500  1001.00     ...        1001.00    1020.040    7624498.0
2017-07-31  1019.050  1019.0500   987.02     ...         987.02     987.780    7246638.0
2017-08-01   996.110  1006.4000   991.58     ...         991.58     996.190    4421395.0
2017-08-02  1001.770  1003.2100   981.73     ...         981.73     995.890    4017780.0
2017-08-03   999.470   999.5000   984.59     ...         984.59     986.920    3203134.0
2017-08-04   989.680   991.6720   982.00     ...         982.00     987.580    2704026.0
2017-08-08   994.350   996.2800   985.79     ...         985.79     989.840    2847527.0
2017-08-09   982.600   988.0000   975.27     ...         975.27     982.010    3434077.0
2017-08-10   976.295   979.8600   954.68     ...         954.68     956.920    5560388.0
2017-08-11   960.000   970.3900   951.38     ...         951.38     967.990    3431423.0
2017-08-14   978.410   985.5000   976.19     ...         976.19     983.300    3008007.0
2017-08-15   988.900   991.7400   982.00     ...         982.00     982.740    2513139.0
2017-08-16   981.650   986.4605   973.22     ...         973.22     978.180    3096051.0
2017-08-17   977.840   977.8400   960.32     ...         960.32     960.570    3396045.0
2017-08-18   961.400   965.4300   954.65     ...         954.65     958.470    3248677.0
2017-08-21   957.570   961.2000   945.46     ...         945.46     953.290    3008450.0
...              ...        ...      ...     ...            ...         ...          ...
2018-02-13  1385.930  1419.7200  1383.53     ...        1383.53    1414.510    5858860.0
2018-02-14  1406.250  1452.0600  1403.36     ...        1403.36    1451.050    5881238.0
2018-02-15  1466.890  1468.9400  1436.84     ...        1436.84    1461.760    5598111.0
2018-02-16  1457.370  1465.8000  1446.56     ...        1446.56    1448.690    4410879.0
2018-02-20  1446.490  1488.7700  1446.49     ...        1446.49    1468.350    6388374.0
2018-02-21  1485.000  1503.4900  1478.92     ...        1478.92    1482.920    6216694.0
2018-02-22  1495.360  1502.5400  1475.76     ...        1475.76    1484.760    4732555.0
2018-02-23  1495.340  1500.0000  1486.50     ...        1486.50    1500.000    4327008.0
2018-02-26  1509.200  1522.8400  1507.00     ...        1507.00    1521.950    4909053.0
2018-02-27  1524.500  1526.7800  1507.21     ...        1507.21    1511.980    4708378.0
2018-02-28  1519.510  1528.7000  1512.00     ...        1512.00    1512.450    4426580.0
2018-03-01  1513.600  1518.4900  1465.00     ...        1465.00    1493.450    6835230.0
2018-03-02  1469.100  1501.0500  1455.01     ...        1455.01    1500.250    6587564.0
2018-03-05  1494.240  1525.3800  1481.00     ...        1481.00    1523.610    5233934.0
2018-03-06  1533.200  1542.1300  1528.00     ...        1528.00    1537.640    4561718.0
2018-03-07  1526.520  1545.9000  1522.51     ...        1522.51    1545.000    4174123.0
2018-03-08  1550.000  1554.8800  1545.25     ...        1545.25    1551.860    3512528.0
2018-03-09  1563.500  1578.9400  1559.08     ...        1559.08    1578.890    4417059.0
2018-03-12  1592.600  1605.3300  1586.70     ...        1586.70    1598.390    5115886.0
2018-03-13  1615.960  1617.5400  1578.01     ...        1578.01    1588.180    6427066.0
2018-03-14  1597.000  1606.4400  1590.89     ...        1590.89    1591.000    4164395.0
2018-03-15  1595.000  1596.9100  1578.11     ...        1578.11    1582.320    4026744.0
2018-03-16  1583.450  1589.4400  1567.50     ...        1567.50    1571.680    5145054.0
2018-03-19  1554.530  1561.6600  1525.35     ...        1525.35    1544.930    6376619.0
2018-03-20  1550.340  1587.0000  1545.41     ...        1545.41    1586.510    4507049.0
2018-03-21  1586.450  1590.0000  1563.17     ...        1563.17    1581.860    4667291.0
2018-03-22  1565.470  1573.8500  1542.40     ...        1542.40    1544.100    6177737.0
2018-03-23  1539.010  1549.0200  1495.36     ...        1495.36    1495.560    7843966.0
2018-03-26  1530.000  1556.9900  1499.25     ...        1499.25    1555.860    5547618.0
2018-03-27  1572.400  1575.9600  1482.32     ...        1482.32    1497.050    6793279.0

[179 rows x 12 columns]

有很多服务。 例如,进行 Google 搜索:

已支付

免费

也看看这个 list

python yahoofinancials 模块可以轻松地为您处理这个问题。除了每月、每日等历史股票数据外,它还可以为您提供雅虎财经上任何一家公司的所有基本财务数据(余额sheet、损益表、现金流量、比率等)

您可以使用 pip install yahoofinancials 安装它。它不依赖于封闭的 Yahoo API,也不需要配置网络驱动程序即可工作。此外,无需注册或 API 密钥。它通过散列在每个雅虎金融网页中找到的数据存储对象来工作。

您可以将单个 stock/index 代码作为字符串传递,也可以传递代码字符串列表以一次提取多个 stocks/indexes。数据也很容易放入 Pandas 数据框进行分析。

$ pip install yahoofinancials

多代码拉取示例:

from yahoofinancials import YahooFinancials

tech_stocks = ['AAPL', 'MSFT', 'INTC']
bank_stocks = ['WFC', 'BAC', 'C']

yahoo_financials_tech = YahooFinancials(tech_stocks)
yahoo_financials_banks = YahooFinancials(bank_stocks)

tech_cash_flow_data_an = yahoo_financials_tech.get_financial_stmts('annual', 'cash')
bank_cash_flow_data_an = yahoo_financials_banks.get_financial_stmts('annual', 'cash')

banks_net_ebit = yahoo_financials_banks.get_ebit()
tech_stock_price_data = tech_cash_flow_data.get_stock_price_data()
daily_bank_stock_prices = yahoo_financials_banks.get_historical_stock_data('2008-09-15', '2017-09-15', 'daily')

用法示例:

from yahoofinancials import YahooFinancials

yahoo_financials = YahooFinancials('WFC')
print(yahoo_financials.get_historical_stock_data("2017-09-10", "2017-10-10", "monthly"))

JSON 输出:

{
    "WFC": {
        "prices": [
            {
                "volume": 260271600,
                "formatted_date": "2017-09-30",
                "high": 55.77000045776367,
                "adjclose": 54.91999816894531,
                "low": 52.84000015258789,
                "date": 1506830400,
                "close": 54.91999816894531,
                "open": 55.15999984741211
            }
        ],
        "eventsData": [],
        "firstTradeDate": {
            "date": 76233600,
            "formatted_date": "1972-06-01"
        },
        "isPending": false,
        "timeZone": {
            "gmtOffset": -14400
        },
        "id": "1mo15050196001507611600"
    }
}