# install python-poloniex
!pip install https://github.com/s4w3d0ff/python-poloniex/archive/v0.4.6.zip
Collecting https://github.com/s4w3d0ff/python-poloniex/archive/v0.4.6.zip Downloading https://github.com/s4w3d0ff/python-poloniex/archive/v0.4.6.zip / 40kB 112kB/s Collecting requests (from poloniex==0.4.6) Downloading requests-2.18.4-py2.py3-none-any.whl (88kB) 100% |################################| 92kB 2.1MB/s ta 0:00:01 Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python2.7/dist-packages (from requests->poloniex==0.4.6) Collecting chardet<3.1.0,>=3.0.2 (from requests->poloniex==0.4.6) Downloading chardet-3.0.4-py2.py3-none-any.whl (133kB) 100% |################################| 143kB 2.6MB/s ta 0:00:01 Collecting idna<2.7,>=2.5 (from requests->poloniex==0.4.6) Downloading idna-2.6-py2.py3-none-any.whl (56kB) 100% |################################| 61kB 3.7MB/s ta 0:00:011 Collecting urllib3<1.23,>=1.21.1 (from requests->poloniex==0.4.6) Downloading urllib3-1.22-py2.py3-none-any.whl (132kB) 100% |################################| 133kB 2.8MB/s ta 0:00:01 Installing collected packages: chardet, idna, urllib3, requests, poloniex Running setup.py install for poloniex ... done Successfully installed chardet-3.0.4 idna-2.6 poloniex-0.4.6 requests-2.18.4 urllib3-1.22
import poloniex
import time
# prep poloniex API
polo = poloniex.Poloniex()
# 5min(300 seconds)100days
chart_data = polo.returnChartData('BTC_ETH', period=300, start=time.time()-polo.DAY*100, end=time.time())
#poloniex source code which explain how interval data we can get
#https://github.com/s4w3d0ff/python-poloniex
# import pandas
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
price_data = pd.DataFrame([float(i.get('open')) for i in chart_data])
mss = MinMaxScaler()
input_dataframe = pd.DataFrame(mss.fit_transform(price_data))
#devide into training data and validation data
import numpy as np
def _load_data(data, n_prev=50):
docX, docY = [], []
for i in range(len(data)-n_prev):
docX.append(data.iloc[i:i+n_prev].as_matrix())
docY.append(data.iloc[i+n_prev].as_matrix())
alsX = np.array(docX)
alsY = np.array(docY)
return alsX, alsY
#trainning data is 70%. Validationdata is 30%
def train_test_split(df, test_size=0.3, n_prev=50):
ntrn = round(len(df) * (1 - test_size))
ntrn = int(ntrn)
X_train, y_train = _load_data(df.iloc[0:ntrn], n_prev)
X_test, y_test = _load_data(df.iloc[ntrn:], n_prev)
return (X_train, y_train), (X_test, y_test)
(X_train, y_train), (X_test, y_test) = train_test_split(input_dataframe)
!pip install keras
Collecting keras Downloading Keras-2.1.3-py2.py3-none-any.whl (319kB) 100% |################################| 327kB 1.8MB/s ta 0:00:01 Requirement already satisfied: numpy>=1.9.1 in /usr/local/lib/python2.7/dist-packages (from keras) Requirement already satisfied: scipy>=0.14 in /usr/local/lib/python2.7/dist-packages (from keras) Requirement already satisfied: six>=1.9.0 in /usr/local/lib/python2.7/dist-packages (from keras) Collecting pyyaml (from keras) Downloading PyYAML-3.12.tar.gz (253kB) 100% |################################| 256kB 2.1MB/s ta 0:00:01 Building wheels for collected packages: pyyaml Running setup.py bdist_wheel for pyyaml ... done Stored in directory: /root/.cache/pip/wheels/2c/f7/79/13f3a12cd723892437c0cfbde1230ab4d82947ff7b3839a4fc Successfully built pyyaml Installing collected packages: pyyaml, keras Successfully installed keras-2.1.3 pyyaml-3.12
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.layers.recurrent import LSTM
in_out_neurons = 1
hidden_neurons = 300
length_of_sequences = 50
model = Sequential()
model.add(LSTM(hidden_neurons, batch_input_shape=(None, length_of_sequences, in_out_neurons), return_sequences=False))
model.add(Dense(in_out_neurons))
model.add(Activation("linear"))
model.compile(loss="mean_squared_error", optimizer="adam",)
/usr/local/lib/python2.7/dist-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`. from ._conv import register_converters as _register_converters Using TensorFlow backend.
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.layers.recurrent import LSTM
in_out_neurons = 1
hidden_neurons = 300
length_of_sequences = 50
model = Sequential()
model.add(LSTM(hidden_neurons, batch_input_shape=(None, length_of_sequences, in_out_neurons), return_sequences=False))
model.add(Dense(in_out_neurons))
model.add(Activation("linear"))
model.compile(loss="mean_squared_error", optimizer="adam",)
from keras.callbacks import EarlyStopping
early_stopping = EarlyStopping(monitor='val_loss', mode='auto', patience=0)
history = model.fit(X_train, y_train, batch_size=600, epochs=10, validation_split=0.1, callbacks=[early_stopping])
Train on 18099 samples, validate on 2011 samples Epoch 1/10 18099/18099 [==============================] - 45s 3ms/step - loss: 0.0041 - val_loss: 0.0033 Epoch 2/10 18099/18099 [==============================] - 45s 2ms/step - loss: 2.5816e-04 - val_loss: 3.4416e-04 Epoch 3/10 18099/18099 [==============================] - 42s 2ms/step - loss: 2.2754e-05 - val_loss: 2.9926e-05 Epoch 4/10 18099/18099 [==============================] - 41s 2ms/step - loss: 1.2393e-05 - val_loss: 2.4886e-05 Epoch 5/10 18099/18099 [==============================] - 42s 2ms/step - loss: 1.1886e-05 - val_loss: 2.4197e-05 Epoch 6/10 18099/18099 [==============================] - 47s 3ms/step - loss: 1.1829e-05 - val_loss: 2.4798e-05
%matplotlib inline
from matplotlib import pyplot as plt
pred_data = model.predict(X_train)
plt.plot(y_train, label='train')
plt.plot(pred_data, label='pred')
plt.legend(loc='upper left')
plt.show()
pred_data = model.predict(X_test)
plt.plot(y_test, label='test')
plt.plot(pred_data, label='pred')
plt.legend(loc='upper left')
plt.show()
pred_data = model.predict(X_test)
plt.plot(y_test, label='test')
plt.plot(pred_data, label='pred')
plt.legend(loc='upper left')
plt.xlim(7800, 8100)
plt.show()