Spaces:
Sleeping
Sleeping
Commit ·
fecb358
1
Parent(s): 2a02c57
stock_analysis_agent: use response format; tuned prompt
Browse files8 unmodified lines9101112131438 unmodified lines53545556575838 unmodified lines97989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813916 unmodified lines1561571581591601611628 unmodified linesfrom langgraph.prebuilt import ToolNode, tools_conditionfrom langgraph.graph.message import add_messagesimport loggingfrom src.technical_analysis import TechnicalAnalysisfrom src.fundamental_analysis import FundamentalAnalysis38 unmodified lineselse:return f"Error fetching financial metrics for ticker: {ticker}"class StockAnalyst():def __init__(38 unmodified linesstock_analyst_prompt= """You are a stock analyst specializing in evaluating the performance of a given company (whose symbol is {company})based on recent price data and technical indicators as well as financial metrics.Your task is to provide a comprehensive summary of the technical and fundamental analysis for a given stock,and based on the analysis, choose an action among the following options:- strong sell: there are overwhelmingly bad signals for the stock, sell large quantities- sell: there are some bad signals for the stock, sell medium quantities- hold: there are either neutral signals, or good signals mixed with bad signals for the stock, hold- buy: there are some good signals for the stock, buy medium quantities- strong buy: there are overwhelmingly good signals for the stock, buy large quantitiesYou have access to the following tools:1. **get_stock_prices**: Retrieves the latest stock price, historical price data and technical Indicators like VWAP, RSI, Stochastic Oscillator and MACD metrics.2. **get_financial_metrics**: Retrieves key financial metrics, such as revenue, earnings per share (EPS), price-to-earnings ratio (P/E), and debt-to-equity ratio.### Your Task:1. **Input Stock Symbol**: Use the provided stock symbol to query the tools and gather the relevant information.2. **Analyze Data**: Evaluate the results from the tools and identify potential resistance, key trends, strengths, or concerns.3. **Provide Summary**: Write a concise, well-structured summary that highlights:- Recent stock price movements, trends and potential resistance.- Key insights from technical indicators (e.g., whether the stock is overbought or oversold).- Financial health and performance based on financial metrics.### Constraints:- Use only the data provided by the tools.- If any tool fails to provide data, clearly state that in your summary.- Avoid speculative language; focus on observable data and trends.- Ensure that your response is objective, concise, and actionable.### Output Format:Respond in the following format:"Stock": "<Stock symbol>","Price analysis": "<Detailed analysis of stock price trends>","Technical analysis": "<Detailed analysis of technical indicators>","Fundamental analysis": "<Detailed analysis of financial metrics>","Final summary": "<Conclusive summary of the analyses above>""Recommended action": "<Suggested action based on the above analyses among options: [strong sell, sell, hold, buy, strong buy]>""""return stock_analyst_prompt.format(company=company)def build_graph(self) -> StateGraph:16 unmodified linestools = [get_stock_prices, get_financial_metrics]llm = ChatOpenAI(model='gpt-4o-mini')llm_with_tool = llm.bind_tools(tools)def stock_analyst(state: State):8 unmodified lines91011121314151638 unmodified lines555657585960616263646566676838 unmodified lines10710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214316 unmodified lines1601611621631641651661671681698 unmodified linesfrom langgraph.prebuilt import ToolNode, tools_conditionfrom langgraph.graph.message import add_messagesimport loggingfrom pydantic import BaseModel, Fieldfrom src.technical_analysis import TechnicalAnalysisfrom src.fundamental_analysis import FundamentalAnalysis38 unmodified lineselse:return f"Error fetching financial metrics for ticker: {ticker}"class StockAnalysisResponse(BaseModel):"""Stock Analysis Response Schema"""stock: str = Field(description="Stock symbol")price_analysis: str = Field(description="Detailed analysis of stock price trends")technical_analysis: str = Field(description="Detailed analysis of technical indicators")technical_analysis: str = Field(description="Detailed analysis of financial metrics")final_summary: str = Field(description="Conclusive summary of the analyses above")recommended_action: str = Field(description="Suggested action based on the above analyses among options: [strong sell, sell, hold, buy, strong buy]")class StockAnalyst():def __init__(38 unmodified linesstock_analyst_prompt= """You are a stock analyst specializing in evaluating the performance of a given company (whose symbol is {company})based on recent price data and technical indicators as well as financial metrics.Your task is to provide a comprehensive summaries of price movements, technical and fundamental analysis for a given stock,and based on the analysis, provide receommended action (see below) for details.You have access to the following tools:1. **get_stock_prices**: Retrieves the latest stock price, historical price data and technical Indicators like VWAP, RSI, Stochastic Oscillator and MACD metrics.2. **get_financial_metrics**: Retrieves key financial metrics, such as revenue, earnings per share (EPS), price-to-earnings ratio (P/E), and debt-to-equity ratio.### Your Tasks:1. **Input Stock Symbol**: use the provided stock symbol to query the tools and gather the relevant information.2. **Analyze Data**: evaluate the results from the tools3. **Summarize and Synthesize**: in particular, we need:a) A summary of recent stock price movements, highlighting final available closing prices.b) A summary of trends and potential resistance.c) A summary of technical indicators (e.g., whether the stock is overbought or oversold).d) A summary of Financial health and performance based on financial metrics.e) A final, conclusive synthesis that highlights key concerns and strenghtsf) Recommended action among following options:- strong sell: if there are overwhelmingly bad signals- sell: if there are some bad signals- hold: there are either neutral signals, or good signals mixed with bad signals- buy: there are some good signals- strong buy: there are overwhelmingly good signals### Constraints:- Use only the data provided by the tools.- If any tool fails to provide data, clearly state that in your summary.- Avoid speculative language; focus on observable data and trends.- Ensure that your response is objective, concise, and actionable."""return stock_analyst_prompt.format(company=company)def build_graph(self) -> StateGraph:16 unmodified linestools = [get_stock_prices, get_financial_metrics]llm = ChatOpenAI(model='gpt-4o-mini')llm_with_tool = llm.bind_tools(tools,strict=True,response_format=StockAnalysisResponse)def stock_analyst(state: State):
src/stock_analysis_agent.py+33-26