{"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 numpy as np \nimport pandas as pd \nimport os\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport time\nfrom datetime import datetime\nfrom sklearn.preprocessing import MinMaxScaler\nfrom keras.preprocessing.sequence import TimeseriesGenerator\nfrom tensorflow.keras.optimizers import Adam\nfrom keras.models import Sequential\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, TensorBoard\nfrom keras.layers import Dropout, CuDNNLSTM, GRU, Masking, Embedding, Dense, Activation, Flatten, LSTM, TimeDistributed, RepeatVector,Input\nfrom keras.callbacks import ReduceLROnPlateau\nfrom tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras.layers import Bidirectional","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-19T07:37:41.256550Z","iopub.execute_input":"2022-01-19T07:37:41.257029Z","iopub.status.idle":"2022-01-19T07:37:46.406532Z","shell.execute_reply.started":"2022-01-19T07:37:41.256912Z","shell.execute_reply":"2022-01-19T07:37:46.405779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_train = pd.read_csv('../input/g-research-crypto-forecasting/train.csv')\nds_asset = pd.read_csv('../input/g-research-crypto-forecasting/asset_details.csv')\nds_example = pd.read_csv('../input/g-research-crypto-forecasting/example_test.csv')\nds_suplement_train = pd.read_csv('../input/g-research-crypto-forecasting/supplemental_train.csv')\nds_example_submission = pd.read_csv('../input/g-research-crypto-forecasting/example_sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:37:46.408245Z","iopub.execute_input":"2022-01-19T07:37:46.408497Z","iopub.status.idle":"2022-01-19T07:38:45.047534Z","shell.execute_reply.started":"2022-01-19T07:37:46.408464Z","shell.execute_reply":"2022-01-19T07:38:45.046511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:38:45.049059Z","iopub.execute_input":"2022-01-19T07:38:45.049381Z","iopub.status.idle":"2022-01-19T07:38:45.078010Z","shell.execute_reply.started":"2022-01-19T07:38:45.049340Z","shell.execute_reply":"2022-01-19T07:38:45.077175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_asset.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:38:45.079660Z","iopub.execute_input":"2022-01-19T07:38:45.079948Z","iopub.status.idle":"2022-01-19T07:38:45.091002Z","shell.execute_reply.started":"2022-01-19T07:38:45.079909Z","shell.execute_reply":"2022-01-19T07:38:45.089889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_example.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:38:45.094484Z","iopub.execute_input":"2022-01-19T07:38:45.094816Z","iopub.status.idle":"2022-01-19T07:38:45.115043Z","shell.execute_reply.started":"2022-01-19T07:38:45.094773Z","shell.execute_reply":"2022-01-19T07:38:45.114241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_suplement_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:38:45.116442Z","iopub.execute_input":"2022-01-19T07:38:45.116750Z","iopub.status.idle":"2022-01-19T07:38:45.135089Z","shell.execute_reply.started":"2022-01-19T07:38:45.116711Z","shell.execute_reply":"2022-01-19T07:38:45.134079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_example_submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:38:45.136907Z","iopub.execute_input":"2022-01-19T07:38:45.137807Z","iopub.status.idle":"2022-01-19T07:38:45.148998Z","shell.execute_reply.started":"2022-01-19T07:38:45.137765Z","shell.execute_reply":"2022-01-19T07:38:45.148200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_train[ds_train['Target']==np.nan]\nds_train.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:38:45.150471Z","iopub.execute_input":"2022-01-19T07:38:45.150977Z","iopub.status.idle":"2022-01-19T07:38:45.638078Z","shell.execute_reply.started":"2022-01-19T07:38:45.150934Z","shell.execute_reply":"2022-01-19T07:38:45.637434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define plot space\nfig, ax = plt.subplots(figsize=(10, 6))\n\n# Create bar plot\nax.bar(ds_asset['Asset_Name'], \n       ds_asset['Weight'])\nplt.xlabel('Asset Name')\nplt.ylabel('Weight')\nplt.xticks(rotation=70)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:38:45.640254Z","iopub.execute_input":"2022-01-19T07:38:45.640678Z","iopub.status.idle":"2022-01-19T07:38:45.901644Z","shell.execute_reply.started":"2022-01-19T07:38:45.640641Z","shell.execute_reply":"2022-01-19T07:38:45.900973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_train_copy = ds_train\nds_train_copy['date'] = pd.to_datetime(ds_train_copy['timestamp'], unit='s')\nds_train_copy.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:38:45.903359Z","iopub.execute_input":"2022-01-19T07:38:45.903750Z","iopub.status.idle":"2022-01-19T07:38:47.307991Z","shell.execute_reply.started":"2022-01-19T07:38:45.903708Z","shell.execute_reply":"2022-01-19T07:38:47.307225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Break train dataset into by asset and make timestamp as a index**","metadata":{}},{"cell_type":"code","source":"bnc = ds_train_copy[ds_train_copy['Asset_ID']==0].set_index('timestamp')\nbtc = ds_train_copy[ds_train_copy['Asset_ID']==1].set_index('timestamp')\nbtcsh = ds_train_copy[ds_train_copy['Asset_ID']==2].set_index('timestamp')\ncar = ds_train_copy[ds_train_copy['Asset_ID']==3].set_index('timestamp')\ndog = ds_train_copy[ds_train_copy['Asset_ID']==4].set_index('timestamp')\neos = ds_train_copy[ds_train_copy['Asset_ID']==5].set_index('timestamp')\neth = ds_train_copy[ds_train_copy['Asset_ID']==6].set_index('timestamp')\neth_csc = ds_train_copy[ds_train_copy['Asset_ID']==7].set_index('timestamp')\niot = ds_train_copy[ds_train_copy['Asset_ID']==8].set_index('timestamp')\nltc = ds_train_copy[ds_train_copy['Asset_ID']==9].set_index('timestamp')\nmak = ds_train_copy[ds_train_copy['Asset_ID']==10].set_index('timestamp')\nmon = ds_train_copy[ds_train_copy['Asset_ID']==11].set_index('timestamp')\nste = ds_train_copy[ds_train_copy['Asset_ID']==12].set_index('timestamp')\ntro = ds_train_copy[ds_train_copy['Asset_ID']==13].set_index('timestamp')\neth.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:38:47.309368Z","iopub.execute_input":"2022-01-19T07:38:47.309612Z","iopub.status.idle":"2022-01-19T07:38:51.540816Z","shell.execute_reply.started":"2022-01-19T07:38:47.309581Z","shell.execute_reply":"2022-01-19T07:38:51.540150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Check the train dataset start and end for each asset**","metadata":{}},{"cell_type":"code","source":"beg_btcsh = btcsh.index[0].astype('datetime64[s]')\nend_btcsh = btcsh.index[-1].astype('datetime64[s]')\nbeg_bnc = bnc.index[0].astype('datetime64[s]')\nend_bnc = bnc.index[-1].astype('datetime64[s]')\nbeg_btc = btc.index[0].astype('datetime64[s]')\nend_btc = btc.index[-1].astype('datetime64[s]')\nbeg_eos = eos.index[0].astype('datetime64[s]')\nend_eos = eos.index[-1].astype('datetime64[s]')\nbeg_eth_csc = eth_csc.index[0].astype('datetime64[s]')\nend_eth_csc = eth_csc.index[-1].astype('datetime64[s]')\nbeg_ltc = ltc.index[0].astype('datetime64[s]')\nend_ltc = ltc.index[-1].astype('datetime64[s]')\nbeg_mon = mon.index[0].astype('datetime64[s]')\nend_mon = mon.index[-1].astype('datetime64[s]')\nbeg_tro = tro.index[0].astype('datetime64[s]')\nend_tro = tro.index[-1].astype('datetime64[s]')\nbeg_eth = eth.index[0].astype('datetime64[s]')\nend_eth = eth.index[-1].astype('datetime64[s]')\nbeg_ste = ste.index[0].astype('datetime64[s]')\nend_ste = ste.index[-1].astype('datetime64[s]')\nbeg_car = car.index[0].astype('datetime64[s]')\nend_car = car.index[-1].astype('datetime64[s]')\nbeg_iot = iot.index[0].astype('datetime64[s]')\nend_iot = iot.index[-1].astype('datetime64[s]')\nbeg_mak = mak.index[0].astype('datetime64[s]')\nend_mak = mak.index[-1].astype('datetime64[s]')\nbeg_dog = dog.index[0].astype('datetime64[s]')\nend_dog = dog.index[-1].astype('datetime64[s]')\nprint('Bitcoin Cash     :', beg_btcsh, 'to', end_btcsh)\nprint('Binance Coin     :', beg_bnc, 'to', end_bnc)\nprint('Bitcoin          :', beg_btc, 'to', end_btc)\nprint('EOS IO           :', beg_eos, 'to', end_eos)\nprint('Etherium Classic :', beg_eth_csc, 'to', end_eth_csc)\nprint('Ethereum         :', beg_eth, 'to', end_eth)\nprint('Lite Coin        :', beg_ltc, 'to', end_ltc)\nprint('Monero           :', beg_mon, 'to', end_mon)\nprint('TRON             :', beg_tro, 'to', end_tro)\nprint('Stellar          :', beg_ste, 'to', end_ste)\nprint('Cardano          :', beg_car, 'to', end_car)\nprint('IOTA             :', beg_iot, 'to', end_iot)\nprint('Maker            :', beg_mak, 'to', end_mak)\nprint('Dogecoin         :', beg_dog, 'to', end_dog)","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:38:51.542106Z","iopub.execute_input":"2022-01-19T07:38:51.542348Z","iopub.status.idle":"2022-01-19T07:38:51.568718Z","shell.execute_reply.started":"2022-01-19T07:38:51.542316Z","shell.execute_reply":"2022-01-19T07:38:51.568082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Check the missing timestamp for each data**","metadata":{}},{"cell_type":"code","source":"(eth.index[1:]-eth.index[:-1]).value_counts().head()","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:38:51.569953Z","iopub.execute_input":"2022-01-19T07:38:51.570198Z","iopub.status.idle":"2022-01-19T07:38:51.594057Z","shell.execute_reply.started":"2022-01-19T07:38:51.570167Z","shell.execute_reply":"2022-01-19T07:38:51.593276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Reindex all asset to remove the missing data in each timestamp**","metadata":{}},{"cell_type":"code","source":"eth     = eth.reindex(range(eth.index[0],eth.index[-1]+60,60),method='pad')\nbtc     = btc.reindex(range(btc.index[0],btc.index[-1]+60,60),method='pad')\nbtcsh   = btcsh.reindex(range(btcsh.index[0],btcsh.index[-1]+60,60),method='pad')\nbnc     = bnc.reindex(range(bnc.index[0],bnc.index[-1]+60,60),method='pad')\neos     = eos.reindex(range(eos.index[0],eos.index[-1]+60,60),method='pad')\neth_csc = eth_csc.reindex(range(eth_csc.index[0],eth_csc.index[-1]+60,60),method='pad')\nltc     = ltc.reindex(range(ltc.index[0],ltc.index[-1]+60,60),method='pad')\nmon     = mon.reindex(range(mon.index[0],mon.index[-1]+60,60),method='pad')\ntro     = tro.reindex(range(tro.index[0],tro.index[-1]+60,60),method='pad')\nste     = ste.reindex(range(ste.index[0],ste.index[-1]+60,60),method='pad')\ncar     = car.reindex(range(car.index[0],car.index[-1]+60,60),method='pad')\niot     = iot.reindex(range(iot.index[0],iot.index[-1]+60,60),method='pad')\nmak     = mak.reindex(range(mak.index[0],mak.index[-1]+60,60),method='pad')\ndog     = dog.reindex(range(dog.index[0],dog.index[-1]+60,60),method='pad')","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:38:51.597520Z","iopub.execute_input":"2022-01-19T07:38:51.597703Z","iopub.status.idle":"2022-01-19T07:38:54.856718Z","shell.execute_reply.started":"2022-01-19T07:38:51.597680Z","shell.execute_reply":"2022-01-19T07:38:54.855970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Show all close value for each asset from beginning until end**","metadata":{}},{"cell_type":"code","source":"# Define plot space\nfig, ax = plt.subplots(5, 3, figsize=(18, 22))\n\n# Bitcoin Cash\nax[0, 0].plot(btcsh['Close'], label='BTCSH')\nax[0, 0].set_title('Bitcoin Cash')\nax[0, 1].plot(bnc['Close'], label='BNC')\nax[0, 1].set_title('Binance Coin')\nax[0, 2].plot(btc['Close'], label='BTC')\nax[0, 2].set_title('Bitcoin')\nax[1, 0].plot(eos['Close'], label='EOS')\nax[1, 0].set_title('EOS.IO')\nax[1, 1].plot(eth_csc['Close'], label='ETH_CSC')\nax[1, 1].set_title('Etherium Cash')\nax[1, 2].plot(ltc['Close'], label='LTC')\nax[1, 2].set_title('Lite Coin')\nax[2, 0].plot(mon['Close'], label='MON')\nax[2, 0].set_title('Monero')\nax[2, 1].plot(tro['Close'], label='TRO')\nax[2, 1].set_title('TRON')\nax[2, 2].plot(eth['Close'], label='ETH')\nax[2, 2].set_title('Etherium')\nax[3, 0].plot(ste['Close'], label='STE')\nax[3, 0].set_title('Stelar')\nax[3, 1].plot(car['Close'], label='CAR')\nax[3, 1].set_title('Cardano')\nax[3, 2].plot(iot['Close'], label='IOT')\nax[3, 2].set_title('IOTA')\nax[4, 0].plot(mak['Close'], label='MAK')\nax[4, 0].set_title('Maker')\nax[4, 1].plot(dog['Close'], label='DOG')\nax[4, 1].set_title('Dogecoin')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:38:54.858026Z","iopub.execute_input":"2022-01-19T07:38:54.858289Z","iopub.status.idle":"2022-01-19T07:38:59.007613Z","shell.execute_reply.started":"2022-01-19T07:38:54.858254Z","shell.execute_reply":"2022-01-19T07:38:59.006939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# define function to compute log returns\ndef log_return(series, periods=1):\n    return np.log(series).diff(periods=periods)\n\n# auxiliary function, from datetime to timestamp\ntotimestamp = lambda s: np.int32(time.mktime(datetime.strptime(s, \"%d/%m/%Y\").timetuple()))\n\nall_assets_2021 = pd.DataFrame([])\nfor asset_id, asset_name in zip(ds_asset.Asset_ID, ds_asset.Asset_Name):\n    asset = ds_train[ds_train[\"Asset_ID\"]==asset_id].set_index(\"timestamp\")\n    asset = asset.loc[totimestamp('01/01/2021'):totimestamp('01/05/2021')]\n    asset = asset.reindex(range(asset.index[0],asset.index[-1]+60,60),method='pad')\n    lret = log_return(asset.Close.fillna(0))[1:]\n    all_assets_2021 = all_assets_2021.join(lret, rsuffix=asset_name, how=\"outer\")","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:38:59.008582Z","iopub.execute_input":"2022-01-19T07:38:59.008825Z","iopub.status.idle":"2022-01-19T07:39:03.564505Z","shell.execute_reply.started":"2022-01-19T07:38:59.008793Z","shell.execute_reply":"2022-01-19T07:39:03.563768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_assets_2021.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:39:03.565802Z","iopub.execute_input":"2022-01-19T07:39:03.566064Z","iopub.status.idle":"2022-01-19T07:39:03.587815Z","shell.execute_reply.started":"2022-01-19T07:39:03.566019Z","shell.execute_reply":"2022-01-19T07:39:03.586984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Show correlation between Asset**","metadata":{}},{"cell_type":"code","source":"plt.imshow(all_assets_2021.corr());\nplt.yticks(ds_asset.Asset_ID.values, ds_asset.Asset_Name.values);\nplt.xticks(ds_asset.Asset_ID.values, ds_asset.Asset_Name.values, rotation='vertical');\nplt.colorbar();","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:39:03.589066Z","iopub.execute_input":"2022-01-19T07:39:03.591454Z","iopub.status.idle":"2022-01-19T07:39:04.017864Z","shell.execute_reply.started":"2022-01-19T07:39:03.591411Z","shell.execute_reply":"2022-01-19T07:39:04.016489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Select some input features from the trading data: \n# 5 min log return, abs(5 min log return), upper shadow, and lower shadow.\nupper_shadow = lambda asset: asset.High - np.maximum(asset.Close,asset.Open)\nlower_shadow = lambda asset: np.minimum(asset.Close,asset.Open)- asset.Low\nX_bnc = pd.concat([log_return(bnc.VWAP,periods=15), log_return(bnc.VWAP,periods=1).abs(), upper_shadow(bnc), lower_shadow(bnc)], axis=1)\ny_bnc = bnc.Target\nX_btc = pd.concat([log_return(btc.VWAP,periods=15), log_return(btc.VWAP,periods=1).abs(), upper_shadow(btc), lower_shadow(btc)], axis=1)\ny_btc = btc.Target\nX_btcsh = pd.concat([log_return(btcsh.VWAP,periods=15), log_return(btcsh.VWAP,periods=1).abs(), upper_shadow(btcsh), lower_shadow(btcsh)], axis=1)\ny_btcsh = btcsh.Target\nX_car = pd.concat([log_return(car.VWAP,periods=15), log_return(car.VWAP,periods=1).abs(), upper_shadow(car), lower_shadow(car)], axis=1)\ny_car = car.Target\nX_dog = pd.concat([log_return(dog.VWAP,periods=15), log_return(dog.VWAP,periods=1).abs(), upper_shadow(dog), lower_shadow(dog)], axis=1)\ny_dog = dog.Target\nX_eos = pd.concat([log_return(eos.VWAP,periods=15), log_return(eos.VWAP,periods=1).abs(), upper_shadow(eos), lower_shadow(eos)], axis=1)\ny_eos = eos.Target\nX_eth = pd.concat([log_return(eth.VWAP,periods=15), log_return(eth.VWAP,periods=1).abs(), upper_shadow(eth), lower_shadow(eth)], axis=1)\ny_eth = eth.Target\nX_eth_csc = pd.concat([log_return(eth_csc.VWAP,periods=15), log_return(eth_csc.VWAP,periods=1).abs(), upper_shadow(eth_csc), lower_shadow(eth_csc)], axis=1)\ny_eth_csc = eth_csc.Target\nX_iot = pd.concat([log_return(iot.VWAP,periods=15), log_return(iot.VWAP,periods=1).abs(), upper_shadow(iot), lower_shadow(iot)], axis=1)\ny_iot = iot.Target\nX_ltc = pd.concat([log_return(ltc.VWAP,periods=15), log_return(ltc.VWAP,periods=1).abs(), upper_shadow(ltc), lower_shadow(ltc)], axis=1)\ny_ltc = ltc.Target\nX_mak = pd.concat([log_return(mak.VWAP,periods=15), log_return(mak.VWAP,periods=1).abs(), upper_shadow(mak), lower_shadow(mak)], axis=1)\ny_mak = mak.Target\nX_mon = pd.concat([log_return(mon.VWAP,periods=15), log_return(mon.VWAP,periods=1).abs(), upper_shadow(mon), lower_shadow(mon)], axis=1)\ny_mon = mon.Target\nX_ste = pd.concat([log_return(ste.VWAP,periods=15), log_return(ste.VWAP,periods=1).abs(), upper_shadow(ste), lower_shadow(ste)], axis=1)\ny_ste = ste.Target\nX_tro = pd.concat([log_return(tro.VWAP,periods=15), log_return(tro.VWAP,periods=1).abs(), upper_shadow(tro), lower_shadow(tro)], axis=1)\ny_tro = tro.Target","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:39:04.019549Z","iopub.execute_input":"2022-01-19T07:39:04.020097Z","iopub.status.idle":"2022-01-19T07:39:05.389687Z","shell.execute_reply.started":"2022-01-19T07:39:04.020054Z","shell.execute_reply":"2022-01-19T07:39:05.388952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_btc.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:39:05.390927Z","iopub.execute_input":"2022-01-19T07:39:05.391173Z","iopub.status.idle":"2022-01-19T07:39:05.404708Z","shell.execute_reply.started":"2022-01-19T07:39:05.391141Z","shell.execute_reply":"2022-01-19T07:39:05.403990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# select training and test periods\ntrain_window = [totimestamp(\"01/05/2021\"), totimestamp(\"30/06/2021\")]\ntest_window = [totimestamp(\"01/07/2021\"), totimestamp(\"30/08/2021\")]\n\n# divide data into train and test, compute X and y\n# we aim to build simple regression models using a window_size of 1\nX_bnc_train = X_bnc.loc[train_window[0]:train_window[1]].ffill().to_numpy()\nX_bnc_train = X_bnc.loc[train_window[0]:train_window[1]].bfill().to_numpy()\ny_bnc_train = y_bnc.loc[train_window[0]:train_window[1]].ffill().to_numpy()\ny_bnc_train = y_bnc.loc[train_window[0]:train_window[1]].bfill().to_numpy()\nX_bnc_test = X_bnc.loc[test_window[0]:test_window[1]].ffill().to_numpy() \nX_bnc_test = X_bnc.loc[test_window[0]:test_window[1]].bfill().to_numpy() \ny_bnc_test = y_bnc.loc[test_window[0]:test_window[1]].ffill().to_numpy() \ny_bnc_test = y_bnc.loc[test_window[0]:test_window[1]].bfill().to_numpy() \n\nX_btc_train = X_btc.loc[train_window[0]:train_window[1]].ffill().to_numpy()\nX_btc_train = X_btc.loc[train_window[0]:train_window[1]].bfill().to_numpy()\ny_btc_train = y_btc.loc[train_window[0]:train_window[1]].ffill().to_numpy()  \ny_btc_train = y_btc.loc[train_window[0]:train_window[1]].bfill().to_numpy() \nX_btc_test = X_btc.loc[test_window[0]:test_window[1]].ffill().to_numpy() \nX_btc_test = X_btc.loc[test_window[0]:test_window[1]].bfill().to_numpy()\ny_btc_test = y_btc.loc[test_window[0]:test_window[1]].ffill().to_numpy() \ny_btc_test = y_btc.loc[test_window[0]:test_window[1]].bfill().to_numpy() \n\nX_btcsh_train = X_btcsh.loc[train_window[0]:train_window[1]].ffill().to_numpy()\nX_btcsh_train = X_btcsh.loc[train_window[0]:train_window[1]].bfill().to_numpy()\ny_btcsh_train = y_btcsh.loc[train_window[0]:train_window[1]].ffill().to_numpy()\ny_btcsh_train = y_btcsh.loc[train_window[0]:train_window[1]].bfill().to_numpy()  \nX_btcsh_test = X_btcsh.loc[test_window[0]:test_window[1]].ffill().to_numpy()\nX_btcsh_test = X_btcsh.loc[test_window[0]:test_window[1]].bfill().to_numpy()\ny_btcsh_test = y_btcsh.loc[test_window[0]:test_window[1]].ffill().to_numpy()\ny_btcsh_test = y_btcsh.loc[test_window[0]:test_window[1]].bfill().to_numpy() \n\nX_car_train = X_car.loc[train_window[0]:train_window[1]].ffill().to_numpy()\nX_car_train = X_car.loc[train_window[0]:train_window[1]].bfill().to_numpy()\ny_car_train = y_car.loc[train_window[0]:train_window[1]].ffill().to_numpy()\ny_car_train = y_car.loc[train_window[0]:train_window[1]].bfill().to_numpy()  \nX_car_test = X_car.loc[test_window[0]:test_window[1]].ffill().to_numpy()   \nX_car_test = X_car.loc[test_window[0]:test_window[1]].bfill().to_numpy() \ny_car_test = y_car.loc[test_window[0]:test_window[1]].ffill().to_numpy() \ny_car_test = y_car.loc[test_window[0]:test_window[1]].bfill().to_numpy() \n\nX_dog_train = X_dog.loc[train_window[0]:train_window[1]].ffill().to_numpy()\nX_dog_train = X_dog.loc[train_window[0]:train_window[1]].bfill().to_numpy()\ny_dog_train = y_dog.loc[train_window[0]:train_window[1]].ffill().to_numpy()\ny_dog_train = y_dog.loc[train_window[0]:train_window[1]].bfill().to_numpy()  \nX_dog_test = X_dog.loc[test_window[0]:test_window[1]].ffill().to_numpy()   \nX_dog_test = X_dog.loc[test_window[0]:test_window[1]].bfill().to_numpy() \ny_dog_test = y_dog.loc[test_window[0]:test_window[1]].ffill().to_numpy() \ny_dog_test = y_dog.loc[test_window[0]:test_window[1]].bfill().to_numpy() \n\nX_eos_train = X_eos.loc[train_window[0]:train_window[1]].ffill().to_numpy() \nX_eos_train = X_eos.loc[train_window[0]:train_window[1]].bfill().to_numpy()\ny_eos_train = y_eos.loc[train_window[0]:train_window[1]].ffill().to_numpy()\ny_eos_train = y_eos.loc[train_window[0]:train_window[1]].bfill().to_numpy()  \nX_eos_test = X_eos.loc[test_window[0]:test_window[1]].ffill().to_numpy()   \nX_eos_test = X_eos.loc[test_window[0]:test_window[1]].bfill().to_numpy() \ny_eos_test = y_eos.loc[test_window[0]:test_window[1]].ffill().to_numpy() \ny_eos_test = y_eos.loc[test_window[0]:test_window[1]].bfill().to_numpy() \n\nX_eth_train = X_eth.loc[train_window[0]:train_window[1]].ffill().to_numpy() \nX_eth_train = X_eth.loc[train_window[0]:train_window[1]].bfill().to_numpy() \ny_eth_train = y_eth.loc[train_window[0]:train_window[1]].ffill().to_numpy() \ny_eth_train = y_eth.loc[train_window[0]:train_window[1]].bfill().to_numpy()  \nX_eth_test = X_eth.loc[test_window[0]:test_window[1]].ffill().to_numpy()  \nX_eth_test = X_eth.loc[test_window[0]:test_window[1]].bfill().to_numpy() \ny_eth_test = y_eth.loc[test_window[0]:test_window[1]].ffill().to_numpy() \ny_eth_test = y_eth.loc[test_window[0]:test_window[1]].bfill().to_numpy() \n\nX_eth_csc_train = X_eth_csc.loc[train_window[0]:train_window[1]].ffill().to_numpy()\nX_eth_csc_train = X_eth_csc.loc[train_window[0]:train_window[1]].bfill().to_numpy()  \ny_eth_csc_train = y_eth_csc.loc[train_window[0]:train_window[1]].ffill().to_numpy()  \ny_eth_csc_train = y_eth_csc.loc[train_window[0]:train_window[1]].bfill().to_numpy()  \nX_eth_csc_test = X_eth_csc.loc[test_window[0]:test_window[1]].ffill().to_numpy()   \nX_eth_csc_test = X_eth_csc.loc[test_window[0]:test_window[1]].bfill().to_numpy() \ny_eth_csc_test = y_eth_csc.loc[test_window[0]:test_window[1]].ffill().to_numpy() \ny_eth_csc_test = y_eth_csc.loc[test_window[0]:test_window[1]].bfill().to_numpy() \n\nX_iot_train = X_iot.loc[train_window[0]:train_window[1]].ffill().to_numpy()\nX_iot_train = X_iot.loc[train_window[0]:train_window[1]].bfill().to_numpy()  \ny_iot_train = y_iot.loc[train_window[0]:train_window[1]].ffill().to_numpy()  \ny_iot_train = y_iot.loc[train_window[0]:train_window[1]].bfill().to_numpy()  \nX_iot_test = X_iot.loc[test_window[0]:test_window[1]].ffill().to_numpy()   \nX_iot_test = X_iot.loc[test_window[0]:test_window[1]].bfill().to_numpy() \ny_iot_test = y_iot.loc[test_window[0]:test_window[1]].ffill().to_numpy() \ny_iot_test = y_iot.loc[test_window[0]:test_window[1]].bfill().to_numpy() \n\nX_ltc_train = X_ltc.loc[train_window[0]:train_window[1]].ffill().to_numpy()  \nX_ltc_train = X_ltc.loc[train_window[0]:train_window[1]].bfill().to_numpy()\ny_ltc_train = y_ltc.loc[train_window[0]:train_window[1]].ffill().to_numpy()\ny_ltc_train = y_ltc.loc[train_window[0]:train_window[1]].bfill().to_numpy()  \nX_ltc_test = X_ltc.loc[test_window[0]:test_window[1]].ffill().to_numpy()   \nX_ltc_test = X_ltc.loc[test_window[0]:test_window[1]].bfill().to_numpy() \ny_ltc_test = y_ltc.loc[test_window[0]:test_window[1]].ffill().to_numpy() \ny_ltc_test = y_ltc.loc[test_window[0]:test_window[1]].bfill().to_numpy() \n\nX_mak_train = X_mak.loc[train_window[0]:train_window[1]].ffill().to_numpy()\nX_mak_train = X_mak.loc[train_window[0]:train_window[1]].bfill().to_numpy()   \ny_mak_train = y_mak.loc[train_window[0]:train_window[1]].ffill().to_numpy()   \ny_mak_train = y_mak.loc[train_window[0]:train_window[1]].bfill().to_numpy()  \nX_mak_test = X_mak.loc[test_window[0]:test_window[1]].ffill().to_numpy()  \nX_mak_test = X_mak.loc[test_window[0]:test_window[1]].bfill().to_numpy() \ny_mak_test = y_mak.loc[test_window[0]:test_window[1]].ffill().to_numpy()\ny_mak_test = y_mak.loc[test_window[0]:test_window[1]].bfill().to_numpy()\n\nX_mak_train = X_mak.loc[train_window[0]:train_window[1]].ffill().to_numpy()\nX_mak_train = X_mak.loc[train_window[0]:train_window[1]].bfill().to_numpy()  \ny_mak_train = y_mak.loc[train_window[0]:train_window[1]].ffill().to_numpy()  \ny_mak_train = y_mak.loc[train_window[0]:train_window[1]].bfill().to_numpy()  \nX_mak_test = X_mak.loc[test_window[0]:test_window[1]].ffill().to_numpy()   \nX_mak_test = X_mak.loc[test_window[0]:test_window[1]].bfill().to_numpy() \ny_mak_test = y_mak.loc[test_window[0]:test_window[1]].ffill().to_numpy()\ny_mak_test = y_mak.loc[test_window[0]:test_window[1]].bfill().to_numpy()\n\nX_mon_train = X_mon.loc[train_window[0]:train_window[1]].fillna(0).to_numpy()\nX_mon_train = X_mon.loc[train_window[0]:train_window[1]].fillna(0).to_numpy()  \ny_mon_train = y_mon.loc[train_window[0]:train_window[1]].fillna(0).to_numpy()  \ny_mon_train = y_mon.loc[train_window[0]:train_window[1]].fillna(0).to_numpy()  \nX_mon_test = X_mon.loc[test_window[0]:test_window[1]].fillna(0).to_numpy()   \nX_mon_test = X_mon.loc[test_window[0]:test_window[1]].fillna(0).to_numpy() \ny_mon_test = y_mon.loc[test_window[0]:test_window[1]].fillna(0).to_numpy()\n\nX_ste_train = X_ste.loc[train_window[0]:train_window[1]].fillna(0).to_numpy()  \ny_ste_train = y_ste.loc[train_window[0]:train_window[1]].fillna(0).to_numpy()  \nX_ste_test = X_ste.loc[test_window[0]:test_window[1]].fillna(0).to_numpy() \ny_ste_test = y_ste.loc[test_window[0]:test_window[1]].fillna(0).to_numpy()\n\nX_tro_train = X_tro.loc[train_window[0]:train_window[1]].fillna(0).to_numpy()  \ny_tro_train = y_tro.loc[train_window[0]:train_window[1]].fillna(0).to_numpy()  \nX_tro_test = X_tro.loc[test_window[0]:test_window[1]].fillna(0).to_numpy() \ny_tro_test = y_tro.loc[test_window[0]:test_window[1]].fillna(0).to_numpy()","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:39:05.406444Z","iopub.execute_input":"2022-01-19T07:39:05.406709Z","iopub.status.idle":"2022-01-19T07:39:05.554443Z","shell.execute_reply.started":"2022-01-19T07:39:05.406674Z","shell.execute_reply":"2022-01-19T07:39:05.553690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\n# simple preprocessing of the data \nscaler = StandardScaler()\n\nX_bnc_train_scaled = scaler.fit_transform(X_bnc_train)\nX_bnc_test_scaled = scaler.transform(X_bnc_test)\n\nX_btc_train_scaled = scaler.fit_transform(X_btc_train)\nX_btc_test_scaled = scaler.transform(X_btc_test)\n\nX_btcsh_train_scaled = scaler.fit_transform(X_btcsh_train)\nX_btcsh_test_scaled = scaler.transform(X_btcsh_test)\n\nX_car_train_scaled = scaler.fit_transform(X_car_train)\nX_car_test_scaled = scaler.transform(X_car_test)\n\nX_dog_train_scaled = scaler.fit_transform(X_dog_train)\nX_dog_test_scaled = scaler.transform(X_dog_test)\n\nX_eos_train_scaled = scaler.fit_transform(X_eos_train)\nX_eos_test_scaled = scaler.transform(X_eos_test)\n\nX_eth_train_scaled = scaler.fit_transform(X_eth_train)\nX_eth_test_scaled = scaler.transform(X_eth_test)\n\nX_eth_csc_train_scaled = scaler.fit_transform(X_eth_csc_train)\nX_eth_csc_test_scaled = scaler.transform(X_eth_csc_test)\n\nX_iot_train_scaled = scaler.fit_transform(X_iot_train)\nX_iot_test_scaled = scaler.transform(X_iot_test)\n\nX_ltc_train_scaled = scaler.fit_transform(X_ltc_train)\nX_ltc_test_scaled = scaler.transform(X_ltc_test)\n\nX_mak_train_scaled = scaler.fit_transform(X_mak_train)\nX_mak_test_scaled = scaler.transform(X_mak_test)\n\nX_mon_train_scaled = scaler.fit_transform(X_mon_train)\nX_mon_test_scaled = scaler.transform(X_mon_test)\n\nX_ste_train_scaled = scaler.fit_transform(X_ste_train)\nX_ste_test_scaled = scaler.transform(X_ste_test)\n\nX_tro_train_scaled = scaler.fit_transform(X_tro_train)\nX_tro_test_scaled = scaler.transform(X_tro_test)","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:39:05.556333Z","iopub.execute_input":"2022-01-19T07:39:05.556727Z","iopub.status.idle":"2022-01-19T07:39:05.658943Z","shell.execute_reply.started":"2022-01-19T07:39:05.556691Z","shell.execute_reply":"2022-01-19T07:39:05.658228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Create list of x_train, y_train, x_test and y_test for each asset**","metadata":{}},{"cell_type":"code","source":"list_asset = [['bnc',[X_bnc_train_scaled,y_bnc_train], [X_bnc_test_scaled, y_bnc_test]],\n              ['btc',[X_btc_train_scaled,y_btc_train], [X_btc_test_scaled, y_btc_test]],\n              ['btcsh',[X_btcsh_train_scaled,y_btcsh_train], [X_btcsh_test_scaled, y_btcsh_test]],\n              ['car',[X_car_train_scaled,y_car_train], [X_car_test_scaled, y_car_test]],\n              ['dog',[X_dog_train_scaled,y_dog_train], [X_dog_test_scaled, y_dog_test]],\n              ['eos',[X_eos_train_scaled,y_eos_train], [X_eos_test_scaled, y_eos_test]],\n              ['eth',[X_eth_train_scaled,y_eth_train], [X_eth_test_scaled, y_eth_test]],\n              ['eth_csc',[X_eth_csc_train_scaled,y_eth_csc_train], [X_eth_csc_test_scaled, y_eth_csc_test]],\n              ['iot',[X_iot_train_scaled,y_iot_train], [X_iot_test_scaled, y_iot_test]],\n              ['ltc',[X_ltc_train_scaled,y_ltc_train], [X_ltc_test_scaled, y_ltc_test]],\n              ['mak',[X_mak_train_scaled,y_mak_train], [X_mak_test_scaled, y_mak_test]],\n              ['mon',[X_mon_train_scaled,y_mon_train], [X_mon_test_scaled, y_mon_test]],\n              ['ste',[X_ste_train_scaled,y_ste_train], [X_ste_test_scaled, y_ste_test]],\n              ['tro',[X_tro_train_scaled,y_tro_train], [X_tro_test_scaled, y_tro_test]]]","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:39:05.661337Z","iopub.execute_input":"2022-01-19T07:39:05.661798Z","iopub.status.idle":"2022-01-19T07:39:05.669948Z","shell.execute_reply.started":"2022-01-19T07:39:05.661756Z","shell.execute_reply":"2022-01-19T07:39:05.669246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_bnc_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:39:05.671118Z","iopub.execute_input":"2022-01-19T07:39:05.671650Z","iopub.status.idle":"2022-01-19T07:39:05.685113Z","shell.execute_reply.started":"2022-01-19T07:39:05.671600Z","shell.execute_reply":"2022-01-19T07:39:05.684187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def reshape(X_samples, y_samples):\n    # Reshape the Input as a 3D (number of samples, Time Steps, Features)\n    # X_data=np.array(X_samples)\n    print(X_samples.shape)\n    X_data = X_samples\n    y_data = y_samples\n    X_data=X_data.reshape(X_data.shape[0],1, X_data.shape[1])\n    # We do not reshape y as a 3D data  as it is supposed to be a single column only\n    # y_data=np.array(y_samples)\n    y_data=y_data.reshape(y_data.shape[0], 1)\n    \n    return X_data, y_data","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:39:05.686582Z","iopub.execute_input":"2022-01-19T07:39:05.686877Z","iopub.status.idle":"2022-01-19T07:39:05.692856Z","shell.execute_reply.started":"2022-01-19T07:39:05.686796Z","shell.execute_reply":"2022-01-19T07:39:05.691864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model_ltsm(x_train):\n    TimeSteps=x_train.shape[0]\n    TotalFeatures=x_train.shape[2]\n    model = Sequential()\n    model.add(LSTM(units = 15, activation = 'relu', input_shape = (TimeSteps, TotalFeatures), return_sequences=True))\n    model.add(LSTM(units = 10, activation = 'relu', input_shape = (TimeSteps, TotalFeatures), return_sequences=True))\n    model.add(LSTM(units = 5, activation = 'relu', return_sequences=False ))\n    model.add(Dropout(0.3))\n    model.add(Dense(units = 1))\n    model.compile(optimizer = 'adam', loss = 'mean_squared_error')\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:39:05.694335Z","iopub.execute_input":"2022-01-19T07:39:05.694679Z","iopub.status.idle":"2022-01-19T07:39:05.703408Z","shell.execute_reply.started":"2022-01-19T07:39:05.694646Z","shell.execute_reply":"2022-01-19T07:39:05.702355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model_CuDNNLSTM(x_train):\n    TimeSteps=x_train.shape[0]\n    TotalFeatures=x_train.shape[2]\n    model = Sequential()\n    model.add(CuDNNLSTM(units = 128, input_shape = (TimeSteps, TotalFeatures), return_sequences=True))\n    model.add(Dropout(0.1))\n    model.add(CuDNNLSTM(units = 64, return_sequences=True))\n    model.add(Dropout(0.1))\n    model.add(Dense(units = 32, activation = 'relu'))\n    model.add(Dropout(0.1))\n    model.add(Dense(units = 16, activation = 'relu' ))\n    model.add(Dropout(0.1))\n    model.add(Dense(units = 8, activation = 'relu' ))\n    model.add(Dropout(0.1))\n    model.add(Dense(units = 1, activation = 'relu'))\n    model.compile(optimizer=tf.optimizers.Adam(0.0001), loss = 'mae', metrics=['mse'])\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:39:05.704973Z","iopub.execute_input":"2022-01-19T07:39:05.705228Z","iopub.status.idle":"2022-01-19T07:39:05.715713Z","shell.execute_reply.started":"2022-01-19T07:39:05.705195Z","shell.execute_reply":"2022-01-19T07:39:05.714929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model_GRU(x_train):\n    TimeSteps=x_train.shape[0]\n    TotalFeatures=x_train.shape[2]\n    model = Sequential()\n    model.add(GRU(units = 10, input_shape = (TimeSteps, TotalFeatures)))\n    model.add(Dropout(0.25))\n    model.add(Dense(units = 1, activation = 'relu'))\n    model.compile(optimizer=tf.optimizers.Adam(0.0001), loss = 'mae', metrics=['mse'])\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:39:05.717075Z","iopub.execute_input":"2022-01-19T07:39:05.717353Z","iopub.status.idle":"2022-01-19T07:39:05.724418Z","shell.execute_reply.started":"2022-01-19T07:39:05.717318Z","shell.execute_reply":"2022-01-19T07:39:05.723610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model_GRU2(train_x):\n    from tensorflow.keras.optimizers import SGD\n    TimeSteps=x_train.shape[0]\n    TotalFeatures=x_train.shape[2]\n    regressorGRU = Sequential()\n    regressorGRU.add(GRU(units=50, return_sequences=True, input_shape=(TimeSteps, TotalFeatures), activation='tanh'))\n    regressorGRU.add(Dropout(0.2))\n    regressorGRU.add(GRU(units=50, return_sequences=True, input_shape=(TimeSteps, TotalFeatures), activation='tanh'))\n    regressorGRU.add(Dropout(0.2))\n    regressorGRU.add(GRU(units=50, return_sequences=True, input_shape=(TimeSteps, TotalFeatures), activation='tanh'))\n    regressorGRU.add(Dropout(0.2))\n    regressorGRU.add(GRU(units=50, activation='tanh'))\n    regressorGRU.add(Dropout(0.2))\n    regressorGRU.add(Dense(units=1))\n    regressorGRU.compile(optimizer=tf.optimizers.Adam(0.0001), loss = 'mae', metrics=['mse'])\n    return regressorGRU","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:39:05.725682Z","iopub.execute_input":"2022-01-19T07:39:05.726384Z","iopub.status.idle":"2022-01-19T07:39:05.736083Z","shell.execute_reply.started":"2022-01-19T07:39:05.726350Z","shell.execute_reply":"2022-01-19T07:39:05.735268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nlist_models  = []\nlist_history = []\nfor dataset in list_asset:\n    asset_name = dataset[0]\n    x_train = dataset[1][0]\n    y_train = dataset[1][1]\n    x_test  = dataset[2][0]\n    y_test  = dataset[2][1]\n    x_train, y_train = reshape(x_train, y_train)\n    print(x_train.shape)\n    print(y_train.shape)\n    # model = get_model_ltsm(x_train)\n    # model = get_model_CuDNNLSTM(x_train)\n    # model = get_model_GRU(x_train)\n    model = get_model_GRU2(x_train)\n    model.summary()\n    checkpoint = ModelCheckpoint(asset_name+'_GRU_{epoch:03d}.h5', verbose=1, monitor='loss', save_best_only=True, mode='auto')\n    StartTime  = time.time()\n    print('Train for ' + asset_name)\n    history = model.fit(x_train, \n              y_train, \n              batch_size = 15, \n              epochs = 5,\n              callbacks = [checkpoint])\n    EndTime = time.time()\n    print(\"## Total \" + asset_name + \" Time Taken: \", round((EndTime-StartTime)/60), 'Minutes ##')\n    list_models.append(model)\n    list_history.append(history)","metadata":{"execution":{"iopub.status.busy":"2022-01-19T07:39:05.737239Z","iopub.execute_input":"2022-01-19T07:39:05.737618Z","iopub.status.idle":"2022-01-19T08:25:59.153662Z","shell.execute_reply.started":"2022-01-19T07:39:05.737577Z","shell.execute_reply":"2022-01-19T08:25:59.152926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(list_history[0].history['loss'])\nlabel_counter = 0\nlist_label = ['Binance Coin', 'Bitcoin', 'Bitcoin Cash', 'Cardano', 'Dogecoin', 'EOS.IO', 'Ethereum', 'Ethereum Classic', 'IOTA', 'Litecoin', 'Maker', 'Monero', 'Stellar', 'TRON']\nfor hist in list_history:\n    fig, ax = plt.subplots()\n    e1 = hist.history['loss'][0]\n    e2 = hist.history['loss'][1]\n    e3 = hist.history['loss'][2]\n    e4 = hist.history['loss'][3]\n    e5 = hist.history['loss'][4]\n    list_y = [e1, e2, e3, e4, e5]\n    list_x = ['E1', 'E2', 'E3', 'E4', 'E5']\n    ax.plot(list_x, list_y, linewidth=2.0)\n    plt.title(list_label[label_counter]+' Loss Value')\n    label_counter += 1","metadata":{"execution":{"iopub.status.busy":"2022-01-19T08:25:59.154954Z","iopub.execute_input":"2022-01-19T08:25:59.155236Z","iopub.status.idle":"2022-01-19T08:26:01.228616Z","shell.execute_reply.started":"2022-01-19T08:25:59.155198Z","shell.execute_reply":"2022-01-19T08:26:01.227924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list_predict = []\nfor asset in list_asset:\n    if asset[0]=='bnc':\n        model = list_models[0]\n        x_test, y_test = reshape(asset[2][0], asset[2][1])\n        predicted_price = model.predict(x_test)\n        original_price  = y_test\n        print('#### Predicted Prices ####')\n        print(predicted_price)\n        print('\\n#### Original Prices ####')\n        print(original_price)\n        list_predict.append([0,predicted_price, original_price])\n    elif asset[0]=='btc':\n        model = list_models[1]\n        x_test, y_test = reshape(asset[2][0], asset[2][1])\n        predicted_price = model.predict(x_test)\n        original_price  = y_test\n        print('#### Predicted Prices ####')\n        print(predicted_price)\n        print('\\n#### Original Prices ####')\n        print(original_price)\n        list_predict.append([1,predicted_price, original_price])\n    elif asset[0]=='btcsh':\n        model = list_models[2]\n        x_test, y_test = reshape(asset[2][0], asset[2][1])\n        predicted_price = model.predict(x_test)\n        original_price  = y_test\n        print('#### Predicted Prices ####')\n        print(predicted_price)\n        print('\\n#### Original Prices ####')\n        print(original_price)\n        list_predict.append([2,predicted_price, original_price])\n    elif asset[0]=='car':\n        model = list_models[3]\n        x_test, y_test = reshape(asset[2][0], asset[2][1])\n        predicted_price = model.predict(x_test)\n        original_price  = y_test\n        print('#### Predicted Prices ####')\n        print(predicted_price)\n        print('\\n#### Original Prices ####')\n        print(original_price)\n        list_predict.append([3,predicted_price, original_price])\n    elif asset[0]=='dog':\n        model = list_models[4]\n        x_test, y_test = reshape(asset[2][0], asset[2][1])\n        predicted_price = model.predict(x_test)\n        original_price  = y_test\n        print('#### Predicted Prices ####')\n        print(predicted_price)\n        print('\\n#### Original Prices ####')\n        print(original_price)\n        list_predict.append([4,predicted_price, original_price])\n    elif asset[0]=='eos':\n        model = list_models[5]\n        x_test, y_test = reshape(asset[2][0], asset[2][1])\n        predicted_price = model.predict(x_test)\n        original_price  = y_test\n        print('#### Predicted Prices ####')\n        print(predicted_price)\n        print('\\n#### Original Prices ####')\n        print(original_price)\n        list_predict.append([5,predicted_price, original_price])\n    elif asset[0]=='eth':\n        model = list_models[6]\n        x_test, y_test = reshape(asset[2][0], asset[2][1])\n        predicted_price = model.predict(x_test)\n        original_price  = y_test\n        print('#### Predicted Prices ####')\n        print(predicted_price)\n        print('\\n#### Original Prices ####')\n        print(original_price)\n        list_predict.append([6,predicted_price, original_price])\n    elif asset[0]=='eth_csc':\n        model = list_models[7]\n        x_test, y_test = reshape(asset[2][0], asset[2][1])\n        predicted_price = model.predict(x_test)\n        original_price  = y_test\n        print('#### Predicted Prices ####')\n        print(predicted_price)\n        print('\\n#### Original Prices ####')\n        print(original_price)\n        list_predict.append([7,predicted_price, original_price])\n    elif asset[0]=='iot':\n        model = list_models[8]\n        x_test, y_test = reshape(asset[2][0], asset[2][1])\n        predicted_price = model.predict(x_test)\n        original_price  = y_test\n        print('#### Predicted Prices ####')\n        print(predicted_price)\n        print('\\n#### Original Prices ####')\n        print(original_price)\n        list_predict.append([8,predicted_price, original_price])\n    elif asset[0]=='ltc':\n        model = list_models[9]\n        x_test, y_test = reshape(asset[2][0], asset[2][1])\n        predicted_price = model.predict(x_test)\n        original_price  = y_test\n        print('#### Predicted Prices ####')\n        print(predicted_price)\n        print('\\n#### Original Prices ####')\n        print(original_price)\n        list_predict.append([9,predicted_price, original_price])\n    elif asset[0]=='mak':\n        model = list_models[10]\n        x_test, y_test = reshape(asset[2][0], asset[2][1])\n        predicted_price = model.predict(x_test)\n        original_price  = y_test\n        print('#### Predicted Prices ####')\n        print(predicted_price)\n        print('\\n#### Original Prices ####')\n        print(original_price)\n        list_predict.append([10,predicted_price, original_price])\n    elif asset[0]=='mon':\n        model = list_models[11]\n        x_test, y_test = reshape(asset[2][0], asset[2][1])\n        predicted_price = model.predict(x_test)\n        original_price  = y_test\n        print('#### Predicted Prices ####')\n        print(predicted_price)\n        print('\\n#### Original Prices ####')\n        print(original_price)\n        list_predict.append([11,predicted_price, original_price])\n    elif asset[0]=='ste':\n        model = list_models[12]\n        x_test, y_test = reshape(asset[2][0], asset[2][1])\n        predicted_price = model.predict(x_test)\n        original_price  = y_test\n        print('#### Predicted Prices ####')\n        print(predicted_price)\n        print('\\n#### Original Prices ####')\n        print(original_price)\n        list_predict.append([12,predicted_price, original_price])\n    elif asset[0]=='tro':\n        model = list_models[13]\n        x_test, y_test = reshape(asset[2][0], asset[2][1])\n        predicted_price = model.predict(x_test)\n        original_price  = y_test\n        print('#### Predicted Prices ####')\n        print(predicted_price)\n        print('\\n#### Original Prices ####')\n        print(original_price)\n        list_predict.append([13,predicted_price, original_price])","metadata":{"execution":{"iopub.status.busy":"2022-01-19T08:26:01.230049Z","iopub.execute_input":"2022-01-19T08:26:01.230310Z","iopub.status.idle":"2022-01-19T08:27:55.799787Z","shell.execute_reply.started":"2022-01-19T08:26:01.230261Z","shell.execute_reply":"2022-01-19T08:27:55.798971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #Making predictions on test data\n# model.load_weights('./'+'bnc_GRU_'+'.h5')\n# predicted_Price = model.predict(x_test)\n# #predicted_Price = DataScaler.inverse_transform(predicted_Price)\n# print('#### Predicted Prices ####')\n# print(predicted_Price)\n \n# # Getting the original price values for testing data\n# orig=y_test\n# #orig=DataScaler.inverse_transform(y_test)\n# print('\\n#### Original Prices ####')\n# print(orig)","metadata":{"execution":{"iopub.status.busy":"2022-01-19T08:27:55.801385Z","iopub.execute_input":"2022-01-19T08:27:55.801682Z","iopub.status.idle":"2022-01-19T08:27:55.805872Z","shell.execute_reply.started":"2022-01-19T08:27:55.801642Z","shell.execute_reply":"2022-01-19T08:27:55.805070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# len(list_models)","metadata":{"execution":{"iopub.status.busy":"2022-01-19T08:27:55.807276Z","iopub.execute_input":"2022-01-19T08:27:55.807537Z","iopub.status.idle":"2022-01-19T08:27:55.819156Z","shell.execute_reply.started":"2022-01-19T08:27:55.807503Z","shell.execute_reply":"2022-01-19T08:27:55.818279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def x_feature(x_samples):\n    # Select some input features from the trading data: \n    # 5 min log return, abs(5 min log return), upper shadow, and lower shadow.\n    upper_shadow = lambda asset: asset.High - np.maximum(asset.Close,asset.Open)\n    lower_shadow = lambda asset: np.minimum(asset.Close,asset.Open)- asset.Low\n\n    X_samples = pd.concat([log_return(x_samples.VWAP,periods=15), log_return(x_samples.VWAP,periods=1).abs(), \n                   upper_shadow(x_samples), lower_shadow(x_samples)], axis=1)\n    \n    return X_samples","metadata":{"execution":{"iopub.status.busy":"2022-01-19T08:27:55.820246Z","iopub.execute_input":"2022-01-19T08:27:55.820584Z","iopub.status.idle":"2022-01-19T08:27:55.828530Z","shell.execute_reply.started":"2022-01-19T08:27:55.820544Z","shell.execute_reply":"2022-01-19T08:27:55.827831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# len(list_history)","metadata":{"execution":{"iopub.status.busy":"2022-01-19T08:27:55.829769Z","iopub.execute_input":"2022-01-19T08:27:55.830046Z","iopub.status.idle":"2022-01-19T08:27:55.837102Z","shell.execute_reply.started":"2022-01-19T08:27:55.830007Z","shell.execute_reply":"2022-01-19T08:27:55.836321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gresearch_crypto\nenv = gresearch_crypto.make_env()\niter_test = env.iter_test()\n\nfor i, (df_test, df_pred) in enumerate(iter_test):\n    for _, row in df_test.iterrows():\n        asset_id = int(row['Asset_ID'])\n        if list_models[asset_id] is not None:\n            try:\n                model  = list_models[asset_id]\n                x_test = x_feature(row)\n                y_pred = model.predict(pd.DataFrame([x_test]))[0]\n                df_pred.loc[df_pred['row_id'] == row['row_id'], 'Target'] = y_pred\n            except:\n                df_pred.loc[df_pred['row_id'] == row['row_id'], 'Target'] = 0\n        else: \n            df_pred.loc[df_pred['row_id'] == row['row_id'], 'Target'] = 0\n        \n    env.predict(df_pred)","metadata":{"execution":{"iopub.status.busy":"2022-01-19T08:27:55.838448Z","iopub.execute_input":"2022-01-19T08:27:55.838797Z","iopub.status.idle":"2022-01-19T08:27:55.914435Z","shell.execute_reply.started":"2022-01-19T08:27:55.838764Z","shell.execute_reply":"2022-01-19T08:27:55.913800Z"},"trusted":true},"execution_count":null,"outputs":[]}]}