Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save marcosan93/a77c3f072fe5bf1c78cce709945bfa0f to your computer and use it in GitHub Desktop.

Select an option

Save marcosan93/a77c3f072fe5bf1c78cce709945bfa0f to your computer and use it in GitHub Desktop.
def getPrices(df, ticker):
"""
Gets the stock price at the time for each date in the financial statements for
the given ticker and dataframe of financial information.
"""
# Getting stock price at the time
prices = client.get_prices_eod(ticker, period='d')
prices = pd.DataFrame(prices).set_index('date')[['adjusted_close', 'close', 'volume']]
# Converting to date time
prices.index = pd.to_datetime(prices.index)
# Filling in missing price data
prices = prices.reindex(
pd.date_range(prices.index[0], prices.index[-1]),
method='ffill'
)
# Converting back to string for merging later
prices.index = prices.index.strftime("%Y-%m-%d")
price_dates = [i for i in prices.index if i in df.index]
prices = prices.loc[price_dates]
# Joining together
df = df.join(prices, how='outer')
return df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment