0

I'm working on a personal project - more of a learning venture than anything and the project consists of a few what may seem common tasks for data wrangling in python. I've tried multiple avenues to solve on my own or with information gleaned form other threads, but no luck just yet.

Some background: I'm trying to take a list or dataframe of stock tickers and pass into the ticker module of yfinance to pull history for a large list of stocks. Sounds simple - and probably is for someone who's lived with Python for a while.

My script:

import yfinance as yf
import pandas as pd
from get_all_tickers import get_tickers as gt
list_of_tickers = gt.get_tickers()
tickerlist = pd.DataFrame(list_of_tickers, columns = ["Ticker"])
tickerlist = tickerlist.values.tolist()
tc = ""
for x in tickerlist:
    tc += str(x)

tc = tc.replace(" ", "")
tc = tc.replace("[", ",")
tc = tc.replace("]", "")
tc = tc.replace("'", "")
#tc = ",".join(tc)   

def Convert(string): 
    li = list(string.split(",")) 
    return li 

tc = Convert(tc)
df = pd.DataFrame(data=tc, columns = ["Ticker"])
df_filtered = df.iloc[1: , : ]
#df_filtered.update('"' + df_filtered[['Ticker']].astype(str) + '"')
# df_filtered.to_csv("./StockTickers.csv", sep=',',index=False)
list_filtered = df_filtered.values.tolist()

# valid periods: 1d,5d,1mo,3mo,6mo,1y,2y,5y,10y,ytd,max
period= "1d"
# valid intervals: 1m,2m,5m,15m,30m,60m,90m,1h,1d,5d,1wk,1mo,3mo
interval = "1d"

#=============================================================================
for ticker in df_filtered: #df_filtered.iterrows():
    ticker = str(ticker).upper()
    ticker = yf.Ticker("DDD")
    history = ticker.history(
                            period = period,
                            interval = interval
                            )
    

    print(history)
    print(df_filtered)
  • what is this? `from get_all_tickers import get_tickers as gt` – Rob Raymond Jul 11 '20 at 04:29
  • Your question is not clear. What do you need exactly? – Akshay Sehgal Jul 11 '20 at 05:58
  • at first search was unable to ascertain if it was your own library or on pypi. I then found it on pypi – Rob Raymond Jul 11 '20 at 09:52
  • Does this answer your question? [How to read in the multi-index columns from csv and return the yfinance dataframe to the correct form?](https://stackoverflow.com/questions/63107594/how-to-read-in-the-multi-index-columns-from-csv-and-return-the-yfinance-datafram) – Trenton McKinney Aug 03 '20 at 23:46

1 Answers1

0

I've been intending to do a personal project to track my investments.

  1. You are doing a lot of prep that's not necessary. Tickers are fine as input into yf.Ticker(). Construct data frame which is list of tickers, then filter it to wanted set. (I've taken first 5 tickers)
  2. looping is done in list comprehension to pd.concat()
  3. move Ticker column to be first column (this I've used some coding sugar)
  4. have data frame that is the history data for all wanted tickers
import yfinance as yf
from get_all_tickers import get_tickers as gt
df = pd.DataFrame(gt.get_tickers(), columns=["Ticker"])
df_f = df.iloc[:5, :]
df_r = pd.concat([yf.Ticker(t).history(period="1d", interval="1d")\
                  .reset_index()\
                  .assign(Ticker=t) for t in df_f["Ticker"].values])
df_r = df_r.reindex(columns=df_r.columns.insert(0,df_r.columns[-1:][0])[:-1])
df_r

output

    Ticker  Date    Open    High    Low Close   Volume  Dividends   Stock Splits
0   DDD 2020-07-10  6.48    6.64    6.39    6.63    1369600 0   0
1   DDD 2020-07-10  6.48    6.64    6.39    6.63    1336656 0   0
0   MMM 2020-07-10  151.69  153.25  150.99  152.85  1954500 0   0
1   MMM 2020-07-10  151.69  153.25  150.99  152.85  1934202 0   0
0   WBAI    2020-07-10  3.71    4.06    3.71    3.80    30000   0   0
1   WBAI    2020-07-10  3.71    4.06    3.71    3.80    29887   0   0
0   WUBA    2020-07-10  54.12   54.84   54.04   54.81   1690400 0   0
1   WUBA    2020-07-10  54.12   54.84   54.04   54.81   1686841 0   0
0   EGHT    2020-07-10  16.43   16.43   16.00   16.10   1610200 0   0
1   EGHT    2020-07-10  16.43   16.43   15.96   15.96   1610215 0   0

Rob Raymond
  • 29,118
  • 3
  • 14
  • 30