-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmushy.py
443 lines (280 loc) · 11.2 KB
/
mushy.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
'''
3580: Stock Prediction / Forecasting
Author: Mujtaba Ashfaq
Date: 4/15/21
This class picks one stock that seems the best
based on arbitrary values.
'''
# Used for reading in and processing data
import DataPreProcessor as dp
# Library for handling dataframes
import pandas as pd
# Use for logistical regression modeling
#--------------------
# working with arrays
import numpy as np
# splitting the data
from sklearn.model_selection import train_test_split
# model algorithm
from sklearn.linear_model import LinearRegression
# data normalization
from sklearn.preprocessing import StandardScaler
# Metrics library
from sklearn.metrics import mean_squared_error
# For RFE
from sklearn.feature_selection import RFE
#--------------------
# Used for holt winters
from statsmodels.tsa.holtwinters import ExponentialSmoothing
# Used for plotting
import matplotlib.pyplot as plt
import graeme as g
# Make sklearn shutup about errors
pd.options.mode.chained_assignment = None # default='warn'
# Make data readable during testing
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
'''
Creates new features to find the single best stock.
Figuring out best stock
------------------------
RFE: (day trading)
Loop through each stock company
# Get tomorrows change percent
Split up stock data into training and test data
Use RFE to find best feature for each company
Put top 1 feature name in new column for each company
# Put accuracy in new column
Forecasting: (Find long term stock)
Pull out top 50 stocks
Use holt winters for each stock
List highest percentage return for prediction
Get top 3 stock
Predict future outside of algorithm
Return top stock (good accuracy, and best return)
'''
def findBestStock(nasdaq_df):
# Remove comma found in some numbers
nasdaq_df.replace(',', '', regex=True, inplace=True)
# Get a list of all unique companies
companies = nasdaq_df['Name'].unique()
# Create empty column for top company feature
nasdaq_df['TopFeature'] = 'null'
# Create empty column for company prediction accuracy
nasdaq_df['ModelAccuracy'] = 0.0
# Track how many companies have been looped through
# Max for nasdaq is about 2000
comp_count = 0
# Loop through each company
for company in companies:
# Limit number of stocks looped
#if comp_count == 51:
#break
# Get data frame for company
comp_df = nasdaq_df.loc[(nasdaq_df['Name'] == company)]
# Get tomorrow change percent in new column
comp_df['TomorrowPercentChange'] = comp_df.groupby('Name')['% Chg'].shift(-1)
# Remove last row (TomorrowPercentChange is null on last row)
comp_df.drop(comp_df.tail(1).index, inplace=True)
# Get available columns for RFE
columns = list(comp_df.columns)
# Remove columns we don't care about
columns.remove('Name')
columns.remove('Symbol')
columns.remove('Net Chg')
columns.remove('% Chg')
columns.remove('Day')
columns.remove('Date')
columns.remove('TopFeature')
columns.remove('ModelAccuracy')
# Convert list to array
features = np.array(columns)
# Set correct data type (for columns we care about)
for f in features:
comp_df[f] = pd.to_numeric(comp_df[f], downcast="float", errors='coerce')
# Clean tomorrow percentage before cleaning rest of data
# (Prevents column from getting entirely deleted)
comp_df = comp_df[comp_df['TomorrowPercentChange'].notna()]
# Drop columns with missing values from data frame
comp_df = comp_df.dropna(axis='columns')
# Drop any rows missing data
comp_df = comp_df.dropna()
# Reset index (causes problems with array conversion)
comp_df.reset_index(drop=True, inplace=True)
# Get the remaining columns we care about
columns = list(comp_df.columns)
# Remove columns we don't care about
columns.remove('Name')
columns.remove('Symbol')
columns.remove('Net Chg')
columns.remove('% Chg')
columns.remove('Day')
columns.remove('Date')
columns.remove('TopFeature')
columns.remove('ModelAccuracy')
columns.remove('TomorrowPercentChange')
# Convert list to array
features = np.array(columns)
# Hold top feature and accuracy
topFeature = 'null'
accuracy = 0.0
# Define dependent variable
y_var = comp_df['TomorrowPercentChange']
# Track number of features left
feat_rem = 0
#TODO Multithread this
# Keep making models while reducing the number of features
for num_components in range(len(features), 0, -1):
# Skip empty dataframe
if len(comp_df.index) < 25:
break
# Skip if less than one feature
if len(features) < 2:
break
# Show data frame
#if feat_rem == 0:
#print(comp_df)
# Hold model
rfe_model = LinearRegression()
# Reset independent variable
X_var = comp_df.loc[:, features]
# Scale data to remove bias
X_var = StandardScaler().fit(X_var).transform(X_var)
# Create rfe model (test with specified number of features)
rfe = RFE(rfe_model, n_features_to_select=num_components)
rfe = rfe.fit(X_var, y_var)
# Get top features
top_features = features[rfe.support_]
# Update features X var with top features
X_var = comp_df.loc[:, top_features]
# Scale data to remove bias
X_var = StandardScaler().fit(X_var).transform(X_var)
# Split data into training and testing data sets (train using top features)
X_train, X_test, y_train, y_test = train_test_split(X_var, y_var, test_size=0.2, random_state=0)
# Train logistic regression model
rfe_model.fit(X_train, y_train)
# Use logsitic regression for predictions
yhat = rfe_model.predict(X_test)
# Display feature ranking
#print(rfe.support_)
#print(rfe.ranking_)
# Display top features
#print(top_features)
# List accuracy of model
#print(mean_squared_error(y_test, yhat))
# Track features remaining
feat_rem += 1
# Hold result data
if feat_rem == len(features):
topFeature = top_features[0]
accuracy = mean_squared_error(y_test, yhat)
# Track number of companies parsed
comp_count += 1
print("Calculated company... " + str(comp_count) + " of " + "1996")
#print(topFeature)
#print(accuracy)
# Save top feature selected for stock in new column
nasdaq_df.loc[nasdaq_df['Name'] == company, 'TopFeature'] = topFeature
# Save prediction accuracy of company
nasdaq_df.loc[nasdaq_df['Name'] == company, 'ModelAccuracy'] = accuracy
#print(company)
#print(nasdaq_df)
# Calculate average accuracy of stocks
averageAcc = nasdaq_df['ModelAccuracy'].mean()
averageAcc = averageAcc - (averageAcc*0.25)
# Get rid of 'safe' stocks
nasdaq_df = nasdaq_df[nasdaq_df['ModelAccuracy'] < averageAcc]
# Shrink the data frame to get accuracy of each company
bastardized_df = nasdaq_df.drop_duplicates('ModelAccuracy')
# Sort df by accuracy
bastardized_df = bastardized_df.sort_values(by='ModelAccuracy', ascending=True)
print(bastardized_df)
# Get a list of all unique companies (now sorted by accuracy)
companies = bastardized_df['Name'].unique()
# Create empty column for company prediction accuracy
nasdaq_df['LongTermReturn'] = 0.0
#TODO Multithread this
# Loop through top 50 stocks
for i in range(0, 250):
# Get data frame for company
comp_df = nasdaq_df.loc[(nasdaq_df['Name'] == companies[i])]
try:
# Convert everything to float
comp_df = comp_df.apply(pd.to_numeric, errors='coerce')
comp_df.index.freq = 'MS'
# Calculate train cutoff
length = len(comp_df.index)
cutoff = length * 0.8
cutoff = int(cutoff)
# Split into train and test data frames
train, test = comp_df.iloc[:cutoff], comp_df.iloc[cutoff:]
# Train the model
model = ExponentialSmoothing(train['Close'], seasonal='mul', seasonal_periods=12).fit()
pred = model.forecast(24)
# TODO Display plot with holt winters
# Display the plot
#plt.plot(train['Date'], train['Close'], label='Train')
#plt.plot(test['Date'], test['Close'], label='Test')
#plt.plot(pred.index, pred, label='Holt-Winters')
#plt.legend(loc='best')
#plt.show()
# Assumes money was invested at end of train data
# and sold at all time high
highestPredValue = pred.max()
returnPercentage = (highestPredValue / train['Close'].iloc[-1]) - 1
#TODO: Calculate accuracy of holt winters
#longTermPred = 0
#longTermActl = 0
#predAccuracy = longTermPred - longTermActl
nasdaq_df.loc[nasdaq_df['Name'] == companies[i], 'LongTermReturn'] = returnPercentage
except:
continue
# Shrink the data frame to get accuracy of each company
bastardized_df = nasdaq_df.drop_duplicates('LongTermReturn')
bastardized_df.reset_index(inplace=True)
# Sort df by long term return
bastardized_df = bastardized_df.sort_values(by='LongTermReturn', ascending=False)
print(bastardized_df)
# Hold top 3 stocks
stocks = []
# Get top 3 stocks
stocks.append(bastardized_df['Name'].iloc[0])
stocks.append(bastardized_df['Name'].iloc[1])
stocks.append(bastardized_df['Name'].iloc[2])
# Show top 3 stocks
print(bastardized_df.iloc[:3])
# Show
for s in stocks:
comp_df = nasdaq_df.loc[(nasdaq_df['Name'] == s)]
# Convert everything to float
comp_df = comp_df.apply(pd.to_numeric, errors='coerce')
comp_df.index.freq = 'MS'
# Calculate train cutoff
length = len(comp_df.index)
cutoff = length * 0.8
cutoff = int(cutoff)
# Split into train and test data frames
train, test = comp_df.iloc[:cutoff], comp_df.iloc[cutoff:]
# Display the plot
plt.plot(train['Date'], train['Close'], label='Train')
plt.plot(test['Date'], test['Close'], label='Test')
plt.legend(loc='best')
plt.title("Stock Price Over Time: " + s)
plt.xlabel("Date")
plt.ylabel("Price")
# Show plots
plt.show()
# Show advanced chart
g.graemeStuff(s)
name = ''
return name
# Give user heads up
print("Loading data...")
# Get data frames
nasdaq_df, nyse_df, spac_df, allDf = dp.initializeDf()
# Give user heads up
print("Starting calculations. This will take a while!")
# Get top stock
mushyStock = findBestStock(nasdaq_df)
# Show top stock
print(mushyStock)