In [321]:
import csv
import requests
import datetime
import pandas as pd
import seaborn
import matplotlib.pyplot as plt
import numpy as np
import math

from sklearn.linear_model import LinearRegression
from sklearn import preprocessing, cross_validation, svm
In [322]:
# Pretty print the JSON
import uuid
from IPython.display import display_javascript, display_html, display
import json

class RenderJSON(object):
    def __init__(self, json_data):
        if isinstance(json_data, dict):
            self.json_str = json.dumps(json_data)
        else:
            self.json_str = json_data
        self.uuid = str(uuid.uuid4())

    def _ipython_display_(self):
        display_html('<div id="{}" style="height: 600px; width:100%;"></div>'.format(self.uuid), raw=True)
        display_javascript("""
        require(["https://rawgit.com/caldwell/renderjson/master/renderjson.js"], function() {
        document.getElementById('%s').appendChild(renderjson(%s))
        });
        """ % (self.uuid, self.json_str), raw=True)
In [323]:
#Defining Price Data From Crypto cryptocompare.com
def price(symbol, comparison_symbols=['BTC'], exchange=''):
    url = 'https://min-api.cryptocompare.com/data/price?fsym={}&tsyms={}'\
            .format(symbol.upper(), ','.join(comparison_symbols).upper())
    if exchange:
        url += '&e={}'.format(exchange)
    page = requests.get(url)
    data = page.json()
    return data
In [324]:
# Displaying Currency
price('LTC', exchange='binance')
Out[324]:
{'BTC': 0.01268}
In [325]:
# Pulling Historical Minute Data From cryptocompare.com API
def minute_price_historical(symbol, comparison_symbol, limit, aggregate, exchange=''):
    url = 'https://min-api.cryptocompare.com/data/histominute?fsym={}&tsym={}&limit={}&aggregate={}'\
            .format(symbol.upper(), comparison_symbol.upper(), limit, aggregate)
    if exchange:
        url += '&e={}'.format(exchange)
    page = requests.get(url)
    data = page.json()['Data']
    df = pd.DataFrame(data)
    df['timestamp'] = [datetime.datetime.fromtimestamp(d) for d in df.time]
    return df
In [326]:
time_delta = 1 # Bar width in minutes
df = minute_price_historical('BTC', 'USD', 9999, time_delta)
df = df[['close','high','low','open','time','volumefrom','volumeto']]
#print('Max length = %s' % len(df))
#print('Max time = %s' % (df.timestamp.max() - df.timestamp.min()))

time_delta = 1 # Bar width in minutes
ts = minute_price_historical('BTC', 'USD', 9999, time_delta)
ts = ts[['close','high','low','open','time','volumefrom','volumeto','timestamp']]
#print('Max length = %s' % len(df))
#print('Max time = %s' % (df.timestamp.max() - df.timestamp.min()))

# Plotting Pulled Data
plt.title('BTC vs USD')
plt.xlabel('Time')
plt.ylabel('Price')
plt.plot(ts.timestamp, df.close)
plt.xticks(rotation=45)
plt.show()
#df.to_csv('btc_minute.csv')
In [327]:
# Displaying Data
df.tail()
Out[327]:
close high low open time volumefrom volumeto
1996 6344.39 6346.10 6341.66 6341.69 1530381960 21.59 137489.93
1997 6342.10 6347.01 6341.95 6344.39 1530382020 24.00 152175.73
1998 6344.86 6347.16 6342.07 6342.07 1530382080 25.57 162427.74
1999 6346.48 6347.11 6342.81 6344.86 1530382140 23.02 146362.87
2000 6344.77 6346.86 6344.77 6346.47 1530382200 8.25 52359.32
In [328]:
#forecast_out = int(30) # predicting 21 minutes into future
forecast_out = int(math.ceil(0.01*len(df)))
df['Prediction'] = df[['close']].shift(-forecast_out) #  label column with data shifted 21 units up
#df.to_csv('prediction')

X = np.array(df.drop(['Prediction'], 1))
X = preprocessing.scale(X)

X_forecast = X[-forecast_out:] # set X_forecast equal to last 21
X = X[:-forecast_out] # remove last 21 from X

y = np.array(df['Prediction'])
y = y[:-forecast_out]

X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size = 0.2)

# Training
clf = LinearRegression()
clf.fit(X_train,y_train)
# Testing
confidence = clf.score(X_test, y_test)
print("confidence: ", confidence)
confidence:  0.962726183878
In [329]:
forecast_prediction = clf.predict(X_forecast)
forecast_prediction
Out[329]:
array([ 6354.81457621,  6354.84693344,  6355.51358134,  6355.5441647 ,
        6354.10256002,  6352.1649325 ,  6350.36762175,  6349.71327774,
        6346.20948474,  6343.13710109,  6347.02449051,  6350.73222549,
        6351.94186358,  6351.87095975,  6353.19890519,  6353.49974969,
        6352.05390689,  6352.81579194,  6353.14852856,  6353.29820626,
        6352.74883958])
In [330]:
pre = pd.DataFrame(forecast_prediction)
pre.columns = ['prediction']

# Date magic for our dataframe 
now = datetime.datetime.now()

time = pd.date_range(now,periods=21, freq='1min')
time
minute = pd.DataFrame(time)
#start = pre.index.min().date() 
#end = pre.index.max().date() + pd.Timedelta(1, 'D')
#pre.reindex(pd.date_range(start, end, freq='T', closed='left')).fillna(method='ffill')

pre.tail()
#date = pd.date_range(start='4/1/2018', end='4/1/2018', freq='m')
#date = pd.DataFrame(date)
#date.columns = ['date']
#date
Out[330]:
prediction
16 6352.053907
17 6352.815792
18 6353.148529
19 6353.298206
20 6352.748840
In [331]:
minute.tail()
Out[331]:
0
16 2018-06-30 14:26:59.090775
17 2018-06-30 14:27:59.090775
18 2018-06-30 14:28:59.090775
19 2018-06-30 14:29:59.090775
20 2018-06-30 14:30:59.090775
In [332]:
prediction = pd.concat([pre,minute], axis=1)
prediction.columns = ['prediction','time']

sell = prediction.prediction.max()
prediction.tail()
Out[332]:
prediction time
16 6352.053907 2018-06-30 14:26:59.090775
17 6352.815792 2018-06-30 14:27:59.090775
18 6353.148529 2018-06-30 14:28:59.090775
19 6353.298206 2018-06-30 14:29:59.090775
20 6352.748840 2018-06-30 14:30:59.090775
In [333]:
#buy = prediction.prediction.min()
In [334]:
#signal =(buy,sell)
#signal
#stuff = pd.concat([buy,sell], axis=1)
#stuff
In [335]:
#gains = sell/buy
#gains
In [336]:
confidence
Out[336]:
0.96272618387805275
In [337]:
plt.suptitle('21 Minute Future Prediction BTC vs USD',fontsize=17)
#plt.title('Potential Gains %s' % gains, fontsize=10)
plt.xlabel('Time')
plt.ylabel('Price')
plt.plot(prediction.time, prediction.prediction)
plt.xticks(rotation=45)
plt.show()
In [338]:
time_delta = 1 # Line width in minutes
ts = minute_price_historical('BTC', 'USD', 9999, time_delta)
ts = ts[['close','high','low','open','time','volumefrom','volumeto','timestamp']]

ts = ts.tail(19)

plt.title('Past BTC vs USD Data', fontsize=17)
plt.xlabel('Time')
plt.ylabel('Price')
plt.plot(ts.timestamp, ts.close)
plt.xticks(rotation=45)
plt.show()