{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfrom lightgbm import LGBMRegressor\n\nos.chdir('/kaggle/input/g-research-crypto-forecasting')\nos.listdir()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-10T19:45:29.761947Z","iopub.execute_input":"2021-11-10T19:45:29.762527Z","iopub.status.idle":"2021-11-10T19:45:31.868156Z","shell.execute_reply.started":"2021-11-10T19:45:29.762423Z","shell.execute_reply":"2021-11-10T19:45:31.867293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_df = pd.read_csv('train.csv')\nassets_details_data_df = pd.read_csv('asset_details.csv')","metadata":{"execution":{"iopub.status.busy":"2021-11-10T19:46:34.310611Z","iopub.execute_input":"2021-11-10T19:46:34.311208Z","iopub.status.idle":"2021-11-10T19:47:40.402558Z","shell.execute_reply.started":"2021-11-10T19:46:34.311164Z","shell.execute_reply":"2021-11-10T19:47:40.401552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-10T19:47:48.51573Z","iopub.execute_input":"2021-11-10T19:47:48.516549Z","iopub.status.idle":"2021-11-10T19:47:48.541896Z","shell.execute_reply.started":"2021-11-10T19:47:48.51649Z","shell.execute_reply":"2021-11-10T19:47:48.541327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"assets_details_data_df","metadata":{"execution":{"iopub.status.busy":"2021-11-10T19:47:55.457636Z","iopub.execute_input":"2021-11-10T19:47:55.458072Z","iopub.status.idle":"2021-11-10T19:47:55.468526Z","shell.execute_reply.started":"2021-11-10T19:47:55.45803Z","shell.execute_reply":"2021-11-10T19:47:55.467727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def upper_bound(data):\n    return data['High'] - np.maximum(data['Close'], data['Open'])","metadata":{"execution":{"iopub.status.busy":"2021-11-10T19:48:07.252653Z","iopub.execute_input":"2021-11-10T19:48:07.253062Z","iopub.status.idle":"2021-11-10T19:48:07.257256Z","shell.execute_reply.started":"2021-11-10T19:48:07.253031Z","shell.execute_reply":"2021-11-10T19:48:07.256332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def lower_bound(data):\n    return np.minimum(data['Close'], data['Open']) - data['Low']","metadata":{"execution":{"iopub.status.busy":"2021-11-10T19:48:12.455827Z","iopub.execute_input":"2021-11-10T19:48:12.456092Z","iopub.status.idle":"2021-11-10T19:48:12.460346Z","shell.execute_reply.started":"2021-11-10T19:48:12.456064Z","shell.execute_reply":"2021-11-10T19:48:12.459474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Building features from the original data Frame\ndef get_feature_df(data):\n    features_df = data[['Count', 'Open', 'High', 'Low', 'Close', 'Volume', 'VWAP']].copy()\n    features_df['Upper_Shadow'] = upper_bound(features_df)\n    features_df['Lower_Shadow'] = lower_bound(features_df)\n    return features_df\n\ndef get_x_Y_and_assets(df_train, asset_id):\n    df =df_train[df_train['Asset_ID'] == asset_id]\n    \n    new_df = get_feature_df(df)\n    new_df['y'] = df['Target']\n    new_df = new_df.dropna(how=\"any\")\n    \n    X = new_df.drop(\"y\", axis=1)\n    y = new_df[\"y\"]\n    \n    model = LGBMRegressor(n_estimators=800)\n    model.fit(X, y)\n    return X, y, model","metadata":{"execution":{"iopub.status.busy":"2021-11-10T19:48:19.477386Z","iopub.execute_input":"2021-11-10T19:48:19.477926Z","iopub.status.idle":"2021-11-10T19:48:19.485478Z","shell.execute_reply.started":"2021-11-10T19:48:19.477875Z","shell.execute_reply":"2021-11-10T19:48:19.484858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Iterating over assets\nX_s = {}\ny_s = {}\nmodels = {}\nfor asset_id, asset_name in zip(assets_details_data_df['Asset_ID'], assets_details_data_df['Asset_Name']):\n    print(f\"Training model for {asset_name:<18} (ID={asset_id:<2})\")\n    X, y, model = get_x_Y_and_assets(training_df, asset_id)\n    X_s[asset_id], y_s[asset_id], models[asset_id] = X, y, model","metadata":{"execution":{"iopub.status.busy":"2021-11-10T19:48:33.765817Z","iopub.execute_input":"2021-11-10T19:48:33.766689Z","iopub.status.idle":"2021-11-10T19:53:35.664476Z","shell.execute_reply.started":"2021-11-10T19:48:33.766645Z","shell.execute_reply":"2021-11-10T19:53:35.663659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking the model\nx = get_feature_df(training_df.iloc[1])\ny_pred = models[0].predict([x])\ny_pred[0]","metadata":{"execution":{"iopub.status.busy":"2021-11-10T19:58:57.580643Z","iopub.execute_input":"2021-11-10T19:58:57.582002Z","iopub.status.idle":"2021-11-10T19:58:57.642138Z","shell.execute_reply.started":"2021-11-10T19:58:57.581952Z","shell.execute_reply":"2021-11-10T19:58:57.641311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# os.chdir('/kaggle/input/g-research-crypto-forecasting/gresearch_crypto')\n# os.listdir()\n##import pdb; pdb.set_trace()","metadata":{"execution":{"iopub.status.busy":"2021-11-04T12:27:35.8412Z","iopub.execute_input":"2021-11-04T12:27:35.841657Z","iopub.status.idle":"2021-11-04T12:27:35.84837Z","shell.execute_reply.started":"2021-11-04T12:27:35.841621Z","shell.execute_reply":"2021-11-04T12:27:35.847364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gresearch_crypto\n#env = gresearch_crypto.make_env()\n#iter_test = env.iter_test()\n\nfor i, (test_df, pred_df) in enumerate(iter_test):\n    for j , row in test_df.iterrows():\n        \n        model = models[row['Asset_ID']]\n        x_test = get_feature_df(row)\n        y_pred = model.predict([x_test])[0]\n        \n        pred_df.loc[pred_df['row_id'] == row['row_id'], 'Target'] = y_pred\n        \n        if i == 0 and j == 0:\n            display(x_test)\n    if i == 0:\n        display(pred_df)\n\n    # Send submissions\n    env.predict(pred_df)","metadata":{"execution":{"iopub.status.busy":"2021-11-10T20:02:11.155162Z","iopub.execute_input":"2021-11-10T20:02:11.156182Z","iopub.status.idle":"2021-11-10T20:02:11.164345Z","shell.execute_reply.started":"2021-11-10T20:02:11.156133Z","shell.execute_reply":"2021-11-10T20:02:11.16318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#pred_df\n#.to_csv(\"submission.csv\")\n#example_df = pd.read_csv('example_sample_submission.csv')\n#example_df\npred_df.to_csv(\"/kaggle/working/submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-11-10T20:02:16.29921Z","iopub.execute_input":"2021-11-10T20:02:16.299493Z","iopub.status.idle":"2021-11-10T20:02:16.304389Z","shell.execute_reply.started":"2021-11-10T20:02:16.299464Z","shell.execute_reply":"2021-11-10T20:02:16.30379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}