GitHub Repository: bauer-jan/stock-analysis-with-llm

This project aims to develop an automated system for stock analysis and portfolio management, using different data sources such as balance sheet data, technical indicators, news, industry information, and general market sentiment — powered by large language models (LLMs) like Claude 3 and leveraging the AWS Bedrock infrastructure.

Key Features

Stock Analyst Module

The system runs weekly stock analysis on the S&P 500, Nasdaq 100, and EURO STOXX 50, ranking each stock within its respective industry based on balance sheet information, technical indicators, and relevant news. The LLM ranks stocks within their industries, stores its reasoning, and outputs BUY/SELL recommendations for each stock. All results are stored in a database for further analysis and trend tracking.

Portfolio Manager Module

On a weekly basis, the portfolio manager updates holdings by buying or selling stocks based on the analyst’s recommendations and general market sentiment. It also allows user prompts to influence the selection and weighting of stocks in the portfolio. The portfolio is stored in a database for ongoing analysis and inspection.

Note: No real trades are executed. Stock data comes from the Yahoo Finance API.

Architecture Overview

The stock analysis application leverages various AWS services and external APIs. Here’s how the key components work together:

  1. The process is triggered by an AWS EventBridge event.
  2. The event initiates a task within AWS Elastic Container Service (ECS), which fetches earnings reports from the Yahoo Finance API.
  3. A prompt is sent to an Amazon Bedrock Agent, which performs web searches and summarizes key data points and relevant news for each stock.
  4. Amazon Bedrock Claude summarizes the news and relevant data gathered.
  5. Earnings reports, news, and industry benchmarks are sent to Amazon Bedrock Claude to rank stocks within their industries.
  6. The LLM’s recommendation, along with the summarized data, is saved in Amazon DynamoDB.
  7. The portfolio management process is triggered by a separate AWS EventBridge event.
  8. A prompt is sent to an Amazon Bedrock Agent to collect general market news.
  9. The LLM acts as a portfolio manager with the information provided by the analyst and updates the portfolio accordingly.

Example Usage

Comparing Individual Stocks

You can configure the system to compare individual stocks against each other and rank them by updating src/config.ini:

COMPARE_SYMBOLS_WITHOUT_INDUSTRY = true
INDEX_SYMBOLS = 
SYMBOLS = EVD.F,ENR.F

This produces a ranked output with explanations and investment decisions:

[
    {
     "name": "Siemens Energy AG",
     "rank": 1,
     "explanation": "Siemens Energy AG has shown impressive growth and a strong turnaround in profitability, reporting net income of 289 million euros in Q3 2024 compared to a loss in the previous year. The stock has quadrupled in price over 2024, hitting new all-time highs. Analysts have raised their price targets and maintain 'Buy' ratings, citing the company's positive outlook. With a forward P/E of 20.94 and a PEG ratio in line with industry averages, the stock appears undervalued given its growth potential in the renewable energy sector.",
     "investment_decision": "BUY"
    },
    {
     "name": "CTS Eventim AG & Co. KGaA",
     "rank": 2,
     "explanation": "CTS Eventim has reported strong financial results with revenue and EBITDA growth in the double digits for the first nine months of 2024. The company has secured major ticketing contracts like the 2024 Paris Olympics and 2026 Milan Winter Olympics. It is also expanding its live entertainment business with new arena projects. While the stock trades at a premium valuation with a forward P/E of 23.52, its growth prospects and leading market position in the events industry justify a 'Buy' rating.",
     "investment_decision": "BUY"
    }
]

Comparing All Stocks Within Indexes

You can also run the analysis across entire indexes. Note that this can result in higher costs when using Bedrock, due to the large number of stocks involved.

COMPARE_SYMBOLS_WITHOUT_INDUSTRY = false
INDEX_SYMBOLS = S&P 500,NASDAQ 100
SYMBOLS = 

This produces a ranking of each stock in the index against others in its industry.

Prompt Engineering

To improve the performance and behavior of the models, you can modify the prompts in src/schema/prompts.yaml.

Financial Data Collected

The project collects a comprehensive set of data for each stock, including financial performance metrics, valuation and market position data, governance and risk factors, industry comparisons, and company information.

References