Spaces:
Sleeping
Sleeping
Commit
·
7935906
1
Parent(s):
d90be64
stock_analysis_agent: include forecasts in the analyses
Browse files- src/stock_analysis_agent.py +29 -8
src/stock_analysis_agent.py
CHANGED
|
@@ -12,6 +12,7 @@ from langgraph.graph.message import add_messages
|
|
| 12 |
import logging
|
| 13 |
from pydantic import BaseModel, Field
|
| 14 |
|
|
|
|
| 15 |
from src.technical_analysis import TechnicalAnalysis
|
| 16 |
from src.fundamental_analysis import FundamentalAnalysis
|
| 17 |
from src.ticker_finder import TickerFinder
|
|
@@ -26,20 +27,37 @@ def get_stock_prices(
|
|
| 26 |
ticker: str
|
| 27 |
The stock ticker symbol to fetch data for.
|
| 28 |
"""
|
|
|
|
| 29 |
df, _ = TechnicalAnalysis(
|
| 30 |
ticker=ticker,
|
| 31 |
fetchperiodinweeks=12,
|
|
|
|
|
|
|
| 32 |
plot_ta=False,
|
| 33 |
savefig=False,
|
| 34 |
debug=False).run()
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
df['Date'] = df.index.astype(str)
|
| 37 |
# split the data into price and indicators, and take the last 10 days of data
|
| 38 |
-
|
| 39 |
indicators = df[['VWAP', 'RSI', 'StochOsc', 'MACD', 'MACDsig', 'MACDdif']].iloc[-10:,:].to_dict(orient='records')
|
| 40 |
-
|
| 41 |
-
|
| 42 |
return f"Error fetching technical data for ticker: {ticker}"
|
|
|
|
|
|
|
| 43 |
|
| 44 |
@tool
|
| 45 |
def get_financial_metrics(
|
|
@@ -62,6 +80,7 @@ class StockAnalysisResponse(BaseModel):
|
|
| 62 |
"""Stock Analysis Response Schema"""
|
| 63 |
stock: str = Field(description="Stock symbol")
|
| 64 |
price_analysis: str = Field(description="Detailed analysis of stock price trends")
|
|
|
|
| 65 |
technical_analysis: str = Field(description="Detailed analysis of technical indicators")
|
| 66 |
fundamental_analysis: str = Field(description="Detailed analysis of financial metrics")
|
| 67 |
final_summary: str = Field(description="Conclusive summary of the analyses above")
|
|
@@ -117,7 +136,7 @@ class StockAnalyst():
|
|
| 117 |
and based on the analysis, provide receommended action (see below) for details.
|
| 118 |
|
| 119 |
You have access to the following tools:
|
| 120 |
-
1. **get_stock_prices**: Retrieves the
|
| 121 |
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.
|
| 122 |
|
| 123 |
### Your Tasks:
|
|
@@ -127,9 +146,10 @@ class StockAnalyst():
|
|
| 127 |
a) A summary of recent stock price movements, highlighting final available closing prices.
|
| 128 |
b) A summary of trends and potential resistance.
|
| 129 |
c) A summary of technical indicators (e.g., whether the stock is overbought or oversold).
|
| 130 |
-
d) A summary of
|
| 131 |
-
e) A
|
| 132 |
-
f)
|
|
|
|
| 133 |
- strong sell: if there are overwhelmingly bad signals
|
| 134 |
- sell: if there are some bad signals
|
| 135 |
- hold: there are either neutral signals, or good signals mixed with bad signals
|
|
@@ -139,6 +159,7 @@ class StockAnalyst():
|
|
| 139 |
### Constraints:
|
| 140 |
- Use only the data provided by the tools.
|
| 141 |
- If any tool fails to provide data, clearly state that in your summary.
|
|
|
|
| 142 |
- Avoid speculative language; focus on observable data and trends.
|
| 143 |
- Ensure that your response is objective, concise, and actionable.
|
| 144 |
"""
|
|
|
|
| 12 |
import logging
|
| 13 |
from pydantic import BaseModel, Field
|
| 14 |
|
| 15 |
+
from src.fetch_forecast import FetchForecast
|
| 16 |
from src.technical_analysis import TechnicalAnalysis
|
| 17 |
from src.fundamental_analysis import FundamentalAnalysis
|
| 18 |
from src.ticker_finder import TickerFinder
|
|
|
|
| 27 |
ticker: str
|
| 28 |
The stock ticker symbol to fetch data for.
|
| 29 |
"""
|
| 30 |
+
df_past, df_fcst = FetchForecast(ticker).run()
|
| 31 |
df, _ = TechnicalAnalysis(
|
| 32 |
ticker=ticker,
|
| 33 |
fetchperiodinweeks=12,
|
| 34 |
+
df_past=df_past,
|
| 35 |
+
df_fcst=df_fcst,
|
| 36 |
plot_ta=False,
|
| 37 |
savefig=False,
|
| 38 |
debug=False).run()
|
| 39 |
+
|
| 40 |
+
if df_past is None:
|
| 41 |
+
fcst_prices = "Price forecasts could not be obtained"
|
| 42 |
+
fcst_returns = "Return forecasts could not be obtained"
|
| 43 |
+
else:
|
| 44 |
+
df_fcst['Date'] = df_fcst['Date'].astype(str)
|
| 45 |
+
fcst_prices = df_fcst[['Date','Close']].to_dict(orient='records')
|
| 46 |
+
fcst_returns = df_fcst[['Date','Returns']].to_dict(orient='records')
|
| 47 |
+
|
| 48 |
+
if df.shape[0] == 0:
|
| 49 |
+
hist_prices = "Recent price data could not be obtained"
|
| 50 |
+
indicators = "Indicator data could not be obtained"
|
| 51 |
+
else:
|
| 52 |
df['Date'] = df.index.astype(str)
|
| 53 |
# split the data into price and indicators, and take the last 10 days of data
|
| 54 |
+
hist_prices = df[['Date','Close', 'High', 'Low', 'Open', 'Volume']].iloc[-10:,:].to_dict(orient='records')
|
| 55 |
indicators = df[['VWAP', 'RSI', 'StochOsc', 'MACD', 'MACDsig', 'MACDdif']].iloc[-10:,:].to_dict(orient='records')
|
| 56 |
+
|
| 57 |
+
if (df_past is None) or (df.shape[0] == 0):
|
| 58 |
return f"Error fetching technical data for ticker: {ticker}"
|
| 59 |
+
else:
|
| 60 |
+
return {'recent prices': hist_prices, "forecasted prices": fcst_prices, "forecasted returns": fcst_returns, 'indicators': indicators}
|
| 61 |
|
| 62 |
@tool
|
| 63 |
def get_financial_metrics(
|
|
|
|
| 80 |
"""Stock Analysis Response Schema"""
|
| 81 |
stock: str = Field(description="Stock symbol")
|
| 82 |
price_analysis: str = Field(description="Detailed analysis of stock price trends")
|
| 83 |
+
forecast_analysis: str = Field(description="Detailed analysis of stock price forecasts")
|
| 84 |
technical_analysis: str = Field(description="Detailed analysis of technical indicators")
|
| 85 |
fundamental_analysis: str = Field(description="Detailed analysis of financial metrics")
|
| 86 |
final_summary: str = Field(description="Conclusive summary of the analyses above")
|
|
|
|
| 136 |
and based on the analysis, provide receommended action (see below) for details.
|
| 137 |
|
| 138 |
You have access to the following tools:
|
| 139 |
+
1. **get_stock_prices**: Retrieves the historical price data, technical indicators like VWAP, RSI, Stochastic Oscillator and MACD metrics, forecasted prices and relative returns for the next 5 business days.
|
| 140 |
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.
|
| 141 |
|
| 142 |
### Your Tasks:
|
|
|
|
| 146 |
a) A summary of recent stock price movements, highlighting final available closing prices.
|
| 147 |
b) A summary of trends and potential resistance.
|
| 148 |
c) A summary of technical indicators (e.g., whether the stock is overbought or oversold).
|
| 149 |
+
d) A summary of forecasted returns and closing prices for the next 5 business days.
|
| 150 |
+
e) A summary of Financial health and performance based on financial metrics.
|
| 151 |
+
f) A final, conclusive synthesis that highlights key concerns and strenghts
|
| 152 |
+
g) Recommended action among following options:
|
| 153 |
- strong sell: if there are overwhelmingly bad signals
|
| 154 |
- sell: if there are some bad signals
|
| 155 |
- hold: there are either neutral signals, or good signals mixed with bad signals
|
|
|
|
| 159 |
### Constraints:
|
| 160 |
- Use only the data provided by the tools.
|
| 161 |
- If any tool fails to provide data, clearly state that in your summary.
|
| 162 |
+
- Try to provide a balanced synthesis based on the data provided by the tools.
|
| 163 |
- Avoid speculative language; focus on observable data and trends.
|
| 164 |
- Ensure that your response is objective, concise, and actionable.
|
| 165 |
"""
|