Skip to content

Instantly share code, notes, and snippets.

@marcosan93
Created September 13, 2021 00:30
Show Gist options
  • Select an option

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

Select an option

Save marcosan93/fdda9ca996e77ee32b74b3520c7376ad to your computer and use it in GitHub Desktop.
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