In [1]:
import collections
#two third party libraries. Install via command line via pip.
#i.e. "pip install ystockquote" and "pip install pandas"
import ystockquote
import pandas
In [2]:
def get_price_history(ticker, start_date, end_date):
"""
example usage:
get_price_history('appl','2014-11-01','2014-11-30')
returns a pandas dataframe
"""
data = ystockquote.get_historical_prices(ticker, start_date, end_date)
df = pandas.DataFrame(collections.OrderedDict(sorted(data.items()))).T
df = df.convert_objects(convert_numeric=True)
return df
In [3]:
get_price_history('aapl','2014-10-31','2014-11-04')
Out[3]:
In [4]:
get_price_history('aapl','2014-10-31','2014-11-04')['Adj Close']
Out[4]:
In [5]:
get_price_history('aapl','2012-10-31','2014-11-04')['Adj Close'].plot()
Out[5]:
No comments:
Post a Comment