+ def __init__(
+ self,
+ name_to_timeout_dict: Dict[str, int],
+ symbols: List[str],
+ display_subs: Dict[str, str] = None,
+ ) -> None:
+ super().__init__(name_to_timeout_dict)
+ self.symbols = symbols
+ self.display_subs = display_subs
+
+ @staticmethod
+ def get_ticker_name(ticker: yf.ticker.Ticker) -> str:
+ """Get friendly name of a ticker."""
+ info = ticker.get_info()
+ if "shortName" in info:
+ return info["shortName"]
+ return ticker
+
+ @staticmethod
+ def get_price(ticker: yf.ticker.Ticker) -> Optional[float]:
+ """Get most recent price of a ticker."""
+ keys = [
+ "bid",
+ "ask",
+ "regularMarketPrice",
+ "lastMarket",
+ "open",
+ "previousClose",
+ ]
+ info = ticker.get_info()
+ for key in keys:
+ if key in info and info[key] is not None and info[key] != 0.0:
+ print(f"Price: picked {key}, ${info[key]}.")
+ return float(info[key])
+ return None
+
+ @staticmethod
+ def get_change_and_delta(
+ ticker: yf.ticker.Ticker, price: float
+ ) -> Tuple[float, float]:
+ """Given the current price, look up opening price and compute delta."""
+ keys = [
+ "previousClose",
+ "open",
+ ]
+ info = ticker.get_info()
+ for key in keys:
+ if key in info and info[key] is not None:
+ print(f"Change: picked {key}, ${info[key]}.")
+ old_price = float(info[key])
+ delta = price - old_price
+ return (delta / old_price * 100.0, delta)
+ return (0.0, 0.0)
+
+ def periodic_render(self, key: str) -> bool:
+ """Write an up-to-date stock page."""
+ with file_writer.file_writer("stock_3_86400.html") as f:
+ f.write("<H1>Stock Quotes</H1><HR>")
+ f.write("<TABLE WIDTH=99%>")
+ symbols_finished = 0
+ for symbol in self.symbols:
+ ticker = yf.Ticker(symbol)
+ # print(ticker.get_info())
+ if ticker is None:
+ logger.debug(f"Unknown symbol {symbol} -- ignored.")
+ continue
+ name = stock_quote_renderer.get_ticker_name(ticker)
+ price = stock_quote_renderer.get_price(ticker)
+ if price is None:
+ logger.debug(f"No price information for {symbol} -- skipped.")
+ continue
+ (percent_change, delta) = stock_quote_renderer.get_change_and_delta(
+ ticker, price
+ )
+ # print(f"delta: {delta}, change: {percent_change}")
+ cell_color = "#b00000" if percent_change < 0 else "#009000"
+ if symbols_finished % 4 == 0:
+ if symbols_finished > 0:
+ f.write("</TR>")
+ f.write("<TR>")
+ symbols_finished += 1
+ if self.display_subs is not None and symbol in self.display_subs:
+ symbol = self.display_subs[symbol]
+ f.write(
+ f"""
+<TD WIDTH=20% HEIGHT=150 BGCOLOR="{cell_color}">