{"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 pandas as pd\nimport numpy as np\nfrom datetime import datetime\nimport os\nimport matplotlib.pyplot as plt\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-17T13:48:31.419474Z","iopub.execute_input":"2021-11-17T13:48:31.419924Z","iopub.status.idle":"2021-11-17T13:48:31.436834Z","shell.execute_reply.started":"2021-11-17T13:48:31.419794Z","shell.execute_reply":"2021-11-17T13:48:31.436008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_path = '../input/g-research-crypto-forecasting'\nassets = pd.read_csv(os.path.join(data_path, 'asset_details.csv'))\ntrain_df = pd.read_csv(os.path.join(data_path, 'train.csv'))\ntrain_df['asset_name'] = train_df.Asset_ID.map(assets.set_index('Asset_ID').Asset_Name)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:48:33.076726Z","iopub.execute_input":"2021-11-17T13:48:33.077588Z","iopub.status.idle":"2021-11-17T13:49:25.253048Z","shell.execute_reply.started":"2021-11-17T13:48:33.077532Z","shell.execute_reply":"2021-11-17T13:49:25.251980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['Target'] = train_df['Target'].fillna(0)\ntrain_df['VWAP'] = train_df['VWAP'].fillna(0)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:50:55.295374Z","iopub.execute_input":"2021-11-17T13:50:55.295715Z","iopub.status.idle":"2021-11-17T13:50:55.790406Z","shell.execute_reply.started":"2021-11-17T13:50:55.295683Z","shell.execute_reply":"2021-11-17T13:50:55.789447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\ntotimestamp = lambda s: np.int32(time.mktime(datetime.strptime(s, \"%d/%m/%Y\").timetuple()))","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:50:58.237542Z","iopub.execute_input":"2021-11-17T13:50:58.237830Z","iopub.status.idle":"2021-11-17T13:50:58.243392Z","shell.execute_reply.started":"2021-11-17T13:50:58.237801Z","shell.execute_reply":"2021-11-17T13:50:58.242233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bit = train_df[train_df.Asset_ID == 1].set_index('timestamp')\nbit = bit.loc[totimestamp('01/01/2021'):totimestamp('01/04/2021')]\nbit.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:50:59.394989Z","iopub.execute_input":"2021-11-17T13:50:59.395284Z","iopub.status.idle":"2021-11-17T13:50:59.879276Z","shell.execute_reply.started":"2021-11-17T13:50:59.395252Z","shell.execute_reply":"2021-11-17T13:50:59.878551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install stockstats\nfrom stockstats import StockDataFrame","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:51:01.904409Z","iopub.execute_input":"2021-11-17T13:51:01.904935Z","iopub.status.idle":"2021-11-17T13:51:10.903828Z","shell.execute_reply.started":"2021-11-17T13:51:01.904861Z","shell.execute_reply":"2021-11-17T13:51:10.902677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bit.drop(['Asset_ID','asset_name', 'VWAP', 'Target'], axis=1, inplace=True)\nbit.rename(columns = {'Count':'amount','Open':'open','High':'high','Low':'low',\n                     'Close':'close','Volume':'volume'}, inplace=True)\nbit.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:51:10.907444Z","iopub.execute_input":"2021-11-17T13:51:10.907745Z","iopub.status.idle":"2021-11-17T13:51:10.931224Z","shell.execute_reply.started":"2021-11-17T13:51:10.907710Z","shell.execute_reply":"2021-11-17T13:51:10.930308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stock = StockDataFrame.retype(bit)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:51:10.932595Z","iopub.execute_input":"2021-11-17T13:51:10.932848Z","iopub.status.idle":"2021-11-17T13:51:10.944399Z","shell.execute_reply.started":"2021-11-17T13:51:10.932815Z","shell.execute_reply":"2021-11-17T13:51:10.943646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"KDJ_WINDOW = 10\nBOLL_WINDOW = 10\nMACD_EMA_SHORT = 10\nPDI_SMMA = 10\nMDI_SMMA = 10\nDX_SMMA = 10\nADX_EMA = 5\nADXR_EMA = 10\nTRIX_EMA_WINDOW = 10\nTEMA_EMA_WINDOW = 10","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:51:10.946442Z","iopub.execute_input":"2021-11-17T13:51:10.946741Z","iopub.status.idle":"2021-11-17T13:51:10.956615Z","shell.execute_reply.started":"2021-11-17T13:51:10.946707Z","shell.execute_reply":"2021-11-17T13:51:10.955760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bit['volume_delta'] = stock['volume_delta']\nbit['open_-2_r'] = stock['open_-2_r']\nbit['cr']= stock['cr']\nbit['kdjk'] = stock['kdjk']\nbit['open_2_sma'] = stock['open_2_sma']\nbit['macd'] = stock['macd']\nbit['boll'] = stock['boll']\nbit['boll_ub'] = stock['boll_ub']\nbit['boll_lb'] = stock['boll_lb']\nbit['rsi_12'] = stock['rsi_12']\nbit['wr_10'] = stock['wr_10']\nbit['cci'] = stock['cci']\nbit['tr'] = stock['tr']\nbit['atr'] = stock['atr']\nbit['dma'] = stock['dma']\nbit['pdi'] = stock['pdi']\nbit['dx'] = stock['dx']\nbit['adx'] = stock['adx']\nbit['adxr'] = stock['adxr']\nbit['trix'] = stock['trix']\nbit['tema'] = stock['tema']\nbit['vr'] = stock['vr']","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:51:10.957690Z","iopub.execute_input":"2021-11-17T13:51:10.958156Z","iopub.status.idle":"2021-11-17T13:51:57.515567Z","shell.execute_reply.started":"2021-11-17T13:51:10.958104Z","shell.execute_reply":"2021-11-17T13:51:57.514376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bit.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:51:57.517784Z","iopub.execute_input":"2021-11-17T13:51:57.518394Z","iopub.status.idle":"2021-11-17T13:51:57.550171Z","shell.execute_reply.started":"2021-11-17T13:51:57.518348Z","shell.execute_reply":"2021-11-17T13:51:57.549312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bit['target'] = bit.open.shift(-1) - bit.close\nbit['target'] = bit.target.apply(lambda x:1 if x>0 else 0)\nbit.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:51:57.551471Z","iopub.execute_input":"2021-11-17T13:51:57.551720Z","iopub.status.idle":"2021-11-17T13:51:57.676644Z","shell.execute_reply.started":"2021-11-17T13:51:57.551689Z","shell.execute_reply":"2021-11-17T13:51:57.675700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bit.target.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:51:57.677957Z","iopub.execute_input":"2021-11-17T13:51:57.678229Z","iopub.status.idle":"2021-11-17T13:51:57.687422Z","shell.execute_reply.started":"2021-11-17T13:51:57.678197Z","shell.execute_reply":"2021-11-17T13:51:57.686531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bit = bit.dropna()","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:53:02.229385Z","iopub.execute_input":"2021-11-17T13:53:02.229731Z","iopub.status.idle":"2021-11-17T13:53:02.268294Z","shell.execute_reply.started":"2021-11-17T13:53:02.229691Z","shell.execute_reply":"2021-11-17T13:53:02.267204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = bit.drop(['target'],axis=1).values\ny = bit.target.values","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:53:05.907031Z","iopub.execute_input":"2021-11-17T13:53:05.907358Z","iopub.status.idle":"2021-11-17T13:53:05.922224Z","shell.execute_reply.started":"2021-11-17T13:53:05.907322Z","shell.execute_reply":"2021-11-17T13:53:05.921290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size = int(len(X)*0.70)\ntrain, test = X[0:train_size], X[train_size:len(X)]\ny_train, y_test = y[0:train_size], y[train_size:len(X)]","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:53:08.264260Z","iopub.execute_input":"2021-11-17T13:53:08.264931Z","iopub.status.idle":"2021-11-17T13:53:08.271485Z","shell.execute_reply.started":"2021-11-17T13:53:08.264859Z","shell.execute_reply":"2021-11-17T13:53:08.270634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nlogistic_model = LogisticRegression(solver='liblinear')\nlogistic_model.fit(train, y_train)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:54:11.462988Z","iopub.execute_input":"2021-11-17T13:54:11.463324Z","iopub.status.idle":"2021-11-17T13:54:16.644066Z","shell.execute_reply.started":"2021-11-17T13:54:11.463291Z","shell.execute_reply":"2021-11-17T13:54:16.642835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import recall_score\nfrom sklearn.metrics import precision_score\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.metrics import confusion_matrix","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:58:44.881308Z","iopub.execute_input":"2021-11-17T13:58:44.881621Z","iopub.status.idle":"2021-11-17T13:58:44.886474Z","shell.execute_reply.started":"2021-11-17T13:58:44.881588Z","shell.execute_reply":"2021-11-17T13:58:44.885494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = logistic_model.predict(test)\naccuracy = accuracy_score(y_test, y_pred)\nprint(f'Accuracy is :{accuracy}')\nprecision = precision_score(y_test, y_pred, average='binary')\nprint(f'Precision is :{precision}')\nrecall = recall_score(y_test, y_pred, average='binary')\nprint(f'Recall is :{recall}')\nconfusionmatrix = confusion_matrix(y_test, y_pred)\nprint(confusionmatrix)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:58:59.982222Z","iopub.execute_input":"2021-11-17T13:58:59.982544Z","iopub.status.idle":"2021-11-17T13:59:00.142752Z","shell.execute_reply.started":"2021-11-17T13:58:59.982509Z","shell.execute_reply":"2021-11-17T13:59:00.141474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nrf_model = RandomForestClassifier()\nrf_model.fit(train, y_train)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T14:00:15.583198Z","iopub.execute_input":"2021-11-17T14:00:15.583529Z","iopub.status.idle":"2021-11-17T14:01:47.750463Z","shell.execute_reply.started":"2021-11-17T14:00:15.583498Z","shell.execute_reply":"2021-11-17T14:01:47.748968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = rf_model.predict(test)\naccuracy = accuracy_score(y_test, y_pred)\nprint(f'Accuracy is :{accuracy}')\nprecision = precision_score(y_test, y_pred, average='binary')\nprint(f'Precision is :{precision}')\nrecall = recall_score(y_test, y_pred, average='binary')\nprint(f'Recall is :{recall}')\nconfusionmatrix = confusion_matrix(y_test, y_pred)\nprint(confusionmatrix)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T14:01:47.751909Z","iopub.execute_input":"2021-11-17T14:01:47.752324Z","iopub.status.idle":"2021-11-17T14:01:48.950277Z","shell.execute_reply.started":"2021-11-17T14:01:47.752290Z","shell.execute_reply":"2021-11-17T14:01:48.949246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from xgboost import XGBClassifier\nxgb_model = XGBClassifier(n_estimators=500, alpha=0)\nxgb_model.fit(train, y_train)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T14:02:46.042585Z","iopub.execute_input":"2021-11-17T14:02:46.043493Z","iopub.status.idle":"2021-11-17T14:05:30.429949Z","shell.execute_reply.started":"2021-11-17T14:02:46.043444Z","shell.execute_reply":"2021-11-17T14:05:30.428632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = xgb_model.predict(test)\naccuracy = accuracy_score(y_test, y_pred)\nprint(f'Accuracy is :{accuracy}')\nprecision = precision_score(y_test, y_pred, average='binary')\nprint(f'Precision is :{precision}')\nrecall = recall_score(y_test, y_pred, average='binary')\nprint(f'Recall is :{recall}')\nconfusionmatrix = confusion_matrix(y_test, y_pred)\nprint(confusionmatrix)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T14:05:30.432153Z","iopub.execute_input":"2021-11-17T14:05:30.432742Z","iopub.status.idle":"2021-11-17T14:05:30.747191Z","shell.execute_reply.started":"2021-11-17T14:05:30.432683Z","shell.execute_reply":"2021-11-17T14:05:30.746177Z"},"trusted":true},"execution_count":null,"outputs":[]}]}