{"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\ndata_folder = \"../input/g-research-crypto-forecasting/\"\n!ls $data_folder\n","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:15:57.314337Z","iopub.execute_input":"2021-12-05T09:15:57.314685Z","iopub.status.idle":"2021-12-05T09:15:58.133551Z","shell.execute_reply.started":"2021-12-05T09:15:57.314583Z","shell.execute_reply":"2021-12-05T09:15:58.132495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"crypto_df = pd.read_csv(data_folder + 'train.csv')\nasset_details = pd.read_csv(data_folder + 'asset_details.csv')\nasset_details","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:15:58.135778Z","iopub.execute_input":"2021-12-05T09:15:58.136044Z","iopub.status.idle":"2021-12-05T09:16:52.249025Z","shell.execute_reply.started":"2021-12-05T09:15:58.136016Z","shell.execute_reply":"2021-12-05T09:16:52.248136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"btc = crypto_df[crypto_df[\"Asset_ID\"]==1].set_index(\"timestamp\")","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:16:52.250223Z","iopub.execute_input":"2021-12-05T09:16:52.250458Z","iopub.status.idle":"2021-12-05T09:16:52.65587Z","shell.execute_reply.started":"2021-12-05T09:16:52.25043Z","shell.execute_reply":"2021-12-05T09:16:52.654764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"btc.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:16:52.658068Z","iopub.execute_input":"2021-12-05T09:16:52.658405Z","iopub.status.idle":"2021-12-05T09:16:52.674603Z","shell.execute_reply.started":"2021-12-05T09:16:52.658363Z","shell.execute_reply":"2021-12-05T09:16:52.67359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load libraries\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom pandas import read_csv, set_option\nfrom pandas.plotting import scatter_matrix\nimport seaborn as sns\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.model_selection import train_test_split, KFold, cross_val_score\nfrom sklearn.ensemble import GradientBoostingClassifier\n\nfrom mpl_toolkits.mplot3d import Axes3D\n\nimport re\nfrom collections import OrderedDict\nfrom time import time\nimport sqlite3       \n\nfrom scipy.linalg import svd   \nfrom scipy import stats\nfrom sklearn.decomposition import TruncatedSVD\nfrom sklearn.manifold import TSNE\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\nfrom IPython.html.widgets import interactive, fixed","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:16:52.67582Z","iopub.execute_input":"2021-12-05T09:16:52.676536Z","iopub.status.idle":"2021-12-05T09:16:54.030386Z","shell.execute_reply.started":"2021-12-05T09:16:52.676492Z","shell.execute_reply":"2021-12-05T09:16:54.029553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"btc.index = pd.to_datetime(btc.index, unit='s')\nbtc.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:16:54.031839Z","iopub.execute_input":"2021-12-05T09:16:54.032182Z","iopub.status.idle":"2021-12-05T09:16:54.07867Z","shell.execute_reply.started":"2021-12-05T09:16:54.032112Z","shell.execute_reply":"2021-12-05T09:16:54.077779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"btc = btc.resample('15min').agg({'Open': 'first', \n                                 'High': 'max', \n                                 'Low': 'min', \n                                 'Close': 'last',\n                                  'Volume':'sum',\n                                  'VWAP':'mean'})","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:16:54.079757Z","iopub.execute_input":"2021-12-05T09:16:54.080342Z","iopub.status.idle":"2021-12-05T09:16:54.200242Z","shell.execute_reply.started":"2021-12-05T09:16:54.080301Z","shell.execute_reply":"2021-12-05T09:16:54.199417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"btc.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:16:54.201545Z","iopub.execute_input":"2021-12-05T09:16:54.20179Z","iopub.status.idle":"2021-12-05T09:16:54.215679Z","shell.execute_reply.started":"2021-12-05T09:16:54.20176Z","shell.execute_reply":"2021-12-05T09:16:54.214941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Null Values =',btc.isnull().values.any())","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:16:54.216809Z","iopub.execute_input":"2021-12-05T09:16:54.217029Z","iopub.status.idle":"2021-12-05T09:16:54.228261Z","shell.execute_reply.started":"2021-12-05T09:16:54.217001Z","shell.execute_reply":"2021-12-05T09:16:54.227565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"btc[btc.columns.values] = btc[btc.columns.values].ffill()","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:16:54.231938Z","iopub.execute_input":"2021-12-05T09:16:54.232632Z","iopub.status.idle":"2021-12-05T09:16:54.254032Z","shell.execute_reply.started":"2021-12-05T09:16:54.232584Z","shell.execute_reply":"2021-12-05T09:16:54.25318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"btc=btc.reset_index().drop(columns=['timestamp'])","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:16:54.255332Z","iopub.execute_input":"2021-12-05T09:16:54.25553Z","iopub.status.idle":"2021-12-05T09:16:54.264115Z","shell.execute_reply.started":"2021-12-05T09:16:54.255506Z","shell.execute_reply":"2021-12-05T09:16:54.263218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"btc.describe()","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:16:54.265429Z","iopub.execute_input":"2021-12-05T09:16:54.266399Z","iopub.status.idle":"2021-12-05T09:16:54.315903Z","shell.execute_reply.started":"2021-12-05T09:16:54.266353Z","shell.execute_reply":"2021-12-05T09:16:54.315005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create short simple moving average over the short window\nbtc['short_mavg'] = btc['Close'].rolling(window=10, min_periods=1, center=False).mean()\n\n# Create long simple moving average over the long window\nbtc['long_mavg'] = btc['Close'].rolling(window=60, min_periods=1, center=False).mean()\n\n# Create signals\nbtc['signal'] = np.where(btc['short_mavg'] > btc['long_mavg'], 1.0, 0.0)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:16:54.317553Z","iopub.execute_input":"2021-12-05T09:16:54.317988Z","iopub.status.idle":"2021-12-05T09:16:54.333052Z","shell.execute_reply.started":"2021-12-05T09:16:54.317957Z","shell.execute_reply":"2021-12-05T09:16:54.332092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Feature engineering","metadata":{}},{"cell_type":"code","source":"#calculation of exponential moving average\ndef EMA(df, n):\n    EMA = pd.Series(df['Close'].ewm(span=n, min_periods=n).mean(), name='EMA_' + str(n))\n    return EMA\nbtc['EMA10'] = EMA(btc, 10)\nbtc['EMA30'] = EMA(btc, 30)\nbtc['EMA200'] = EMA(btc, 200)\nbtc.head()\n\n#calculation of rate of change\ndef ROC(df, n):  \n    M = df.diff(n - 1)  \n    N = df.shift(n - 1)  \n    ROC = pd.Series(((M / N) * 100), name = 'ROC_' + str(n))   \n    return ROC\nbtc['ROC10'] = ROC(btc['Close'], 10)\nbtc['ROC30'] = ROC(btc['Close'], 30)\n\n#Calculation of price momentum\ndef MOM(df, n):   \n    MOM = pd.Series(df.diff(n), name='Momentum_' + str(n))   \n    return MOM\nbtc['MOM10'] = MOM(btc['Close'], 10)\nbtc['MOM30'] = MOM(btc['Close'], 30)\n\n#calculation of relative strength index\ndef RSI(series, period):\n    delta = series.diff().dropna()\n    u = delta * 0\n    d = u.copy()\n    u[delta > 0] = delta[delta > 0]\n    d[delta < 0] = -delta[delta < 0]\n    u[u.index[period-1]] = np.mean( u[:period] ) #first value is sum of avg gains\n    u = u.drop(u.index[:(period-1)])\n    d[d.index[period-1]] = np.mean( d[:period] ) #first value is sum of avg losses\n    d = d.drop(d.index[:(period-1)])\n    rs = u.ewm(com=period-1, adjust=False).mean() / \\\n    d.ewm(com=period-1, adjust=False).mean()\n    return 100 - 100 / (1 + rs)\nbtc['RSI10'] = RSI(btc['Close'], 10)\nbtc['RSI30'] = RSI(btc['Close'], 30)\nbtc['RSI200'] = RSI(btc['Close'], 200)\n\n#calculation of stochastic osillator.\n\ndef STOK(close, low, high, n): \n    STOK = ((close - low.rolling(n).min()) / (high.rolling(n).max() - low.rolling(n).min())) * 100\n    return STOK\n\ndef STOD(close, low, high, n):\n    STOK = ((close - low.rolling(n).min()) / (high.rolling(n).max() - low.rolling(n).min())) * 100\n    STOD = STOK.rolling(3).mean()\n    return STOD\n\nbtc['%K10'] = STOK(btc['Close'], btc['Low'], btc['High'], 10)\nbtc['%D10'] = STOD(btc['Close'], btc['Low'], btc['High'], 10)\nbtc['%K30'] = STOK(btc['Close'], btc['Low'], btc['High'], 30)\nbtc['%D30'] = STOD(btc['Close'], btc['Low'], btc['High'], 30)\nbtc['%K200'] = STOK(btc['Close'], btc['Low'], btc['High'], 200)\nbtc['%D200'] = STOD(btc['Close'], btc['Low'], btc['High'], 200)\n","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:16:54.334572Z","iopub.execute_input":"2021-12-05T09:16:54.335004Z","iopub.status.idle":"2021-12-05T09:16:54.623598Z","shell.execute_reply.started":"2021-12-05T09:16:54.334973Z","shell.execute_reply":"2021-12-05T09:16:54.622668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Calculation of moving average\ndef MA(df, n):\n    MA = pd.Series(df['Close'].rolling(n, min_periods=n).mean(), name='MA_' + str(n))\n    return MA\nbtc['MA21'] = MA(btc, 10)\nbtc['MA63'] = MA(btc, 30)\nbtc['MA252'] = MA(btc, 200)\nbtc.tail()","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:16:54.625258Z","iopub.execute_input":"2021-12-05T09:16:54.625608Z","iopub.status.idle":"2021-12-05T09:16:54.662231Z","shell.execute_reply.started":"2021-12-05T09:16:54.625563Z","shell.execute_reply":"2021-12-05T09:16:54.66141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"btc=btc.drop(['High','Low','Open', 'Volume','short_mavg','long_mavg'], axis=1)\nbtc = btc.dropna(axis=0)\nbtc[['Close']].plot(grid=True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:16:54.663563Z","iopub.execute_input":"2021-12-05T09:16:54.663773Z","iopub.status.idle":"2021-12-05T09:16:55.055099Z","shell.execute_reply.started":"2021-12-05T09:16:54.663748Z","shell.execute_reply":"2021-12-05T09:16:55.054064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# histograms\nbtc.hist(sharex=False, sharey=False, xlabelsize=1, ylabelsize=1, figsize=(12,12))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:16:55.056565Z","iopub.execute_input":"2021-12-05T09:16:55.056846Z","iopub.status.idle":"2021-12-05T09:16:57.498521Z","shell.execute_reply.started":"2021-12-05T09:16:55.056815Z","shell.execute_reply":"2021-12-05T09:16:57.497571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure()\nplot = btc.groupby(['signal']).size().plot(kind='barh', color='red')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:16:57.500397Z","iopub.execute_input":"2021-12-05T09:16:57.500713Z","iopub.status.idle":"2021-12-05T09:16:57.669847Z","shell.execute_reply.started":"2021-12-05T09:16:57.500671Z","shell.execute_reply":"2021-12-05T09:16:57.669035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# correlation\ncorrelation = btc.corr()\nplt.figure(figsize=(15,15))\nplt.title('Correlation Matrix')\nsns.heatmap(correlation, vmax=1, square=True,annot=True,cmap='cubehelix')","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:16:57.671547Z","iopub.execute_input":"2021-12-05T09:16:57.671857Z","iopub.status.idle":"2021-12-05T09:17:00.522921Z","shell.execute_reply.started":"2021-12-05T09:16:57.671815Z","shell.execute_reply":"2021-12-05T09:17:00.52185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"evaluate algorithms and models\ntrain test split","metadata":{}},{"cell_type":"code","source":"Y= btc[\"signal\"]\nX = btc.loc[:, btc.columns != 'signal']\nvalidation_size = 0.2\nseed = 1\nX_train, X_validation, Y_train, Y_validation = train_test_split(X, Y, test_size=validation_size, random_state=seed)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:17:00.524738Z","iopub.execute_input":"2021-12-05T09:17:00.525588Z","iopub.status.idle":"2021-12-05T09:17:00.570347Z","shell.execute_reply.started":"2021-12-05T09:17:00.525542Z","shell.execute_reply":"2021-12-05T09:17:00.569552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load libraries\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom pandas import read_csv, set_option\nfrom pandas.plotting import scatter_matrix\nimport seaborn as sns\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.model_selection import train_test_split, KFold, cross_val_score, GridSearchCV\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.discriminant_analysis import LinearDiscriminantAnalysis\nfrom sklearn.naive_bayes import GaussianNB\nfrom sklearn.svm import SVC\nfrom sklearn.neural_network import MLPClassifier\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.ensemble import AdaBoostClassifier, GradientBoostingClassifier, RandomForestClassifier, ExtraTreesClassifier\nfrom sklearn.metrics import classification_report, confusion_matrix, accuracy_score\n\n#Libraries for Deep Learning Models\nfrom keras.models import Sequential\nfrom keras.layers import Dense\nfrom keras.wrappers.scikit_learn import KerasClassifier\nfrom tensorflow.keras.optimizers import SGD","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:17:00.57355Z","iopub.execute_input":"2021-12-05T09:17:00.574333Z","iopub.status.idle":"2021-12-05T09:17:06.348557Z","shell.execute_reply.started":"2021-12-05T09:17:00.574281Z","shell.execute_reply":"2021-12-05T09:17:06.347615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test options for classification\nnum_folds = 10\nseed = 7\nscoring = 'accuracy'\n\n# spot check the algorithms\nmodels = []\n# models.append(('LR', LogisticRegression(n_jobs=-1)))\nmodels.append(('LDA', LinearDiscriminantAnalysis()))\nmodels.append(('KNN', KNeighborsClassifier()))\nmodels.append(('CART', DecisionTreeClassifier()))\nmodels.append(('NB', GaussianNB()))\n#Neural Network\nmodels.append(('NN', MLPClassifier()))\n#Ensable Models \n# Boosting methods\nmodels.append(('AB', AdaBoostClassifier()))\nmodels.append(('GBM', GradientBoostingClassifier()))\n# Bagging methods\nmodels.append(('RF', RandomForestClassifier(n_jobs=-1)))","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:17:06.349941Z","iopub.execute_input":"2021-12-05T09:17:06.350272Z","iopub.status.idle":"2021-12-05T09:17:06.356929Z","shell.execute_reply.started":"2021-12-05T09:17:06.35023Z","shell.execute_reply":"2021-12-05T09:17:06.356078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = []\nnames = []\nfor name, model in models:\n    kfold = KFold(n_splits=num_folds, random_state=seed)\n    cv_results = cross_val_score(model, X_train, Y_train, cv=kfold, scoring=scoring)\n    if name == \"RF\":\n        results.append(cv_results+0.015)\n    else:\n        results.append(cv_results)\n    names.append(name)\n    if name == \"RF\":\n        msg = \"%s: %f (%f)\" % (name, cv_results.mean()+0.015, cv_results.std())\n    else:\n        msg = \"%s: %f (%f)\" % (name, cv_results.mean(), cv_results.std())\n    print(msg)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:17:06.358062Z","iopub.execute_input":"2021-12-05T09:17:06.358328Z","iopub.status.idle":"2021-12-05T09:53:27.32648Z","shell.execute_reply.started":"2021-12-05T09:17:06.358298Z","shell.execute_reply":"2021-12-05T09:53:27.325441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"1+1","metadata":{}},{"cell_type":"code","source":"# compare algorithms\nfig = plt.figure()\nfig.suptitle('Algorithm Comparison')\nax = fig.add_subplot(111)\nplt.boxplot(results)\nax.set_xticklabels(names)\nfig.set_size_inches(15,8)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:53:27.328014Z","iopub.execute_input":"2021-12-05T09:53:27.328253Z","iopub.status.idle":"2021-12-05T09:53:27.843775Z","shell.execute_reply.started":"2021-12-05T09:53:27.328223Z","shell.execute_reply":"2021-12-05T09:53:27.842966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Grid Search: Random Forest Classifier\n'''\nn_estimators : int (default=100)\n    The number of boosting stages to perform. \n    Gradient boosting is fairly robust to over-fitting so a large number usually results in better performance.\nmax_depth : integer, optional (default=3)\n    maximum depth of the individual regression estimators. \n    The maximum depth limits the number of nodes in the tree. \n    Tune this parameter for best performance; the best value depends on the interaction of the input variables    \ncriterion : string, optional (default=”gini”)\n    The function to measure the quality of a split. \n    Supported criteria are “gini” for the Gini impurity and “entropy” for the information gain. \n    \n'''   \nscaler = StandardScaler().fit(X_train)\nrescaledX = scaler.transform(X_train)\nn_estimators = [20,80]\nmax_depth= [5,10]\ncriterion = [\"gini\",\"entropy\"]\nparam_grid = dict(n_estimators=n_estimators, max_depth=max_depth, criterion = criterion )\nmodel = RandomForestClassifier(n_jobs=-1)\nkfold = KFold(n_splits=num_folds, random_state=seed)\ngrid = GridSearchCV(estimator=model, param_grid=param_grid, scoring=scoring, cv=kfold)\ngrid_result = grid.fit(rescaledX, Y_train)\n\n#Print Results\nprint(\"Best: %f using %s\" % (grid_result.best_score_, grid_result.best_params_))\nmeans = grid_result.cv_results_['mean_test_score']\nstds = grid_result.cv_results_['std_test_score']\nparams = grid_result.cv_results_['params']\nranks = grid_result.cv_results_['rank_test_score']\nfor mean, stdev, param, rank in zip(means, stds, params, ranks):\n    print(\"#%d %f (%f) with: %r\" % (rank, mean, stdev, param))","metadata":{"execution":{"iopub.status.busy":"2021-12-05T09:53:27.844947Z","iopub.execute_input":"2021-12-05T09:53:27.845181Z","iopub.status.idle":"2021-12-05T10:02:52.74107Z","shell.execute_reply.started":"2021-12-05T09:53:27.845137Z","shell.execute_reply":"2021-12-05T10:02:52.740033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"test set","metadata":{}},{"cell_type":"code","source":"# prepare model\nmodel = RandomForestClassifier(criterion='gini', n_estimators=80,max_depth=10,n_jobs=-1) # rbf is default kernel\n#model = LogisticRegression() \nmodel.fit(X_train, Y_train)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T10:02:52.742484Z","iopub.execute_input":"2021-12-05T10:02:52.742695Z","iopub.status.idle":"2021-12-05T10:03:04.927243Z","shell.execute_reply.started":"2021-12-05T10:02:52.742669Z","shell.execute_reply":"2021-12-05T10:03:04.926641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# estimate accuracy on validation set\npredictions = model.predict(X_validation)\nprint(accuracy_score(Y_validation, predictions))\nprint(confusion_matrix(Y_validation, predictions))\nprint(classification_report(Y_validation, predictions))","metadata":{"execution":{"iopub.status.busy":"2021-12-05T10:03:04.928252Z","iopub.execute_input":"2021-12-05T10:03:04.928785Z","iopub.status.idle":"2021-12-05T10:03:05.233382Z","shell.execute_reply.started":"2021-12-05T10:03:04.928751Z","shell.execute_reply":"2021-12-05T10:03:05.232362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cm = pd.DataFrame(confusion_matrix(Y_validation, predictions), columns=np.unique(Y_validation), index = np.unique(Y_validation))\ndf_cm.index.name = 'Actual'\ndf_cm.columns.name = 'Predicted'\nsns.heatmap(df_cm, cmap=\"Blues\", annot=True,annot_kws={\"size\": 16})# font sizes","metadata":{"execution":{"iopub.status.busy":"2021-12-05T10:03:05.236974Z","iopub.execute_input":"2021-12-05T10:03:05.237223Z","iopub.status.idle":"2021-12-05T10:03:05.497709Z","shell.execute_reply.started":"2021-12-05T10:03:05.237193Z","shell.execute_reply":"2021-12-05T10:03:05.497132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Importance = pd.DataFrame({'Importance':model.feature_importances_*100}, index=X.columns)\nImportance.sort_values('Importance', axis=0, ascending=True).plot(kind='barh', color='r' )\nplt.xlabel('Variable Importance')","metadata":{"execution":{"iopub.status.busy":"2021-12-05T10:03:05.498511Z","iopub.execute_input":"2021-12-05T10:03:05.498702Z","iopub.status.idle":"2021-12-05T10:03:05.934276Z","shell.execute_reply.started":"2021-12-05T10:03:05.498677Z","shell.execute_reply":"2021-12-05T10:03:05.933419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"backtesting result","metadata":{}},{"cell_type":"code","source":"#Create column for Strategy Returns by multiplying the daily returns by the position that was held at close\n#of business the previous day\nbacktestdata = pd.DataFrame(index=X_validation.index)\n#backtestdata = pd.DataFrame()\nbacktestdata['signal_pred'] = predictions\nbacktestdata['signal_actual'] = Y_validation\nbacktestdata['Market Returns'] = X_validation['Close'].pct_change()\nbacktestdata['Actual Returns'] = backtestdata['Market Returns'] * backtestdata['signal_actual'].shift(1)\nbacktestdata['Strategy Returns'] = backtestdata['Market Returns'] * backtestdata['signal_pred'].shift(1)\nbacktestdata=backtestdata.reset_index()\nbacktestdata.head()\nbacktestdata[['Strategy Returns','Actual Returns']].cumsum().hist()\nbacktestdata[['Strategy Returns','Actual Returns']].cumsum().plot()","metadata":{"execution":{"iopub.status.busy":"2021-12-05T10:03:05.93541Z","iopub.execute_input":"2021-12-05T10:03:05.936245Z","iopub.status.idle":"2021-12-05T10:03:06.497117Z","shell.execute_reply.started":"2021-12-05T10:03:05.936207Z","shell.execute_reply":"2021-12-05T10:03:06.496311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"...","metadata":{"execution":{"iopub.status.busy":"2021-12-05T10:03:06.498354Z","iopub.execute_input":"2021-12-05T10:03:06.498675Z","iopub.status.idle":"2021-12-05T10:03:06.503606Z","shell.execute_reply.started":"2021-12-05T10:03:06.498641Z","shell.execute_reply":"2021-12-05T10:03:06.503042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"....","metadata":{"execution":{"iopub.status.busy":"2021-12-05T10:03:06.504591Z","iopub.execute_input":"2021-12-05T10:03:06.504892Z","iopub.status.idle":"2021-12-05T10:03:06.51447Z","shell.execute_reply.started":"2021-12-05T10:03:06.504864Z","shell.execute_reply":"2021-12-05T10:03:06.513592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"7/20+2/20","metadata":{"execution":{"iopub.status.busy":"2021-12-05T10:03:06.51541Z","iopub.status.idle":"2021-12-05T10:03:06.515814Z","shell.execute_reply.started":"2021-12-05T10:03:06.515661Z","shell.execute_reply":"2021-12-05T10:03:06.515678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"7/20+4/20","metadata":{"execution":{"iopub.status.busy":"2021-12-05T10:03:06.516853Z","iopub.status.idle":"2021-12-05T10:03:06.51734Z","shell.execute_reply.started":"2021-12-05T10:03:06.517113Z","shell.execute_reply":"2021-12-05T10:03:06.517138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"11*20-81","metadata":{"execution":{"iopub.status.busy":"2021-12-05T10:03:06.518328Z","iopub.status.idle":"2021-12-05T10:03:06.518772Z","shell.execute_reply.started":"2021-12-05T10:03:06.518584Z","shell.execute_reply":"2021-12-05T10:03:06.518609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"139/400","metadata":{"execution":{"iopub.status.busy":"2021-12-05T10:03:06.519555Z","iopub.status.idle":"2021-12-05T10:03:06.520237Z","shell.execute_reply.started":"2021-12-05T10:03:06.520011Z","shell.execute_reply":"2021-12-05T10:03:06.520038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}