Advertisement
Guest User

Untitled

a guest
Sep 7th, 2022
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. import yfinance as yf
  2. import datetime
  3. import os
  4.  
  5. def get_ticker(symbol, startDate=datetime.datetime(2022, 4, 1), endDate=datetime.datetime(2022, 4, 8)):
  6.     ticker = yf.Ticker(symbol)
  7.     data = ticker.history(start=startDate, end=endDate, interval="1m")
  8.     return data
  9.  
  10.  
  11. def get_ticker_sharesOutstanding(symbol):
  12.     ticker = yf.Ticker(symbol)
  13.     return int(ticker.get_info()['sharesOutstanding'])
  14.  
  15.  
  16. ticker_names = []
  17. with open('./Stocks in the Russell 1000 Index.csv', 'r') as f:
  18.     lines = f.readlines()
  19.  
  20.     for line in lines:
  21.  
  22.             symbol = line.split(',')[0].replace('"','')
  23.             ticker_names.append(symbol)
  24.  
  25. res = []
  26. for name in ticker_names[1:]:
  27.     try:
  28.         print('Getting data for:', name)
  29.  
  30.         delta = datetime.timedelta(days=7)
  31.         num_days = 5
  32.         start_week = datetime.datetime(2022, 9, 5)
  33.  
  34.         date_list = [start_week - datetime.timedelta(days=x*7) for x in reversed(range(num_days))]
  35.  
  36.         start_date_list = []
  37.         gap_p = []
  38.  
  39.         for date in date_list[:-1]:
  40.             print(name, date, date + delta)
  41.             data = get_ticker(name, startDate=date, endDate=date + delta)
  42.  
  43.             #data = get_ticker(name)
  44.             shares_out = get_ticker_sharesOutstanding(name)
  45.             output_path = './data_2/' + name + '_' + str(shares_out) + '.csv'
  46.             output_file_exists = os.path.exists(output_path)
  47.             data.to_csv(output_path, mode='a', header=not output_file_exists)
  48.  
  49.     except Exception as err:
  50.         print(err)
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement