Search code examples
pythonpandasfinancestocks

How to pull stock data for every stock on a given exchange


I'm using pandas to pull stock data. For example, the code below, basically lifted from examples found all around the web, works fine.

What I want to do is analyze the price data for every single stock listed on a given exchange. For example, in the code below, the list called "stocks" would ideally be populated with the ticker of every single stock ticker from the NYSE. I've found places where I can pull the stock tickers of all the components of the S&P 500 index, but no place where I can pull the tickers for every stock on a given exchange. I figure that must exist somewhere - hopefully in a place that pandas already plays nicely with. Very much appreciate any tips/advice.

import pandas.io.data as web
import datetime

start = datetime.datetime(2010,1,1)
end = datetime.datetime(2015,10,26)


stocks = ['F','IBM', 'WDC']
g=web.DataReader(stocks, 'yahoo', start, end)

h = g['Close']

# h contains the closing prices of every element of stocks
# for the time span from start to end 

Solution

  • To obtain the list of the tickers of US markets, you can use:

    # NYSE
    url_nyse = "http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nyse&render=download"
    # Nasdaq
    url_nasdaq = "http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nasdaq&render=download"
    # AMEX
    url_amex = "http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=amex&render=download"
    
    import pandas as pd
    
    df = pd.DataFrame.from_csv(url_nyse)
    stocks = df.index.tolist()