This repository has been archived by the owner on Jul 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
146 lines (126 loc) · 4.42 KB
/
model.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
import sys
sys.path.append("../")
import pandas as pd
import numpy as np
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.metrics import confusion_matrix as cm
import ta
from sklearn.pipeline import Pipeline
from sklearn.model_selection import KFold
import copy
import pickle
class OceanModel:
def __init__(self, exchange, pair, timeframe):
self.model_name = self.__class__.__name__
self.model = None
self.exchange = exchange.lower()
self.pair = pair.replace("/", "-").lower()
self.timeframe = timeframe
self.predictor = LinearDiscriminantAnalysis()
def feature_extraction(self, dataframe):
return dataframe[["close"]]
def format_train_data(self, path):
data = pd.read_csv(path)
data = data.set_index("timestamp")
data.index = pd.to_datetime(data.index, utc=True).tz_convert(None)
mindate = pd.to_datetime("2019-01-01", utc=True).tz_convert(None)
data = data.iloc[data.index > mindate, :]
freqstr = self.timeframe[:-1] + "T"
data = data.asfreq(freqstr)
data = data[["close"]]
data = self.feature_extraction(data)
data["labels"] = np.where(data["close"].diff().shift(-1) > 0, 1, 0)
data["returns"] = data["close"].diff().shift(-1) / data["close"]
data = data.dropna()
x = data.drop(["labels", "returns"], axis=1).values
y = data["labels"].values
r = data["returns"].values
return x, y, r
def format_test_data(self, dataframe):
dataframe = self.feature_extraction(dataframe)
dataframe = dataframe.dropna()
return dataframe
def train_from_csv(self, path):
x, y, r = self.format_train_data(path)
self.train(x, y, r)
def train(self, x, y, r):
if self.predictor is None:
raise Exception("Division by zero is not allowed.")
print("creating model")
self.model = Pipeline(
steps=[
("Predictor", self.predictor),
]
)
self.model.fit(x, y)
def predict(self, last_candles):
dataframe = copy.deepcopy(last_candles)
dataframe = self.feature_extraction(dataframe)
prob = self.model.predict_proba(dataframe.values[[-1], :])[0]
yhat = self.model.predict(dataframe.values[[-1], :])[0]
return yhat, prob[yhat]
def back_test_crossval(self, nfolds, path):
model = Pipeline(
steps=[
("Predictor", self.predictor),
]
)
x, y, r = self.format_train_data(path)
kf = KFold(n_splits=nfolds)
pred_returns = np.zeros((nfolds,))
opt_returns = np.zeros((nfolds,))
timeframes = np.zeros((nfolds,))
ACC = np.zeros((nfolds,))
conf_mat = np.zeros((2, 2))
for i, (ind_train, ind_test) in enumerate(kf.split(x)):
model.fit(x[ind_train, :], y[ind_train])
yhat = model.predict(x[ind_test, :])
sample_weight_test = np.abs(r[ind_test])
ACC[i] = model.score(x[ind_test, :], y[ind_test])
conf_mat += cm(y[ind_test], yhat)
pred_returns[i] = np.sum(r[ind_test][yhat == 1])
opt_returns[i] = np.sum(r[ind_test][y[ind_test] == 1])
timeframes[i] = ind_test.shape[0]
# returns:
# mean of predicted_returns across folds
# mean of returns using a perfect oracle for predictions
# average number of smaples per fold
# average accuracy
# confusion matrix
return (
np.mean(pred_returns),
np.mean(opt_returns),
np.mean(timeframes),
np.mean(ACC),
conf_mat,
)
def pickle_model(self, path):
model_name = (
path
+ "/"
+ self.model_name
+ "_"
+ self.exchange
+ "_"
+ self.pair
+ "_"
+ self.timeframe
+ ".pkl"
)
with open(model_name, "wb") as f:
pickle.dump(self.model, f)
def unpickle_model(self, path):
model_name = (
path
+ "/"
+ self.model_name
+ "_"
+ self.exchange
+ "_"
+ self.pair
+ "_"
+ self.timeframe
+ ".pkl"
)
with open(model_name, "rb") as f:
self.model = pickle.load(f)