-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUSStockMarketActivityProgram.py
More file actions
106 lines (76 loc) · 2.99 KB
/
USStockMarketActivityProgram.py
File metadata and controls
106 lines (76 loc) · 2.99 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# U.S. Stock Market Activity Program (User Interface)
# 2/23/17
import stockmarket_csv_reader
import stockmarket_csv_writer
import stockmarket_class
import urllib.error
import time
def read_exchange() -> str:
'''
Reads and returns the user input exchange.
'''
exchange = input('Enter exchange symbol (NYSE, NASDAQ, or AMEX): ').strip().upper()
return exchange
def read_api() -> str:
'''
Reads and returns the user input API.
'''
while True:
api = input('Enter API (Google or Yahoo): ').strip().capitalize()
if api == 'Google' or 'Yahoo':
return api
else:
print('Invalid API')
def _split_symbols_list(companies: list) -> [list]:
'''
Returns a list of lists split 95 each for Google Finance limits.
'''
new_list = []
if len(companies) % 95 != 0:
num_of_lists = (len(companies) // 95) + 1
else:
num_of_lists = len(companies)
for lst_num in range(num_of_lists):
new_list.append(companies[:95])
del companies[:95]
return new_list
def user_interface() -> None:
exchange = read_exchange()
api = read_api()
start_time = time.time()
lst_companies = stockmarket_csv_reader.get_list_companies(exchange)
csv_list = []
if api == 'Google':
for lst in _split_symbols_list(lst_companies):
stocks = stockmarket_class.GoogleFinanceStocks(lst)
for index in range(len(stocks.get_base_data())):
print(stocks.get_stock_symbol(index), ':',
stocks.get_current_share_price(index))
csv_list.append([stocks.get_stock_symbol(index),
stocks.get_current_share_price(index)])
elif api == 'Yahoo':
count = 0
for company in lst_companies:
if count % 50 == 0 and count != 0:
time.sleep(60)
stock = stockmarket_class.YahooFinanceStocks(company)
print(stock.get_stock_symbol(), ': ', stock.get_current_share_price())
csv_list.append([stock.get_stock_symbol(), stock.get_current_share_price()])
## for company in lst_companies:
## try:
## stock = stockmarket_class.GoogleFinanceStock(company)
## print(stock.get_stock_symbol(), ': ', stock.get_current_share_price())
##
## except urllib.error.HTTPError as e:
## print('HTTP Error: {}'.format(e.code))
## if e.code == 503:
## time.sleep(10)
print('CSV File Path: ', end='')
print(stockmarket_csv_writer.write_into_csv(csv_list, ['Symbol', 'Price per Share']))
print()
print('******************************')
print('Total execution time:')
print('--- %s seconds ---' % (time.time() - start_time))
print('******************************')
if __name__ == '__main__':
user_interface()