Reputation:
Am trying to create a program that fetches data from yahoo finance when a user provides an ticker, start date and end date. How do you get the data for a particular user entered stock ticker from yahoo finance?
I tried the following but this doesn't work:
base_url = "http://ichart.finance.yahoo.com/"
def make_url(ticker_symbol,start_date, end_date):
print ticker_symbol
a = start_date
b = end_date
dt_url = '%s&a=%d&b=%d&c=%d&d=%d&e=%d&f=%d&g=d&ignore=.csv'% (ticker_symbol, a.month-1, a.day, a.year, b.month-1, b.day,b.year)
return base_url + dt_url
Upvotes: 1
Views: 2038
Reputation: 21
I recommend pandas_datareader
. Check this link: http://pandas-datareader.readthedocs.io/en/stable/remote_data.html#yahoo-finance
Here's the code I would use to answer your question:
[python3] C:\OneDrive\Todd\projects\Miniconda2\Scripts>python
Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pandas_datareader import data
>>> from datetime import datetime
>>> def get_stock_data():
... symbol = input("Stock symbol: ");
... start_date_str = input("Start date (mm/dd/yyyy): ");
... end_date_str = input("End date (mm/dd/yyyy): ");
... start_date = datetime.strptime(start_date_str,"%m/%d/%Y");
... end_date = datetime.strptime(end_date_str,"%m/%d/%Y");
... df = data.DataReader(symbol, 'yahoo', start_date, end_date);
... return(df);
...
>>> dis=get_stock_data();
Stock symbol: dis
Start date (mm/dd/yyyy): 08/01/2016
End date (mm/dd/yyyy): 08/31/2016
>>> dis.head(2)
Open High Low Close Volume Adj Close
Date
2016-08-01 96.150002 96.199997 95.080002 95.540001 7131600 95.540001
2016-08-02 95.349998 95.750000 94.559998 95.010002 7644100 95.010002
>>> dis.tail(2)
Open High Low Close Volume Adj Close
Date
2016-08-30 94.769997 95.290001 94.769997 94.860001 6845000 94.860001
2016-08-31 94.690002 94.900002 94.160004 94.459999 7375200 94.459999
>>> spy=get_stock_data()
Stock symbol: spy
Start date (mm/dd/yyyy): 08/01/2016
End date (mm/dd/yyyy): 08/31/2016
>>> spy.head(5)
Open High Low Close Volume \
Date
2016-08-01 217.190002 217.649994 216.410004 216.940002 73311400
2016-08-02 216.649994 216.830002 214.570007 215.550003 92295500
2016-08-03 215.479996 216.250000 215.130005 216.179993 53993600
2016-08-04 216.309998 216.779999 214.250000 216.410004 46585500
2016-08-05 216.410004 218.229996 216.410004 218.179993 71892200
Adj Close
Date
2016-08-01 216.940002
2016-08-02 215.550003
2016-08-03 216.179993
2016-08-04 216.410004
2016-08-05 218.179993
>>> spy.tail(5)
Open High Low Close Volume \
Date
2016-08-25 217.399994 218.190002 217.220001 217.699997 69224800
2016-08-26 217.919998 219.119995 216.250000 217.289993 122506300
2016-08-29 217.440002 218.669998 217.399994 218.360001 70502200
2016-08-30 218.259995 218.589996 217.350006 218.000000 58114500
2016-08-31 217.610001 217.750000 216.470001 217.380005 85269500
Adj Close
Date
2016-08-25 217.699997
2016-08-26 217.289993
2016-08-29 218.360001
2016-08-30 218.000000
2016-08-31 217.380005
>>> exit()
[python3] C:\OneDrive\Todd\projects\Miniconda2\Scripts>
If the code above doesn't work you probably need to install some modules:
#Installing Anaconda: https://docs.continuum.io/anaconda/install#anaconda-for-windows-install
#Installing Python 3.3 on Anaconda Python for Windows: http://www.walkingrandomly.com/?p=5089
## NOTE: I named my python 3 environment "python3", not "py3", so my commands vary slightly from those at the website above
#Installing pandas: conda install -c https://conda.anaconda.org/anaconda pandas
#Installing datareader: conda install -c https://conda.anaconda.org/anaconda pandas-datareader
Once you've installed everything you can then confirm all is well with the following:
C:\OneDrive\Todd\projects\Miniconda2\Scripts>activate python3
Deactivating environment "C:\OneDrive\Todd\projects\Miniconda2"...
Activating environment "C:\OneDrive\Todd\projects\Miniconda2\envs\python3"...
[python3] C:\OneDrive\Todd\projects\Miniconda2\Scripts>python
Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>help('module pandas_datareader') #Confirm the module is installed
Here is a list of modules whose name or summary contains 'pandas_datareader'.
If there are any, enter a module name to get more help.
...<several items listed, including...>
pandas_datareader.data - Module contains tools for collecting data from various remote sources
pandas_datareader.yahoo
pandas_datareader.yahoo.actions
pandas_datareader.yahoo.components
pandas_datareader.yahoo.daily
pandas_datareader.yahoo.options
pandas_datareader.yahoo.quotes
Now you're ready to get stock market data from Yahoo! finance.
Upvotes: 1
Reputation: 7358
I think the best way to do this is to use 'pandas'.
from pandas.io.data import DataReader
data = DataReader('AAPL','yahoo',start = '1950-1-1', end = '2013-12-1')
The data object is a pandas DataFrame which is very easy to manipulate and work with
Upvotes: 2