Skip to content

Commit 36860f8

Browse files
committed
fix: guard portfolio_risk against empty DataFrames
1 parent 72d9ded commit 36860f8

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

backend/app/services/risk_metrics.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,13 @@ def portfolio_risk(
8686
VaR (95%), and CVaR (95%) using historical return covariance.
8787
All array ops use NumPy views to avoid RAM cloning on Render free tier.
8888
"""
89+
if any(df.empty or len(df) == 0 for df in processed.values()):
90+
return 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
8991
if period_type == "intraday" and any(len(df) < 5 for df in processed.values()):
9092
return 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
9193

9294
symbols = [s for s in weights if weights[s] > 0 and s in processed]
93-
if not symbols:
95+
if not symbols or any(processed[s].empty for s in symbols if s in processed):
9496
return 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
9597

9698
returns_df = pd.DataFrame(

0 commit comments

Comments
 (0)