{"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":"markdown","source":"# Load Data","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"\nimport pandas as pd\nimport numpy as np\n\nimport plotly.express as px\n","metadata":{"execution":{"iopub.status.busy":"2021-11-28T05:14:53.739171Z","iopub.execute_input":"2021-11-28T05:14:53.739873Z","iopub.status.idle":"2021-11-28T05:14:53.746058Z","shell.execute_reply.started":"2021-11-28T05:14:53.739815Z","shell.execute_reply":"2021-11-28T05:14:53.744868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('../input/g-research-crypto-forecasting/train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T05:14:53.917179Z","iopub.execute_input":"2021-11-28T05:14:53.917497Z","iopub.status.idle":"2021-11-28T05:15:38.750406Z","shell.execute_reply.started":"2021-11-28T05:14:53.917463Z","shell.execute_reply":"2021-11-28T05:15:38.749699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['y']=df['Target']\ndf=df.drop(columns=['Target'])","metadata":{"execution":{"iopub.status.busy":"2021-11-28T05:15:38.75203Z","iopub.execute_input":"2021-11-28T05:15:38.752412Z","iopub.status.idle":"2021-11-28T05:15:40.683796Z","shell.execute_reply.started":"2021-11-28T05:15:38.752373Z","shell.execute_reply":"2021-11-28T05:15:40.683049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"asset_details=pd.read_csv('../input/g-research-crypto-forecasting/asset_details.csv')\nasset_details.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T05:15:40.685144Z","iopub.execute_input":"2021-11-28T05:15:40.685497Z","iopub.status.idle":"2021-11-28T05:15:40.701031Z","shell.execute_reply.started":"2021-11-28T05:15:40.68546Z","shell.execute_reply":"2021-11-28T05:15:40.700199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dic=dict(zip(asset_details['Asset_ID'],asset_details['Asset_Name']))","metadata":{"execution":{"iopub.status.busy":"2021-11-28T05:15:40.703215Z","iopub.execute_input":"2021-11-28T05:15:40.703497Z","iopub.status.idle":"2021-11-28T05:15:40.708131Z","shell.execute_reply.started":"2021-11-28T05:15:40.703461Z","shell.execute_reply":"2021-11-28T05:15:40.707426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plot","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set()\nsubplots=plt.subplots(14,1,figsize=(10,40))[1]\nfor asset in range(14):\n    crop=df[df['Asset_ID']==asset]\n    subplots[asset].plot(pd.to_datetime(crop['timestamp'],unit='s').dt.date,crop['High']-np.min(crop['High'])/(np.max(crop['High'])-np.min(crop['High'])),label=dic[asset])\n    subplots[asset].legend()\n\nplt.show()\nplt.close()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T05:15:40.709415Z","iopub.execute_input":"2021-11-28T05:15:40.710095Z","iopub.status.idle":"2021-11-28T05:16:53.596032Z","shell.execute_reply.started":"2021-11-28T05:15:40.710038Z","shell.execute_reply":"2021-11-28T05:16:53.595346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def reduce_mem_usage(df):\n    \"\"\" iterate through all the columns of a dataframe and modify the data type\n        to reduce memory usage.\n    \"\"\"\n    start_mem = df.memory_usage().sum() / 1024**2\n    print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))\n\n    for col in df.columns:\n        col_type = df[col].dtype.name\n        \n        if col_type not in ['object', 'category', 'datetime64[ns, UTC]', 'datetime64[ns]']:\n            c_min = df[col].min()\n            c_max = df[col].max()\n            if str(col_type)[:3] == 'int':\n                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n                    df[col] = df[col].astype(np.int16)\n                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n                    df[col] = df[col].astype(np.int16)\n                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n                    df[col] = df[col].astype(np.int32)\n                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n                    df[col] = df[col].astype(np.int64)  \n            else:\n                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n                    df[col] = df[col].astype(np.float16)\n                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n                    df[col] = df[col].astype(np.float32)\n                else:\n                    df[col] = df[col].astype(np.float64)\n\n    end_mem = df.memory_usage().sum() / 1024**2\n    print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))\n    print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))\n\n    return df","metadata":{"execution":{"iopub.status.busy":"2021-11-28T05:16:53.597038Z","iopub.execute_input":"2021-11-28T05:16:53.597331Z","iopub.status.idle":"2021-11-28T05:16:53.61361Z","shell.execute_reply.started":"2021-11-28T05:16:53.597278Z","shell.execute_reply":"2021-11-28T05:16:53.612952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Features","metadata":{}},{"cell_type":"code","source":"def upper_shadow(df):\n    return df['High'] - np.maximum(df['Close'], df['Open'])\n\ndef lower_shadow(df):\n    return np.minimum(df['Close'], df['Open']) - df['Low']\n\ndef add_features(dataframe):\n    dataframe['dat']=pd.to_datetime(dataframe['timestamp'],unit='s')\n    dataframe['date']=dataframe['dat'].dt.date\n    dataframe[\"Day of Week\"] = dataframe['dat'].dt.dayofweek\n    dataframe['weekend']=np.where(dataframe['Day of Week']>4,1,0)\n    date_of_elon_musk_tweet='2018-6-04'\n    dataframe['after']=(dataframe['dat']>date_of_elon_musk_tweet).astype(int)\n    date_of_elon_musk_tweet='2021-03-29'\n    dataframe['afterdoge']=(dataframe['dat']>date_of_elon_musk_tweet).astype(int)\n  \n    dataframe['Month']=dataframe['dat'].dt.month\n    dataframe['upper_Shadow'] = upper_shadow(dataframe)\n    dataframe['lower_Shadow'] = lower_shadow(dataframe)\n    dataframe[\"high_div_low\"] = dataframe[\"High\"] / dataframe[\"Low\"]\n    #dataframe[\"open_sub_close\"] = dataframe[\"Open\"] - dataframe[\"Close\"]\n    dataframe['trade']=dataframe['Close']-dataframe['Open']\n    dataframe['gtrade']=dataframe['trade']/dataframe['Count']\n    dataframe['shadow1']=dataframe['trade']/dataframe['Volume']\n    #dataframe['shadow2']=dataframe['upper_Shadow']/df['Low']\n    dataframe['shadow3']=dataframe['upper_Shadow']/dataframe['Volume']\n    #dataframe['shadow4']=dataframe['lower_Shadow']/dataframe['High']\n    dataframe['shadow5']=dataframe['lower_Shadow']/dataframe['Volume']\n    \n \n    return dataframe.drop(columns=['dat','date'])\ndf=add_features(reduce_mem_usage(df))\ndf.head()\n","metadata":{"execution":{"iopub.status.busy":"2021-11-28T05:16:53.615905Z","iopub.execute_input":"2021-11-28T05:16:53.61613Z","iopub.status.idle":"2021-11-28T05:17:23.029048Z","shell.execute_reply.started":"2021-11-28T05:16:53.616088Z","shell.execute_reply":"2021-11-28T05:17:23.027606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def draw_card(df1,df2=False):\n    for column in list(df1.keys()):\n      \n        \n        try:\n            column2=df2[column]\n            print(f'|{column} : {np.mean(np.nan_to_num(df1[column]))-np.mean(np.nan_to_num(column2))}|')\n        except:\n            try:\n                print(f'|{column} : {np.mean(np.nan_to_num(df1[column]))}|')\n            except:\n                pass\n                    \n        ","metadata":{"execution":{"iopub.status.busy":"2021-11-28T05:32:03.280715Z","iopub.execute_input":"2021-11-28T05:32:03.281038Z","iopub.status.idle":"2021-11-28T05:32:03.291767Z","shell.execute_reply.started":"2021-11-28T05:32:03.281002Z","shell.execute_reply":"2021-11-28T05:32:03.29107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Weekend\n\nIn weekend as it is a holiday Count feature or number of trades will decrease and price in return will decrease because crypto follows  only 1 rule 'low demand low price high demand high price'","metadata":{}},{"cell_type":"code","source":"for asset in range(14):\n    crop=df[df['Asset_ID']==asset]\n    print(dic[asset])\n    crop=crop.dropna(how='any')\n\n    \n    \n    draw_card(crop[crop['weekend']==1],crop[crop['weekend']==0])\n","metadata":{"execution":{"iopub.status.busy":"2021-11-28T05:32:03.953582Z","iopub.execute_input":"2021-11-28T05:32:03.953867Z","iopub.status.idle":"2021-11-28T05:32:24.774472Z","shell.execute_reply.started":"2021-11-28T05:32:03.953819Z","shell.execute_reply":"2021-11-28T05:32:24.773745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2 elon musk tweets \n\n'You can now buy tesla with bitcoin' - We all know what happened to crypto price , come on it went to space\n\n'Doge to the moon' - Dodgecoin price increased by 30 %\n\n\nObserve the differences\n","metadata":{}},{"cell_type":"code","source":"print('Bitcoin After Elon')\nasset=1\ncrop=df[df['Asset_ID']==asset]\nprint('|'+dic[asset]+'|')\n    \ndraw_card(crop[crop['after']==1],crop[crop['after']==0])\n\nprint('-------\\nDogecoin after Elon')\n\nasset=4\ncrop=df[df['Asset_ID']==asset]\nprint('|'+dic[asset]+'|')\n    \ndraw_card(crop[crop['afterdoge']==1],crop[crop['afterdoge']==0])\n","metadata":{"execution":{"iopub.status.busy":"2021-11-28T05:34:05.763132Z","iopub.execute_input":"2021-11-28T05:34:05.763614Z","iopub.status.idle":"2021-11-28T05:34:07.600367Z","shell.execute_reply.started":"2021-11-28T05:34:05.763576Z","shell.execute_reply":"2021-11-28T05:34:07.599648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Months\n\nNot quite sure why this happens but mainly i think because natural disasters are prone at certain months which can effect crypto price","metadata":{}},{"cell_type":"code","source":"for asset in range(1,13):\n    print(dic[asset])\n  \n\n    \n    \n    draw_card(df[df['Month']==asset])\n","metadata":{"execution":{"iopub.status.busy":"2021-11-28T05:35:24.813332Z","iopub.execute_input":"2021-11-28T05:35:24.814237Z","iopub.status.idle":"2021-11-28T05:35:32.289432Z","shell.execute_reply.started":"2021-11-28T05:35:24.814198Z","shell.execute_reply":"2021-11-28T05:35:32.288684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training\n\nhttps://www.kaggle.com/swaralipibose/new-features-training-notebook-and-feature-eval","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}