Created
October 27, 2021 23:05
-
-
Save marcosan93/a77c3f072fe5bf1c78cce709945bfa0f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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