Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download history stock prices automatically from yahoo finance in python

Is there a way to automatically download historical prices of stocks from yahoo finance or google finance (csv format)? Preferably in Python.

like image 866
Bob Avatar asked Sep 14 '12 23:09

Bob


People also ask

How do you download stock price data in Python?

You can use pandas_datareader or yfinance module to get the data and then can download or store in a csv file by using pandas. to_csv method. If yfinance is not installed on your computer, then run the below line of code from your Jupyter Notebook to install yfinance.


2 Answers

When you're going to work with such time series in Python, pandas is indispensable. And here's the good news: it comes with a historical data downloader for Yahoo: pandas.io.data.DataReader.

from pandas.io.data import DataReader from datetime import datetime  ibm = DataReader('IBM',  'yahoo', datetime(2000, 1, 1), datetime(2012, 1, 1)) print(ibm['Adj Close']) 

Here's an example from the pandas documentation.

Update for pandas >= 0.19:

The pandas.io.data module has been removed from pandas>=0.19 onwards. Instead, you should use the separate pandas-datareader package. Install with:

pip install pandas-datareader 

And then you can do this in Python:

import pandas_datareader as pdr from datetime import datetime  ibm = pdr.get_data_yahoo(symbols='IBM', start=datetime(2000, 1, 1), end=datetime(2012, 1, 1)) print(ibm['Adj Close']) 

Downloading from Google Finance is also supported.

There's more in the documentation of pandas-datareader.

like image 104
Def_Os Avatar answered Sep 18 '22 18:09

Def_Os


Short answer: Yes. Use Python's urllib to pull the historical data pages for the stocks you want. Go with Yahoo! Finance; Google is both less reliable, has less data coverage, and is more restrictive in how you can use it once you have it. Also, I believe Google specifically prohibits you from scraping the data in their ToS.

Longer answer: This is the script I use to pull all the historical data on a particular company. It pulls the historical data page for a particular ticker symbol, then saves it to a csv file named by that symbol. You'll have to provide your own list of ticker symbols that you want to pull.

import urllib  base_url = "http://ichart.finance.yahoo.com/table.csv?s=" def make_url(ticker_symbol):     return base_url + ticker_symbol  output_path = "C:/path/to/output/directory" def make_filename(ticker_symbol, directory="S&P"):     return output_path + "/" + directory + "/" + ticker_symbol + ".csv"  def pull_historical_data(ticker_symbol, directory="S&P"):     try:         urllib.urlretrieve(make_url(ticker_symbol), make_filename(ticker_symbol, directory))     except urllib.ContentTooShortError as e:         outfile = open(make_filename(ticker_symbol, directory), "w")         outfile.write(e.content)         outfile.close() 
like image 26
Joe C. Avatar answered Sep 17 '22 18:09

Joe C.