Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- from time import sleep
- from key import api_key, api_secret
- import sys
- from math import floor, exp
- from datetime import datetime
- import matplotlib.pyplot as plt
- import tensorflow as tf
- import numpy as np
- from binance.client import Client
- def dump_data(d, f):
- with open(f, 'w') as df:
- json.dump(d, df, indent=4)
- def read_data(f):
- with open(f, 'r') as df:
- return json.load(df)
- class DataCollector(object):
- def __init__(self):
- self.training_data_file = 'data/training/train_data1.json'
- self.testing_data_file = 'data/testing/test_data1.json'
- self.sleeptime = .5
- self.model = tf.keras.models.Sequential()
- self.client = Client(api_key, api_secret)
- self.training_data = read_data(self.training_data_file)
- self.testing_data = read_data(self.testing_data_file)
- self.tr_x, self.tr_y = self.parse_data(self.training_data)
- self.te_x, self.te_y = self.parse_data(self.testing_data)
- self.prev = 40
- self.aft = 20
- def collect(self, run=True, outfile=None):
- dp = 0
- data = dict()
- while run is True:
- try:
- time = str(datetime.now())[11:21]
- price = float(self.client.get_ticker(symbol='BTCUSDT')['lastPrice'])
- data[time] = price
- print('(Datapoint: {}) BTC-USD | {}: {}$'.format(dp, time, price))
- sleep(self.sleeptime)
- dp += 1
- except KeyboardInterrupt:
- print('\n KeyboardInterrupt Writing Data to Outfile')
- dump_data(data, outfile)
- sys.exit(1)
- def get_point_array(self, a, p, prev=None, aft=None):
- if prev is None: prev = self.prev
- if aft is None: aft = self.aft
- return a[p-prev:p+aft+1]
- def graph(self, max, min=0, interval=1):
- #not working
- x, y = self.parse_data(self.data)
- plt.plot(x, y)
- plt.show()
- def parse_data(self, d):
- x = [i for i in range(len(d))]
- y = [d[x] for x in d]
- return x, y
- def get_trend(self, array):
- trend = []
- for v in range(len(array)):
- if v != 0:
- avg = ((array[v]+array[v-1])/2)
- trend.append(round((array[v]-array[v-1]), 10))
- return trend
- def get_training_set(self):
- x_set = []
- y_set = []
- for i in range(len(self.tr_y)):
- if i > self.prev and i < len(self.tr_y) - self.aft:
- x, y = self.training_certify(p=i)
- x_set.append(x)
- y_set.append(y)
- return x_set, y_set
- def get_testing_set(self):
- x_set = []
- y_set = []
- for i in range(len(self.te_y)):
- if i > self.prev and i < len(self.te_y) - self.aft:
- x, y = self.testing_certify(p=i)
- x_set.append(x)
- y_set.append(y)
- return x_set, y_set
- def create_model(self):
- x, y = self.get_training_set()
- x_train = tf.keras.utils.normalize(x, axis=1)
- y_train = np.array(y)
- self.model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
- self.model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
- self.model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))
- self.model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
- self.model.fit(x_train, y_train, epochs=3)
- def test_model(self):
- x, y = self.get_testing_set()
- x_test = tf.keras.utils.normalize(x, axis=1)
- y_test = np.array(y)
- val_loss, val_acc = self.model.evaluate(x_test, y_test)
- print('LOST VALUES:', val_loss)
- print('ACCURACY:', val_acc)
- def sigmoid(self, x):
- return 1 / (1 + exp(-x))
- def training_certify(self, p):
- target_y = self.tr_y[p]
- sub_y = self.get_point_array(self.tr_y, p)
- div = len(sub_y) - self.aft - 1
- pre_y = sub_y[:div]
- post_y = sub_y[div::]
- post_trend = self.get_trend(post_y)
- worth = sum(post_trend)
- out_set = sub_y + [target_y]
- if worth > 0: worth = 1
- else: worth = 0
- return out_set, worth
- def testing_certify(self, p):
- target_y = self.te_y[p]
- sub_y = self.get_point_array(self.te_y, p)
- div = len(sub_y) - self.aft - 1
- pre_y = sub_y[:div]
- post_y = sub_y[div::]
- post_trend = self.get_trend(post_y)
- worth = sum(post_trend)
- out_set = sub_y + [target_y]
- if worth > 0: worth = 1
- else: worth = 0
- return out_set, worth
- dc = DataCollector()
- #dc.collect(outfile='data/testing/test_data2.json')
- dc.create_model()
- #0.82490754
- dc.test_model()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement