{"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":"# G-Research Crypto Forecasting\n# Test challenge\n## Goal: Use your ML expertise to predict real crypto market data.\n### This notebook mainly focuses on the Time Series Analysis","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set_style('whitegrid')\n%matplotlib inline\nfrom pandas_datareader.data import DataReader\nfrom datetime import datetime\nimport time","metadata":{"execution":{"iopub.status.busy":"2021-11-03T10:06:08.729830Z","iopub.execute_input":"2021-11-03T10:06:08.730337Z","iopub.status.idle":"2021-11-03T10:06:09.725816Z","shell.execute_reply.started":"2021-11-03T10:06:08.730236Z","shell.execute_reply":"2021-11-03T10:06:09.724983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('../input/g-research-crypto-forecasting/asset_details.csv')\nprint(df['Asset_Name'].tolist())","metadata":{"execution":{"iopub.status.busy":"2021-11-03T10:06:09.727398Z","iopub.execute_input":"2021-11-03T10:06:09.727632Z","iopub.status.idle":"2021-11-03T10:06:09.748042Z","shell.execute_reply.started":"2021-11-03T10:06:09.727604Z","shell.execute_reply":"2021-11-03T10:06:09.747201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The list of cypto we'll using are:\ncypto_list = ['Bitcoin Cash', 'Binance Coin', 'Bitcoin', 'EOS.IO', \n             'Ethereum Classic', 'Ethereum', 'Litecoin', 'Monero', \n             'TRON', 'Stellar', 'Cardano', 'IOTA', 'Maker', 'Dogecoin']\n\n","metadata":{"execution":{"iopub.status.busy":"2021-11-03T10:06:09.749629Z","iopub.execute_input":"2021-11-03T10:06:09.750198Z","iopub.status.idle":"2021-11-03T10:06:09.755251Z","shell.execute_reply.started":"2021-11-03T10:06:09.750152Z","shell.execute_reply":"2021-11-03T10:06:09.754468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2021-11-03T10:06:09.757521Z","iopub.execute_input":"2021-11-03T10:06:09.757737Z","iopub.status.idle":"2021-11-03T10:06:09.781035Z","shell.execute_reply.started":"2021-11-03T10:06:09.757711Z","shell.execute_reply":"2021-11-03T10:06:09.780095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1=pd.read_csv('../input/g-research-crypto-forecasting/train.csv')\ndf1.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-03T10:06:09.782428Z","iopub.execute_input":"2021-11-03T10:06:09.782809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"crypto_id = dict()\nfor i, j in zip(df['Asset_ID'].tolist(), df['Asset_Name'].tolist()):\n    crypto_id[i]=j","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1['Asset_Name'] = [crypto_id[i] for i in df1['Asset_ID'].tolist()]\ndf1['Date'] = [time.ctime(int(i)) for i in df1['timestamp'].tolist()]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.index = df1['Date']\ndf1.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Described Dogecoin's stock data\nDogecoin = df1[df1['Asset_Name']=='Dogecoin'].describe()\nDogecoin.describe()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Information regarding Dogecoin's stock\nDogecoin.info()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# To see a historical view of the closing price. Closing price\n# We will check the first 1000 examples for each crypto\n\nplt.figure(figsize=(18, 128))\n\n#plt.subplots_adjust(top=1.25, bottom=1.2)\n\nfor i, crypto in enumerate(cypto_list, 1):\n    plt.subplot(14, 1, i)\n    cypto_df = df1[df1['Asset_Name']==crypto]\n    cypto_df['Close'].plot()\n    plt.ylabel('Close')\n    plt.xlabel(None)\n    plt.title(f\"{cypto_list[i - 1]}\")\n    plt.tight_layout()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# To see a historical view of the Volume price. Volume price\n# We will check the first 1000 examples for each crypto\n\nplt.figure(figsize=(18, 128))\n#plt.subplots_adjust(top=1.25, bottom=1.2)\n\nfor i, crypto in enumerate(cypto_list, 1):\n    plt.subplot(14, 1, i)\n    cypto_df = df1[df1['Asset_Name']==crypto]\n    cypto_df['Volume'].plot()\n    plt.ylabel('Volume')\n    plt.xlabel(None)\n    plt.title(f\"{cypto_list[i - 1]}\")\n    plt.tight_layout()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Moving Average\nma_day = [10, 20, 50]\n\ncrypto = 'Dogecoin'\ncypto_df = df1[df1['Asset_Name']==crypto]\nfor ma in ma_day:\n    column_name = f\"MA for {ma} days\"\n    cypto_df[column_name] = cypto_df['Close'].rolling(ma).mean()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(cypto_df.columns)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cypto_df.hist(figsize=(24, 24))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(nrows=1, ncols=1)\nfig.set_figheight(8)\nfig.set_figwidth(30)\n\ncypto_df[['Close', 'MA for 10 days', 'MA for 20 days', 'MA for 50 days']].plot(ax=axes)\naxes.set_title('Dogecoin')\n\nfig.tight_layout()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# We have used pct_change to find the percent change for each day\ncrypto = 'Dogecoin'\ncypto_df = df1[df1['Asset_Name']==crypto]\n\ncypto_df['Daily Return'] = cypto_df['Close'].pct_change()\n\n# To plot the daily return percentage\nfig, axes = plt.subplots(nrows=1, ncols=1)\nfig.set_figheight(8)\nfig.set_figwidth(30)\n\ncypto_df['Daily Return'].plot(ax=axes, legend=True, linestyle='--', marker='o')\naxes.set_title('Dogecoin')\n\nfig.tight_layout()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# To get a better view of above chart\nplt.figure(figsize=(18, 128))\n\nfor i, crypto in enumerate(cypto_list, 1):\n    plt.subplot(14, 1, i)\n    crypto_df = df1[df1['Asset_Name']==crypto]\n    crypto_df['Daily Return'] = crypto_df['Close'].pct_change()\n    sns.distplot(crypto_df['Daily Return'].dropna(), bins=100, color='red')\n    plt.ylabel('Daily Return')\n    plt.title(f'{cypto_list[i - 1]}')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# I am not sure yet if cypto_list have the same time frame which is clearly not,\n# so I am taking the maximum possible examples from each crypto\n# You need to take samples from the same dates\nclosing_df = pd.DataFrame()\nfor i, crypto in enumerate(cypto_list, 1):\n    crypto_df = df1[df1['Asset_Name']==crypto]\n    closing_df[crypto] = crypto_df['Close'].tolist()[:670497]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"closing_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Making a new DataFrame for returns\ncrypto_rets = closing_df.pct_change()\ncrypto_rets.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Comparing Dogecoin to itself should show a perfectly linear relationship\nsns.jointplot('Dogecoin', 'Dogecoin', crypto_rets, kind='scatter', color='seagreen')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# To compare the daily returns of Dogecoin and Bitcoin\nsns.jointplot('Dogecoin', 'Bitcoin', crypto_rets, kind='scatter')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.pairplot(crypto_rets, kind='reg')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"return_fig = sns.PairGrid(crypto_rets.dropna())\n\nreturn_fig.map_upper(plt.scatter, color='purple')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"returns_fig = sns.PairGrid(closing_df)\n\nreturns_fig.map_upper(plt.scatter,color='purple')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Correlation plot for the daily returns\nsns.heatmap(crypto_rets.corr(), annot=True, cmap='summer')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Correlation plot for the closing price\nsns.heatmap(closing_df.corr(), annot=True, cmap='summer')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rets = crypto_rets.dropna()\n\narea = np.pi*20\n\nplt.figure(figsize=(12, 10))\nplt.scatter(rets.mean(), rets.std(), s=area)\nplt.xlabel('Expected return')\nplt.ylabel('Risk')\n\nfor label, x, y in zip(rets.columns, rets.mean(), rets.std()):\n    plt.annotate(label, xy=(x, y), xytext=(50, 50), textcoords='offset points', ha='right', va='bottom', \n                 arrowprops=dict(arrowstyle='-', color='blue', connectionstyle='arc3,rad=-0.3'))","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}