3 from typing import Dict, List, Tuple
10 class stock_quote_renderer(renderer.debuggable_abstaining_renderer):
11 """Render the stock prices page."""
14 self, name_to_timeout_dict: Dict[str, int], symbols: List[str]
16 super(stock_quote_renderer, self).__init__(name_to_timeout_dict, False)
17 self.symbols = symbols
19 def debug_prefix(self) -> str:
23 def get_ticker_name(ticker: yf.ticker.Ticker) -> str:
24 """Get friendly name of a ticker."""
25 info = ticker.get_info()
26 return info["shortName"]
29 def get_price(ticker: yf.ticker.Ticker) -> float:
30 """Get most recent price of a ticker."""
39 info = ticker.get_info()
41 if key in info and info[key] is not None and info[key] != 0.0:
42 print(f"Price: picked {key}, ${info[key]}.")
43 return float(info[key])
47 def get_change_and_delta(
48 ticker: yf.ticker.Ticker, price: float
49 ) -> Tuple[float, float]:
50 """Given the current price, look up opening price and compute delta."""
55 info = ticker.get_info()
57 if key in info and info[key] is not None:
58 print(f"Change: picked {key}, ${info[key]}.")
59 old_price = float(info[key])
60 delta = price - old_price
61 return (delta / old_price * 100.0, delta)
64 def periodic_render(self, key: str) -> bool:
65 """Write an up-to-date stock page."""
66 with file_writer.file_writer("stock_3_86400.html") as f:
67 f.write("<H1>Stock Quotes</H1><HR>")
68 f.write("<TABLE WIDTH=99%>")
70 for symbol in self.symbols:
71 # print(f"--- Symbol: {symbol} ---")
72 ticker = yf.Ticker(symbol)
74 # print(ticker.get_info())
76 self.debug_print(f"Unknown symbol {symbol} -- ignored.")
78 name = stock_quote_renderer.get_ticker_name(ticker)
79 price = stock_quote_renderer.get_price(ticker)
81 self.debug_print(f"No price information for {symbol} -- skipped.")
83 (percent_change, delta) = stock_quote_renderer.get_change_and_delta(
86 # print(f"delta: {delta}, change: {percent_change}")
87 cell_color = "#b00000" if percent_change < 0 else "#009000"
88 if symbols_finished % 4 == 0:
89 if symbols_finished > 0:
95 <TD WIDTH=20% HEIGHT=150 BGCOLOR="{cell_color}">
97 <DIV style="position:relative;
100 <DIV style="position:absolute;
103 -webkit-transform:rotate(-90deg);
105 font-family: helvetica, arial, sans-serif;
107 -webkit-text-stroke: 2px black;
111 <!-- Current price, Change today and percent change today, name -->
112 <DIV style="position:absolute;
116 font-family: helvetica, arial, sans-serif;
119 <I>({percent_change:.1f}%)</I><BR>
125 f.write("</TR></TABLE>")
130 # x = stock_quote_renderer({}, ["MSFT", "GOOG", "GBTC", "OPTAX", "VNQ"])
131 # x.periodic_render(None)