-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
106 lines (78 loc) · 3.5 KB
/
main.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
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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import time
from alpha_vantage.timeseries import TimeSeries
from pprint import pprint
# alpha vantage api key
alpha_key = 'YNY88LER76HPO0G3'
#alpha_key = 'KN8HZ3R0G7J9XHQ8'
# list of stocks to retrieve
# stock_list_original = ['MSFT', 'AAPL', 'AMD', 'FB', 'ROKU', 'ENPH', 'LYFT', 'AAL']
# stock_list = ['SPX']
stock_list = ['AAPL','ABBV','ABT','ACN','ADBE','AGN','AIG','ALL','AMGN','AMZN','AXP','BA','BAC','BIIB','BK','BKNG',
'BLK','BMY','BRK.B','C','CAT','CHTR','CL','CMCSA','COF','COST','CSCO','CVS','CVX','DD','DHR','DIS',
'DOW','DUK','EMR','EXC','F','FB','FDX','GD','GE','GILD','GM','GOOG','GOOGL','GS','HD','HON','IBM',
'INTC','JNJ','JPM','KHC','KMI','KO','LLY','LMT','LOW','MA','MCD','MDLZ','MDT','MET','MMM','MO','MRK',
'MS','MSFT','NEE','NFLX','NKE','NVDA','ORCL','OXY','PEP','PFE','PG','PM','PYPL','QCOM','RTN','SBUX','SLB',
'SO','SPG','T','TGT','TMO','TXN','UNH','UNP','UPS','USB','UTX','V','VZ','WBA','WFC','WMT','XOM']
def convert_csv_to_tickers():
df = pd.read_csv('nasdaqlisted.txt', sep='|')['Symbol']
array = np.array(df.values)
array = np.delete(array, len(array) - 1)
f = open("NASDAQ1.txt", "w")
for a in array:
f.write(a + ',')
f.close()
def set_stock_list():
arr = []
with open("NASDAQ2.txt", "r") as filestream:
for line in filestream:
currentline = line.split(',')
arr.append(currentline)
newarr = np.array(arr[0])
return newarr
# stock_list = ['AAPL','ABBV','ABT','ACN','ADBE','AGN','AIG','ALL','AMGN','AMZN','AXP','BA','BAC','BIIB','BK','BKNG',
# 'BLK','BMY','BRK.B','C','CAT','CHTR','CL','CMCSA','COF','COST','CSCO','CVS','CVX','DD','DHR','DIS',
# 'DOW','DUK','EMR','EXC','F','FB','FDX','GD','GE','GILD','GM','GOOG','GOOGL','GS','HD','HON','IBM',
# 'INTC','JNJ','JPM','KHC','KMI','KO','LLY','LMT','LOW','MA','MCD','MDLZ','MDT','MET','MMM','MO','MRK',
# 'MS','MSFT','NEE','NFLX','NKE','NVDA','ORCL','OXY','PEP','PFE','PG','PM','PYPL','QCOM','RTN','SBUX','SLB',
# 'SO','SPG','T','TGT','TMO','TXN','UNH','UNP','UPS','USB','UTX','V','VZ','WBA','WFC','WMT','XOM']
def retrieve(stock_list):
count = 1
for stock in stock_list:
# stock is a string type so we do not convert for csv
print(type(stock))
# exception handling
if count != 5:
print("For stock:", stock)
ts = TimeSeries(key=alpha_key, output_format='pandas')
data, meta_data = ts.get_daily(symbol=stock, outputsize='full')
plot_graph(stock, data)
print(data.head(), '\n')
print(len(data))
convert_csv(data, stock)
count += 1
else:
time.sleep(60)
count = 0
def convert_csv(data, stock):
data.to_csv('../Stocks/Stock_CSV/' + stock + '.csv')
def plot_graph(stock, data):
to_plot = plt.plot(data['4. close'], color='darkolivegreen')
# to see the inline plotting - unneccessary now
# to_plot.plot()
# plt.color
plt.xlabel('Years', color='firebrick')
plt.ylabel('Prices', color='firebrick')
plt.title(stock + ': Prices over Time', color='darkblue')
plt.savefig('../Stocks/Stock_Plot/' + stock + '.png')
# plt.show()
def main():
stock_list = set_stock_list()
retrieve(stock_list)
if __name__ == "__main__":
try:
main()
except:
print('no')