forked from jcwill415/Stock_Market_Data_Analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stock_Market_DataAnalysis_DataVisualization.py
533 lines (416 loc) · 18.3 KB
/
Stock_Market_DataAnalysis_DataVisualization.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# Stock Market Data Analysis: Using Python to Scrape, Analyze, & Visualize Data from the S&P 500
# Import Packages (You may need to install, using pip)
# For further info on installing packages with Python visit: https://packaging.python.org/tutorials/installing-packages/
# Import Packages
import bs4 as bs
import pickle
import requests
import datetime as dt
import os
import io
import pandas as pd
import pandas_datareader.data as web
import pandas.plotting
from pandas.plotting import register_matplotlib_converters
import pandas.testing
from pandas.testing import assert_frame_equal
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
from matplotlib import style
import mplfinance as mpf
import mpl_finance as mplf
from mpl_finance import candlestick_ohlc
import collections
from collections import Counter
import sklearn
from sklearn import svm, neighbors
from sklearn.svm import LinearSVC
from sklearn.model_selection import train_test_split
from sklearn.ensemble import VotingClassifier, RandomForestClassifier
style.use('ggplot')
# Parts 1-3: Using Stock Market Data
# Here we are using Tesla (ticker = TSLA) as an example.
# PLEASE NOTE: When Matplotlib displays a visual/chart for you to inspect, the computations
# will pause. Please close out the chart when you are ready for the script to continue.
start = dt.datetime(2000,1,1)
end = dt.datetime.now()
df = web.DataReader('TSLA', 'yahoo', start, end)
df.to_csv('tsla.csv')
print(df.tail(10))
daily = pd.read_csv('tsla.csv', index_col=0,parse_dates=True)
daily.index.name = 'Date'
daily.shape
daily.head(3)
daily.tail(3)
mpf.plot(daily)
# Example of a candlestick chart with 3, 6, and 9 day moving averages
mpf.plot(daily, type='candle', mav = (3, 6, 9), volume = True)
# Example of a candlestick chart (3, 6, 9 ma) with nontrading movement shown
mpf.plot(daily, type = 'candle', mav =(3, 6, 9), volume = True, show_nontrading = True)
# Here we are looking at Microsoft (ticker = MSFT)
start = dt.datetime(2000,1,1)
end = dt.datetime.now()
df = web.DataReader('MSFT', 'yahoo', start, end)
df.to_csv('msft.csv')
print(df.tail(10))
daily = pd.read_csv('msft.csv',index_col=0,parse_dates=True)
daily.index.name = 'Date'
daily.shape
daily.head(3)
daily.tail(3)
mpf.plot(daily)
# Example of a line chart
mpf.plot(daily, type = 'line')
# Example of a renko chart
mpf.plot(daily, type = 'renko')
# Example of a pnf chart
mpf.plot(daily, type = 'pnf')
# Example of a chart showing open, high, low, close (ohlc) with 4 day moving average
mpf.plot(daily, type = 'ohlc', mav = 4)
# Example of candlestick chart with 3, 6, 9 day moving averages
mpf.plot(daily, type='candle', mav=(3,6,9))
# Additional visualizations using Tesla (ticker = TSLA) as an example
datafile = 'tsla.csv'
data = pd.read_csv(datafile, index_col = 'Date')
# Convert dates from string to datetime format
data.index = pd.to_datetime(data.index)
data
# Example of plotting nontrading movement
mpf.plot(data[-50:], show_nontrading = True)
# Example of candlestick chart
mpf.plot(data[-50:], type='candlestick', show_nontrading = True)
df = web.DataReader('TSLA', 'yahoo', start, end)
df.to_csv('tsla.csv')
df = pd.read_csv('tsla.csv', parse_dates = True, index_col = 'Date')
df['100ma'] = df['Adj Close'].rolling(window = 100, min_periods = 0).mean()
df.dropna(inplace = True)
print(df.tail())
# Example of line chart with moving average, with subplot showing volume as a bar chart
ax1 = plt.subplot2grid((6,1), (0,0), rowspan = 5, colspan = 1)
ax2 = plt.subplot2grid((6,1), (5,0), rowspan = 1, colspan = 1, sharex = ax1)
ax1.plot(df.index, df['Adj Close'])
ax1.plot(df.index, df['100ma'])
ax2.bar(df.index, df['Volume'])
plt.show()
plt.show()
mpf.plot(data)
df_ohlc = df['Adj Close'].resample('10D').ohlc()
df_volume = df['Volume'].resample('10D').sum()
df_ohlc.reset_index(inplace = True)
print(df_ohlc.head())
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
df_ohlc['Date'] = df_ohlc['Date'].map(mdates.date2num)
print(df_ohlc.head())
df_ohlc.head()
ax1 = plt.subplot2grid((6,1), (0,0), rowspan = 5, colspan = 1)
ax2 = plt.subplot2grid((6,1), (5,0), rowspan = 1, colspan = 1, sharex = ax1)
#mplfinance.plot(ax1, df_ohlc.values, width = 2, colorup = 'g')
candlestick_ohlc(ax1, df_ohlc.values, width = 2, colorup = 'g')
ax2.fill_between(df_volume.index.map(mdates.date2num), df_volume.values, 0)
plt.show()
# Part 4: Resampling to improve data accuracy
df = pd.read_csv('tsla.csv', parse_dates = True, index_col = 0)
df['100ma'] = df['Adj Close'].rolling(window = 100, min_periods = 0).mean()
print(df.tail(10))
df_ohlc = df['Adj Close'].resample('10D').mean()
# Resample data for 10 day period
df_ohlc = df['Adj Close'].resample('10D').ohlc()
df_volume = df['Volume'].resample('10D').sum()
df_ohlc.reset_index(inplace = True)
# Convert datetime object to mdate
df_ohlc['Date'] = df_ohlc['Date'].map(mdates.date2num)
print(df_ohlc.head())
ax1 = plt.subplot2grid((6,1), (0,0), rowspan = 5, colspan = 1)
ax2 = plt.subplot2grid((6,1), (5,0), rowspan = 1, colspan = 1, sharex = ax1)
ax1.xaxis_date()
ax1.plot(df.index, df['Adj Close'])
ax1.plot(df.index, df['100ma'])
ax2.bar(df.index, df['Volume'])
# plt.show()
# Part 5: Getting S&P500 List
# Here we are using BeautifulSoup to scrape data from Wikipedia, and save the current S&P500 list.
def save_sp500_tickers():
resp = requests.get('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
soup = bs.BeautifulSoup(resp.text, "lxml")
table = soup.find('table', {'id': 'constituents'})
tickers = []
for row in table.findAll('tr')[1:]:
ticker = row.find_all('td')[0].text.strip()
tickers.append(ticker)
with open("sp500tickers.pickle", "wb") as f:
pickle.dump(tickers, f)
print(tickers)
return tickers
save_sp500_tickers()
# Part 6: Getting Stock Prices
# Get data from Yahoo and call SP500 tickers list as sp500
def get_data_from_yahoo(reload_sp500 = False):
if reload_sp500:
tickers = save_sp500_tickers()
else:
with open("sp500tickers.pickle", "rb") as f:
tickers = pickle.load(f)
# Take all of the data for stocks and store in a directory
# Working with API, parsing website, take entire dataset and store locally
# Here we will look at Adjusted Close, but we can look at other columns later
if not os.path.exists('stock_dfs'):
os.makedirs('stock_dfs')
# to change your start or end time update the dates
# Here we look at 1/1/2000 to current date.
start = dt.datetime(2000,1,1)
end = dt.datetime.now()
# Grab all ticker data
for ticker in tickers:
print(ticker)
if not os.path.exists('stock_dfs/{}.csv'.format(ticker)):
try:
df = web.DataReader(ticker, 'yahoo', start, end)
df.to_csv('stock_dfs/{}.csv'.format(ticker))
except:
print(f'Problems retrieving data for{ticker}. Skipping!')
else:
print('Already have {}'.format(ticker))
get_data_from_yahoo()
# Part 7: Combine S&P500 List with Adjusted Close Price for All Companies
def compile_data():
with open("sp500tickers.pickle", "rb") as f:
tickers = pickle.load(f)
mainDataSet = pd.DataFrame()
# Count in SP500 tickers list
for count, ticker in enumerate(tickers):
fileDataSet = pd.read_csv('stock_dfs/{}.csv'.format(ticker))
fileDataSet.set_index('Date', inplace = True)
fileDataSet.rename(columns = {'Adj Close':ticker}, inplace = True)
fileDataSet.drop(['Open', 'High', 'Low', 'Close', 'Volume'], 1, inplace = True)
if mainDataSet.empty:
mainDataSet = fileDataSet
else:
mainDataSet = mainDataSet.join(fileDataSet)
print(mainDataSet.head())
mainDataSet.to_csv('sp500_joined_closes.csv')
compile_data()
# Part 8: Correlation Tables & Data Visualization
# Visualizing data from the SP500 close price csv
def visualize_data():
df = pd.read_csv('sp500_joined_closes.csv')
# Example of plotting one company close price over time for Apple (ticker = AAPL)
# df['AAPL'].plot()
# plt.show()
# Create correlation table for all data in df for SP500 close price
df_corr = df.corr()
print(df_corr.head())
# Visualize inner values of dataframe (numpy array of columns and rows)
# Specify figure and define axes using parameters(111) : one subplot is 1x1 for plot 1)
data = df_corr.values
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
# Define heatmap using a range from red to yellow to green
# Red = Negative, Yellow = Neutral, Green = Positive
# Colorbar for legend
heatmap = ax.pcolor(data, cmap = plt.cm.RdYlGn)
fig.colorbar(heatmap)
# Build graph
ax.set_xticks(np.arange(data.shape[0]) + 0.5, minor = False)
ax.set_yticks(np.arange(data.shape[1]) + 0.5, minor = False)
ax.invert_yaxis()
ax.xaxis.tick_top()
column_labels = df_corr.columns
row_labels = df_corr.index
# Make x labels vertical, set limit of colors (-1 = min, 1 = max)
ax.set_xticklabels(column_labels)
ax.set_yticklabels(row_labels)
plt.xticks(rotation = 90)
heatmap.set_clim(-1, 1)
plt.tight_layout()
# plt.show()
visualize_data()
# Next create features/labels, use ML for trading strategy & possible investments.
# Part 9: Preprocessing Data for Machine Learning (ML) Model
# Part 10: Setting Target
# svm = support-vector machine
# Cross validation to shuffle data and create training & testing samples
# Voting classifier - use many classifiers, let them voite on best classifier
# For tutorials on Practical Machine Learning with Python visit: /
# https://pythonprogramming.net/machine-learning-tutorial-python-introduction/
# Machine Learnings: Preprocess Data for Trading Strategy
# Disclaimer: This script is for educational purposes only.
# To build more accurate ML models, you might focus on specific time frames (i.e. 1-2 yrs)
# Further, you might need more data.
# We use one-day data because it is available for free.
# Create features/labels, use ML for trading strategy & possible investments.
# Theory: groups of companies might move up/down together (using pricing data).
# Take S&P500 dataset (closing price over time, 2000-2020), use machine learning.
# Normalize dataset by converting pricing data to % change
# Features = define, labels = target
# Labels = Buy, Sell, or Hold
# Take feature data and determine label by asking:
# "Within next 7 trading days did price go up more than x% (i.e. 2%)?"
# If yes, sell company.
# If no, hold company.
# Each model generated per company
# Each company model considers pricing data from entire SP500 dataset
# To look further into the future, i.e. 30 days, change to "hm_days = 30:
def process_data_for_labels(ticker):
hm_days = 7
fileDataSet = pd.read_csv('sp500_joined_closes.csv', index_col = 0)
tickers = fileDataSet.columns.values.tolist()
fileDataSet.fillna(0, inplace = True)
# The range will go up to a certain point (for 7 days)
# Create custom dataset to predict future values based on percentage change
# Value in percent change = price in two days from now
# less today's price, divided by today's price, multiplied by 100.
for i in range(1, hm_days+1):
fileDataSet['{}d'.format(ticker, i)] = (fileDataSet[ticker].shift(-i) - fileDataSet[ticker]) / fileDataSet[ticker]
fileDataSet.fillna(0, inplace = True)
return tickers, fileDataSet
# Here we look at " " company (ticker = " ")
process_data_for_labels('BAC')
# Next, generate labels for targets using machine learning for investing with Python
# Based on percent change pricing information, should we buy, sell, or hold company?
# See mapping functions tutorials at https://pythonprogramming.net/python-function-mapping-pandas/
# Args and kwargs tutorials at https://pythonprogramming.net/args-kwargs-intermediate-python-tutorial/
# We will also back-test out strategy.
# Example here using percent change = 2% in a week.
# If company stock price changes by 2% in 7 days get out of position for that company.
# 0 = hold, -1 = sell, +1 = buy
# If data is inbalanced you can adjust the algorithm for higher accuracy
# Aiming for > 33% accuracy (for this example)
# Example if you are incorrect on the hold, did not think it would change more than 2%, but it did...
def buy_sell_hold(*args):
cols = [c for c in args]
requirement = 0.02
for col in cols:
if col > requirement:
return 1
if col < -requirement:
return -1
return 0
# Part 11 ML Labels
# Map the function above to a column
def extract_featuresets(ticker):
tickers, fileDataSet = process_data_for_labels(ticker)
# Define new column, value = mapped function using arg
# Arg = 7-day & change for future price; Pass paramters to function
# Generate labels for buy, sell, or hold
fileDataSet['{}_target'.format(ticker)] = list(map(buy_sell_hold, fileDataSet[[c for c in fileDataSet.columns if c not in tickers]].values))
vals = fileDataSet['{}_target'.format(ticker)].values.tolist()
str_vals = [str(i) for i in vals]
print('Data spread:', Counter(str_vals))
# List of options
fileDataSet.fillna(0, inplace = True)
fileDataSet = fileDataSet.replace([np.inf, -np.inf], np.nan)
fileDataSet.dropna(inplace = True)
# Normalizing data set for ticker (prices) by using percent change.
fileDataSet_vals = fileDataSet[[ticker_name for ticker_name in tickers]].pct_change()
fileDataSet_vals = fileDataSet_vals.replace([np.inf, -np.inf], 0)
fileDataSet_vals.fillna(0, inplace = True)
X = fileDataSet_vals.values
y = fileDataSet['{}_target'.format(ticker)].values
return X, y, fileDataSet
extract_featuresets('BAC')
# Create new Machine Learning function
# Create training and testing, 25% sample data will be tested against for accuracy
# Create classifier, define
# X_train is (fileDataset_vals, above) the percent change data for all companies, including company testing for
# y is the target classification (0 = hold, 1 = buy, -1 = sell)
# Use classifier that will fit input data to target
# clf = neighbors.KNeighborsClassifier()
#
# clf.fit(X_train, y_train)
# confidence = clf.score(X_test, y_test)
# print('Accuracy', confidence)
# Going forward, if you train and do not wish to retrain this model (above) pickle the classifier
# Load the pickle file, clf.predict will run and return list of predictions
# predictions = clf.predict(X_test)
# print('Predicted spread:', Counter(predictions))
#
# print('Confidence:', Counter(confidence))
# return confidence
# Here we are looking at Bank of America (ticker = BAC)
# do_ml('BAC')
# Example Output: 0 = hold, -1 = sell, 1 = buy (for BAC we had more holds, followed by sells and least for buys)
# Data spread: Counter({'0': 2558, '1': 1462, '-1': 1110})
# Data spread: Counter({'0': 2026, '1': 1701, '-1': 1403})
# Accuracy 0.38347622759158223
# Predicted spread: Counter({0: 758, -1: 318, 1: 207})
# Part 12: ML Algorithm with Voting Classifier
# Repeat above using Voting Classifier
def do_ml(ticker):
X, y, fileDataSet = extract_featuresets(ticker)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25)
#clf = neighbors.KNeighborsClassifier()
# Replace simple classifier with voting classifier:
# Voting classifier will take list of tuples of classifier by name, classifier
# List contains tuples (i.e. 3 classifiers: linear svc, neigbors, random forest classifiers)
#clf = VotingClassifier([('lsvc', svm.LinearSVC()),
clf = VotingClassifier([('lsvc', LinearSVC()),
('knn', neighbors.KNeighborsClassifier()),
('rfor', RandomForestClassifier())])
clf.fit(X_train, y_train)
confidence = clf.score(X_test, y_test)
print('Accuracy', confidence)
predictions = clf.predict(X_test)
print('Predicted spread:', Counter(predictions))
return confidence
do_ml('BAC')
# STOCK MARKET DATA VISUALIZATIONS
# Change the ticker to look at a different company
# Here we are using Tesla(ticker = TSLA) as an example
# Loading data into dataframe:
datafile = 'tsla.csv'
data = pd.read_csv(datafile, index_col = 'Date')
# Converting the dates from string to datetime format:
data.index = pd.to_datetime(data.index)
# We need to exctract the OHLC prices into a list of lists:
dvalues = data[['Open', 'High', 'Low', 'Close']].values.tolist()
# Dates in our index column are in datetime format, we need to comvert them
# to Matplotlib date format (see https://matplotlib.org/3.1.1/api/dates_api.html):
pdates = mdates.date2num(data.index)
# If dates in our index column are strings instead of datetime objects, we should use:
# pdates = mplf.dates.datestr2num(data.index)
# We prepare a list of lists where each single list is a [date, open, high, low, close] sequence:
ohlc = [ [pdates[i]] + dvalues[i] for i in range(len(pdates)) ]
# Pass ohlc matrix into mpl-finance to create candlestick chart:
plt.style.use('fivethirtyeight')
fig, ax = plt.subplots(figsize = (12,6))
mplf.plot_day_summary_ohlc(ax, ohlc[-50:], ticksize = 5)
ax.set_xlabel('Date')
ax.set_ylabel('Price ($)')
ax.set_title('Tesla - Bar Chart')
# Display dates as "Month Year":
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %Y'))
# Automatically arrange data labels in a readable way:
fig.autofmt_xdate()
plt.show() # add this if you're not using Jupyter Notebook
fig, ax = plt.subplots(figsize = (12,6))
mplf.candlestick_ohlc(ax, ohlc[-50:], width=0.4)
ax.set_xlabel('Date')
ax.set_ylabel('Price ($)')
ax.set_title('Tesla - Candlestick Chart')
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %Y'))
fig.autofmt_xdate()
plt.show() # add this if you're not using Jupyter Notebook
# Loading data into dataframe for Tesla (ticker = TSLA):
datafile = 'tsla.csv'
data = pd.read_csv(datafile, index_col = 'Date')
# Converting the dates from string to datetime format:
data.index = pd.to_datetime(data.index)
hsma40 = data['High'].rolling(40).mean()
lsma40 = data['Low'].rolling(40).mean()
ema15 = data['Close'].ewm(15).mean()
fig, ax = plt.subplots(figsize = (12,6))
mplf.plot_day_summary_ohlc(ax, ohlc[-100:], ticksize = 4, colorup='#77d879', colordown='#db3f3f')
ax.plot(hsma40[-100:], color = 'blue', linewidth = 2, label='High, 40-Day SMA')
ax.plot(lsma40[-100:], color = 'blue', linewidth = 2, label='Low, 40-Day SMA')
ax.plot(ema15[-100:], color = 'red', linestyle='--', linewidth = 2, label='Close, 15-Day EMA')
ax.set_xlabel('Date')
ax.set_ylabel('Price ($)')
ax.set_title('Tesla - Bar Chart with Moving Averages')
ax.legend()
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %Y'))
fig.autofmt_xdate()
plt.show() # add this if you're not using Jupyter Notebook