Research View
This commit is contained in:
Binary file not shown.
@@ -100,3 +100,45 @@ def delete_portfolio_item(item_id: int, db: Session = Depends(get_db), current_u
|
||||
db.delete(item)
|
||||
db.commit()
|
||||
return {"message": "Stock sold/removed successfully"}
|
||||
|
||||
@router.get("/recommendations/{symbol}")
|
||||
def get_recommendations(symbol: str, current_user: User = Depends(get_current_user)):
|
||||
try:
|
||||
ticker = yf.Ticker(symbol)
|
||||
recs = ticker.recommendations
|
||||
|
||||
if recs is None or recs.empty:
|
||||
return {"error": "No recommendation data available", "confidence": 0}
|
||||
|
||||
# Get the most recent month's data (period '0m' is usually index 0)
|
||||
latest = recs.iloc[0]
|
||||
|
||||
strong_buy = int(latest.get("strongBuy", 0))
|
||||
buy = int(latest.get("buy", 0))
|
||||
hold = int(latest.get("hold", 0))
|
||||
sell = int(latest.get("sell", 0))
|
||||
strong_sell = int(latest.get("strongSell", 0))
|
||||
|
||||
total = strong_buy + buy + hold + sell + strong_sell
|
||||
|
||||
if total == 0:
|
||||
return {"error": "No recommendations found", "confidence": 0}
|
||||
|
||||
# Calculate confidence score
|
||||
# Strong Buy = 100, Buy = 75, Hold = 50, Sell = 25, Strong Sell = 0
|
||||
score = (strong_buy * 100 + buy * 75 + hold * 50 + sell * 25) / total
|
||||
|
||||
return {
|
||||
"symbol": symbol,
|
||||
"period": str(latest.get("period", "0m")),
|
||||
"strongBuy": strong_buy,
|
||||
"buy": buy,
|
||||
"hold": hold,
|
||||
"sell": sell,
|
||||
"strongSell": strong_sell,
|
||||
"total": total,
|
||||
"confidence": round(score, 1)
|
||||
}
|
||||
except Exception as e:
|
||||
print(f"Error fetching recommendations for {symbol}: {e}")
|
||||
return {"error": str(e), "confidence": 0}
|
||||
|
||||
Reference in New Issue
Block a user