{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import seaborn as sns\nfrom datetime import datetime\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-20T12:33:26.704690Z","iopub.execute_input":"2022-01-20T12:33:26.705151Z","iopub.status.idle":"2022-01-20T12:33:27.511192Z","shell.execute_reply.started":"2022-01-20T12:33:26.705040Z","shell.execute_reply":"2022-01-20T12:33:27.510313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1. Read the Data\n\nRead the datasets from 'g-research-crypto-forecasting datasets","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/g-research-crypto-forecasting/train.csv')\nasset_ID = pd.read_csv('/kaggle/input/g-research-crypto-forecasting/asset_details.csv')","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:33:27.512865Z","iopub.execute_input":"2022-01-20T12:33:27.513197Z","iopub.status.idle":"2022-01-20T12:34:22.204947Z","shell.execute_reply.started":"2022-01-20T12:33:27.513161Z","shell.execute_reply":"2022-01-20T12:34:22.204150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"asset_ID","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:34:22.206210Z","iopub.execute_input":"2022-01-20T12:34:22.206469Z","iopub.status.idle":"2022-01-20T12:34:22.226488Z","shell.execute_reply.started":"2022-01-20T12:34:22.206435Z","shell.execute_reply":"2022-01-20T12:34:22.225618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **2. Data exploration**\n## **Take the Bitcoin data**\nTake the each Asset_ID data, the Bitcoin is Asset_ID == 1","metadata":{}},{"cell_type":"code","source":"Bitcoin_Cash = train_data[train_data['Asset_ID'] == 2]\nBinance_Coin = train_data[train_data['Asset_ID'] == 0]\nBitcoin = train_data[train_data['Asset_ID'] == 1]\nEOS_IO = train_data[train_data['Asset_ID'] == 5]\nEthereum_Classic = train_data[train_data['Asset_ID'] == 6]\nEthereum = train_data[train_data['Asset_ID'] == 6]\nLitecoin = train_data[train_data['Asset_ID'] == 9]\nMonero = train_data[train_data['Asset_ID'] == 11]\nTRON = train_data[train_data['Asset_ID'] == 13]\nStellar = train_data[train_data['Asset_ID'] == 12]\nCardano = train_data[train_data['Asset_ID'] == 3]\nIOTA = train_data[train_data['Asset_ID'] == 8]\nMaker = train_data[train_data['Asset_ID'] == 10]\nDogecoin = train_data[train_data['Asset_ID'] == 4]","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:34:22.228633Z","iopub.execute_input":"2022-01-20T12:34:22.228956Z","iopub.status.idle":"2022-01-20T12:34:25.526736Z","shell.execute_reply.started":"2022-01-20T12:34:22.228919Z","shell.execute_reply":"2022-01-20T12:34:25.526004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **3. Data Clean**\nAdd the row which is NaN (More information can search the G-Research Crypto Forecasting notebook)","metadata":{}},{"cell_type":"code","source":"Bitcoin_Cash = Bitcoin_Cash.reindex(range(Bitcoin_Cash.index[0], Bitcoin_Cash.index[-1], 60), method = 'pad')\nBinance_Coin = Binance_Coin.reindex(range(Binance_Coin.index[0], Binance_Coin.index[-1], 60), method = 'pad')\nBitcoin = Bitcoin.reindex(range(Bitcoin.index[0], Bitcoin.index[-1], 60), method = 'pad')\nEOS_IO = EOS_IO.reindex(range(EOS_IO.index[0], EOS_IO.index[-1], 60), method = 'pad')\nEthereum_Classic = Ethereum_Classic.reindex(range(Ethereum_Classic.index[0], Ethereum_Classic.index[-1], 60), method = 'pad')\nEthereum = Ethereum.reindex(range(Ethereum.index[0], Ethereum.index[-1], 60), method = 'pad')\nLitecoin = Litecoin.reindex(range(Litecoin.index[0], Litecoin.index[-1], 60), method = 'pad')\nMonero = Monero.reindex(range(Monero.index[0], Monero.index[-1], 60), method = 'pad')\nTRON = TRON.reindex(range(TRON.index[0], TRON.index[-1], 60), method = 'pad')\nStellar = Stellar.reindex(range(Stellar.index[0], Stellar.index[-1], 60), method = 'pad')\nCardano = Cardano.reindex(range(Cardano.index[0], Cardano.index[-1], 60), method = 'pad')\nIOTA = IOTA.reindex(range(IOTA.index[0], IOTA.index[-1], 60), method = 'pad')\nMaker = Maker.reindex(range(Maker.index[0], Maker.index[-1], 60), method = 'pad')\nDogecoin = Dogecoin.reindex(range(Dogecoin.index[0], Dogecoin.index[-1], 60), method = 'pad')","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:34:25.528275Z","iopub.execute_input":"2022-01-20T12:34:25.528773Z","iopub.status.idle":"2022-01-20T12:34:27.871884Z","shell.execute_reply.started":"2022-01-20T12:34:25.528736Z","shell.execute_reply":"2022-01-20T12:34:27.871148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Bit_coin = Bitcoin.copy()","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:34:27.873341Z","iopub.execute_input":"2022-01-20T12:34:27.873581Z","iopub.status.idle":"2022-01-20T12:34:27.884695Z","shell.execute_reply.started":"2022-01-20T12:34:27.873549Z","shell.execute_reply":"2022-01-20T12:34:27.883896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Bitcoin = Bitcoin.Close.fillna(method = 'pad')","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:34:27.886386Z","iopub.execute_input":"2022-01-20T12:34:27.886821Z","iopub.status.idle":"2022-01-20T12:34:27.893732Z","shell.execute_reply.started":"2022-01-20T12:34:27.886786Z","shell.execute_reply":"2022-01-20T12:34:27.893045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **4. Data Preprocessing for Deep Learning Model**\n## Split the train_data and test_data\nI prepare the bit_train_data and bit_test_data.\nthe first 8 of 19 is bit_train_data, remaining the data as bit_test_data\n","metadata":{}},{"cell_type":"code","source":"import math\nBitcoin_Target = Bitcoin.values\ntrain_len = math.ceil(len(Bitcoin_Target) * 0.8)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:34:27.895219Z","iopub.execute_input":"2022-01-20T12:34:27.895590Z","iopub.status.idle":"2022-01-20T12:34:27.901378Z","shell.execute_reply.started":"2022-01-20T12:34:27.895553Z","shell.execute_reply":"2022-01-20T12:34:27.900506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Bitcoin_Target = Bitcoin_Target.reshape(-1, 1)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:34:27.902806Z","iopub.execute_input":"2022-01-20T12:34:27.903196Z","iopub.status.idle":"2022-01-20T12:34:27.910650Z","shell.execute_reply.started":"2022-01-20T12:34:27.903163Z","shell.execute_reply":"2022-01-20T12:34:27.909685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Scaler the train, test data\n\nI ues the MinMaxScaler method to scale the data","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import MinMaxScaler\nNormalize = MinMaxScaler(feature_range=(0, 1))\nBitcoin_Target = Normalize.fit_transform(Bitcoin_Target)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:34:27.914662Z","iopub.execute_input":"2022-01-20T12:34:27.915154Z","iopub.status.idle":"2022-01-20T12:34:28.025893Z","shell.execute_reply.started":"2022-01-20T12:34:27.915122Z","shell.execute_reply":"2022-01-20T12:34:28.025194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bit_train_data, bit_test_data = Bitcoin_Target[0:train_len, :], Bitcoin_Target[train_len:len(Bitcoin_Target), :1]","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:34:28.027045Z","iopub.execute_input":"2022-01-20T12:34:28.027387Z","iopub.status.idle":"2022-01-20T12:34:28.031882Z","shell.execute_reply.started":"2022-01-20T12:34:28.027350Z","shell.execute_reply":"2022-01-20T12:34:28.031008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prepare the Time series data\nThe data from No.0 to No.99 is x_train and predicting No.100's data is y_train","metadata":{}},{"cell_type":"code","source":"x_train = []\ny_train = []\nfor i in range(100, len(bit_train_data)):\n   x_train.append(bit_train_data[i-100:i, 0])\n   y_train.append(bit_train_data[i, 0])\nx_train, y_train = np.array(x_train), np.array(y_train)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:34:28.033157Z","iopub.execute_input":"2022-01-20T12:34:28.033534Z","iopub.status.idle":"2022-01-20T12:34:28.638416Z","shell.execute_reply.started":"2022-01-20T12:34:28.033500Z","shell.execute_reply":"2022-01-20T12:34:28.637675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], 1)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:34:28.639880Z","iopub.execute_input":"2022-01-20T12:34:28.640155Z","iopub.status.idle":"2022-01-20T12:34:28.645167Z","shell.execute_reply.started":"2022-01-20T12:34:28.640120Z","shell.execute_reply":"2022-01-20T12:34:28.643550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The data from No.0 to No.99 is x_test and predicting No.100's data is y_test","metadata":{}},{"cell_type":"code","source":"x_test = []\ny_test = []\nfor i in range(100, len(bit_test_data)):\n   x_test.append(bit_test_data[i-100:i, 0])\n   y_test.append(bit_test_data[i, 0])\nx_test, y_test = np.array(x_test), np.array(y_test)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:34:28.646362Z","iopub.execute_input":"2022-01-20T12:34:28.646918Z","iopub.status.idle":"2022-01-20T12:34:28.803428Z","shell.execute_reply.started":"2022-01-20T12:34:28.646883Z","shell.execute_reply":"2022-01-20T12:34:28.802559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], 1)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:34:28.804697Z","iopub.execute_input":"2022-01-20T12:34:28.805203Z","iopub.status.idle":"2022-01-20T12:34:28.811074Z","shell.execute_reply.started":"2022-01-20T12:34:28.805168Z","shell.execute_reply":"2022-01-20T12:34:28.810436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:34:28.813859Z","iopub.execute_input":"2022-01-20T12:34:28.814124Z","iopub.status.idle":"2022-01-20T12:34:28.821175Z","shell.execute_reply.started":"2022-01-20T12:34:28.814064Z","shell.execute_reply":"2022-01-20T12:34:28.820429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **5. Deep Learning LSTM Model**","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, LSTM, Dropout, Conv1D, MaxPooling1D, TimeDistributed, Flatten\nfrom tensorflow.keras.utils import plot_model\nmodel = Sequential()\nmodel.add(LSTM(50, return_sequences=True, input_shape=(x_train.shape[1], 1)))\nmodel.add(LSTM(50, return_sequences=True))\nmodel.add(LSTM(50))\nmodel.add(Dense(25))\nmodel.add(Dense(1))\nmodel.compile(loss = 'mse', optimizer = 'adam')\nhistory = model.fit(x_train,y_train, batch_size = 512 , epochs = 30)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:34:28.822491Z","iopub.execute_input":"2022-01-20T12:34:28.822951Z","iopub.status.idle":"2022-01-20T12:42:52.372125Z","shell.execute_reply.started":"2022-01-20T12:34:28.822914Z","shell.execute_reply":"2022-01-20T12:42:52.371202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set()\ndf_history = pd.DataFrame(history.history)\nsns.lineplot(x = df_history.index, y = df_history.loss)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:42:52.376177Z","iopub.execute_input":"2022-01-20T12:42:52.376523Z","iopub.status.idle":"2022-01-20T12:42:52.806781Z","shell.execute_reply.started":"2022-01-20T12:42:52.376486Z","shell.execute_reply":"2022-01-20T12:42:52.805976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **6. The Comparsion between Reality and Pridiction data**\n\n#### After MinMaxScaler comparsion between prediction(y_pred) and reality(y_test)","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\ny_pred = model.predict(x_test)\nplt.scatter(y_test, y_pred)\nplt.plot([x for x in range(2)], [x for x in range(2)], color = 'r')\nplt.xlabel(\"Reality MinMax\")\nplt.ylabel(\"Predicted MinMax\")\nplt.title('Bitcoin')\nplt.show()\nplt.clf()","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:42:52.811069Z","iopub.execute_input":"2022-01-20T12:42:52.811333Z","iopub.status.idle":"2022-01-20T12:43:14.601070Z","shell.execute_reply.started":"2022-01-20T12:42:52.811300Z","shell.execute_reply":"2022-01-20T12:43:14.600378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### No MinMaxScaler comparsion between prediction(y_pred) price and reality(y_test) price","metadata":{}},{"cell_type":"code","source":"y_pred = model.predict(x_test)\ny_test = y_test.reshape(-1, 1)\ny_pred = Normalize.inverse_transform(y_pred)\ny_test = Normalize.inverse_transform(y_test)\ny_pred = y_pred.reshape(len(y_pred), 1)\ny_test = y_test.reshape(len(y_pred), 1)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:43:14.605045Z","iopub.execute_input":"2022-01-20T12:43:14.607054Z","iopub.status.idle":"2022-01-20T12:43:35.182648Z","shell.execute_reply.started":"2022-01-20T12:43:14.607015Z","shell.execute_reply":"2022-01-20T12:43:35.181876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.scatter(y_test, y_pred)\nplt.plot([10000*x for x in range(2, 8)], [10000*x for x in range(2, 8)], color = 'r')\nplt.xlabel(\"Reality Prices\")\nplt.ylabel(\"Predicted prices\")\nplt.title('Bitcoin')\nplt.show()\nplt.clf()","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:43:35.184149Z","iopub.execute_input":"2022-01-20T12:43:35.184393Z","iopub.status.idle":"2022-01-20T12:43:35.540654Z","shell.execute_reply.started":"2022-01-20T12:43:35.184360Z","shell.execute_reply":"2022-01-20T12:43:35.539916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_pred = pd.DataFrame(y_pred, columns = ['Prediction'], index = Bitcoin.index[train_len +100:])\ndf_pred['Reality'] = y_test","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:43:35.541989Z","iopub.execute_input":"2022-01-20T12:43:35.542259Z","iopub.status.idle":"2022-01-20T12:43:35.550016Z","shell.execute_reply.started":"2022-01-20T12:43:35.542225Z","shell.execute_reply":"2022-01-20T12:43:35.549262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_pred","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:43:35.551283Z","iopub.execute_input":"2022-01-20T12:43:35.551619Z","iopub.status.idle":"2022-01-20T12:43:35.566831Z","shell.execute_reply.started":"2022-01-20T12:43:35.551583Z","shell.execute_reply":"2022-01-20T12:43:35.566142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def log_return(close):\n    return np.log(close).diff(periods = 15)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:43:35.569643Z","iopub.execute_input":"2022-01-20T12:43:35.569830Z","iopub.status.idle":"2022-01-20T12:43:35.575746Z","shell.execute_reply.started":"2022-01-20T12:43:35.569808Z","shell.execute_reply":"2022-01-20T12:43:35.574970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Bitcoin_Target[train_len:len(Bitcoin_Target), :1]","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:43:35.577274Z","iopub.execute_input":"2022-01-20T12:43:35.577697Z","iopub.status.idle":"2022-01-20T12:43:35.586852Z","shell.execute_reply.started":"2022-01-20T12:43:35.577663Z","shell.execute_reply":"2022-01-20T12:43:35.586127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_pred['Log_return_prediction'] = log_return(df_pred['Prediction'])[15: ]\ndf_pred['Log_return_reality'] = log_return(df_pred['Reality'])[15: ]","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:43:35.588399Z","iopub.execute_input":"2022-01-20T12:43:35.588742Z","iopub.status.idle":"2022-01-20T12:43:35.604036Z","shell.execute_reply.started":"2022-01-20T12:43:35.588705Z","shell.execute_reply":"2022-01-20T12:43:35.603318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,10))\nplt.plot(df_pred['Log_return_prediction'], color = 'r' , label = 'Prediction', alpha = 0.5)\nplt.plot(df_pred['Log_return_reality'], color = 'b', label = 'Reality', alpha = 0.5)\nplt.title('Log_Return Between the Prediction and Reality')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:43:35.605521Z","iopub.execute_input":"2022-01-20T12:43:35.605766Z","iopub.status.idle":"2022-01-20T12:43:36.045604Z","shell.execute_reply.started":"2022-01-20T12:43:35.605734Z","shell.execute_reply":"2022-01-20T12:43:36.043691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### the Bitcoin prediction and reality comparsion","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize = (30, 15))\nsns.lineplot(x = Bitcoin.index[-100000:], y = Bitcoin.values[-100000:], label = f'The Bitcoin Close')\nsns.lineplot(x = df_pred.index, y = df_pred.Prediction, label = 'Prediction', color = 'r')\nsns.lineplot(x = df_pred.index, y = df_pred.Reality, label = 'Reality', color = 'g')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-20T12:43:36.049477Z","iopub.execute_input":"2022-01-20T12:43:36.049785Z","iopub.status.idle":"2022-01-20T12:43:44.251554Z","shell.execute_reply.started":"2022-01-20T12:43:36.049743Z","shell.execute_reply":"2022-01-20T12:43:44.250864Z"},"trusted":true},"execution_count":null,"outputs":[]}]}