Abid Ali Awan
commited on
Commit
·
fa3d0f6
1
Parent(s):
563fd53
Enhance financial tools error handling: added fallback for stock price retrieval, improved exception handling for individual stock errors, and updated README formatting for clarity.
Browse files- README.md +4 -4
- agents/tools.py +46 -20
README.md
CHANGED
@@ -215,10 +215,10 @@ The system automatically detects your preference:
|
|
215 |
|
216 |
## Issues to Resolve
|
217 |
The problem is in the current flow:
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
|
223 |
## 🤝 Contributing
|
224 |
|
|
|
215 |
|
216 |
## Issues to Resolve
|
217 |
The problem is in the current flow:
|
218 |
+
1. Agent executor runs all tools
|
219 |
+
2. We collect ALL results
|
220 |
+
3. Then display everything at once
|
221 |
+
4. Then stream the final response
|
222 |
|
223 |
## 🤝 Contributing
|
224 |
|
agents/tools.py
CHANGED
@@ -475,20 +475,41 @@ class FinancialTools:
|
|
475 |
symbol = holding["symbol"]
|
476 |
shares = holding["shares"]
|
477 |
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
"
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
492 |
|
493 |
# Calculate allocations
|
494 |
for item in portfolio_data:
|
@@ -498,7 +519,11 @@ class FinancialTools:
|
|
498 |
|
499 |
# Sector diversification
|
500 |
df = pd.DataFrame(portfolio_data)
|
501 |
-
|
|
|
|
|
|
|
|
|
502 |
|
503 |
analysis = {
|
504 |
"total_portfolio_value": f"${total_value:.2f}",
|
@@ -516,11 +541,12 @@ class FinancialTools:
|
|
516 |
"Consider diversifying with more holdings"
|
517 |
)
|
518 |
|
519 |
-
|
520 |
-
|
521 |
-
|
522 |
-
|
523 |
-
|
|
|
524 |
|
525 |
return json.dumps(analysis, indent=2)
|
526 |
|
|
|
475 |
symbol = holding["symbol"]
|
476 |
shares = holding["shares"]
|
477 |
|
478 |
+
try:
|
479 |
+
stock = yf.Ticker(symbol)
|
480 |
+
info = stock.info
|
481 |
+
current_price = info.get("currentPrice", 0)
|
482 |
+
|
483 |
+
# Fallback for current price if not available
|
484 |
+
if current_price == 0:
|
485 |
+
hist = stock.history(period="1d")
|
486 |
+
if not hist.empty:
|
487 |
+
current_price = hist["Close"].iloc[-1]
|
488 |
+
|
489 |
+
value = current_price * shares
|
490 |
+
total_value += value
|
491 |
+
|
492 |
+
portfolio_data.append(
|
493 |
+
{
|
494 |
+
"symbol": symbol,
|
495 |
+
"shares": shares,
|
496 |
+
"current_price": current_price,
|
497 |
+
"value": value,
|
498 |
+
"sector": info.get("sector", "Unknown"),
|
499 |
+
}
|
500 |
+
)
|
501 |
+
except Exception as e:
|
502 |
+
# Handle individual stock errors gracefully
|
503 |
+
portfolio_data.append(
|
504 |
+
{
|
505 |
+
"symbol": symbol,
|
506 |
+
"shares": shares,
|
507 |
+
"current_price": 0,
|
508 |
+
"value": 0,
|
509 |
+
"sector": "Unknown",
|
510 |
+
"error": f"Failed to fetch data: {str(e)}"
|
511 |
+
}
|
512 |
+
)
|
513 |
|
514 |
# Calculate allocations
|
515 |
for item in portfolio_data:
|
|
|
519 |
|
520 |
# Sector diversification
|
521 |
df = pd.DataFrame(portfolio_data)
|
522 |
+
# Handle case where portfolio_data is empty or sector column has issues
|
523 |
+
if not df.empty and "sector" in df.columns:
|
524 |
+
sector_allocation = df.groupby("sector")["allocation"].sum().to_dict()
|
525 |
+
else:
|
526 |
+
sector_allocation = {}
|
527 |
|
528 |
analysis = {
|
529 |
"total_portfolio_value": f"${total_value:.2f}",
|
|
|
541 |
"Consider diversifying with more holdings"
|
542 |
)
|
543 |
|
544 |
+
if portfolio_data:
|
545 |
+
max_allocation = max(item["allocation"] for item in portfolio_data)
|
546 |
+
if max_allocation > 30:
|
547 |
+
analysis["recommendations"].append(
|
548 |
+
f"High concentration risk: largest holding is {max_allocation:.1f}%"
|
549 |
+
)
|
550 |
|
551 |
return json.dumps(analysis, indent=2)
|
552 |
|