Skip to content

Instantly share code, notes, and snippets.

@romanmichaelpaolucci
Created January 17, 2020 02:38
Show Gist options
  • Save romanmichaelpaolucci/12f2295be8ac08dedf812c6b1e28d0a5 to your computer and use it in GitHub Desktop.
Save romanmichaelpaolucci/12f2295be8ac08dedf812c6b1e28d0a5 to your computer and use it in GitHub Desktop.
Portfolio Data Request from Quandl
class PortfolioDataRequest:
""" stocks = [], start_date/end_date are strings 'YYYY-MM-DD' """
"""
Class for portfolfio optimization, downloads relevant portfolio data
and caches it for use in optimization without saving it locally.
"""
def __init__(self, stocks, start_date, end_date):
QuandlSocket()
data = quandl.get_table(
'WIKI/PRICES',
ticker=stocks,
qopts={'columns': ['date', 'ticker', 'adj_close']},
date={'gte': start_date, 'lte': end_date},
paginate=True
)
df = data.set_index('date')
self.table = df.pivot(columns='ticker')
# By specifying col[1] in below list comprehension
# You can select the stock names under multi-level column
self.table.columns = [col[1] for col in self.table.columns]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment