{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":85340,"databundleVersionId":9630059,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2024-10-31T21:34:40.734407Z","iopub.execute_input":"2024-10-31T21:34:40.734840Z","iopub.status.idle":"2024-10-31T21:34:43.585458Z","shell.execute_reply.started":"2024-10-31T21:34:40.734801Z","shell.execute_reply":"2024-10-31T21:34:43.584139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/directional-forecasting-in-cryptocurrencies/train.csv')\ntest_df = pd.read_csv('/kaggle/input/directional-forecasting-in-cryptocurrencies/test.csv')\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2024-10-31T21:34:47.895210Z","iopub.execute_input":"2024-10-31T21:34:47.895776Z","iopub.status.idle":"2024-10-31T21:34:55.245896Z","shell.execute_reply.started":"2024-10-31T21:34:47.895733Z","shell.execute_reply":"2024-10-31T21:34:55.244591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.corr()","metadata":{"execution":{"iopub.status.busy":"2024-10-31T21:35:30.140150Z","iopub.execute_input":"2024-10-31T21:35:30.140589Z","iopub.status.idle":"2024-10-31T21:35:31.020701Z","shell.execute_reply.started":"2024-10-31T21:35:30.140547Z","shell.execute_reply":"2024-10-31T21:35:31.019481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nplt.figure(figsize=(20,10))\nsns.heatmap(train_df.corr(), annot=True, linewidths=1)\nplt.title('Heatmap of Features')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-31T21:35:21.434378Z","iopub.execute_input":"2024-10-31T21:35:21.434766Z","iopub.status.idle":"2024-10-31T21:35:23.030183Z","shell.execute_reply.started":"2024-10-31T21:35:21.434731Z","shell.execute_reply":"2024-10-31T21:35:23.028923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n\ncorrelation_matrix = train_df.corr()\n\nsignal_corr = correlation_matrix[['target']].sort_values(by='target', ascending=False)\n\nplt.figure(figsize=(7, 0.5))\nsns.heatmap(signal_corr.T, annot=True, cmap='coolwarm', cbar=False)\nplt.title(\"Correlation of Features with 'target'\")\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-10-31T19:27:14.773355Z","iopub.execute_input":"2024-10-31T19:27:14.773845Z","iopub.status.idle":"2024-10-31T19:27:15.801322Z","shell.execute_reply.started":"2024-10-31T19:27:14.773794Z","shell.execute_reply":"2024-10-31T19:27:15.800159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datetime import datetime, timezone\n\ntrain_df['timestamp'] = pd.to_datetime(train_df['timestamp'], unit='s')\ntrain_df['minute'] = train_df['timestamp'].dt.minute\ntrain_df.drop('timestamp', axis=1, inplace=True)\n\ntest_df['timestamp'] = pd.to_datetime(test_df['timestamp'], unit='s')\ntest_df['minute'] = test_df['timestamp'].dt.minute\ntest_df.drop('timestamp', axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-10-31T19:27:15.802507Z","iopub.execute_input":"2024-10-31T19:27:15.802813Z","iopub.status.idle":"2024-10-31T19:27:16.223965Z","shell.execute_reply.started":"2024-10-31T19:27:15.802782Z","shell.execute_reply":"2024-10-31T19:27:16.223032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#USING Z-SCORE METHOD TO GET RID OF OUTLIERS\n\nz_threshold = 3\n\nz_scores = np.abs((train_df - train_df.mean()) / train_df.std())\n\noutliers = (z_scores > z_threshold)\n\nprint(\"Outliers:\\n\", train_df[outliers.any(axis=1)])\n\ntrain_df = train_df[~outliers.any(axis=1)]","metadata":{"execution":{"iopub.status.busy":"2024-10-31T19:27:16.225231Z","iopub.execute_input":"2024-10-31T19:27:16.225557Z","iopub.status.idle":"2024-10-31T19:27:16.833711Z","shell.execute_reply.started":"2024-10-31T19:27:16.225524Z","shell.execute_reply":"2024-10-31T19:27:16.832481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['close_shift_1'] = train_df.shift(-1)['close']\ntrain_df['close_shift_2'] = train_df.shift(-2)['close']\ntrain_df['close_shift_3'] = train_df.shift(-3)['close']\ntrain_df['close_shift_4'] = train_df.shift(-4)['close']\ntrain_df['close_shift_5'] = train_df.shift(-5)['close']\n\n\ntest_df['close_shift_1'] = test_df.shift(-1)['close']\ntest_df['close_shift_2'] = test_df.shift(-2)['close']\ntest_df['close_shift_3'] = test_df.shift(-3)['close']\ntest_df['close_shift_4'] = test_df.shift(-4)['close']\ntest_df['close_shift_5'] = test_df.shift(-5)['close']\n","metadata":{"execution":{"iopub.status.busy":"2024-10-31T19:27:16.835282Z","iopub.execute_input":"2024-10-31T19:27:16.836197Z","iopub.status.idle":"2024-10-31T19:27:17.482164Z","shell.execute_reply.started":"2024-10-31T19:27:16.836142Z","shell.execute_reply":"2024-10-31T19:27:17.481152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Feature_Engineering(df):\n    train_df = pd.DataFrame(df)\n\n    \n#   train_df['true_range'] = train_df[['high', 'low', 'close']].max(axis=1) - train_df[['high', 'low', 'close']].min(axis=1)    -->BAD INDICATORS\n#   train_df['atr'] = (train_df[['high', 'low', 'close']].max(axis=1) - train_df[['high', 'low', 'close']].min(axis=1)).rolling(window=14).mean()     -->BAD INDICATOR\n    \n    train_df['sma_5'] = train_df['close'].rolling(window=5).mean()\n    train_df['sma_10'] = train_df['close'].rolling(window=10).mean()\n    train_df['sma_shift_5'] = train_df['close_shift_1'].rolling(window=5).mean()\n    train_df['sma_shiftmedian_5'] = train_df['close_shift_1'].rolling(window=5).median()\n    train_df['sma_shiftmedian_10'] = train_df['close_shift_1'].rolling(window=10).median()\n\n    \n    train_df['sma_20'] = train_df['close'].rolling(window=20).mean()\n\n    train_df['bb_up'] = train_df['sma_20'] + ((train_df['close'].rolling(window=20).std()) * 2)\n    train_df['bb_low'] = train_df['sma_20'] - ((train_df['close'].rolling(window=20).std()) * 2)\n    \n    train_df['ema_5'] = train_df['close'].ewm(span=5, adjust=False).mean()\n    train_df['ema_10'] = train_df['close'].ewm(span=10, adjust=False).mean()\n    train_df['ema_shift_5'] = train_df['close_shift_1'].ewm(span=5, adjust=False).mean()\n    train_df['ema_crossover'] = train_df['ema_5'] - train_df['ema_10']\n    train_df['close_diff'] = train_df['close'] - train_df['close_shift_1']\n    train_df['close_shift_1_volatility'] = train_df['close_shift_1'].rolling(window=10).std()\n    \n    train_df['close_shift_1_ma20'] = train_df['close_shift_1'].rolling(window=20).mean()\n    train_df['close_shift_1_upper_bb'] = train_df['close_shift_1_ma20'] + 2 * train_df['close_shift_1_volatility']\n    train_df['close_shift_1_lower_bb'] = train_df['close_shift_1_ma20'] - 2 * train_df['close_shift_1_volatility']\n    train_df['close_momentum_5'] = train_df['close'] - train_df['close'].shift(5)\n    train_df['close_to_shift_ratio'] = train_df['close'] / train_df['close_shift_1']\n\n    \n    return train_df\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-10-31T19:27:17.483563Z","iopub.execute_input":"2024-10-31T19:27:17.483985Z","iopub.status.idle":"2024-10-31T19:27:17.495602Z","shell.execute_reply.started":"2024-10-31T19:27:17.483940Z","shell.execute_reply":"2024-10-31T19:27:17.494506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = Feature_Engineering(train_df)\ntest_df = Feature_Engineering(test_df)\n","metadata":{"execution":{"iopub.status.busy":"2024-10-31T19:27:17.497055Z","iopub.execute_input":"2024-10-31T19:27:17.497433Z","iopub.status.idle":"2024-10-31T19:27:20.557563Z","shell.execute_reply.started":"2024-10-31T19:27:17.497397Z","shell.execute_reply":"2024-10-31T19:27:20.556451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df.dropna()","metadata":{"execution":{"iopub.status.busy":"2024-10-31T19:27:20.561259Z","iopub.execute_input":"2024-10-31T19:27:20.561722Z","iopub.status.idle":"2024-10-31T19:27:21.176258Z","shell.execute_reply.started":"2024-10-31T19:27:20.561685Z","shell.execute_reply":"2024-10-31T19:27:21.175245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.columns","metadata":{"execution":{"iopub.status.busy":"2024-10-31T19:27:21.177660Z","iopub.execute_input":"2024-10-31T19:27:21.178100Z","iopub.status.idle":"2024-10-31T19:27:21.185860Z","shell.execute_reply.started":"2024-10-31T19:27:21.178052Z","shell.execute_reply":"2024-10-31T19:27:21.184700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX, y = train_df.drop(columns=['target']), train_df['target']\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)\n\nfrom sklearn.preprocessing import MinMaxScaler\n\nscaler = MinMaxScaler()\nX_train_scaled = scaler.fit_transform(X_train)\nX_test_scaled = scaler.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-10-31T19:27:21.187210Z","iopub.execute_input":"2024-10-31T19:27:21.187579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\n\nmodel = RandomForestClassifier(random_state=42, n_estimators=100, n_jobs=-1, verbose=2)\nmodel.fit(X_train_scaled, y_train)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predictions = model.predict(X_test_scaled)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, classification_report\n\nprint(accuracy_score(y_test, y_predictions))\n\nprint(classification_report(y_test, y_predictions))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"importance_df = pd.DataFrame({\n    'feature': X_train.columns,\n    'importance': model.feature_importances_\n}).sort_values('importance', ascending=False)\n\nimportance_df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tree_depths = [estimator.tree_.max_depth for estimator in model.estimators_]\ntree_depths","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_cols=['open', 'high', 'low', 'close', 'volume', 'quote_asset_volume',\n       'number_of_trades', 'taker_buy_base_volume', 'taker_buy_quote_volume',\n       'minute', 'close_shift_1', 'close_shift_2', 'close_shift_3',\n       'close_shift_4', 'close_shift_5', 'sma_5', 'sma_10', 'sma_shift_5',\n       'sma_shiftmedian_5', 'sma_shiftmedian_10', 'sma_20', 'bb_up', 'bb_low',\n       'ema_5', 'ema_10', 'ema_shift_5', 'ema_crossover', 'close_diff',\n       'close_shift_1_volatility', 'close_shift_1_ma20',\n       'close_shift_1_upper_bb', 'close_shift_1_lower_bb', 'close_momentum_5',\n       'close_to_shift_ratio']","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.impute import SimpleImputer\n\nimputer = SimpleImputer(strategy='mean')  \ntest_df[input_cols] = imputer.fit_transform(test_df[input_cols])\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df[input_cols] = scaler.transform(test_df[input_cols])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = test_df.drop(['row_id'], axis=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.columns","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_target=model.predict(test_df)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'row_id': range(0, 909617),\n    'target': test_target\n})\n\nsubmission.to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}