In cryptocurrency price variability, relative highs and lows in the price point are common indicators for investors to buy and sell currencies for profit gain. This machine learning algorithm takes advantage of this trend for profit maximization. The problem with traditional algorithms is they attempt to learn the statistics of the price dynamics. However, the statistics are unpredictable and are rarely constant. However, algorithms that use data such as relative high and low price points during sampled time intervals can learn smart decisions regardless of unpredictable statistics in price dynamics.

!/usr/bin/env python3

-- coding: utf-8 --

import gdax import datetime import numpy as np import sys import sklearn

from sklearn.neural_network import MLPClassifier

product = "{}-{}".format("ETH", "USD") publicClient = gdax.PublicClient() size = 0 data = np.zeros([100,5])

count = 0

for num in range(0,10): while size<100: unit = 60 ten_min = datetime.timedelta(minutes=10) now = datetime.datetime.now() - (count*ten_min) start_time = now - ten_min hist = publicClient.get_product_historic_rates( product, start=start_time.isoformat(), end=now.isoformat(), granularity=unit) #print(datetime.datetime.fromtimestamp(int(hist[0][0])).isoformat()) #print(datetime.datetime.fromtimestamp(int(hist[-1][0])).isoformat()) for i in range(0,len(hist)): #print(hist) if 'message' in hist: continue h = hist[i] for j in range(0,5): data[size,j] = h[j] size = size + 1 if size>=99: break count = count + 1 np.flipud(data) iterations = 0 arr_profit = [] arr_profit.insert(0,-sys.maxsize) initial_amount = 100

if num==1:
    clf = MLPClassifier(solver='sgd',hidden_layer_sizes=(5,8,3,1))
    clf.fit(data[:,1:5],-initial_amount*np.ones((100,1)) )


while True:

   if iterations>10:
       if check_minima_maxima(arr_profit):
           break;
       else:
           if iterations>=50:
               break;

   batch_size = 100

   usd_wallet = np.zeros([batch_size,1])
   bitcoin_wallet = np.zeros([batch_size,1])
   prices = (data[:,1:2] + data[:,2:3] + data[:,3:4] + data[:,4:5])/4
   labels = np.empty([batch_size,1])
   amount = np.zeros([batch_size,1])
   labels[0] = 0
   bitcoin_wallet[0] = initial_amount/prices[0]
   amount[0] = initial_amount
   predictions = clf.predict(data[:,1:5])
   print(predictions)
   for i in range(1,len(predictions)):
        predict_ = predictions[i]
        usd = usd_wallet[i-1]
        bitcoin = bitcoin_wallet[i-1]
        price = prices[i]
        if predict_>0:
            if predict_>bitcoin:
                labels[i] = (bitcoin - predict_)*price

                amount[i] = amount[i-1] + (bitcoin - predict_)*price
                continue
            else:
                usd_wallet[j] = usd + predict_*price
                bitcoin_wallet[j] = bitcoin - predict_/price    

        elif predict_<0:
            predict_ = abs(predict_)
            if predict_>usd:
                labels[i] = usd - predict_

                amount[i] = amount[i-1] + usd - predict_
                continue
            else:
                usd_wallet[j] = usd_wallet[j-1] - predict_
                bitcoin_wallet[j] = bitcoin_wallet[j-1] + predict_/price

        else:
            labels[i] = 0
            amount[i] = amount[i-1]
            continue             

        amount[i] = usd_wallet[i] + bitcoin_wallet[i]*price   

   #train neural network with data and amount
   clf.fit(data[:,1:5],amount)    
   final_amount = amount[batch_size - 1]
   arr_profit.insert(0,final_amount)    
   iterations = iterations + 1

def check_minima_maxima(var): ptr = 0 temp = var[ptr] similar = 0 ptr = ptr + 1 flag = True while 1: element = var[ptr] if abs(element - temp)<=10: similar = similar + 1 if similar>=10: return flag ptr = ptr + 1
flag = False return flag

Built With

Share this project:
×

Updates