diff --git a/Claude_Investor.ipynb b/Claude_Investor.ipynb index 55168b4..21d5477 100644 --- a/Claude_Investor.ipynb +++ b/Claude_Investor.ipynb @@ -1,26 +1,10 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "provenance": [], - "authorship_tag": "ABX9TyPuI82WtCRP7mS26mCp9B+j", - "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" + "colab_type": "text", + "id": "view-in-github" }, "source": [ "\"Open" @@ -28,15 +12,15 @@ }, { "cell_type": "markdown", + "metadata": { + "id": "gQWLeJCFS6C4" + }, "source": [ "## claude-investor\n", "By Matt Shumer (https://twitter.com/mattshumer_)\n", "\n", "Github repo: https://github.com/mshumer/gpt-investor" - ], - "metadata": { - "id": "gQWLeJCFS6C4" - } + ] }, { "cell_type": "code", @@ -51,17 +35,22 @@ }, { "cell_type": "code", - "source": [ - "ANTHROPIC_API_KEY = \"YOUR_API_KEY\" # Replace with your Anthropic API key" - ], + "execution_count": null, "metadata": { "id": "50ZEVdt53Xkr" }, - "execution_count": null, - "outputs": [] + "outputs": [], + "source": [ + "ANTHROPIC_API_KEY = \"YOUR_API_KEY\" # Replace with your Anthropic API key" + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "tg78Hup6xona" + }, + "outputs": [], "source": [ "import yfinance as yf\n", "from datetime import datetime, timedelta\n", @@ -74,62 +63,77 @@ "def get_article_text(url):\n", " try:\n", " response = requests.get(url)\n", + " response.raise_for_status() # Raise an exception for HTTP errors\n", " soup = BeautifulSoup(response.content, 'html.parser')\n", " article_text = ' '.join([p.get_text() for p in soup.find_all('p')])\n", " return article_text\n", - " except:\n", - " return \"Error retrieving article text.\"\n", + " except Exception as e:\n", + " print(f\"Error retrieving article text: {e}\")\n", + " return None\n", "\n", "def get_stock_data(ticker, years):\n", - " end_date = datetime.now().date()\n", - " start_date = end_date - timedelta(days=years*365)\n", + " try:\n", + " end_date = datetime.now().date()\n", + " start_date = end_date - timedelta(days=years*365)\n", "\n", - " stock = yf.Ticker(ticker)\n", + " stock = yf.Ticker(ticker)\n", "\n", - " # Retrieve historical price data\n", - " hist_data = stock.history(start=start_date, end=end_date)\n", + " # Retrieve historical price data\n", + " hist_data = stock.history(start=start_date, end=end_date)\n", "\n", - " # Retrieve balance sheet\n", - " balance_sheet = stock.balance_sheet\n", + " # Retrieve balance sheet\n", + " balance_sheet = stock.balance_sheet\n", "\n", - " # Retrieve financial statements\n", - " financials = stock.financials\n", + " # Retrieve financial statements\n", + " financials = stock.financials\n", "\n", - " # Retrieve news articles\n", - " news = stock.news\n", + " # Retrieve news articles\n", + " news = stock.news\n", "\n", - " return hist_data, balance_sheet, financials, news\n", + " return hist_data, balance_sheet, financials, news\n", + " except Exception as e:\n", + " print(f\"Error retrieving stock data for {ticker}: {e}\")\n", + " return None, None, None, None\n", "\n", "def get_claude_comps_analysis(ticker, hist_data, balance_sheet, financials, news):\n", - " system_prompt = f\"You are a financial analyst assistant. Analyze the given data for {ticker} and suggest a few comparable companies to consider. Do so in a Python-parseable list.\"\n", - "\n", - " news = \"\"\n", - "\n", - " for article in news:\n", - " article_text = get_article_text(article['link'])\n", - " news = news + f\"\\n\\n---\\n\\nTitle: {article['title']}\\nText: {article_text}\"\n", + " try:\n", + " system_prompt = f\"You are a financial analyst assistant. Analyze the given data for {ticker} and suggest a few comparable companies to consider. Do so in a Python-parseable list.\"\n", "\n", - " messages = [\n", - " {\"role\": \"user\", \"content\": f\"Historical price data:\\n{hist_data.tail().to_string()}\\n\\nBalance Sheet:\\n{balance_sheet.to_string()}\\n\\nFinancial Statements:\\n{financials.to_string()}\\n\\nNews articles:\\n{news.strip()}\\n\\n----\\n\\nNow, suggest a few comparable companies to consider, in a Python-parseable list. Return nothing but the list. Make sure the companies are in the form of their tickers.\"},\n", - " ]\n", + " news_content = \"\"\n", "\n", + " for article in news:\n", + " article_text = get_article_text(article['link'])\n", + " news_content = news_content + f\"\\n\\n---\\n\\nTitle: {article['title']}\\nText: {article_text}\"\n", "\n", - " headers = {\n", - " \"x-api-key\": ANTHROPIC_API_KEY,\n", - " \"anthropic-version\": \"2023-06-01\",\n", - " \"content-type\": \"application/json\"\n", - " }\n", - " data = {\n", - " \"model\": 'claude-3-haiku-20240307',\n", - " \"max_tokens\": 2000,\n", - " \"temperature\": 0.5,\n", - " \"system\": system_prompt,\n", - " \"messages\": messages,\n", - " }\n", - " response = requests.post(\"https://api.anthropic.com/v1/messages\", headers=headers, json=data)\n", - " response_text = response.json()['content'][0]['text']\n", + " messages = [\n", + " {\"role\": \"user\", \"content\": f\"Historical price data:\\n{hist_data.tail().to_string()}\\n\\nBalance Sheet:\\n{balance_sheet.to_string()}\\n\\nFinancial Statements:\\n{financials.to_string()}\\n\\nNews articles:\\n{news_content.strip()}\\n\\n----\\n\\nNow, suggest a few comparable companies to consider, in a Python-parseable list. Return nothing but the list. Make sure the companies are in the form of their tickers.\"},\n", + " ]\n", "\n", - " return ast.literal_eval(response_text)\n", + " headers = {\n", + " \"x-api-key\": ANTHROPIC_API_KEY,\n", + " \"anthropic-version\": \"2023-06-01\",\n", + " \"content-type\": \"application/json\"\n", + " }\n", + " data = {\n", + " \"model\": 'claude-3-haiku-20240307',\n", + " \"max_tokens\": 2000,\n", + " \"temperature\": 0.5,\n", + " \"system\": system_prompt,\n", + " \"messages\": messages,\n", + " }\n", + " response = requests.post(\"https://api.anthropic.com/v1/messages\", headers=headers, json=data)\n", + " \n", + " # Check if the response was successful\n", + " response.raise_for_status()\n", + " \n", + " response_text = response.json()['content'][0]['text']\n", + " return ast.literal_eval(response_text)\n", + " except requests.exceptions.RequestException as req_error:\n", + " print(f\"Request error occurred for {ticker}: {req_error}\")\n", + " return None\n", + " except Exception as e:\n", + " print(f\"Error occurred for {ticker}: {e}\")\n", + " return None\n", "\n", "def compare_companies(main_ticker, main_data, comp_ticker, comp_data):\n", " system_prompt = f\"You are a financial analyst assistant. Compare the data of {main_ticker} against {comp_ticker} and provide a detailed comparison, like a world-class analyst would. Be measured and discerning. Truly think about the positives and negatives of each company. Be sure of your analysis. You are a skeptical investor.\"\n", @@ -352,12 +356,23 @@ "ranking = rank_companies(industry, analyses, prices)\n", "print(f\"\\nRanking of Companies in the {industry} Industry:\")\n", "print(ranking)" - ], - "metadata": { - "id": "tg78Hup6xona" - }, - "execution_count": null, - "outputs": [] + ] + } + ], + "metadata": { + "colab": { + "authorship_tag": "ABX9TyPuI82WtCRP7mS26mCp9B+j", + "include_colab_link": true, + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + }, + "language_info": { + "name": "python" } - ] -} \ No newline at end of file + }, + "nbformat": 4, + "nbformat_minor": 0 +}