{"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":"2021-11-19T21:47:40.241762Z","iopub.execute_input":"2021-11-19T21:47:40.242106Z","iopub.status.idle":"2021-11-19T21:47:40.279384Z","shell.execute_reply.started":"2021-11-19T21:47:40.242000Z","shell.execute_reply":"2021-11-19T21:47:40.278606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_folder = \"../input/g-research-crypto-forecasting/\"\ncrypto_df = pd.read_csv(data_folder + 'train.csv')\nasset_details_df = pd.read_csv(data_folder + 'asset_details.csv')\nsupp_train_df = pd.read_csv(data_folder + 'supplemental_train.csv')\nexpl_test = pd.read_csv(data_folder + 'example_test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-11-19T21:47:43.475118Z","iopub.execute_input":"2021-11-19T21:47:43.475409Z","iopub.status.idle":"2021-11-19T21:48:49.083436Z","shell.execute_reply.started":"2021-11-19T21:47:43.475378Z","shell.execute_reply":"2021-11-19T21:48:49.082259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"asset_details_df[\"Asset_Name\"] = asset_details_df[\"Asset_Name\"].str.replace(' ','_')\nasset_details_df[\"Asset_Name\"] = asset_details_df[\"Asset_Name\"].str.replace('.','_')","metadata":{"execution":{"iopub.status.busy":"2021-11-19T21:48:49.085176Z","iopub.execute_input":"2021-11-19T21:48:49.085429Z","iopub.status.idle":"2021-11-19T21:48:49.098527Z","shell.execute_reply.started":"2021-11-19T21:48:49.085399Z","shell.execute_reply":"2021-11-19T21:48:49.097528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"asset_details = asset_details_df.copy()\nasset_details_df = asset_details_df.set_index(\"Asset_ID\")","metadata":{"execution":{"iopub.status.busy":"2021-11-19T21:48:49.100296Z","iopub.execute_input":"2021-11-19T21:48:49.100617Z","iopub.status.idle":"2021-11-19T21:48:49.112915Z","shell.execute_reply.started":"2021-11-19T21:48:49.100574Z","shell.execute_reply":"2021-11-19T21:48:49.112035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Create a dictionary of data frames assigned to each coin \ndataframes = {}\nfor asset_id, asset_name in zip(asset_details.Asset_ID, asset_details.Asset_Name):    \n    keys = range(4)\n    vars()[asset_name] = crypto_df[crypto_df[\"Asset_ID\"]==asset_id].set_index(\"timestamp\")\n    dataframes[asset_id] = vars()[asset_name]","metadata":{"execution":{"iopub.status.busy":"2021-11-19T21:48:49.114948Z","iopub.execute_input":"2021-11-19T21:48:49.115198Z","iopub.status.idle":"2021-11-19T21:48:52.989102Z","shell.execute_reply.started":"2021-11-19T21:48:49.115167Z","shell.execute_reply":"2021-11-19T21:48:52.988276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cleaned_dataframes = {}\nfor i in list(dataframes):\n    cleaned_dataframes[i] = dataframes[i].reindex(range(dataframes[i].index[0],dataframes[i].index[-1]+60,60),method='pad')","metadata":{"execution":{"iopub.status.busy":"2021-11-19T21:48:52.990629Z","iopub.execute_input":"2021-11-19T21:48:52.990943Z","iopub.status.idle":"2021-11-19T21:48:57.785983Z","shell.execute_reply.started":"2021-11-19T21:48:52.990902Z","shell.execute_reply":"2021-11-19T21:48:57.785027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Reduce the datastes to ease the correlation calculation\nfrom datetime import datetime\nimport time\ntotimestamp = lambda s: np.int32(time.mktime(datetime.strptime(s, \"%d/%m/%Y\").timetuple()))\nreduced_dataframes = {}\nfor i in list(cleaned_dataframes):\n    #reduced_dataframes[i] = cleaned_dataframes[i].loc[totimestamp('01/01/2021'):totimestamp('01/05/2021')]\n    reduced_dataframes[i] = cleaned_dataframes[i].iloc[-100:]","metadata":{"execution":{"iopub.status.busy":"2021-11-19T21:48:57.787429Z","iopub.execute_input":"2021-11-19T21:48:57.787843Z","iopub.status.idle":"2021-11-19T21:48:57.795167Z","shell.execute_reply.started":"2021-11-19T21:48:57.787784Z","shell.execute_reply":"2021-11-19T21:48:57.793930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_macd(df) :\n    # Moving Average Convergence Divergence\n    # The formula for MACD = 12-Period EMA − 26-Period EMA\n    # Luckily, the Pandas DataFrame provides a function ewm(), which together with the mean-function can calculate the Exponential Moving Averages\n    exp1 = df[\"Close\"].ewm(span=12, adjust=False).mean()\n    exp2 = df[\"Close\"].ewm(span=26, adjust=False).mean()\n    macd = exp1 - exp2\n    ema_macd = macd.ewm(span=9, adjust=False).mean()\n    df[\"MACD\"] = macd\n    df[\"MACD_Signal\"] = ema_macd","metadata":{"execution":{"iopub.status.busy":"2021-11-19T22:01:49.401763Z","iopub.execute_input":"2021-11-19T22:01:49.402059Z","iopub.status.idle":"2021-11-19T22:01:49.409367Z","shell.execute_reply.started":"2021-11-19T22:01:49.402026Z","shell.execute_reply":"2021-11-19T22:01:49.408170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_rsi(df) :\n    #Relative Strenght Index\n    #measures the magnitude of recent price changes to evaluate overbought or oversold conditions in the price of a stock or other asset\n    delta = df['Close'].diff()\n    up = delta.clip(lower=0)\n    down = -1*delta.clip(upper=0)\n    ema_up = up.ewm(com=13, adjust=False).mean()\n    ema_down = down.ewm(com=13, adjust=False).mean()\n    rs = ema_up/ema_down\n    df['RSI'] = 100 - (100/(1 + rs))","metadata":{"execution":{"iopub.status.busy":"2021-11-19T22:01:52.177135Z","iopub.execute_input":"2021-11-19T22:01:52.177406Z","iopub.status.idle":"2021-11-19T22:01:52.184551Z","shell.execute_reply.started":"2021-11-19T22:01:52.177374Z","shell.execute_reply":"2021-11-19T22:01:52.183673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_mfi(df) :\n    #Money Flow Index\n    #Calculated base on typical price : Typical price = (high + low + close) / 3\n    Typical_price = (df['High']+df['Low']+df[\"Close\"])/3\n    period =  14 #The typical period used for MFI is 14 days\n    money_flow = Typical_price * df['Volume']\n    \n    positive_flow = []\n    negative_flow = []\n    \n    for j in range(1, len(Typical_price)):\n        if Typical_price.iloc[j] > Typical_price.iloc[j-1]:\n            positive_flow.append(money_flow.iloc[j-1])\n            negative_flow.append(0)\n        elif Typical_price.iloc[j] < Typical_price.iloc[j-1]:\n            negative_flow.append(money_flow.iloc[j-1])\n            positive_flow.append(0)\n        \n        else:\n            positive_flow.append(0)\n            negative_flow.append(0)\n    \n    positive_mf = []\n    negative_mf = []\n\n    for j in range(period-1, len(positive_flow)):\n        positive_mf.append( sum(positive_flow[j + 1- period : j+1]))\n    \n    for j in range(period-1, len(negative_flow)):\n        negative_mf.append( sum(negative_flow[j + 1- period : j+1]))\n    MFI = 100 * (np.array(positive_mf) / (np.array(positive_mf) + np.array(negative_mf) ))\n    df[\"MFI\"] = 0 \n    df[\"MFI\"][14:] = MFI","metadata":{"execution":{"iopub.status.busy":"2021-11-19T22:20:03.679836Z","iopub.execute_input":"2021-11-19T22:20:03.680142Z","iopub.status.idle":"2021-11-19T22:20:03.692868Z","shell.execute_reply.started":"2021-11-19T22:20:03.680111Z","shell.execute_reply":"2021-11-19T22:20:03.691885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfi_macd_rsi_dataframes = reduced_dataframes\nfor i in list(mfi_macd_rsi_dataframes) :\n    get_macd(mfi_macd_rsi_dataframes[i])\n    get_rsi(mfi_macd_rsi_dataframes[i])\n    get_mfi(mfi_macd_rsi_dataframes[i])","metadata":{"execution":{"iopub.status.busy":"2021-11-19T22:20:19.285392Z","iopub.execute_input":"2021-11-19T22:20:19.285943Z","iopub.status.idle":"2021-11-19T22:20:19.468193Z","shell.execute_reply.started":"2021-11-19T22:20:19.285894Z","shell.execute_reply":"2021-11-19T22:20:19.467219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfi_macd_rsi_dataframes[3]","metadata":{"execution":{"iopub.status.busy":"2021-11-19T22:20:59.696633Z","iopub.execute_input":"2021-11-19T22:20:59.697053Z","iopub.status.idle":"2021-11-19T22:20:59.721029Z","shell.execute_reply.started":"2021-11-19T22:20:59.697020Z","shell.execute_reply":"2021-11-19T22:20:59.720116Z"},"trusted":true},"execution_count":null,"outputs":[]}]}