0

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.

Sheri
  • 1,383
  • 3
  • 10
  • 26
andres
  • 1,558
  • 7
  • 24
  • 62

1 Answers1

2

This answer might help: Create new dataframe in pandas with dynamic names also add new column

The approach suggested in the post would be to create a dictionary that would store the dataframe as the value and the stock ticker as the key.

df_dict = {}
for stock in tickers:
     df = pdr.get_data_yahoo(tickers, start=start_date,end=end_date)
     df_dict[stock] = df

Then you can iterate over the dictionary using the stock tickers as keys.

Charles Carriere
  • 347
  • 1
  • 12
  • 1
    Charles, thank you for your help and contribution. Your post is great and has helped me overcome this roadblock for now, however. When I used your method, it creates one dataframe for all, I would like to indivdually create a uniqe DF for each model. – andres Nov 25 '19 at 04:00