Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified prompt to show Indian Stocks on .NS ticker #13

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
246 changes: 198 additions & 48 deletions Claude_Investor.ipynb
@@ -1,67 +1,177 @@
{
"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": [
"<a href=\"https://colab.research.google.com/github/mshumer/gpt-investor/blob/main/Claude_Investor.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"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",
"execution_count": null,
"execution_count": 4,
"metadata": {
"id": "rlKQLdG8v23g"
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: yfinance in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (0.2.37)\n",
"Requirement already satisfied: requests in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (2.31.0)\n",
"Requirement already satisfied: beautifulsoup4 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (4.12.3)\n",
"Requirement already satisfied: nsepy in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (0.8)\n",
"Requirement already satisfied: pandas>=1.3.0 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from yfinance) (2.2.0)\n",
"Requirement already satisfied: numpy>=1.16.5 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from yfinance) (1.26.3)\n",
"Requirement already satisfied: multitasking>=0.0.7 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from yfinance) (0.0.11)\n",
"Requirement already satisfied: lxml>=4.9.1 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from yfinance) (5.1.0)\n",
"Requirement already satisfied: appdirs>=1.4.4 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from yfinance) (1.4.4)\n",
"Requirement already satisfied: pytz>=2022.5 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from yfinance) (2023.3.post1)\n",
"Requirement already satisfied: frozendict>=2.3.4 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from yfinance) (2.4.0)\n",
"Requirement already satisfied: peewee>=3.16.2 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from yfinance) (3.17.1)\n",
"Requirement already satisfied: html5lib>=1.1 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from yfinance) (1.1)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from requests) (3.3.2)\n",
"Requirement already satisfied: idna<4,>=2.5 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from requests) (3.6)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from requests) (2.1.0)\n",
"Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from requests) (2023.11.17)\n",
"Requirement already satisfied: soupsieve>1.2 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from beautifulsoup4) (2.5)\n",
"Requirement already satisfied: six in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from nsepy) (1.16.0)\n",
"Requirement already satisfied: click in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from nsepy) (8.1.7)\n",
"Requirement already satisfied: webencodings in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from html5lib>=1.1->yfinance) (0.5.1)\n",
"Requirement already satisfied: python-dateutil>=2.8.2 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from pandas>=1.3.0->yfinance) (2.8.2)\n",
"Requirement already satisfied: tzdata>=2022.7 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from pandas>=1.3.0->yfinance) (2023.4)\n",
"Requirement already satisfied: colorama in c:\\users\\administrator\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (from click->nsepy) (0.4.6)\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n",
"[notice] A new release of pip is available: 23.3.2 -> 24.0\n",
"[notice] To update, run: python.exe -m pip install --upgrade pip\n"
]
}
],
"source": [
"!pip install yfinance requests beautifulsoup4"
"!pip install yfinance requests beautifulsoup4 nsepy"
]
},
{
"cell_type": "code",
"source": [
"ANTHROPIC_API_KEY = \"YOUR_API_KEY\" # Replace with your Anthropic API key"
],
"execution_count": 2,
"metadata": {
"id": "50ZEVdt53Xkr"
},
"execution_count": null,
"outputs": []
"outputs": [],
"source": [
"ANTHROPIC_API_KEY = \"\" # Replace with your Anthropic API key"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"id": "tg78Hup6xona"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Status Code: 200\n",
"Response Content: {\"id\":\"msg_01MYx15ovtpF4HndyWYoiX83\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"type\":\"text\",\"text\":\"['TATASTEEL.NS', 'SAIL.NS', 'HINDALCO.NS', 'JINDAL STEEL.NS', 'NMDC.NS']\"}],\"model\":\"claude-3-haiku-20240307\",\"stop_reason\":\"end_turn\",\"stop_sequence\":null,\"usage\":{\"input_tokens\":103,\"output_tokens\":45}}\n",
"\n",
"Ticker Ideas for Metal Industry:\n",
"TATASTEEL.NS, SAIL.NS, HINDALCO.NS, JINDAL STEEL.NS, NMDC.NS\n",
"\n",
"Analyzing TATASTEEL.NS...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"404 Client Error: Not Found for url: https://query2.finance.yahoo.com/v10/finance/quoteSummary/TATASTEEL.NS?modules=recommendationTrend&corsDomain=finance.yahoo.com&formatted=false&symbol=TATASTEEL.NS&crumb=DS0wco8y2%2FQ\n",
"C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_6132\\2337801615.py:242: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
" return data['Close'][-1]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Analyzing SAIL.NS...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_6132\\2337801615.py:242: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
" return data['Close'][-1]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Analyzing HINDALCO.NS...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"404 Client Error: Not Found for url: https://query2.finance.yahoo.com/v10/finance/quoteSummary/HINDALCO.NS?modules=recommendationTrend&corsDomain=finance.yahoo.com&formatted=false&symbol=HINDALCO.NS&crumb=DS0wco8y2%2FQ\n",
"C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_6132\\2337801615.py:242: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
" return data['Close'][-1]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Analyzing JINDAL STEEL.NS...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"JINDAL STEEL.NS: No timezone found, symbol may be delisted\n",
"404 Client Error: Not Found for url: https://query2.finance.yahoo.com/v10/finance/quoteSummary/JINDAL%20STEEL.NS?modules=recommendationTrend&corsDomain=finance.yahoo.com&formatted=false&symbol=JINDAL+STEEL.NS&crumb=DS0wco8y2%2FQ\n",
"404 Client Error: Not Found for url: https://query2.finance.yahoo.com/v10/finance/quoteSummary/JINDAL%20STEEL.NS?modules=financialData%2CquoteType%2CdefaultKeyStatistics%2CassetProfile%2CsummaryDetail&corsDomain=finance.yahoo.com&formatted=false&symbol=JINDAL+STEEL.NS&crumb=DS0wco8y2%2FQ\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Analyzing NMDC.NS...\n"
]
}
],
"source": [
"import yfinance as yf\n",
"from datetime import datetime, timedelta\n",
Expand All @@ -81,6 +191,7 @@
" return \"Error retrieving article text.\"\n",
"\n",
"def get_stock_data(ticker, years):\n",
" \n",
" end_date = datetime.now().date()\n",
" start_date = end_date - timedelta(days=years*365)\n",
"\n",
Expand Down Expand Up @@ -258,15 +369,16 @@
"\n",
" return response_text\n",
"\n",
"def generate_ticker_ideas(industry):\n",
" system_prompt = f\"You are a financial analyst assistant. Generate a list of 5 ticker symbols for major companies in the {industry} industry, as a Python-parseable list.\"\n",
"def generate_ticker_ideas(industry, country, stock_exchange):\n",
" # Adjusted prompt to include user-specified country and stock exchange\n",
" system_prompt = f\"You are a financial analyst assistant with knowledge of global markets. Generate a list of 5 ticker symbols for major companies in the {industry} industry within {country}, focusing on companies listed on the {stock_exchange}. Provide the list as a Python-parseable list.\"\n",
"\n",
" messages = [\n",
" {\"role\": \"user\", \"content\": f\"Please provide a list of 5 ticker symbols for major companies in the {industry} industry as a Python-parseable list. Only respond with the list, no other text.\"},\n",
" {\"role\": \"user\", \"content\": f\"Considering the {industry} industry in {country}, particularly on the {stock_exchange}, please provide a list of 5 major company ticker symbols as a Python-parseable list. Restrict your response to this list without additional commentary.\"},\n",
" ]\n",
"\n",
" headers = {\n",
" \"x-api-key\": ANTHROPIC_API_KEY,\n",
" \"x-api-key\": ANTHROPIC_API_KEY,\n",
" \"anthropic-version\": \"2023-06-01\",\n",
" \"content-type\": \"application/json\"\n",
" }\n",
Expand All @@ -277,11 +389,27 @@
" \"system\": system_prompt,\n",
" \"messages\": messages,\n",
" }\n",
" \n",
" # Replace 'requests.post' with your method of sending HTTP requests\n",
" response = requests.post(\"https://api.anthropic.com/v1/messages\", headers=headers, json=data)\n",
" response_text = response.json()['content'][0]['text']\n",
"\n",
" ticker_list = ast.literal_eval(response_text)\n",
" return [ticker.strip() for ticker in ticker_list]\n",
" \n",
" # Logging for clarity and debugging\n",
" print(\"Status Code:\", response.status_code)\n",
" print(\"Response Content:\", response.text)\n",
" \n",
" if response.status_code == 200:\n",
" try:\n",
" response_json = response.json()\n",
" response_text = response_json['content'][0]['text']\n",
" # Assuming the response format is a list of strings representing ticker symbols\n",
" ticker_list = ast.literal_eval(response_text)\n",
" return [ticker.strip() for ticker in ticker_list]\n",
" except (ValueError, KeyError) as e:\n",
" print(f\"Error processing response: {e}\")\n",
" return []\n",
" else:\n",
" print(\"Failed to fetch data. Check the logs for details.\")\n",
" return []\n",
"\n",
"def get_current_price(ticker):\n",
" stock = yf.Ticker(ticker)\n",
Expand Down Expand Up @@ -319,10 +447,12 @@
"\n",
"# User input\n",
"industry = input(\"Enter the industry to analyze: \")\n",
"years = 1 # int(input(\"Enter the number of years for analysis: \"))\n",
"years = int(input(\"Enter the number of years for analysis: \"))\n",
"country = input(\"Enter the country of interest: \")\n",
"stock_exchange = input(\"Enter the stock exchange of interest (e.g., NSE for National Stock Exchange of India): \")\n",
"\n",
"# Generate ticker ideas for the industry\n",
"tickers = generate_ticker_ideas(industry)\n",
"# Now, when you call the generate_ticker_ideas function, you pass these variables as arguments:\n",
"tickers = generate_ticker_ideas(industry, country, stock_exchange)\n",
"print(f\"\\nTicker Ideas for {industry} Industry:\")\n",
"print(\", \".join(tickers))\n",
"\n",
Expand Down Expand Up @@ -352,12 +482,32 @@
"ranking = rank_companies(industry, analyses, prices)\n",
"print(f\"\\nRanking of Companies in the {industry} Industry:\")\n",
"print(ranking)"
],
"metadata": {
"id": "tg78Hup6xona"
]
}
],
"metadata": {
"colab": {
"authorship_tag": "ABX9TyPuI82WtCRP7mS26mCp9B+j",
"include_colab_link": true,
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"execution_count": null,
"outputs": []
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.0"
}
]
}
},
"nbformat": 4,
"nbformat_minor": 0
}