Created
October 30, 2023 08:59
-
-
Save firmai/514840e25d2491717ff4c26daebac0eb to your computer and use it in GitHub Desktop.
multiple-label-alternatives.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"authorship_tag": "ABX9TyNONstWofDjDfzs2VTqmzyX", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/firmai/514840e25d2491717ff4c26daebac0eb/multiple-label-alternatives.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"Return Horizon (Eg., 14-day return)" | |
], | |
"metadata": { | |
"id": "3fqyHLXxcnk-" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "kR2hXUvxcliN" | |
}, | |
"outputs": [], | |
"source": [ | |
"def return_features(df_pricing, windows=[1,7,14], feature=None):\n", | |
" for w in windows:\n", | |
" df_pricing[f\"return_{feature}_{w}\"] = df_pricing[feature].pct_change(w)\n", | |
" return df_pricing" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"window_returns = [1,3,7,14,28,56,112,224,448]\n", | |
"df_pricing = return_features(df_pricing, windows=window_returns, feature=\"closeadj\")\n", | |
"df_pricing = return_features(df_pricing, windows=window_returns, feature=\"volume\")" | |
], | |
"metadata": { | |
"id": "VKDMUqmtcw6P" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"Return Lag (E.g., 14-day return lag 4)" | |
], | |
"metadata": { | |
"id": "leiGv6NWcprR" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"## rolling returns are correlated, create lags\n", | |
"feature = \"closeadj\"\n", | |
"for return_type in [1,3,7,14,28,56,112]:\n", | |
" for lag in [1,2,3,4]:\n", | |
" df_pricing[f\"return_{feature}_{return_type}_lag_{lag}\"] = df_pricing[f'return_{feature}_{return_type}'].shift(return_type * lag)" | |
], | |
"metadata": { | |
"id": "lcE9gZsecytv" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"Rolling Return/Standard Deviation (Eg. 14-day_lag_4_rolling_8_mean/14-day_lag_4_rolling_8_std)" | |
], | |
"metadata": { | |
"id": "33xaeln1dGtB" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"def col_join(df, win):\n", | |
" df.columns = [\"_\".join(x) for x in df.columns.ravel()]\n", | |
" return df.add_suffix(\"_rolling_{}\".format(win))\n", | |
"\n", | |
"def rolling_features(df_daily, features=[\"Close\"], windows=[7,14], functions=[\"mean\",\"std\"], method=False, ticker=False):\n", | |
"\n", | |
" if method == \"Fast\":\n", | |
" rolling_dfs = [df_daily[features].rolling(i) # 1. Create rolling window\n", | |
" .agg(functions, engine='numba').reset_index(drop=True) # 2. Apply function\n", | |
" for i in windows]\n", | |
"\n", | |
" rolling_dfs = [col_join(df, win) for df, win in zip(rolling_dfs,windows)] # piece of code to create pretty column names\n", | |
"\n", | |
" df_daily = pd.concat((df_daily.reset_index(drop=True), *rolling_dfs), axis=1)\n", | |
" return df_daily\n", | |
"\n" | |
], | |
"metadata": { | |
"id": "v0mGrgStdI5j" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"ticker = \"ticker\"\n", | |
"windows = [8]\n", | |
"columns = [\"return_closeadj_14_lag_4\"]\n", | |
"functions=[\"mean\",\"std\"]\n", | |
"\n", | |
"df_pricing_features = rolling_features(df_pricing, features=columns, windows=windows, functions=functions, method=\"Fast\", ticker=ticker)\n", | |
"\n", | |
"df_pricing_features[f\"std_adjusted_return_14\"] = df_pricing_features[\"return_closeadj_14_lag_4_rolling_8_mean\"]/df_pricing_features[\"return_closeadj_14_lag_4_rolling_8_std\"].\n" | |
], | |
"metadata": { | |
"id": "manLsCP1dUQt" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment