MCPcopy
hub / github.com/owocki/pytrader / get_classifier

Method get_classifier

history/models.py:278–388  ·  view source on GitHub ↗
(self, train=True, test=True)

Source from the content-addressed store, hash-verified

276 type='mock')
277
278 def get_classifier(self, train=True, test=True):
279
280 all_output = ""
281 h = .02 # step size in the mesh
282 self.names = ["Nearest Neighbors", "Linear SVM", "RBF SVM", "Decision Tree",
283 "Random Forest", "AdaBoost", "Naive Bayes", "Linear Discriminant Analysis",
284 "Quadratic Discriminant Analysis"]
285 classifiers = [
286 KNeighborsClassifier(3),
287 SVC(kernel="linear", C=0.025),
288 SVC(gamma=2, C=1),
289 DecisionTreeClassifier(max_depth=5),
290 RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
291 AdaBoostClassifier(),
292 GaussianNB(),
293 LinearDiscriminantAnalysis(),
294 QuadraticDiscriminantAnalysis()]
295
296 for i in range(0, len(self.names)):
297 if self.names[i] == self.name:
298 clf = classifiers[i]
299
300 if train:
301 start_time = int(time.time())
302 data = self.get_latest_prices(normalize=False)
303 price_datasets = [[], []]
304 for i, val in enumerate(data):
305 try:
306 # get classifier projection
307 sample = create_sample_row(data, i, self.datasetinputs)
308 last_price = data[i + self.datasetinputs - 1]
309 next_price = data[i + self.datasetinputs]
310 change = next_price - last_price
311 pct_change = change / last_price
312 fee_pct = get_fee_amount()
313 fee_pct = fee_pct * 2 # fee x 2 since we'd need to clear both buy and sell fees to be profitable
314 fee_pct = fee_pct * settings.FEE_MANAGEMENT_STRATEGY # see desc in settings.py
315 do_buy = ClassifierTest.HOLD if abs(pct_change) < fee_pct else (
316 ClassifierTest.BUY if change > 0 else ClassifierTest.SELL)
317 price_datasets[0].append(sample)
318 price_datasets[1].append(do_buy)
319 except Exception:
320 pass
321
322 data = price_datasets
323 if self.timedelta_back_in_granularity_increments == 0:
324 train_data = data
325 test_data = [[], []]
326 else:
327 train_data = [data[0][0:(-1 * self.timedelta_back_in_granularity_increments)],
328 data[1][0:(-1 * self.timedelta_back_in_granularity_increments)]]
329 test_data = [data[0][len(train_data[0]):], data[1][len(train_data[1]):]]
330 self.datasets = train_data
331
332 X, y = train_data
333 X = StandardScaler().fit_transform(X)
334 self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(X, y, test_size=.4)
335

Callers 2

get_tradersMethod · 0.95
do_classifier_testFunction · 0.95

Calls 4

create_sample_rowFunction · 0.90
get_fee_amountFunction · 0.90
get_latest_pricesMethod · 0.80
predictMethod · 0.45

Tested by

no test coverage detected