Skip to content

Commit

Permalink
Create stock_selection.ipynb
Browse files Browse the repository at this point in the history
  • Loading branch information
BruceYanghy committed Oct 23, 2022
1 parent 2884984 commit 1a6a261
Showing 1 changed file with 200 additions and 0 deletions.
200 changes: 200 additions & 0 deletions stock_selection.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import time\n",
"import os"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"sectors = range(10, 65, 5)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"#gvkey is unique identifier\n",
"df_dict = {'gvkey':[], 'predicted_return':[], 'trade_date':[]}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"start=time.time()\n",
"for sector in sectors:\n",
" os.system(f\"python3 fundamental_run_model.py -sector_name sector{sector} -tic_column gvkey -fundamental final_ratios.csv -sector sector{sector}.xlsx \")\n",
" df = pd.read_csv(f\"results/sector{sector}/df_predict_best.csv\", index_col=0)\n",
" for idx in df.index:\n",
" predicted_return = df.loc[idx]\n",
" top_q = predicted_return.quantile(0.75)\n",
" predicted_return = predicted_return[predicted_return >= top_q]\n",
" for gvkey in predicted_return.index:\n",
" df_dict[\"gvkey\"].append(gvkey)\n",
" df_dict[\"predicted_return\"].append(predicted_return[gvkey])\n",
" df_dict[\"trade_date\"].append(idx)\n",
"end=time.time()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"it took 0.40141645272572835 minutes\n"
]
}
],
"source": [
"print(\"it took \", (end-start)/60, ' minutes')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"df_result = pd.DataFrame(df_dict)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"df_result.to_csv(\"stock_selected.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>gvkey</th>\n",
" <th>predicted_return</th>\n",
" <th>trade_date</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2991</td>\n",
" <td>-0.000871</td>\n",
" <td>2001-03-01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>4430</td>\n",
" <td>0.001057</td>\n",
" <td>2001-03-01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>7017</td>\n",
" <td>-0.001656</td>\n",
" <td>2001-03-01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>7912</td>\n",
" <td>0.002009</td>\n",
" <td>2001-03-01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>8068</td>\n",
" <td>-0.000118</td>\n",
" <td>2001-03-01</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" gvkey predicted_return trade_date\n",
"0 2991 -0.000871 2001-03-01\n",
"1 4430 0.001057 2001-03-01\n",
"2 7017 -0.001656 2001-03-01\n",
"3 7912 0.002009 2001-03-01\n",
"4 8068 -0.000118 2001-03-01"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_result.head()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.7"
},
"vscode": {
"interpreter": {
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 1a6a261

Please sign in to comment.