forked from makeuseofcode/Stock-Data-Fetcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
daves_data_stuff.py
54 lines (42 loc) · 1.7 KB
/
daves_data_stuff.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Importing the yfinance package
import yfinance as yf
import csv
file_name = '/home/kirstyellis/dave_ws/src/Stock-Data-Fetcher/list_tickers.txt'
ticker_strings = []
with open(file_name) as file:
with open(file_name) as f:
lines = f.read().splitlines()
ticker_strings.append(lines[0].split(','))
tickers = {}
for i in ticker_strings[0]:
try:
tickers[i] = yf.Ticker(i).info
market_price = tickers[i]['regularMarketDayLow']
previous_close_price = tickers[i]['regularMarketPreviousClose']
print('Ticker: ', i)
print('Market Price:', market_price)
print('Previous Close Price:', previous_close_price)
except:
continue
header_list = ['Ticker', 'regularMarketDayLow', 'regularMarketPreviousClose']
csv_file = "/home/kirstyellis/dave_ws/src/Stock-Data-Fetcher/ticker_output.csv"
data_i_want = ['regularMarketDayLow', 'regularMarketPreviousClose']
try:
with open(csv_file, 'w') as csvfile:
writer = csv.writer(csvfile)
# writer.writeheader()
writer.writerow(header_list)
for data in tickers:
# create an empty list for my output
data_list = []
# The first thing I want in my output list is the ticker name string
data_list.append(data)
# for each data item in the list that I want, I check if it exists in the ticker dictionary, if it doesn't exist, I put an empty string in that field
for j in data_i_want:
if j in tickers[data]:
data_list.append(tickers[data][j])
else:
data_list.append('')
writer.writerow(data_list)
except IOError:
print("I/O error")