{"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 os\nimport random\nimport pandas as pd\nimport numpy as np\nimport lightgbm as lgb\nimport gresearch_crypto\nimport time\nimport datetime\nimport datatable as dt\n\nTRAIN_CSV = '/kaggle/input/g-research-crypto-forecasting/train.csv'\nASSET_DETAILS_CSV = '/kaggle/input/g-research-crypto-forecasting/asset_details.csv'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-14T11:50:42.261758Z","iopub.execute_input":"2022-01-14T11:50:42.26236Z","iopub.status.idle":"2022-01-14T11:50:44.165648Z","shell.execute_reply.started":"2022-01-14T11:50:42.262241Z","shell.execute_reply":"2022-01-14T11:50:44.164834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = dt.fread(TRAIN_CSV).to_pandas()\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-14T11:51:17.823119Z","iopub.execute_input":"2022-01-14T11:51:17.823753Z","iopub.status.idle":"2022-01-14T11:51:49.414103Z","shell.execute_reply.started":"2022-01-14T11:51:17.823721Z","shell.execute_reply":"2022-01-14T11:51:49.413268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_asset_details = dt.fread(ASSET_DETAILS_CSV).to_pandas().sort_values(\"Asset_ID\")\ndf_asset_details.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-14T11:51:57.626101Z","iopub.execute_input":"2022-01-14T11:51:57.626818Z","iopub.status.idle":"2022-01-14T11:51:57.656094Z","shell.execute_reply.started":"2022-01-14T11:51:57.626763Z","shell.execute_reply":"2022-01-14T11:51:57.655243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Feature extraction**","metadata":{}},{"cell_type":"code","source":"def hlco_ratio(df): return (df['High'] - df['Low'])/(df['Close']-df['Open'])\ndef upper_shadow(df): return df['High'] - np.maximum(df['Close'], df['Open'])\ndef lower_shadow(df): return np.minimum(df['Close'], df['Open']) - df['Low']\n\n\ndef get_features(df):\n    df_feat = df[['Count', 'Open', 'High', 'Low', 'Close', 'Volume', 'VWAP']].copy()\n    df_feat['Upper_Shadow'] = upper_shadow(df_feat)\n    df_feat['Lower_Shadow'] = lower_shadow(df_feat)\n    df_feat['hlco_ration'] = hlco_ratio(df_feat)\n    return df_feat","metadata":{"execution":{"iopub.status.busy":"2022-01-14T11:52:00.495721Z","iopub.execute_input":"2022-01-14T11:52:00.496319Z","iopub.status.idle":"2022-01-14T11:52:00.504354Z","shell.execute_reply.started":"2022-01-14T11:52:00.496275Z","shell.execute_reply":"2022-01-14T11:52:00.503484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_Xy_and_model_for_asset(df_train, asset_id):\n    df = df_train[df_train[\"Asset_ID\"] == asset_id]\n    \n    \n    df_proc = get_features(df)\n    df_proc['y'] = df['Target']\n    df_proc = df_proc.dropna(how=\"any\")\n    \n    X = df_proc.drop(\"y\", axis=1)\n    y = df_proc[\"y\"]    \n    model = LGBMRegressor(device = 'gpu')\n    model.fit(X, y)\n    return X, y, model","metadata":{"execution":{"iopub.status.busy":"2022-01-14T11:52:30.961713Z","iopub.execute_input":"2022-01-14T11:52:30.962074Z","iopub.status.idle":"2022-01-14T11:52:30.969057Z","shell.execute_reply.started":"2022-01-14T11:52:30.962036Z","shell.execute_reply":"2022-01-14T11:52:30.96808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xs = {}\nys = {}\nmodels = {}\n\nfor asset_id, asset_name in zip(df_asset_details['Asset_ID'], df_asset_details['Asset_Name']):\n    print(f\"Training model for {asset_name:<16} (ID={asset_id:<2})\")\n    try:\n        X, y, model = get_Xy_and_model_for_asset(df_train, asset_id)    \n        Xs[asset_id], ys[asset_id], models[asset_id] = X, y, model\n    except:         \n        Xs[asset_id], ys[asset_id], models[asset_id] = None, None, None ","metadata":{"execution":{"iopub.status.busy":"2022-01-14T11:10:41.208423Z","iopub.execute_input":"2022-01-14T11:10:41.208694Z","iopub.status.idle":"2022-01-14T11:10:48.129506Z","shell.execute_reply.started":"2022-01-14T11:10:41.208643Z","shell.execute_reply":"2022-01-14T11:10:48.128166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = get_features(df_train.iloc[1])\ny_pred = models[0].predict(pd.DataFrame([x]))\ny_pred[0]","metadata":{"execution":{"iopub.status.busy":"2022-01-14T11:13:13.197754Z","iopub.execute_input":"2022-01-14T11:13:13.198159Z","iopub.status.idle":"2022-01-14T11:13:13.22102Z","shell.execute_reply.started":"2022-01-14T11:13:13.198131Z","shell.execute_reply":"2022-01-14T11:13:13.220151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}