From 0cb11574a3e4d7167756adf39f66a59950b60b21 Mon Sep 17 00:00:00 2001 From: "Bobby (aider)" Date: Thu, 13 Feb 2025 08:07:52 -0800 Subject: [PATCH] refactor: Add None checks to Position class property methods --- src/trading/portfolio.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/trading/portfolio.py b/src/trading/portfolio.py index ac45ae1..c5ec315 100644 --- a/src/trading/portfolio.py +++ b/src/trading/portfolio.py @@ -14,21 +14,30 @@ class Position: @property def current_value(self) -> float: # TODO: Implement real-time price fetching + if self.entry_price is None or self.shares is None: + return 0.0 return self.shares * self.entry_price @property def potential_profit(self) -> float: """Calculate potential profit at target price""" + if self.target_price is None or self.entry_price is None or self.shares is None: + return 0.0 return (self.target_price - self.entry_price) * self.shares @property def potential_loss(self) -> float: """Calculate potential loss at stop loss""" + if self.stop_loss is None or self.entry_price is None or self.shares is None: + return 0.0 return (self.stop_loss - self.entry_price) * self.shares @property def risk_reward_ratio(self) -> float: """Calculate risk/reward ratio""" + if (self.target_price is None or self.entry_price is None or + self.stop_loss is None): + return 0.0 potential_gain = self.target_price - self.entry_price potential_risk = self.entry_price - self.stop_loss return abs(potential_gain / potential_risk) if potential_risk != 0 else 0