From 8cb7bce34c0259e5d77591e2492e4b0a999b0f9e Mon Sep 17 00:00:00 2001 From: Anson Date: Mon, 4 Sep 2023 04:19:18 +0000 Subject: [PATCH] fix for marketdata after hours --- common/MarketData.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/common/MarketData.py b/common/MarketData.py index 3bf1a9c..cc32f6f 100644 --- a/common/MarketData.py +++ b/common/MarketData.py @@ -131,7 +131,11 @@ class MarketData: if quoteResp := self.get(f"stocks/quotes/{symbol}/"): price = round(quoteResp["last"][0], 2) - changePercent = round(quoteResp["changepct"][0], 2) + + try: + changePercent = round(quoteResp["changepct"][0], 2) + except TypeError: + return f"The price of {symbol} is {price}" message = f"The current price of {symbol.name} is ${price} and " @@ -148,11 +152,13 @@ class MarketData: def spark_reply(self, symbol: Stock) -> str: if quoteResp := self.get(f"stocks/quotes/{symbol}/"): - changePercent = round(quoteResp["changepct"][0], 2) - return f"`{symbol.tag}`: {changePercent}%" - else: - logging.warning(f"{symbol} did not have 'changepct' field.") - return f"`{symbol.tag}`" + try: + changePercent = round(quoteResp["changepct"][0], 2) + return f"`{symbol.tag}`: {changePercent}%" + except TypeError: + pass + + return f"`{symbol.tag}`" def intra_reply(self, symbol: Stock) -> pd.DataFrame: """Returns price data for a symbol of the past month up until the previous trading days close.