Created
September 13, 2021 00:30
-
-
Save marcosan93/fdda9ca996e77ee32b74b3520c7376ad 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 getStockPrices(ticker, start, end): | |
| """ | |
| Gets the historical daily prices between two dates. Applies the logarithmic function | |
| to return a dataframe of log returns each day. | |
| """ | |
| # Setting the stock | |
| stock = yf.Ticker(ticker) | |
| # Getting historical prices | |
| stock_df = stock.history(start=end, end=start, interval="1d")[['Close']] | |
| # Getting the daily log returns | |
| stock_df = stock_df.apply(np.log).diff().dropna() | |
| # Some reformatting | |
| stock_df = stock_df.reset_index().rename( | |
| columns={ | |
| "Date": "date", | |
| "Close": "log returns" | |
| } | |
| ) | |
| return stock_df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment