Getting Stock Historical Data from API for a python project

16,190

Solution 1

Try Quandl. It's pretty simple and easy to use, but you must register and get an API key for some libraries.

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

If you want to specify a date range use the following

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]

Solution 2

The python yahoofinancials module can easily handle this for you. In addition to monthly, daily, etc. historical stock data, it can also get you all of the fundamental financial data for any company on yahoo finance (balance sheet, income statement, cash flow, ratios, etc.)

You can install it with pip install yahoofinancials. It doesn't rely on the closed Yahoo API and doesn't need a web driver configured to work. Plus no registration or API keys required. It works by hashing out the datastore object found in every yahoo finance web page.

You can pass in either a single stock/index ticker as a string or a list of ticker strings to pull multiple stocks/indexes at once. The data is also easy to put into a Pandas data-frame for analytics.

$ pip install yahoofinancials

Multi-ticker Pull Example:

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')

Usage Example:

from yahoofinancials import YahooFinancials

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

JSON Output:

{
    "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"
    }
}

Solution 3

There are a lot of services. For instance, making a Google search:

PAID

FREE

Check out also this list

Solution 4

You can get stock data from prepared datasets or scrape from other websites.

Here is where you can find it: https://github.com/eliangcs/pystock-data

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

Share:
16,190
Ayo
Author by

Ayo

Hi I like to ask questions mostly about contest math because I suck at them. I like coding also. (C/C++, Python, Java)

Updated on July 28, 2022

Comments

  • Ayo
    Ayo almost 2 years

    I am making a stock analysis program, but for that, I need the NYSE stock historical data API.

    I have searched everywhere, but could not find helpful solutions (i.e. Yahoo/Google Finance APIs are down).

    I tried the Alpha Vantage API, but it did not seem to output values for close/open values for each stock.

    Is there another way that I can access historical data by the masses without downloading every single stock's .csv directly from yahoo finance?

  • Ayo
    Ayo almost 6 years
    Thanks, but I need the latest data. The earliest I found from your link was 5 months old.
  • Ayo
    Ayo almost 6 years
    Hm, I did register and got the API key, but I can't seem to figure out how to output the date, opening value and closing value.
  • Ayo
    Ayo almost 6 years
    Thanks, IEX looks promising, but it looks like it returns JSON files only?
  • aydow
    aydow almost 6 years
    it will depend on the what library you are using. this one only shows Date and Value but others will have OLHC
  • Stefano
    Stefano almost 6 years
    Using WebSockets it returns JSON strings, not files. You could easily process those strings like python dictionaries.
  • Ayo
    Ayo almost 6 years
    I did get it to show all the values, but it seems like the data stops on 3/27 for every stock I tried which is not very ideal.
  • aydow
    aydow almost 6 years
    what library are you using?
  • Ayo
    Ayo almost 6 years
    This is the code I'm using, just imported quandl, not too sure about other libraries... import pandas as pd import os import quandl import time auth_tok = "TOKEN" data = quandl.get("WIKI/AMZN", trim_start = "2017-7-10", trim_end = "2018-7-10", authtoken=auth_tok) print(data)
  • Ayo
    Ayo almost 6 years
    Also, not too sure about how to use it. Do I just run for example, GET /stock/{symbol}/chart/{range} directly in my code with the parameters filled out? There seems to be nothing about this on the manual.
  • Stefano
    Stefano almost 6 years
    You should check here iextrading.com/developer/docs in order to answer your questions
  • Ayo
    Ayo almost 6 years
    but see, the dates end at 2018-03-27, not at 2018-7-10 like I specified.
  • Ayo
    Ayo almost 6 years
    That is where I'm looking, but it does not tell me if I should import anything, only commands to use in order to get the data.
  • aydow
    aydow almost 6 years
    that's because there's no data for 2018-7-10. it hasn't happened yet
  • Stefano
    Stefano almost 6 years
    If you are using REST GET you should use docs.python-requests.org/en/master otherwise check WebSockets client
  • Ayo
    Ayo almost 6 years
    oh my bad I thought the format for the date was year-month-day not year-day-month
  • Darcy
    Darcy almost 5 years
    Looks like "get_historical_stock_data" is no longer an option. All you can get is the price data (e.g. high/low) using "get_historical_price_data". Unless I am missing something obvious....
  • P A N
    P A N almost 5 years
    @Darcy What's the difference between "get_historical_stock_data" and "get_historical_price_data"?