I have been struggling to try to nail down this coding dilemma. I have built a program that models and forecasts individual stock symbols. It works great, but I'm ready to take it to the next level where a user like myself can forecast multiple stock symbols at one go, instead of running the application multiples time for different stocks. It's a time-consuming process running my program for 10+ different stocks individually, for efficiency reasons, I would much rather create a list of stocks for my program to run on.
Below is my code which pulls stock data from yfinance and converts it to a dataframe.
#stock symbol
ticker = "FB"
# mm-dd-yy formate
start_date = "01-01-2014"
end_date = "11-20-2019"
#the code to pull data from yfinance
df = pdr.get_data_yahoo(ticker, start=start_date, end=end_date )
The problem I keep running into is, I don't know how to automate the creation of data-frames properly with each having a unique name that I can pass through in a list into my model. (is that even possible, setting up a list of data frames for my model to pass through?)
This is what I have in mind:
#stock symbols (I want to insert as many symbols as I can to automate my workflow. this example uses 4 stocks, but one day I might test 10 or 15)
tickers = ["FB","D","COF","WING"]
# mm-dd-yy formate
start_date = "01-01-2014"
end_date = "11-20-2019"
#the code to pull data from yfinance
for stock in tickers:
df = pdr.get_data_yahoo(tickers, start=start_date, end=end_date)
return df
I struggle with advanced loops. I do not know how to pass multiple strings of tickers
into the pdr.get_data_yahoo()
with a DF as each individual outcome for each stock. The Dataframes needs to be uniquely named, nothing too complex, and homogeneous. For example, I would need this application to work if I wanted to test for 1 stock, or 20 at a time, while I pass multiple data frames in a loop for my model to evaluate.
I would greatly appreciate some guidance here.