forked from webclinic017/Finance-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse_stocker.py
More file actions
executable file
·180 lines (137 loc) · 4.92 KB
/
use_stocker.py
File metadata and controls
executable file
·180 lines (137 loc) · 4.92 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
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
from stocker import Stocker
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.style
import matplotlib as mpl
from matplotlib.pylab import rcParams
rcParams['figure.figsize'] = 20, 10
from sklearn.preprocessing import MinMaxScaler
from sklearn.linear_model import LinearRegression
from fastai.structured import add_datepart
import tensorflow as tf
from tensorflow.keras import layers
from sklearn import neighbors
from sklearn.model_selection import GridSearchCV
from pandas.util.testing import assert_frame_equal
goog = Stocker('GOOGL')
goog.plot_stock()
# Create model
model, model_data = goog.create_prophet_model(days=90)
goog.evaluate_prediction()
# Optimize the model
goog.changepoint_prior_analysis(changepoint_priors=[0.001, 0.05, 0.1, 0.2])
goog.changepoint_prior_validation(start_date='2016-01-04', end_date='2017-01-03', changepoint_priors=[0.001, 0.05, 0.1, 0.2])
# Evaluate the new model
goog.evaluate_prediction()
print(goog.evaluate_prediction(nshares=1000))
# Getting the dataframe of the data
goog_data = goog.make_df('2004-08-19', '2018-03-27')
print(goog_data.head(50))
goog_data = goog_data[['Date', 'Open', 'High', 'Low', 'Close', 'Adj. Close', 'Volume']]
print(goog_data.head(50))
# Moving Average
scaler = MinMaxScaler(feature_range=(0, 1))
df = goog_data
print(df.head())
df['Date'] = pd.to_datetime(df.Date, format='%Y-%m-%d')
df.index = df['Date']
print(df.head(50))
plt.figure(figsize=(16,8))
plt.plot(df['Date'], df['Adj. Close'], label='Close Price history')
# Creating dataframe with date and the target variable
data = df.sort_index(ascending=True, axis=0)
new_data = pd.DataFrame(index=range(0, len(df)), columns=['Date', 'Adj. Close'])
for i in range(0, len(data)):
new_data['Date'][i] = data['Date'][i]
new_data['Adj. Close'][i] = data['Adj. Close'][i]
# Train-test split
train = new_data[:2600]
test = new_data[2600:]
new_data.shape, train.shape, test.shape
num = test.shape[0]
train['Date'].min(), train['Date'].max(), test['Date'].min(), test['Date'].max()
# Making predictions
preds = []
for i in range(0, num):
a = train['Adj. Close'][len(train)-924+i:].sum() + sum(preds)
b = a/num
preds.append(b)
len(preds)
# Measure accuracy with rmse (Root Mean Squared Error)
rms=np.sqrt(np.mean(np.power((np.array(test['Adj. Close'])-preds),2)))
print(rms)
test['Predictions'] = 0
test['Predictions'] = preds
plt.plot(train['Adj. Close'])
plt.plot(test[['Adj. Close', 'Predictions']])
# Simple Linear Regression
lr_data = goog_data
lr_data.head(50)
lr_data['Date'] = pd.to_datetime(lr_data.Date, format='%Y-%m-%d')
lr_data.index = lr_data['Date']
lr_data = lr_data.sort_index(ascending=True, axis=0)
new_data = pd.DataFrame(index=range(0, len(lr_data)), columns=['Date', 'Adj. Close'])
for i in range(0,len(data)):
new_data['Date'][i] = lr_data['Date'][i]
new_data['Adj. Close'][i] = lr_data['Adj. Close'][i]
print(new_data.head(50))
add_datepart(new_data, 'Date')
new_data.drop('Elapsed', axis=1, inplace=True)
# Train-test split
train = new_data[:2600]
test = new_data[2600:]
x_train = train.drop('Adj. Close', axis=1)
y_train = train['Adj. Close']
x_test = test.drop('Adj. Close', axis=1)
y_test = test['Adj. Close']
# Implementing linear regression
model = LinearRegression()
model.fit(x_train, y_train)
# Predictions
preds = model.predict(x_test)
lr_rms = np.sqrt(np.mean(np.power((np.array(y_test)-np.array(preds)),2)))
print(lr_rms)
# Plot
test['Predictions'] = 0
test['Predictions'] = preds
plt.plot(train['Adj. Close'])
plt.plot(test[['Adj. Close', 'Predictions']])
# k-Nearest Neighbours
scaler = MinMaxScaler(feature_range=(0, 1))
# scaling the data
x_train_scaled = scaler.fit_transform(x_train)
x_train = pd.DataFrame(x_train_scaled)
x_test_scaled = scaler.fit_transform(x_test)
x_test = pd.DataFrame(x_test_scaled)
# using gridsearch to find the best value of k
params = {'n_neighbors': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]}
knn = neighbors.KNeighborsRegressor()
model = GridSearchCV(knn, params, cv=5)
# fitting the model and predicting
model.fit(x_train, y_train)
new_preds = model.predict(x_test)
# Results
k_rms = np.sqrt(np.mean(np.power((np.array(y_test)-np.array(preds)),2)))
print(k_rms)
test['Predictions'] = 0
test['Predictions'] = new_preds
plt.plot(train['Adj. Close'])
plt.plot(test[['Adj. Close', 'Predictions']])
# Multilayer Perceptron
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(100, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(100, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(1, activation=tf.nn.relu))
model.compile(optimizer='adam', loss='mean_squared_error')
X_train = np.array(x_train)
Y_train = np.array(y_train)
model.fit(X_train, Y_train, epochs=500)
preds = model.predict(x_test)
# Results
mlp_rms = np.sqrt(np.mean(np.power((np.array(y_test)-np.array(preds)),2)))
print(mlp_rms)
test['Predictions'] = 0
test['Predictions'] = preds
plt.plot(train['Adj. Close'])
plt.plot(test[['Adj. Close', 'Predictions']])