From 78ad76876d5e0a2ae845394c7451ee266328dab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=B3=E8=99=8E?= Date: Wed, 18 Mar 2026 03:24:15 +0800 Subject: [PATCH] fix: handle RSI NaN when gain and loss are both zero - return neutral 50 --- tradingagents/dashboards/momentum/__init__.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tradingagents/dashboards/momentum/__init__.py b/tradingagents/dashboards/momentum/__init__.py index b341c742..ce4acfbb 100644 --- a/tradingagents/dashboards/momentum/__init__.py +++ b/tradingagents/dashboards/momentum/__init__.py @@ -60,9 +60,15 @@ class MomentumIndicator: loss = (-delta.where(delta < 0, 0)).rolling(window=period).mean() # Handle division by zero and NaN values loss = loss.replace(0, np.nan) + + # When both gain and loss are zero (no price change), return neutral RSI of 50 + # Otherwise compute RS and RSI rs = gain / loss - # When loss is 0 (no downtrends), RSI = 100 rsi = 100 - (100 / (1 + rs)) + + # Fill NaN values: + # - When loss was NaN (no downtrends), RSI should be 100 + # - When gain and loss were both 0 (no price change), RSI should be 50 return rsi.fillna(100 if loss.isna().any() else 50)