File size: 2,816 Bytes
9318644
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a055c55
9318644
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from agno.agent import Agent
from agno.models.groq import Groq
from agno.tools.yfinance import YFinanceTools
from agno.tools.duckduckgo import DuckDuckGoTools
import os
import groq
from dotenv import load_dotenv
import pandas as pd
import matplotlib.pyplot as plt
from textblob import TextBlob
import yfinance as yf

load_dotenv()
groq.api_key = os.getenv("GROQ_API_KEY")

class FinanceAgent:
    def __init__(self):
        self.agent = Agent(
            name="Finance AI Agent",
            model=Groq(id="qwen-qwq-32b"),
            tools=[
                YFinanceTools(
                    stock_price=True,
                    analyst_recommendations=True,
                    stock_fundamentals=True,
                    company_news=True
                ),
            ],
            instructions=["Use tables to display the data", "Always include sources"],
            show_tool_calls=True,
            markdown=True,
        )

    def get_analyst_recommendations_and_news(self, ticker_symbol):
        response = self.agent.run(
            f"Summarize analyst recommendations and share the latest news for {ticker_symbol}", stream=False
        )
        return response

    def get_stock_fundamentals(self, ticker_symbol):
        response = self.agent.run(
            f"Fetch stock fundamentals and key financial metrics for {ticker_symbol}", stream=False
        )
        return response

    def get_historical_price_data(self, ticker_symbol):
        company = yf.Ticker(ticker_symbol)
        hist_data = company.history(period="6mo")
        return hist_data

    def plot_historical_prices(self, hist_data, ticker_symbol):
        plt.figure(figsize=(10, 5))
        plt.plot(hist_data.index, hist_data['Close'], label=f"{ticker_symbol} Close Price", color="blue")
        plt.title(f"{ticker_symbol} Historical Closing Prices (Last 6 Months)")
        plt.xlabel("Date")
        plt.ylabel("Price (USD)")
        plt.legend()
        plt.grid(True)
        plt.show()
    # Add this method to the FinanceAgent class

  # Replace the generate_comprehensive_report method with this implementation

# Add this method to the FinanceAgent class

    def generate_comprehensive_report(self, ticker_symbol):
        prompt = f"""
        Generate a comprehensive financial report for {ticker_symbol} that includes:
        1. Company overview and recent performance
        2. Key financial metrics and stock fundamentals
        3. Detailed analyst recommendations with consensus rating
        4. Recent significant news and their impact on the stock
        5. Technical analysis of price trends
        6. Potential risks and opportunities

        Format as a professional report with clear sections.
        """
        response = self.agent.run(prompt, stream=False)
        return response