{"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":"\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport math\nfrom plotly.subplots import make_subplots\nimport plotly.express as px\nimport plotly.graph_objects as go\n\n#\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-06T08:13:06.034832Z","iopub.execute_input":"2021-11-06T08:13:06.035362Z","iopub.status.idle":"2021-11-06T08:13:07.354258Z","shell.execute_reply.started":"2021-11-06T08:13:06.035323Z","shell.execute_reply":"2021-11-06T08:13:07.353367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('../input/g-research-crypto-forecasting/train.csv') \nasset_details = pd.read_csv('../input/g-research-crypto-forecasting/asset_details.csv') ","metadata":{"execution":{"iopub.status.busy":"2021-11-06T08:13:07.356025Z","iopub.execute_input":"2021-11-06T08:13:07.356270Z","iopub.status.idle":"2021-11-06T08:13:59.551316Z","shell.execute_reply.started":"2021-11-06T08:13:07.356242Z","shell.execute_reply":"2021-11-06T08:13:59.550439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2021-11-06T08:13:59.552491Z","iopub.execute_input":"2021-11-06T08:13:59.552710Z","iopub.status.idle":"2021-11-06T08:13:59.576880Z","shell.execute_reply.started":"2021-11-06T08:13:59.552684Z","shell.execute_reply":"2021-11-06T08:13:59.575916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"asset_details.info()","metadata":{"execution":{"iopub.status.busy":"2021-11-06T08:13:59.578118Z","iopub.execute_input":"2021-11-06T08:13:59.578349Z","iopub.status.idle":"2021-11-06T08:13:59.592878Z","shell.execute_reply.started":"2021-11-06T08:13:59.578321Z","shell.execute_reply":"2021-11-06T08:13:59.592245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Just parallelised different volatitlites from\n[here](https://github.com/jasonstrimpel/volatility-trading)\nand made them all-in-one function","metadata":{}},{"cell_type":"code","source":"def get_estimator(price_data, estimator,trading_periods,window=7*24*60):\n    window=7*24*60\n    if estimator == 'Parkinson':\n        rs = (1.0 / (4.0 * math.log(2.0))) * ((price_data['High'] / price_data['Low']).apply(np.log)) ** 2.0\n    elif estimator == 'GarmanKlass':\n        log_hl = (price_data['High'] / price_data['Low']).apply(np.log)\n        log_co = (price_data['Close'] / price_data['Open']).apply(np.log)\n        rs = 0.5 * log_hl ** 2 - (2 * math.log(2) - 1) * log_co ** 2\n    elif estimator == 'YangZhang':\n        log_ho = (price_data['High'] / price_data['Open']).apply(np.log)\n        log_lo = (price_data['Low'] / price_data['Open']).apply(np.log)\n        log_co = (price_data['Close'] / price_data['Open']).apply(np.log)\n        log_oc = (price_data['Open'] / price_data['Close'].shift(1)).apply(np.log)\n        log_oc_sq = log_oc ** 2\n        log_cc = (price_data['Close'] / price_data['Close'].shift(1)).apply(np.log)\n        log_cc_sq = log_cc ** 2\n        rs = log_ho * (log_ho - log_co) + log_lo * (log_lo - log_co)\n\n    def f(v):\n        return (trading_periods * v.mean()) ** 0.5\n    \n    result = pd.DataFrame()\n\n    result['vol'] = rs.rolling(\n        window=window,\n        center=False\n    ).parallel_apply(func=f)\n\n    result['timestamp'] = price_data['timestamp']\n\n    return result.dropna()","metadata":{"execution":{"iopub.status.busy":"2021-11-06T08:13:59.594530Z","iopub.execute_input":"2021-11-06T08:13:59.595105Z","iopub.status.idle":"2021-11-06T08:13:59.605448Z","shell.execute_reply.started":"2021-11-06T08:13:59.595074Z","shell.execute_reply":"2021-11-06T08:13:59.604597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Plot 3 types of volatilities (monthly, 7-day rolling window) for each asset out of 14**","metadata":{}},{"cell_type":"code","source":"#from tqdm import tqdm\n\n\nimport matplotlib.pyplot as plt\nfrom IPython.display import HTML\nfrom matplotlib.animation import ArtistAnimation, FuncAnimation\n\n%matplotlib inline\n\nfrom pandarallel import pandarallel\npandarallel.initialize(progress_bar=False)\n\nvolatilities = {\n        'YangZhang':\"red\",\n        'GarmanKlass':\"green\",\n        'Parkinson':\"blue\"\n}\n\nperiods = 30*24*60\n\nexs = data['Asset_ID'].value_counts()\nprint(exs)","metadata":{"execution":{"iopub.status.busy":"2021-11-06T08:34:06.686800Z","iopub.execute_input":"2021-11-06T08:34:06.687080Z","iopub.status.idle":"2021-11-06T08:34:06.843577Z","shell.execute_reply.started":"2021-11-06T08:34:06.687050Z","shell.execute_reply":"2021-11-06T08:34:06.842723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def print_vol_graph(asset, volatilities, periods):\n    \n    #plotly\n    #fig = make_subplots(rows=3, cols=1)\n    \n    #matplotlib\n    fig, axes = plt.subplots(nrows=3, ncols=1, sharex=True, figsize=(16,5), dpi= 80)\n    fig.subplots_adjust(hspace=0.5)\n    \n    asset_name = asset_details.loc[asset_details['Asset_ID']==asset]['Asset_Name'].values[0]\n    row_n = 0\n    for vf in volatilities.keys():\n        vol = get_estimator(data.loc[data['Asset_ID']==int(asset)],vf,periods)\n        axes[row_n].plot(\n            vol['vol'],\n            color=volatilities[vf],\n            linewidth=1\n        )\n        axes[row_n].set_ylabel(\" {} \".format(vf))\n        \n        #fig.add_trace(go.Scatter(x=vol['timestamp'], y=vol['vol'],\n        #              name = \"{} volatility\".format(vf),\n        #              line=dict(color=volatilities[vf], width=1)),\n        #              row=row_n,\n        #              col=1\n        #             )\n        row_n+=1\n    #fig.update_layout(height=700, width=900, title_text=\"{} volatilities\".format(asset_name))\n    #fig.show()\n    fig.suptitle(\"{} volatilities\".format(asset_name))\n    ","metadata":{"execution":{"iopub.status.busy":"2021-11-06T09:07:02.630078Z","iopub.execute_input":"2021-11-06T09:07:02.631148Z","iopub.status.idle":"2021-11-06T09:07:02.638814Z","shell.execute_reply.started":"2021-11-06T09:07:02.631105Z","shell.execute_reply":"2021-11-06T09:07:02.637782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**for the sake of memory efficiency lets not use plotly this time**","metadata":{}},{"cell_type":"code","source":"print_vol_graph(0, volatilities, periods)","metadata":{"execution":{"iopub.status.busy":"2021-11-06T09:07:06.759290Z","iopub.execute_input":"2021-11-06T09:07:06.759568Z","iopub.status.idle":"2021-11-06T09:10:43.530431Z","shell.execute_reply.started":"2021-11-06T09:07:06.759539Z","shell.execute_reply":"2021-11-06T09:10:43.529385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_vol_graph(1, volatilities, periods)","metadata":{"execution":{"iopub.status.busy":"2021-11-06T08:13:59.925335Z","iopub.status.idle":"2021-11-06T08:13:59.925832Z","shell.execute_reply.started":"2021-11-06T08:13:59.925660Z","shell.execute_reply":"2021-11-06T08:13:59.925680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_vol_graph(2, volatilities, periods)","metadata":{"execution":{"iopub.status.busy":"2021-11-06T08:13:59.926762Z","iopub.status.idle":"2021-11-06T08:13:59.927220Z","shell.execute_reply.started":"2021-11-06T08:13:59.927023Z","shell.execute_reply":"2021-11-06T08:13:59.927045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_vol_graph(3, volatilities, periods)","metadata":{"execution":{"iopub.status.busy":"2021-11-06T08:13:59.927954Z","iopub.status.idle":"2021-11-06T08:13:59.928710Z","shell.execute_reply.started":"2021-11-06T08:13:59.928507Z","shell.execute_reply":"2021-11-06T08:13:59.928535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_vol_graph(4, volatilities, periods)","metadata":{"execution":{"iopub.status.busy":"2021-11-06T08:13:59.929587Z","iopub.status.idle":"2021-11-06T08:13:59.929881Z","shell.execute_reply.started":"2021-11-06T08:13:59.929724Z","shell.execute_reply":"2021-11-06T08:13:59.929744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_vol_graph(5, volatilities, periods)","metadata":{"execution":{"iopub.status.busy":"2021-11-06T08:13:59.930718Z","iopub.status.idle":"2021-11-06T08:13:59.931057Z","shell.execute_reply.started":"2021-11-06T08:13:59.930857Z","shell.execute_reply":"2021-11-06T08:13:59.930877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_vol_graph(6, volatilities, periods)","metadata":{"execution":{"iopub.status.busy":"2021-11-06T08:13:59.932667Z","iopub.status.idle":"2021-11-06T08:13:59.933145Z","shell.execute_reply.started":"2021-11-06T08:13:59.932878Z","shell.execute_reply":"2021-11-06T08:13:59.932901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_vol_graph(7, volatilities, periods)","metadata":{"execution":{"iopub.status.busy":"2021-11-06T08:13:59.934641Z","iopub.status.idle":"2021-11-06T08:13:59.935096Z","shell.execute_reply.started":"2021-11-06T08:13:59.934840Z","shell.execute_reply":"2021-11-06T08:13:59.934864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_vol_graph(8, volatilities, periods)","metadata":{"execution":{"iopub.status.busy":"2021-11-06T08:13:59.936959Z","iopub.status.idle":"2021-11-06T08:13:59.937543Z","shell.execute_reply.started":"2021-11-06T08:13:59.937358Z","shell.execute_reply":"2021-11-06T08:13:59.937380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_vol_graph(9, volatilities, periods)","metadata":{"execution":{"iopub.status.busy":"2021-11-06T08:13:59.938402Z","iopub.status.idle":"2021-11-06T08:13:59.938925Z","shell.execute_reply.started":"2021-11-06T08:13:59.938750Z","shell.execute_reply":"2021-11-06T08:13:59.938770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_vol_graph(10, volatilities, periods)","metadata":{"execution":{"iopub.status.busy":"2021-11-06T08:13:59.939881Z","iopub.status.idle":"2021-11-06T08:13:59.940544Z","shell.execute_reply.started":"2021-11-06T08:13:59.940366Z","shell.execute_reply":"2021-11-06T08:13:59.940387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_vol_graph(11, volatilities, periods)","metadata":{"execution":{"iopub.status.busy":"2021-11-06T08:13:59.942156Z","iopub.status.idle":"2021-11-06T08:13:59.942672Z","shell.execute_reply.started":"2021-11-06T08:13:59.942501Z","shell.execute_reply":"2021-11-06T08:13:59.942524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_vol_graph(12, volatilities, periods)","metadata":{"execution":{"iopub.status.busy":"2021-11-06T08:13:59.943668Z","iopub.status.idle":"2021-11-06T08:13:59.944232Z","shell.execute_reply.started":"2021-11-06T08:13:59.944058Z","shell.execute_reply":"2021-11-06T08:13:59.944083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_vol_graph(13, volatilities, periods)","metadata":{"execution":{"iopub.status.busy":"2021-11-06T08:13:59.945073Z","iopub.status.idle":"2021-11-06T08:13:59.945496Z","shell.execute_reply.started":"2021-11-06T08:13:59.945336Z","shell.execute_reply":"2021-11-06T08:13:59.945353Z"},"trusted":true},"execution_count":null,"outputs":[]}]}