-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Francisco Silva
committed
Nov 25, 2024
1 parent
6238ed2
commit f9aad58
Showing
6 changed files
with
188 additions
and
30 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,3 +173,6 @@ log/ | |
|
||
# model files | ||
*.pkl | ||
|
||
# reports | ||
reports/report_* |
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from pathlib import Path\n", | ||
"\n", | ||
"import polars as pl\n", | ||
"\n", | ||
"from stocksense.database_handler import DatabaseHandler\n", | ||
"\n", | ||
"REPORT_DIR = Path(\"../reports\")\n", | ||
"\n", | ||
"DATE = \"2023-06-01\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"\u001b[32m2024-11-25 15:15:00.626\u001b[0m | \u001b[32m\u001b[1mSUCCESS \u001b[0m | \u001b[36mstocksense.database_handler.schema\u001b[0m:\u001b[36mcreate_tables\u001b[0m:\u001b[36m122\u001b[0m - \u001b[32m\u001b[1mTables created successfully\u001b[0m\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<div><style>\n", | ||
".dataframe > thead > tr,\n", | ||
".dataframe > tbody > tr {\n", | ||
" text-align: right;\n", | ||
" white-space: pre-wrap;\n", | ||
"}\n", | ||
"</style>\n", | ||
"<small>shape: (467, 6)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>tic</th><th>score</th><th>name</th><th>sector</th><th>freturn</th><th>adj_freturn</th></tr><tr><td>str</td><td>f64</td><td>str</td><td>str</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>"PARA"</td><td>0.762556</td><td>"Paramount Global"</td><td>"Communication Services"</td><td>-21.502723</td><td>-46.531975</td></tr><tr><td>"FICO"</td><td>0.691881</td><td>"Fair Isaac"</td><td>"Information Technology"</td><td>62.615358</td><td>37.586106</td></tr><tr><td>"KEY"</td><td>0.6825847</td><td>"KeyCorp"</td><td>"Financials"</td><td>59.400882</td><td>34.37163</td></tr><tr><td>"FSLR"</td><td>0.668008</td><td>"First Solar"</td><td>"Information Technology"</td><td>30.190669</td><td>5.161417</td></tr><tr><td>"MPWR"</td><td>0.654031</td><td>"Monolithic Power Systems"</td><td>"Information Technology"</td><td>48.461755</td><td>23.432502</td></tr><tr><td>…</td><td>…</td><td>…</td><td>…</td><td>…</td><td>…</td></tr><tr><td>"DUK"</td><td>0.148876</td><td>"Duke Energy"</td><td>"Utilities"</td><td>22.819521</td><td>-2.209732</td></tr><tr><td>"PPL"</td><td>0.1465794</td><td>"PPL Corporation"</td><td>"Utilities"</td><td>16.684222</td><td>-8.34503</td></tr><tr><td>"ED"</td><td>0.137104</td><td>"Consolidated Edison"</td><td>"Utilities"</td><td>6.764106</td><td>-18.265146</td></tr><tr><td>"DTE"</td><td>0.125249</td><td>"DTE Energy"</td><td>"Utilities"</td><td>12.616302</td><td>-12.41295</td></tr><tr><td>"SO"</td><td>0.11732</td><td>"Southern Company"</td><td>"Utilities"</td><td>20.565807</td><td>-4.463445</td></tr></tbody></table></div>" | ||
], | ||
"text/plain": [ | ||
"shape: (467, 6)\n", | ||
"┌──────┬───────────┬──────────────────────────┬────────────────────────┬────────────┬─────────────┐\n", | ||
"│ tic ┆ score ┆ name ┆ sector ┆ freturn ┆ adj_freturn │\n", | ||
"│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n", | ||
"│ str ┆ f64 ┆ str ┆ str ┆ f64 ┆ f64 │\n", | ||
"╞══════╪═══════════╪══════════════════════════╪════════════════════════╪════════════╪═════════════╡\n", | ||
"│ PARA ┆ 0.762556 ┆ Paramount Global ┆ Communication Services ┆ -21.502723 ┆ -46.531975 │\n", | ||
"│ FICO ┆ 0.691881 ┆ Fair Isaac ┆ Information Technology ┆ 62.615358 ┆ 37.586106 │\n", | ||
"│ KEY ┆ 0.6825847 ┆ KeyCorp ┆ Financials ┆ 59.400882 ┆ 34.37163 │\n", | ||
"│ FSLR ┆ 0.668008 ┆ First Solar ┆ Information Technology ┆ 30.190669 ┆ 5.161417 │\n", | ||
"│ MPWR ┆ 0.654031 ┆ Monolithic Power Systems ┆ Information Technology ┆ 48.461755 ┆ 23.432502 │\n", | ||
"│ … ┆ … ┆ … ┆ … ┆ … ┆ … │\n", | ||
"│ DUK ┆ 0.148876 ┆ Duke Energy ┆ Utilities ┆ 22.819521 ┆ -2.209732 │\n", | ||
"│ PPL ┆ 0.1465794 ┆ PPL Corporation ┆ Utilities ┆ 16.684222 ┆ -8.34503 │\n", | ||
"│ ED ┆ 0.137104 ┆ Consolidated Edison ┆ Utilities ┆ 6.764106 ┆ -18.265146 │\n", | ||
"│ DTE ┆ 0.125249 ┆ DTE Energy ┆ Utilities ┆ 12.616302 ┆ -12.41295 │\n", | ||
"│ SO ┆ 0.11732 ┆ Southern Company ┆ Utilities ┆ 20.565807 ┆ -4.463445 │\n", | ||
"└──────┴───────────┴──────────────────────────┴────────────────────────┴────────────┴─────────────┘" | ||
] | ||
}, | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"stock_info = DatabaseHandler().fetch_stock()\n", | ||
"df = pl.read_csv(REPORT_DIR / f\"report_{DATE}.csv\")\n", | ||
"df = df.join(stock_info, on=\"tic\", how=\"left\")\n", | ||
"df = df.select(pl.col(\"tic\", \"score\", \"name\", \"sector\", \"freturn\", \"adj_freturn\"))\n", | ||
"df" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 11, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"\n", | ||
"DATE 2023-06-01\n", | ||
"\n", | ||
"Top 15 stocks:\n", | ||
"Average freturn: 33.63%\n", | ||
"Average adj_freturn: 8.60%\n", | ||
"\n", | ||
"Bottom 15 stocks:\n", | ||
"Average freturn: 7.26%\n", | ||
"Average adj_freturn: -17.77%\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"n = 15\n", | ||
"top = df.head(n)\n", | ||
"bottom = df.tail(n)\n", | ||
"\n", | ||
"# Calculate average returns for top stocks\n", | ||
"top_freturn = top.select(pl.col(\"freturn\")).mean().item()\n", | ||
"top_adj_freturn = top.select(pl.col(\"adj_freturn\")).mean().item()\n", | ||
"\n", | ||
"# Calculate average returns for bottom stocks\n", | ||
"bottom_freturn = bottom.select(pl.col(\"freturn\")).mean().item()\n", | ||
"bottom_adj_freturn = bottom.select(pl.col(\"adj_freturn\")).mean().item()\n", | ||
"\n", | ||
"print(f\"\\nDATE {DATE}\")\n", | ||
"print(f\"\\nTop {n} stocks:\")\n", | ||
"print(f\"Average freturn: {top_freturn:.2f}%\")\n", | ||
"print(f\"Average adj_freturn: {top_adj_freturn:.2f}%\")\n", | ||
"print(f\"\\nBottom {n} stocks:\")\n", | ||
"print(f\"Average freturn: {bottom_freturn:.2f}%\")\n", | ||
"print(f\"Average adj_freturn: {bottom_adj_freturn:.2f}%\")" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": ".venv", | ||
"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.11.0" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
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
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
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