{"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-03-05T18:54:42.507667Z","iopub.execute_input":"2022-03-05T18:54:42.508177Z","iopub.status.idle":"2022-03-05T18:54:42.548917Z","shell.execute_reply.started":"2022-03-05T18:54:42.508098Z","shell.execute_reply":"2022-03-05T18:54:42.548424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_folder = \"../input/g-research-crypto-forecasting/\"\n!ls $data_folder\ncrypto_df = pd.read_csv(data_folder + 'train.csv')\ncrypto_df = pd.DataFrame(crypto_df,columns=['timestamp','Asset_ID','Close','Volume'])\ncrypto_df = crypto_df.fillna(0)\ncrypto_df.tail()","metadata":{"execution":{"iopub.status.busy":"2022-03-05T18:54:42.550033Z","iopub.execute_input":"2022-03-05T18:54:42.550297Z","iopub.status.idle":"2022-03-05T18:55:48.687540Z","shell.execute_reply.started":"2022-03-05T18:54:42.550261Z","shell.execute_reply":"2022-03-05T18:55:48.686525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import r2_score\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import linear_model\nimport matplotlib.pyplot as plt \n\nX = crypto_df[crypto_df[\"Asset_ID\"]==0]\ny = X[\"Close\"]\n\n\nX_train, X_rem, y_train, y_rem = train_test_split(X,y, train_size=1)\n\n \nX_valid, X_test, y_valid, y_test = train_test_split(X_rem,y_rem, test_size=0.5)\n\nregr = linear_model.LinearRegression()\n\n\nregr.fit(X_train, y_train)\n\n\ny_pred = regr.predict(X_test)\n \ny_true = y.iloc[971310:1942619]\n\nprint('Paerson coefficient %', r2_score(y_true, y_pred, multioutput='variance_weighted'))\n\nprint('Last price closed',y.tail(1),'Price predictions', y_pred) \n\nlen(X_test), len(y_test) \n","metadata":{"execution":{"iopub.status.busy":"2022-03-05T18:55:48.688997Z","iopub.execute_input":"2022-03-05T18:55:48.689255Z","iopub.status.idle":"2022-03-05T18:55:51.383406Z","shell.execute_reply.started":"2022-03-05T18:55:48.689220Z","shell.execute_reply":"2022-03-05T18:55:51.382807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 413.39+18.35 (+4.64%)# Previous Close: 398.54\n#  En Wikipedia. https://finance.yahoo.com \n# Yahoo finance. (05/02/2022).Yahoo!.https://finance.yahoo.com/quote/BNB-USD/","metadata":{}},{"cell_type":"code","source":"\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom sklearn import datasets, linear_model\nfrom sklearn.metrics import mean_squared_error, r2_score\n\nXy = np.array(X[\"Close\"]) \n\n\n\nX_train = X[:-971310]\nX_test = X[-971310:]\n\ny_train = y[:-971310]\ny_test = y[-971310:]\n\n\nregr = linear_model.LinearRegression()\n\nregr.fit(X_train, y_train)\n\ny_pred = regr.predict(X_test)\n \ny_true = y.iloc[971309:1942619]\n\nprint('Paerson coefficient %', r2_score(y_true, y_pred, multioutput='variance_weighted'))\n\nprint('Last price closed',y.tail(1),'Price predictions', y_pred) \n\nlen(X_test), len(y_test) \n","metadata":{"execution":{"iopub.status.busy":"2022-03-05T18:55:51.385208Z","iopub.execute_input":"2022-03-05T18:55:51.385837Z","iopub.status.idle":"2022-03-05T18:55:51.625817Z","shell.execute_reply.started":"2022-03-05T18:55:51.385798Z","shell.execute_reply":"2022-03-05T18:55:51.625139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"crypto_df['timestamp'] = pd.to_datetime(crypto_df.timestamp,format='%Y-%m-%d')\ncrypto_df.index = crypto_df['timestamp']\n\n\npreds = regr.predict(X_test)\n#plot\ny_pred = 0\ny_pred = preds\nplt.figure(figsize=(16,8))\nplt.plot(X['Close'])\nplt.title(\"Close Price history at 363.7575.\")\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-03-05T18:55:51.627050Z","iopub.execute_input":"2022-03-05T18:55:51.627435Z","iopub.status.idle":"2022-03-05T18:55:54.013103Z","shell.execute_reply.started":"2022-03-05T18:55:51.627400Z","shell.execute_reply":"2022-03-05T18:55:54.012548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" \nimport pandas as pd\nimport numpy as np\n\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfrom matplotlib.pylab import rcParams\n\nrcParams['figure.figsize'] = 20,10\n\nfrom sklearn.preprocessing import MinMaxScaler\nscaler = MinMaxScaler(feature_range=(0, 1))\n\nBNB_df = pd.read_csv('../input/bnbusd-dec-31-2020-feb-13-2022/BNB-USD(1).csv')\n\nBNB_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-05T18:55:54.014277Z","iopub.execute_input":"2022-03-05T18:55:54.014637Z","iopub.status.idle":"2022-03-05T18:55:54.048799Z","shell.execute_reply.started":"2022-03-05T18:55:54.014604Z","shell.execute_reply":"2022-03-05T18:55:54.048218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BNB_df['Date'] = pd.to_datetime(BNB_df.Date,format='%Y-%m-%d')\nBNB_df.index = BNB_df['Date']\n\n\n#plot\nplt.figure(figsize=(16,8))\nplt.plot(BNB_df['Close'], label='Close Price history')\nplt.title(\"Currency performance according to Yahoo Finance\")\n","metadata":{"execution":{"iopub.status.busy":"2022-03-05T18:55:54.049865Z","iopub.execute_input":"2022-03-05T18:55:54.050199Z","iopub.status.idle":"2022-03-05T18:55:54.277246Z","shell.execute_reply.started":"2022-03-05T18:55:54.050169Z","shell.execute_reply":"2022-03-05T18:55:54.276361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" \nBNB_crypto_df = pd.read_csv('../input/bnbusd-dec-31-2020-feb-13-2022/BNB-USD(1).csv')\nBNB_crypto_df = pd.DataFrame(BNB_crypto_df,columns=['Date','Close'])\nBNB_crypto_df = BNB_crypto_df.fillna(0)\nBNB_crypto_df.tail(5)","metadata":{"execution":{"iopub.status.busy":"2022-03-05T18:55:54.278532Z","iopub.execute_input":"2022-03-05T18:55:54.278772Z","iopub.status.idle":"2022-03-05T18:55:54.296881Z","shell.execute_reply.started":"2022-03-05T18:55:54.278739Z","shell.execute_reply":"2022-03-05T18:55:54.296156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(BNB_crypto_df)","metadata":{"execution":{"iopub.status.busy":"2022-03-05T18:55:54.299667Z","iopub.execute_input":"2022-03-05T18:55:54.300412Z","iopub.status.idle":"2022-03-05T18:55:54.306454Z","shell.execute_reply.started":"2022-03-05T18:55:54.300378Z","shell.execute_reply":"2022-03-05T18:55:54.305757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#split into train and validation\ntrain = BNB_crypto_df[:410]\nvalid = BNB_crypto_df[205:]\n\nx_train = train.drop('Date', axis=1)\ny_train = train['Close']\nx_valid = valid.drop('Date', axis=1)\ny_valid = valid['Close']\n\n#implement linear regression\nfrom sklearn.linear_model import LinearRegression\nmodel = LinearRegression()\nmodel.fit(x_train,y_train)\nmodel.predict(x_valid)\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-03-05T18:55:54.307750Z","iopub.execute_input":"2022-03-05T18:55:54.308618Z","iopub.status.idle":"2022-03-05T18:55:54.330091Z","shell.execute_reply.started":"2022-03-05T18:55:54.308581Z","shell.execute_reply":"2022-03-05T18:55:54.329622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\npreds = model.predict(x_valid)\n\n#plot\nvalid['Predictions'] = 0\nvalid['Predictions'] = preds\n\nvalid.index = BNB_crypto_df[205:].index\ntrain.index = BNB_crypto_df[:410].index\n\nplt.plot(train['Close'])\nplt.plot(valid[['Close','Predictions']])","metadata":{"execution":{"iopub.status.busy":"2022-03-05T18:55:54.331124Z","iopub.execute_input":"2022-03-05T18:55:54.331438Z","iopub.status.idle":"2022-03-05T18:55:54.580154Z","shell.execute_reply.started":"2022-03-05T18:55:54.331412Z","shell.execute_reply":"2022-03-05T18:55:54.579548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = BNB_crypto_df\n\n\nprint(df.head())\nprint('\\n Shape of the data:')\nprint(df.shape)\n\n\ndf['Date'] = pd.to_datetime(df.Date,format='%Y-%m-%d')\ndf.index = df['Date']\n\ndata = df.sort_index(ascending=True, axis=0)\nnew_data = pd.DataFrame(index=range(0,len(df)),columns=['Date', 'Close'])\n\nfor i in range(0,len(data)):\n     new_data['Date'][i] = data['Date'][i]\n     new_data['Close'][i] = data['Close'][i]\n\n\ntrain = new_data[:410]\nvalid = new_data[205:]\n\n\nprint('\\n Shape of training set:')\nprint(train.shape)\n\n\nprint('\\n Shape of validation set:')\nprint(valid.shape)\n\npreds = []\nfor i in range(0,valid.shape[0]):\n    a = train['Close'][len(train)-248+i:].sum() + sum(preds)\n    b = a/248\n    preds.append(b)\n\nrms=np.sqrt(np.mean(np.power((np.array(valid['Close'])-preds),2)))\nprint('\\n RMSE value on validation set:')\nprint(rms)\n\nvalid['Predictions'] = 0\nvalid['Predictions'] = preds\nplt.plot(train['Close'])\nplt.plot(valid[['Close', 'Predictions']])","metadata":{"execution":{"iopub.status.busy":"2022-03-05T18:55:54.581245Z","iopub.execute_input":"2022-03-05T18:55:54.581558Z","iopub.status.idle":"2022-03-05T18:55:54.959648Z","shell.execute_reply.started":"2022-03-05T18:55:54.581523Z","shell.execute_reply":"2022-03-05T18:55:54.958528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" df = BNB_crypto_df\n\n\nprint(df.head())\nprint('\\n Shape of the data:')\nprint(df.shape)\n\n\ndf['Date'] = pd.to_datetime(df.Date,format='%Y-%m-%d')\ndf.index = df['Date']\n\ndata = df.sort_index(ascending=True, axis=0)\nnew_data = pd.DataFrame(index=range(0,len(df)),columns=['Date', 'Close'])\n\nfor i in range(0,len(data)):\n     new_data['Date'][i] = data['Date'][i]\n     new_data['Close'][i] = data['Close'][i]\n\n\ntrain = new_data[:410]\nvalid = new_data[310:]\n\n\nprint('\\n Shape of training set:')\nprint(train.shape)\n\n\nprint('\\n Shape of validation set:')\nprint(valid.shape)\n\npreds = []\nfor i in range(0,valid.shape[0]):\n    a = train['Close'][len(train)-248+i:].sum() + sum(preds)\n    b = a/248\n    preds.append(b)\n\nrms=np.sqrt(np.mean(np.power((np.array(valid['Close'])-preds),2)))\nprint('\\n RMSE value on validation set:')\nprint(rms)\n\nvalid['Predictions'] = 0\nvalid['Predictions'] = preds\nplt.plot(train['Close'])\nplt.plot(valid[['Close', 'Predictions']])","metadata":{"execution":{"iopub.status.busy":"2022-03-05T18:55:54.960848Z","iopub.execute_input":"2022-03-05T18:55:54.961061Z","iopub.status.idle":"2022-03-05T18:55:55.307228Z","shell.execute_reply.started":"2022-03-05T18:55:54.961032Z","shell.execute_reply":"2022-03-05T18:55:55.306103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **VOLABITY **","metadata":{}},{"cell_type":"code","source":"BNB_df['Date'] = pd.to_datetime(BNB_df.Date,format='%Y-%m-%d')\nBNB_df.index = BNB_df['Date']\n\n\n#plot\nplt.figure(figsize=(16,8))\nplt.plot(BNB_df['Volume'], label='Volume')\nplt.title(\"Sense of marks volatibity\")\n\n","metadata":{"execution":{"iopub.status.busy":"2022-03-05T18:55:55.308859Z","iopub.execute_input":"2022-03-05T18:55:55.309589Z","iopub.status.idle":"2022-03-05T18:55:55.591249Z","shell.execute_reply.started":"2022-03-05T18:55:55.309541Z","shell.execute_reply":"2022-03-05T18:55:55.589367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BNB_crypto_df = pd.read_csv('../input/bnbusd-dec-31-2020-feb-13-2022/BNB-USD(1).csv')\nBNB_crypto_df = pd.DataFrame(BNB_crypto_df,columns=['Date','Volume'])\nBNB_crypto_df = BNB_crypto_df.fillna(0)\nBNB_crypto_df.tail(5)","metadata":{"execution":{"iopub.status.busy":"2022-03-05T18:55:55.592330Z","iopub.execute_input":"2022-03-05T18:55:55.592550Z","iopub.status.idle":"2022-03-05T18:55:55.609442Z","shell.execute_reply.started":"2022-03-05T18:55:55.592520Z","shell.execute_reply":"2022-03-05T18:55:55.607988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#split into train and validation\ntrain = BNB_crypto_df[:410]\nvalid = BNB_crypto_df[205:]\n\nx_train = train.drop('Date', axis=1)\ny_train = train['Volume']\nx_valid = valid.drop('Date', axis=1)\ny_valid = valid['Volume']\n\n#implement linear regression\nfrom sklearn.linear_model import LinearRegression\nmodel = LinearRegression()\nmodel.fit(x_train,y_train)\nmodel.predict(x_valid)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-03-05T18:55:55.611239Z","iopub.execute_input":"2022-03-05T18:55:55.612443Z","iopub.status.idle":"2022-03-05T18:55:55.631096Z","shell.execute_reply.started":"2022-03-05T18:55:55.612387Z","shell.execute_reply":"2022-03-05T18:55:55.630562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = BNB_crypto_df\n\n\nprint(df.head())\nprint('\\n Shape of the data:')\nprint(df.shape)\n\n\ndf['Date'] = pd.to_datetime(df.Date,format='%Y-%m-%d')\ndf.index = df['Date']\n\ndata = df.sort_index(ascending=True, axis=0)\nnew_data = pd.DataFrame(index=range(0,len(df)),columns=['Date', 'Volume'])\n\nfor i in range(0,len(data)):\n     new_data['Date'][i] = data['Date'][i]\n     new_data['Volume'][i] = data['Volume'][i]\n\n\ntrain = new_data[:410]\nvalid = new_data[205:]\n\n\nprint('\\n Shape of training set:')\nprint(train.shape)\n\n\nprint('\\n Shape of validation set:')\nprint(valid.shape)\n\npreds = []\nfor i in range(0,valid.shape[0]):\n    a = train['Volume'][len(train)-248+i:].sum() + sum(preds)\n    b = a/248\n    preds.append(b)\n\nrms=np.sqrt(np.mean(np.power((np.array(valid['Volume'])-preds),2)))\nprint('\\n RMSE value on validation set:')\nprint(rms)\n\nvalid['Predictions'] = 0\nvalid['Predictions'] = preds\nplt.plot(train['Volume'])\nplt.plot(valid[['Volume', 'Predictions']])","metadata":{"execution":{"iopub.status.busy":"2022-03-05T18:55:55.632167Z","iopub.execute_input":"2022-03-05T18:55:55.632681Z","iopub.status.idle":"2022-03-05T18:55:56.049792Z","shell.execute_reply.started":"2022-03-05T18:55:55.632649Z","shell.execute_reply":"2022-03-05T18:55:56.049164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}