{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-15T16:20:38.686479Z","iopub.execute_input":"2022-11-15T16:20:38.688681Z","iopub.status.idle":"2022-11-15T16:20:38.708422Z","shell.execute_reply.started":"2022-11-15T16:20:38.688504Z","shell.execute_reply":"2022-11-15T16:20:38.707000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#library\nimport os\nfrom datetime import datetime\nimport time\nfrom sklearn.preprocessing import StandardScaler\nimport plotly.express as px\nimport plotly.graph_objs as go\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport math\n\nimport statsmodels.api as sm\nfrom statsmodels.tsa.ar_model import AutoReg\nfrom statsmodels.tsa.arima_model import ARMA\nfrom statsmodels.tsa.stattools import adfuller\nfrom statsmodels.graphics.tsaplots import plot_acf\nfrom statsmodels.graphics.tsaplots import plot_pacf\nfrom statsmodels.tsa.arima_model import ARIMA\nfrom statsmodels.tsa.statespace.sarimax import SARIMAX\nfrom statsmodels.tsa.seasonal import seasonal_decompose\nfrom scipy import stats\nfrom itertools import product\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\ninfo = pd.read_csv(\"/kaggle/input/g-research-crypto-forecasting/asset_details.csv\")\nctrain = pd.read_csv(\"/kaggle/input/g-research-crypto-forecasting/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-11-15T16:20:39.370458Z","iopub.execute_input":"2022-11-15T16:20:39.370998Z","iopub.status.idle":"2022-11-15T16:21:29.748138Z","shell.execute_reply.started":"2022-11-15T16:20:39.370948Z","shell.execute_reply":"2022-11-15T16:21:29.746837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Color Scheme**","metadata":{}},{"cell_type":"code","source":"sns.color_palette(\"YlOrBr\", 10)","metadata":{"execution":{"iopub.status.busy":"2022-11-15T16:08:32.735769Z","iopub.execute_input":"2022-11-15T16:08:32.736159Z","iopub.status.idle":"2022-11-15T16:08:32.748774Z","shell.execute_reply.started":"2022-11-15T16:08:32.736123Z","shell.execute_reply":"2022-11-15T16:08:32.747326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 id=\"h1\">\n        <div style=\"color:black;\n           display:fill;\n           border-radius:5px;\n           background-color:#ffeea9;\n           font-size:100%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n1. 📝 Introduction\n<a class=\"anchor-link\" \n href=\"https://www.kaggle.com/code/fangya/cryptocurrency-data-visualization-arima\">¶</a>\n</h1>\n\n\nCrypto, a form of digital token or “coins” that exist on a distributed and decentralized ledger called a blockchain, is a popular investment nowadays.\n\nA crypto is a collection of binary data which is designed to work as a medium of exchange. Individual coin ownership records are stored in a ledger, which is a computerized database using strong cryptography to secure transaction records, to control the creation of additional coins, and to verify the transfer of coin ownership.\n\n*KEY idea: Decentralization, Blockchain\n\nThis project consisite of three parts: \n1. Data visualtion for 14 popular cryptocurrency\n2. Price History for selected individual cryptocurrency\n3. Basic Arima Model for price prediction","metadata":{}},{"cell_type":"code","source":"ctrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-15T16:08:32.750746Z","iopub.execute_input":"2022-11-15T16:08:32.751477Z","iopub.status.idle":"2022-11-15T16:08:32.774807Z","shell.execute_reply.started":"2022-11-15T16:08:32.751441Z","shell.execute_reply":"2022-11-15T16:08:32.773213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Impute Missing Time Value\ndef c_time_sub(asset_id, data=ctrain):\n    df=data[ctrain[\"Asset_ID\"]==asset_id].set_index(\"timestamp\")\n    df=df.reindex(range(df.index[0],df.index[-1]+60,60), method=\"pad\")\n    return df\n\n# Subgroup Bitcoin\nbtc=c_time_sub(asset_id=1)\n(btc.index[1:] -btc.index[:-1]).value_counts().head()\n\n# Subgroup Ethereum\neth=c_time_sub(asset_id=6)\n(btc.index[1:] -btc.index[:-1]).value_counts().head()\n\n# Subgroup Cardano\nada=c_time_sub(asset_id=3)\n(ada.index[1:] -ada.index[:-1]).value_counts().head()","metadata":{"execution":{"iopub.status.busy":"2022-11-15T16:08:32.777049Z","iopub.execute_input":"2022-11-15T16:08:32.777403Z","iopub.status.idle":"2022-11-15T16:08:35.109112Z","shell.execute_reply.started":"2022-11-15T16:08:32.777364Z","shell.execute_reply":"2022-11-15T16:08:35.107926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# time frame selection: from datetime to timestamp\ntotimestamp= lambda s: np.int32(time.mktime(datetime.strptime(s,\"%d/%m/%Y\").timetuple()))\n\n# Log Return \ndef log_return(series, periods=1):\n    return np.log(series).diff(periods=periods)","metadata":{"execution":{"iopub.status.busy":"2022-11-15T16:21:29.750796Z","iopub.execute_input":"2022-11-15T16:21:29.751213Z","iopub.status.idle":"2022-11-15T16:21:29.760117Z","shell.execute_reply.started":"2022-11-15T16:21:29.751174Z","shell.execute_reply":"2022-11-15T16:21:29.758905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data Selection\ndef crypto_sub(asset_id ,data= ctrain ):\n    df = data[data[\"Asset_ID\"]==asset_id].reset_index(drop = True)\n    df['timestamp'] = pd.to_datetime(df['timestamp'], unit='s')\n    df = df.set_index('timestamp')\n    return df\n\n#Subgroup Bitcoin\nbtc1=crypto_sub(asset_id=1)\n\n#Subgroup Eth\neth1=crypto_sub(asset_id=6)\n\n#Subgroup Ada\nada1=crypto_sub(asset_id=3)","metadata":{"execution":{"iopub.status.busy":"2022-11-15T16:21:29.761595Z","iopub.execute_input":"2022-11-15T16:21:29.762639Z","iopub.status.idle":"2022-11-15T16:21:31.111454Z","shell.execute_reply.started":"2022-11-15T16:21:29.762585Z","shell.execute_reply":"2022-11-15T16:21:31.110517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 id=\"h2\">\n<div style=\"color:black;\n           display:fill;\n           border-radius:5px;\n           background-color:#ffeea9;\n           font-size:100%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n2. 🎨 Data Visualization\n<a class=\"anchor-link\" href=\"https://www.kaggle.com/code/fangya/cryptocurrency-data-visualization-arima/h2\">¶</a>\n</h1>\n   \nwe will present the weight among the 14 different crypto currency and investigate three crypto currency in depth.\n","metadata":{}},{"cell_type":"markdown","source":"\n <h2 style=\"color:#f88a21;\"> Cryptocurrency summary </h2>\n\n0. Bitcoin (BTC): launched in January 2009. It introduced a novel idea set out in a white paper by the mysterious Satoshi Nakamoto—bitcoin offers the promise of an online currency that is secured without any central authority, unlike government-issued currencies.\n\n\n1. Ethereum (ETH): The goal behind Ethereum is to create a decentralized suite of financial products that anyone in the world can freely access, regardless of nationality, ethnicity, or fait.\n\n\n3. Litecoin(LTC): known as “silver to Bitcoin’s gold.” It is Based on an open-source global payment network that is not controlled by any central authority and uses “scrypt” as a proof of work, which can be decoded with the help of consumer-grade CPUs.\n\n\n4. Cardano (ADA)：an “Ouroboros proof-of-stake” cryptocurrency that was created with a research-based approach by engineers, mathematicians, and cryptography experts.\n\n\n5. Bitcoin Cash (BCH): an important place in the history of altcoins because it is one of the earliest and most successful hard forks of the original Bitcoin\n\n\n6. Stella(XLM): an open blockchain network designed to provide enterprise solutions by connecting financial institutions for the purpose of large transactions.(Transcations between banks)\n\n\n7. Dogecoin(DOGE): as the original \"memecoin\" caused a stir in 2021 as the price of the coin skyrocketed. (Popular because of Elon Musk)\n\n\n8. Binance Coin(BNB): a utility cryptocurrency that operates as a payment method for the fees associated with trading on the Binance Exchange. It is the third-largest cryptocurrency by market capitalization. (initiated from ETH)\n\n\n9. Monero(XMR): a secure, private, and untraceable currency.The development of this cryptocurrency is completely donation-based and community-driven.\n\n\n10. TRON: Founded in 2017 by a Singapore non-profit organization, aims to host a global entertainment system for the cost-effective sharing of digital content.\n\n\n11. IOTA: a distributed ledger designed to record and execute transactions between machines and devices in the Internet of Things (IoT) ecosystem.\n\n\n12. EOS.IO: a blockchain-based, decentralized platform used to develop, host, and run business applications, or dApps.\n\n\n13. Ethererum Classic: an open-source, decentralized, blockchain-based distributed cryptocurrency platform that runs smart contracts. It is as a split version of ETH.\n","metadata":{}},{"cell_type":"markdown","source":"\n <h2 style=\"color:#f88a21;\"> Weight Distribution Plot: </h2>\nThe Bar plot shows Bitcoin, Ethereum, and Cardano are the three most popular crypto currency for trading or investment","metadata":{}},{"cell_type":"code","source":"%matplotlib inline\ninfo_s=info.sort_values(\"Weight\")\nfig_index=px.bar(info_s,x=\"Asset_Name\" , y=\"Weight\", color=\"Weight\", title=\"Popular Cryptocurrency Weight Distribution\",\n                color_continuous_scale=px.colors.sequential.YlOrBr)\nfig_index.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-15T16:21:31.114156Z","iopub.execute_input":"2022-11-15T16:21:31.114598Z","iopub.status.idle":"2022-11-15T16:21:32.771377Z","shell.execute_reply.started":"2022-11-15T16:21:31.114552Z","shell.execute_reply":"2022-11-15T16:21:32.770004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" <h2 style=\"color:#f88a21;\"> 2.2 Cryptocurrency Log Return Correlation Plot for 2021  </h2>\n\nFrom the Correlation plot we can see Bitcoin Cash is highly correlated with EOS.IO.\n\nBinance Coin is correlated with many crypto currencies, such as , Bitcoin, Bitcoin Cash, Cardano, Ethereum. \n\nWe can have a roughly conclude that Binance Coin, Bitcoin Cash prices have a strong relation with other cryptocurrencies. Whereas Dogecoin and Monero prices are quite independent. \nEalier we mentioned Monero is based a donation, which makes sense it is not correlated so much with other coins.\n","metadata":{}},{"cell_type":"code","source":"# time frame selection: from datetime to timestamp\ntotimestamp= lambda s: np.int32(time.mktime(datetime.strptime(s,\"%d/%m/%Y\").timetuple()))\n\n# Log Return \ndef log_return(series, periods=1):\n    return np.log(series).diff(periods=periods)\n\nall2021=pd.DataFrame([])\nfor asset_id, asset_name in zip(info.Asset_ID, info.Asset_Name):\n    asset=ctrain[ctrain[\"Asset_ID\"]==asset_id].set_index(\"timestamp\")\n    asset=asset.loc[totimestamp(\"01/01/2021\"):totimestamp(\"21/09/2021\")]\n    asset=asset.reindex(range(asset.index[0],asset.index[-1]+60,60), method=\"pad\")\n    lret=log_return(asset.Close.fillna(0))[1:]\n    all2021=all2021.join(lret,rsuffix=asset_name,how=\"outer\")\n    \nplt.imshow(all2021.corr());\nplt.yticks(info.Asset_ID, info.Asset_Name.values)\nplt.xticks(info.Asset_ID, info.Asset_Name.values,rotation=\"vertical\");\nplt.colorbar(cmap=\"coolwarm\")","metadata":{"execution":{"iopub.status.busy":"2022-11-15T16:21:52.226873Z","iopub.execute_input":"2022-11-15T16:21:52.227544Z","iopub.status.idle":"2022-11-15T16:21:58.069652Z","shell.execute_reply.started":"2022-11-15T16:21:52.227481Z","shell.execute_reply":"2022-11-15T16:21:58.068307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 id=\"h3\">\n    <div style=\"color:black;\n           display:fill;\n           border-radius:5px;\n           background-color:#ffeea9;\n           font-size:100%;\n           font-family:Verdana;\n           letter-spacing:0.5px\"> 3.✨  Price History      \n<a class=\"anchor-link\" href=\"https://www.kaggle.com/code/fangya/cryptocurrency-data-visualization-arima\">¶</a>\n</h1>\n\nWe will use the Closing Price and Candlestick graph to capture the trends of the crypto currency.","metadata":{}},{"cell_type":"markdown","source":" <h2 style=\"color:#e96d13;\"> 3.1 Closing Price Trend in 2021 </h2>\n\nwe will impute the missing time value using the \"pad\" method and present the overall closing price for Bitcoin, Etherum, Carnado \n ","metadata":{}},{"cell_type":"code","source":"# Impute Missing Time Value\ndef c_time_sub(asset_id, data=ctrain):\n    df=data[ctrain[\"Asset_ID\"]==asset_id].set_index(\"timestamp\")\n    df=df.reindex(range(df.index[0],df.index[-1]+60,60), method=\"pad\")\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-11-15T16:22:06.113782Z","iopub.execute_input":"2022-11-15T16:22:06.114214Z","iopub.status.idle":"2022-11-15T16:22:06.120539Z","shell.execute_reply.started":"2022-11-15T16:22:06.114179Z","shell.execute_reply":"2022-11-15T16:22:06.119301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#1. Subgroup Bitcoin\nbtc=c_time_sub(asset_id=1)\n(btc.index[1:] -btc.index[:-1]).value_counts().head()\nbtc.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-15T16:22:06.471698Z","iopub.execute_input":"2022-11-15T16:22:06.472145Z","iopub.status.idle":"2022-11-15T16:22:07.341479Z","shell.execute_reply.started":"2022-11-15T16:22:06.472105Z","shell.execute_reply":"2022-11-15T16:22:07.340217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#2. Subgroup Ethereum\neth=c_time_sub(asset_id=6)\n(btc.index[1:] -btc.index[:-1]).value_counts().head()\neth.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-15T16:22:18.241639Z","iopub.execute_input":"2022-11-15T16:22:18.242244Z","iopub.status.idle":"2022-11-15T16:22:19.116873Z","shell.execute_reply.started":"2022-11-15T16:22:18.242187Z","shell.execute_reply":"2022-11-15T16:22:19.115510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#3. Subgroup Cardano\nada=c_time_sub(asset_id=3)\n(ada.index[1:] -ada.index[:-1]).value_counts().head()\nada.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-15T16:22:30.403866Z","iopub.execute_input":"2022-11-15T16:22:30.405237Z","iopub.status.idle":"2022-11-15T16:22:31.127579Z","shell.execute_reply.started":"2022-11-15T16:22:30.405171Z","shell.execute_reply":"2022-11-15T16:22:31.126280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create time interval for 2021\ndef dur(start,end,data):\n    df=data.loc[totimestamp(start): totimestamp(end)]\n    return df\n\nbtc2021= dur(start=\"01/01/2021\", end=\"21/09/2021\", data=btc)\neth2021= dur(start=\"01/01/2021\", end=\"21/09/2021\", data=eth)\nada2021= dur(start=\"01/01/2021\", end=\"21/09/2021\", data=ada)","metadata":{"execution":{"iopub.status.busy":"2022-11-15T16:22:44.486844Z","iopub.execute_input":"2022-11-15T16:22:44.487344Z","iopub.status.idle":"2022-11-15T16:22:44.495482Z","shell.execute_reply.started":"2022-11-15T16:22:44.487305Z","shell.execute_reply":"2022-11-15T16:22:44.494197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" <h3 style=\"color:#e96d13;\"> Investment Tips 1 </h3>\nWe can see there is tremedeous price increase for BTC, ETH, and ADA in 2021, which implies they are all good investment","metadata":{}},{"cell_type":"code","source":"# Plot the Closing Price for BTC, ETH, ADA\nf= plt.figure(figsize=(10,12))  \n\ndef gplot(no , data, price, label, ylabel, color):\n    ax=f.add_subplot(no)\n    plt.plot(data[price], label=label, color=color)\n    plt.legend()\n    plt.xlabel(\"Time\")\n    plt.ylabel(ylabel)\n    return plt\n\ngplot(no=311, data=btc, price=\"Close\" , label=\"BTC 2021 Overall Performance\", ylabel=\"BTC Closing Price\", color=\"#fede86\")\ngplot(no=312, data=eth, price=\"Close\" ,label=\"ETH 2021 Overall Performance\", ylabel=\"ETH Closing Price\", color=\"#e96d13\")\ngplot(no=313, data=ada, price=\"Close\" ,label=\"Cardano 2021 Overall Performance\", ylabel=\"ADA Closing Price\", color=\"#b03f03\")\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-15T16:22:57.378769Z","iopub.execute_input":"2022-11-15T16:22:57.379221Z","iopub.status.idle":"2022-11-15T16:23:04.719005Z","shell.execute_reply.started":"2022-11-15T16:22:57.379185Z","shell.execute_reply":"2022-11-15T16:23:04.718073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" <h2 style=\"color:#f88a21;\"> 3.2 Return Rate for BTC, ETH, and ADA </h2>\nwe see the crypto currency price went up dramatically regardless of the type, we would like to investigate one step further for the Target variable.\n*Target : 15 minute resudualized returns.\n\n*Residual Return: An asset's residual return equals its excess return minus beta times the benchmark excess return.\n\nAs we can see ETH, and BTC residual return are relatively stable compared to ADA.\nThis might be a good implication that if the investor would take short time trading opportunities, ADA is a better choice. \n\nIf the investor is risk averse, BTC or ETH will be a better fit","metadata":{}},{"cell_type":"code","source":"f= plt.figure(figsize=(10,12))  \ngplot(no=311, data=btc, price=\"Target\" , label=\"BTC 2021 15min Return Residue\", ylabel=\"BTC residual return\", color=\"#fede86\")\ngplot(no=312, data=eth, price=\"Target\" ,label=\"ETH 2021 15min Return Residue\", ylabel=\"ETH residual return\", color=\"#e96d13\")\ngplot(no=313, data=ada, price=\"Target\" ,label=\"ADA 2021 15min Return Residue\", ylabel=\"ADA residual return\", color=\"#b03f03\")\n\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-15T16:28:21.446956Z","iopub.execute_input":"2022-11-15T16:28:21.447611Z","iopub.status.idle":"2022-11-15T16:28:28.686355Z","shell.execute_reply.started":"2022-11-15T16:28:21.447564Z","shell.execute_reply":"2022-11-15T16:28:28.684426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" <h2 style=\"color:#f88a21;\"> 3.3 Candlestick Chart </h2>\nCandlestick charts are used by traders to determine possible price movement based on past patterns.","metadata":{}},{"cell_type":"code","source":"def c_chart(data,label):\n    candlestick = go.Figure(data = [go.Candlestick(x =data.index, \n                                               open = data[('Open')], \n                                               high = data[('High')], \n                                               low = data[('Low')], \n                                               close = data[('Close')])])\n    candlestick.update_xaxes(title_text = 'Time',\n                             rangeslider_visible = True)\n\n    candlestick.update_layout(\n    title = {\n        'text': '{:} Candelstick Chart'.format(label),\n        \"y\":0.8,\n        \"x\":0.5,\n        'xanchor': 'center',\n        'yanchor': 'top'})\n\n    candlestick.update_yaxes(title_text = 'Price in USD', ticksuffix = '$')\n    return candlestick\n\nbtc1.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" <h3 style=\"color:#b03f03;\"> Investment Tips 2 </h3>\nDuring the time interval 22:40 to 22:50, there is a series of downward red candle, which represent **Oversold.** Theoratically, the price would go up followed by a Oversold interval\n\n**Oversold** means it's trading below what would be a typical or average trading range(sometimes the investors also call it as true value). Normally, oversold draw buyers in the market.\n","metadata":{}},{"cell_type":"code","source":"%matplotlib inline\nbtc_candle=c_chart(btc1[-90:], label=\"BTC Price\")\nbtc_candle.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h3 style=\"color:#e96d13;\"> ETH Candlestick </h3>\n\nWe randomly pick a time interval for the ETh price, we see an over sold around 1:50am, and an overbuy around 02:05 am.","metadata":{}},{"cell_type":"code","source":"%matplotlib inline\nada_candle=c_chart(eth1[-500:-400], label=\"ETH Price \")\nada_candle.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h3 style=\"color:#e96d13;\"> ADA Candle Stick </h3>\n\nOverbuying around 16:20","metadata":{}},{"cell_type":"code","source":"eth_candle=c_chart(eth1[100888:100988], label=\"ETH Price \")\neth_candle.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n<h1 id=\"h4\">\n    <div style=\"color:black;\n           display:fill;\n           border-radius:5px;\n           background-color:#ffeea9;\n           font-size:100%;\n           font-family:Verdana;\n           letter-spacing:0.5px\"> 4.🌅  ARIMA Model   \n<a class=\"anchor-link\" href=\"https://www.kaggle.com/code/fangya/cryptocurrency-data-visualization-arima\">¶</a>\n</h1>","metadata":{}},{"cell_type":"code","source":"# Function to Subset the Price variables\ndef mini_data(data):\n    df=data[[\"Close\",\"Low\",\"High\",\"Open\"]]\n    return df\n\nbtc1_mini=mini_data(btc1)\neth1_mini=mini_data(eth1)\nada1_mini=mini_data(ada1)\nbtc1.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" <h2 style=\"color:#e96d13;\"> 4.1 Seasonal Decomposition</h2> ","metadata":{}},{"cell_type":"code","source":"# Function to Plot\nplt.rcParams[\"figure.figsize\"]=(15,7)\n\ndef season_df(data, label):\n    df=data.resample(\"M\").mean()\n    seasonal_decompose(df.Close).plot()\n    print(label)\n    return plt.show()\n\nseason_df(data=btc1_mini, label=\"BTC Seasonal Decomposition\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ETH Seasonal Plot\nseason_df(data=eth1_mini, label=\"ETH Seasonal Decomposition\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ADA seasonal decompostion\nseason_df(data=ada1_mini, label=\"ADA Seasonal Decomposition\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" <h2 style=\"color:#e96d13;\"> 4.2 Box - Cox Transformation </h2>","metadata":{}},{"cell_type":"code","source":"#Box-Cox Transformation\nbtc_month=btc1_mini.resample(\"M\").mean()\nbtc_month[\"close_box\"], lmbda=stats.boxcox(btc_month.Close)\n\n#btc_month['close_box'], lmbda = stats.boxcox(btc_month.Close)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"qs = range(0, 3)\nps = range(0, 3)\nd=1\nparameters = product(ps, qs)\nparameters_list = list(parameters)\nlen(parameters_list)\n\n# Model Selection\nresults = []\nbest_aic = float(\"inf\")\nwarnings.filterwarnings('ignore')\nfor param in parameters_list:\n    try:\n        model = SARIMAX(btc_month.close_box, order=(param[0], d, param[1])).fit(disp=-1)\n    except ValueError:\n        print('bad parameter combination:', param)\n        continue\n    aic = model.aic\n    if aic < best_aic:\n        best_model = model\n        best_aic = aic\n        best_param = param\n    results.append([param, model.aic])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_table = pd.DataFrame(results)\nresult_table.columns = ['parameters', 'aic']\nprint(result_table.sort_values(by = 'aic', ascending=True).head())\nprint(best_model.summary())","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_model.plot_diagnostics(figsize=(15, 12))\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def invboxcox(y,lmbda):\n    if lmbda == 0:\n        return(np.exp(y))\n    else:\n        return(np.exp(np.log(lmbda*y+1)/lmbda))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" <h2 style=\"color:#e96d13;\"> 4.2 ARIMA for Bitcoin </h2>","metadata":{}},{"cell_type":"code","source":"# Prediction\nbtc_month_pred = btc_month[['Close']]\ndate_list = [datetime(2021, 6, 20), datetime(2021, 7, 20), datetime(2021, 8, 21), datetime(2021, 9, 21)]\nfuture = pd.DataFrame(index=date_list, columns= btc_month.columns)\nbtc_month_pred = pd.concat([btc_month_pred, future])\nbtc_month_pred['forecast'] = invboxcox(best_model.predict(start=datetime(2018, 1, 31), end=datetime(2021, 5, 31)), lmbda)\nplt.figure(figsize=(18,10))\nbtc_month_pred.Close.plot(color='#fede86')\nbtc_month_pred.forecast.plot(color='#b03f03', ls='--', label='Predicted Close')\nplt.legend()\nplt.title('Bitcoin monthly forecast')\nplt.ylabel('USD')\nplt.show()","metadata":{},"execution_count":null,"outputs":[]}]}