I am trying to create growth rates for thirty days after download the data from yahoo finance. I used the following codes and a function but my output prints the same numbers for all days ( 1-30) days of growth. any help is appreciated.
# Extracting data from yahoo finance
import pandas as pd
import requests
import yfinance as yf
import numpy as np
import matplotlib.pyplot as plt
import time
import datetime
symbols = ['MSFT', 'HD', 'TSLA']
stocks_df = pd.DataFrame({'A' : []})
symbols_daily = yf.download(symbols,
period = "max",
interval = "1d")
for i,ticker in enumerate(symbols):
print(i,ticker)
# Work with stock prices
historyPrices = yf.download(tickers = ticker,
period = "max",
interval = "1d")
# generate features for historical prices, and what we want to predict
historyPrices['Ticker'] = ticker
historyPrices['Year']= historyPrices.index.year
historyPrices['Month'] = historyPrices.index.month
historyPrices['Weekday'] = historyPrices.index.weekday
historyPrices['Date'] = historyPrices.index.date
if stocks_df.empty:
stocks_df = historyPrices
else:
stocks_df = pd.concat([stocks_df, historyPrices], ignore_index=True) and
I created the function:
**# function to create growth**
def get_growth_df(df:pd.DataFrame)->pd.DataFrame:
for i in range(1, 31):
df['future_growth_'+str(i)+'d'] = df['Adj Close'].shift(-1) / df['Adj Close']
GROWTH_KEYS = [k for k in df.columns if k.startswith('future_growth_')]
return df[GROWTH_KEYS]
get_growth_df(historyPrices).describe()
it gives me the same values for each of the thirty days.
I am expecting something like this for each of the thirty days growth.
enter image description here
New contributor
Prash is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.