def recommend_trade(self, nn_price, last_sample, fee_amount=get_fee_amount()): fee_amount = fee_amount * 2 # fee x 2 since we'd need to clear both buy and sell fees to be profitable fee_amount = fee_amount * settings.FEE_MANAGEMENT_STRATEGY # see desc in settings.py anticipated_percent_increase = (nn_price - last_sample) / last_sample if abs(anticipated_percent_increase) < fee_amount: should_trade = 'HOLD' elif anticipated_percent_increase > fee_amount: should_trade = 'BUY' elif anticipated_percent_increase < fee_amount: should_trade = 'SELL' return should_trade
def handle(self, *args, **options): # setup buffer_between_prediction_and_this_script_mins = datetime.datetime.now().minute % 10 granularity_mins = settings.TRADER_GRANULARITY_MINS ticker = 'BTC_ETH' # get data date_of_timerange_we_care_about_predictions_start = datetime.datetime.now() - datetime.timedelta( seconds=((granularity_mins) * 60 + (60 * (1 + buffer_between_prediction_and_this_script_mins)))) date_of_timerange_we_care_about_predictions_end = datetime.datetime.now() - datetime.timedelta( seconds=((granularity_mins) * 60)) tr_timerange_end = TradeRecommendation.objects.filter( symbol=ticker, created_on__gte=date_of_timerange_we_care_about_predictions_start, created_on__lte=date_of_timerange_we_care_about_predictions_end).order_by('-created_on').first().created_on tr_timerange_start = tr_timerange_end - datetime.timedelta(seconds=120) price_timerange_start = tr_timerange_end price_timerange_end = tr_timerange_end + datetime.timedelta(seconds=(granularity_mins * 60)) trs = TradeRecommendation.objects.filter(created_on__gte=tr_timerange_start, created_on__lte=tr_timerange_end) price_now = Price.objects.filter(symbol=ticker, created_on__lte=price_timerange_end ).order_by('-created_on').first().price price_then = Price.objects.filter(symbol=ticker, created_on__lte=price_timerange_start ).order_by('-created_on').first().price # nn attributes pct_buy = round(1.0 * sum(tr.recommendation == 'BUY' for tr in trs) / len(trs), 2) pct_sell = round(1.0 * sum(tr.recommendation == 'SELL' for tr in trs) / len(trs), 2) pct_hold = round(1.0 * sum(tr.recommendation == 'HOLD' for tr in trs) / len(trs), 2) price_diff = price_now - price_then price_pct = price_diff / price_then # -1 = sell, 0 = hold, 1 = wait price_buy_hold_sell = 0 if abs(price_pct) < get_fee_amount() else (1 if price_pct > 0 else -1) avg_nn_rec = 1.0 * sum(tr.net_amount for tr in trs) / len(trs) weighted_avg_nn_rec = 1.0 * sum(tr.net_amount * (tr.confidence / 100.0) for tr in trs) / len(trs) directionally_same = ((avg_nn_rec > 0 and price_buy_hold_sell > 0) or (avg_nn_rec < 0 and price_buy_hold_sell < 0)) delta = abs(abs(avg_nn_rec) - abs(price_buy_hold_sell)) * (1 if directionally_same else -1) pc = PerformanceComp(symbol=ticker, price_timerange_start=price_timerange_start, price_timerange_end=price_timerange_end, tr_timerange_start=tr_timerange_start, tr_timerange_end=tr_timerange_end, nn_rec=avg_nn_rec, actual_movement=price_buy_hold_sell, delta=delta, pct_buy=pct_buy, pct_sell=pct_sell, pct_hold=pct_hold, rec_count=trs.count(), weighted_avg_nn_rec=weighted_avg_nn_rec, directionally_same=directionally_same, directionally_same_int=1 if directionally_same else 0, created_on_str=(tr_timerange_end - datetime.timedelta(hours=7)).strftime('%Y-%m-%d %H:%M')) pc.save()
def get_classifier(self, train=True, test=True): all_output = "" h = .02 # step size in the mesh self.names = [ "Nearest Neighbors", "Linear SVM", "RBF SVM", "Decision Tree", "Random Forest", "AdaBoost", "Naive Bayes", "Linear Discriminant Analysis", "Quadratic Discriminant Analysis" ] classifiers = [ KNeighborsClassifier(3), SVC(kernel="linear", C=0.025), SVC(gamma=2, C=1), DecisionTreeClassifier(max_depth=5), RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1), AdaBoostClassifier(), GaussianNB(), LinearDiscriminantAnalysis(), QuadraticDiscriminantAnalysis() ] for i in range(0, len(self.names)): if self.names[i] == self.name: clf = classifiers[i] if train: start_time = int(time.time()) data = self.get_latest_prices(normalize=False) price_datasets = [[], []] for i, val in enumerate(data): try: # get classifier projection sample = create_sample_row(data, i, self.datasetinputs) last_price = data[i + self.datasetinputs - 1] next_price = data[i + self.datasetinputs] change = next_price - last_price pct_change = change / last_price fee_pct = get_fee_amount() fee_pct = fee_pct * 2 # fee x 2 since we'd need to clear both buy and sell fees to be profitable fee_pct = fee_pct * settings.FEE_MANAGEMENT_STRATEGY # see desc in settings.py do_buy = ClassifierTest.HOLD if abs( pct_change) < fee_pct else ( ClassifierTest.BUY if change > 0 else ClassifierTest.SELL) price_datasets[0].append(sample) price_datasets[1].append(do_buy) except Exception: pass data = price_datasets if self.timedelta_back_in_granularity_increments == 0: train_data = data test_data = [[], []] else: train_data = [ data[0][0:(-1 * self.timedelta_back_in_granularity_increments)], data[1][0:(-1 * self.timedelta_back_in_granularity_increments)] ] test_data = [ data[0][len(train_data[0]):], data[1][len(train_data[1]):] ] self.datasets = train_data X, y = train_data X = StandardScaler().fit_transform(X) self.X_train, self.X_test, self.y_train, self.y_test = train_test_split( X, y, test_size=.4) self.min = {} self.max = {} self.xz = () mesh_args = [] for i in range(0, self.datasetinputs): self.min[i], self.max[i] = X[:, i].min() - .5, X[:, i].max() + .5 mesh_args.append(np.arange(self.min[i], self.max[i], h)) self.xz = np.meshgrid(*mesh_args) clf.fit(self.X_train, self.y_train) score = clf.score(self.X_test, self.y_test) # Plot the decision boundary. For that, we will assign a color to each # point in the mesh [self.x_min, m_max]x[self.y_min, self.y_max]. self.ravel_args = [] for i in range(0, self.datasetinputs): self.ravel_args.append(self.xz[i].ravel()) self._input = np.column_stack(self.ravel_args) if hasattr(clf, "decision_function"): self.Z = clf.decision_function(self._input) else: self.Z = clf.predict_proba(self._input)[:, 1] if test and len(test_data) > 0: stats = { 'r': 0, 'w': 0, 'p': { 0: 0, 1: 0, -1: 0 }, 'a': { 0: 0, 1: 0, -1: 0 } } ds = test_data for i in range(0, len(ds[0])): sample = ds[0][i] actual = ds[1][i] sample = StandardScaler().fit_transform(sample) prediction = clf.predict(sample) self.prediction = prediction stats['p'][prediction[0]] += 1 stats['a'][actual] += 1 stats['r' if actual == prediction[0] else 'w'] = stats[ 'r' if actual == prediction[0] else 'w'] + 1 pct_correct = (1.0 * stats['r'] / (stats['r'] + stats['w'])) all_output = all_output + str( ('stats', self.name, round(pct_correct, 2))) all_output = all_output + str(('stats_debug', stats)) self.percent_correct = int(pct_correct * 100) self.prediction_size = len(test_data[0]) all_output = all_output + str((self.name, round(score * 100))) self.score = score * 100 end_time = int(time.time()) self.time = end_time - start_time self.output = all_output self.clf = clf return clf
def calculatefees(self): self.fee_amount = self.amount * get_fee_amount()
def get_classifier(self, train=True, test=True): all_output = "" h = .02 # step size in the mesh self.names = ["Nearest Neighbors", "Linear SVM", "RBF SVM", "Decision Tree", "Random Forest", "AdaBoost", "Naive Bayes", "Linear Discriminant Analysis", "Quadratic Discriminant Analysis"] classifiers = [ KNeighborsClassifier(3), SVC(kernel="linear", C=0.025), SVC(gamma=2, C=1), DecisionTreeClassifier(max_depth=5), RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1), AdaBoostClassifier(), GaussianNB(), LinearDiscriminantAnalysis(), QuadraticDiscriminantAnalysis()] for i in range(0, len(self.names)): if self.names[i] == self.name: clf = classifiers[i] if train: start_time = int(time.time()) data = self.get_latest_prices(normalize=False) price_datasets = [[], []] for i, val in enumerate(data): try: # get classifier projection sample = create_sample_row(data, i, self.datasetinputs) last_price = data[i + self.datasetinputs - 1] next_price = data[i + self.datasetinputs] change = next_price - last_price pct_change = change / last_price fee_pct = get_fee_amount() fee_pct = fee_pct * 2 # fee x 2 since we'd need to clear both buy and sell fees to be profitable fee_pct = fee_pct * settings.FEE_MANAGEMENT_STRATEGY # see desc in settings.py do_buy = ClassifierTest.HOLD if abs(pct_change) < fee_pct else ( ClassifierTest.BUY if change > 0 else ClassifierTest.SELL) price_datasets[0].append(sample) price_datasets[1].append(do_buy) except Exception: pass data = price_datasets if self.timedelta_back_in_granularity_increments == 0: train_data = data test_data = [[], []] else: train_data = [data[0][0:(-1 * self.timedelta_back_in_granularity_increments)], data[1][0:(-1 * self.timedelta_back_in_granularity_increments)]] test_data = [data[0][len(train_data[0]):], data[1][len(train_data[1]):]] self.datasets = train_data X, y = train_data X = StandardScaler().fit_transform(X) self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(X, y, test_size=.4) self.min = {} self.max = {} self.xz = () mesh_args = [] for i in range(0, self.datasetinputs): self.min[i], self.max[i] = X[:, i].min() - .5, X[:, i].max() + .5 mesh_args.append(np.arange(self.min[i], self.max[i], h)) self.xz = np.meshgrid(*mesh_args) clf.fit(self.X_train, self.y_train) score = clf.score(self.X_test, self.y_test) # Plot the decision boundary. For that, we will assign a color to each # point in the mesh [self.x_min, m_max]x[self.y_min, self.y_max]. self.ravel_args = [] for i in range(0, self.datasetinputs): self.ravel_args.append(self.xz[i].ravel()) self._input = np.column_stack(self.ravel_args) if hasattr(clf, "decision_function"): self.Z = clf.decision_function(self._input) else: self.Z = clf.predict_proba(self._input)[:, 1] if test and len(test_data) > 0: stats = {'r': 0, 'w': 0, 'p': {0: 0, 1: 0, -1: 0}, 'a': {0: 0, 1: 0, -1: 0}} ds = test_data for i in range(0, len(ds[0])): sample = ds[0][i] actual = ds[1][i] sample = StandardScaler().fit_transform(sample) prediction = clf.predict(sample) self.prediction = prediction stats['p'][prediction[0]] += 1 stats['a'][actual] += 1 stats['r' if actual == prediction[0] else 'w'] = stats['r' if actual == prediction[0] else 'w'] + 1 pct_correct = (1.0 * stats['r'] / (stats['r'] + stats['w'])) all_output = all_output + str(('stats', self.name, round(pct_correct, 2))) all_output = all_output + str(('stats_debug', stats)) self.percent_correct = int(pct_correct * 100) self.prediction_size = len(test_data[0]) all_output = all_output + str((self.name, round(score * 100))) self.score = score * 100 end_time = int(time.time()) self.time = end_time - start_time self.output = all_output self.clf = clf return clf