-
Notifications
You must be signed in to change notification settings - Fork 0
/
stockplot.py
167 lines (128 loc) · 5.14 KB
/
stockplot.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# this code is checked in as StockAnalysis under the url https://github.com/clairegong16/StockAnalysis.git
import yfinance as yf
# Import the plotting library
import matplotlib.pyplot as plt
import pandas as pd #for understanding https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.html
import sys #for file
import mathstock
'''
plot single chart
'''
'''
# Get the data for the stock AAPL
data = yf.download('AAPL','2020-01-01','2020-08-01')
print(data.head())
print(data.at['2020-01-02','Close'])
# Plot the close price of the AAPL
data['Adj Close'].plot()
plt.show() #shows the graph to the user
'''
# Define the ticker list
stockslist = ['AAPL', 'AMZN', 'GOOGL', 'WMT', 'IBM', 'MSFT']
# returns the y list given the year and month
def dates_to_value_list(company, year, month):
if month == '12':
month2 = '-01'
year2 = int(year) + 1
else:
month2 = '-02'
year2 = year
with open(company + '.stock.txt') as f: #this opens the text file as a file
'''for line in f:
print(line)''' #another way to know when to stop reading the file
stopper = False #for the while loop below
foundbeginning = False
ylist = []
while stopper != True:
line = f.readline()
if len(line) == 0:
break
#locating the beginning date
if line.find(str(year) + '-' + month) != -1: #.find finds the index that the substring is in.
foundbeginning = True
#locating the end date
if foundbeginning == True:
x = line.split()
ylist.append(float(x[5]))
if line.find(str(year2) + month2) != -1: #should stop at 2018-01-xx
stopper = True
if len(line) == 0:
print("year,month,month2,foundbeginning:", year, month, month2, foundbeginning, "fail to locate the date in the file.")
return ylist
def plotm_and_b(m,b,ylist, company, year, month, plot):
y_list = []
for i in range(0,len(ylist)):
y = m*(i+1)+b
y_list.append(y)
if plot == True:
plt.plot(y_list, color ='blue', label= "line of best fit")
plt.plot(ylist, color ='red', label = "actual")
plt.legend()
plt.title(company + ' ' + str(year) + ' ' + month, loc='right')
plt.savefig('plots/' + company + str(year)+ month +'.png')
plt.show()
#my all in one function to loop over through multiple companies:
''' dec = dates_to_value_list('2018-12-03', '2018-12-31', 'MSFT')
jan = dates_to_value_list('2019-01-02', '2019-01-31', 'MSFT')'''
def main(stocklist, d_begdate): #, d_enddate, j_begdate, j_enddate):
generatestockfile(stocklist)
yearlist = findingyears(d_begdate, 10)
markerlist = ['o', '.', 'x', '+', 'v', '^', '<', '>', 's', 'd']
colorlist = ['b', 'g', 'r', 'c', 'm', 'y', 'k', 'w']
a = 0
for i in stocklist:
mdeclist = [] # slopes list
mjanlist = []
marker = markerlist[a]
color = colorlist[a]
for j in yearlist:
dec = dates_to_value_list(i, j, '12')
jan = dates_to_value_list(i, j, '01')
#normalized mdec
mdec,bdec = mathstock.calcm_and_b(dec, i)
n_mdec = mdec*(len(dec))/bdec
mdeclist.append(n_mdec)
plotm_and_b(mdec,bdec,dec, i, j, 'DEC', True)
mjan,bjan = mathstock.calcm_and_b(jan, i)
n_mjan = mjan*(len(jan))/bjan
mjanlist.append(n_mjan)
plotm_and_b(mjan,bjan,jan, i, int(j) + 1, 'JAN', True)
a = a + 1
plt.plot(mdeclist, mjanlist, marker, color =color, label = i)
plt.xlabel('slope dec')
plt.ylabel('slope jan')
'''plt.xlim(-12,12)
plt.ylim(-12,12)'''
plt.legend()
plt.savefig('plots/' + i + '_scatter' +'.png')
plt.show()
plt.close()
#input is for example ('2018-12-03', 1) output is 2018. ex. ('2018-12-03', 5) output is 2014,2015,2016,2017,2018
def findingyears(d_begdate, yearnumber):
enddec = int(d_begdate[:4])
startdec = enddec- (yearnumber-1)
yearlist = []
while startdec <= enddec:
yearlist.append(startdec)
startdec = startdec + 1
return yearlist
#for finding difference between beginning and end.
def generatestockfile(stockslist):
# Fetch the data
date1 = '2000-01-01'
date2 = '2020-12-01'
data = yf.download(stockslist, date1, date2)
'''data['Adj Close'].plot()
plt.show()'''
# Print first 5 rows of the data
#print(data.head(5))
for company in stockslist:
print(company)
datas = yf.download(company, date1, date2) #gets the data of the company between the given dates.
print("datas:\n", datas)
pd.set_option('display.max_rows', len(datas)) #this is here to get all of the data shown in the text file.
#the following section is to put the data into a text file:
with open('rawdata/' + company + '.stock.txt', 'w') as stocks:
print(datas, file=stocks)
pd.reset_option('display.max_rows') #reset the length
main(stockslist,'2019-12')