diff --git a/week1/community-contributions/day1-finviz_stock_analysis.ipynb b/week1/community-contributions/day1-finviz_stock_analysis.ipynb new file mode 100644 index 0000000..4165bde --- /dev/null +++ b/week1/community-contributions/day1-finviz_stock_analysis.ipynb @@ -0,0 +1,159 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "922bb144", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import requests\n", + "from dotenv import load_dotenv\n", + "from bs4 import BeautifulSoup\n", + "from IPython.display import Markdown, display\n", + "from openai import OpenAI" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "870bdcd9", + "metadata": {}, + "outputs": [], + "source": [ + "# Load environment variables in a file called .env\n", + "load_dotenv(override=True)\n", + "api_key = os.getenv(\"OPENAI_API_KEY\")\n", + "\n", + "# Check the key\n", + "if not api_key:\n", + " print(\"No API key was found - please head over to the troubleshooting notebook in this folder to identify & fix!\")\n", + "elif not api_key.startswith(\"sk-proj-\"):\n", + " print(\"An API key was found, but it doesn't start sk-proj-; please check you're using the right key - see troubleshooting notebook\")\n", + "elif api_key.strip() != api_key:\n", + " print(\"An API key was found, but it looks like it might have space or tab characters at the start or end - please remove them - see troubleshooting notebook\")\n", + "else:\n", + " print(\"API key found and looks good so far!\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f6146102", + "metadata": {}, + "outputs": [], + "source": [ + "openai = OpenAI()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2f75573f", + "metadata": {}, + "outputs": [], + "source": [ + "class FinvizWebsite():\n", + " \"\"\"\n", + " Create this Website object from the given url using the BeautifulSoup library\n", + " \"\"\"\n", + " \n", + " def __init__(self, ticker):\n", + " self.ticker = ticker.upper()\n", + " self.url = f\"https://finviz.com/quote.ashx?t={self.ticker}&p=d&ty=ea\"\n", + " self.headers = {\n", + " \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36\"\n", + " }\n", + " response = requests.get(self.url, headers=self.headers)\n", + " soup = BeautifulSoup(response.content, \"html.parser\")\n", + " self.title = soup.title.string if soup.title else \"No title found\"\n", + " self.table = soup.find(\"table\", class_=\"snapshot-table2\") " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "42c7ced6", + "metadata": {}, + "outputs": [], + "source": [ + "def messages_for(website):\n", + " system_prompt = \"\"\"\n", + " You are a financial analysis assistant that analyzes the contents of HTML formated table.\n", + " and provides a summary of the stock's analysis with clear and professional language appropriate for financial research \n", + " with bulleted important list of **pros** and **cons** , ignoring text that might be navigation related. Repond in markdown.\n", + " \"\"\"\n", + " \n", + " user_prompt = f\"\"\"\n", + " You are looking at a website titled {website.title}.\\n\n", + " The contents of this website is as follows; please provide a summary of the stock's analysis from this website in markdown.\\n\\n\n", + " {website.table}\n", + " \"\"\"\n", + " \n", + " return [\n", + " {\"role\": \"system\", \"content\": system_prompt},\n", + " {\"role\": \"user\", \"content\": user_prompt}\n", + " ]\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7bfaa6da", + "metadata": {}, + "outputs": [], + "source": [ + "def display_summary(ticker):\n", + " website = FinvizWebsite(ticker)\n", + " response = openai.chat.completions.create(\n", + " model = \"gpt-4o-mini\",\n", + " messages = messages_for(website)\n", + " )\n", + " summary = response.choices[0].message.content\n", + " display(Markdown(summary))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "eeeff6f7", + "metadata": {}, + "outputs": [], + "source": [ + "display_summary(\"aapl\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5aed2001", + "metadata": {}, + "outputs": [], + "source": [ + "display_summary(\"tsla\")" + ] + } + ], + "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.12.10" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}