Account desk as an HTML page
The same desk as blocks_dashboard.py, written as one HTML page on the platform's brand classes. Use `ui.html` when the arrangement needs markup of your own; the page has no script permission, so every value is computed here.
The same desk as blocks_dashboard.py, written as one HTML page on the platform's brand classes. Use ui.html when the arrangement needs markup of your own; the page has no script permission, so every value is computed here.
Shows: App, ui.layout(ui.html(...)), brand classes (.page .tiles .tile .card .num .c-profit .c-loss .c-dim), Money, absent funds.
"""Account desk as an HTML page
The same desk as blocks_dashboard.py, written as one HTML page on the
platform's brand classes. Use `ui.html` when the arrangement needs markup of
your own; the page has no script permission, so every value is computed here.
Shows: App, ui.layout(ui.html(...)), brand classes (.page .tiles .tile .card .num .c-profit .c-loss .c-dim), Money, absent funds.
"""
import html
import while25
from while25 import App, on, ui
ui.panel("html_dashboard", "Desk (HTML)")
ui.layout(ui.html("view")) # the only widget, so the page IS the panel
def money_cell(money):
"""(text, class) for a Money: the value with its sign colour, or a dash."""
if money is None:
return "—", "c-dim"
v = money.value
return f"{v:,.2f} {money.currency}", ("c-profit" if v >= 0 else "c-loss")
def margin_text(funds):
"""(text, class) for the available-margin tile. Words, never 0.00, when the
broker states nothing (funds None) or no such figure (Alpaca)."""
if funds is None:
return "Unavailable", "c-dim"
if funds["margin_available"] is None:
return "Not stated", "c-dim"
return money_cell(funds["margin_available"])
def row(pos: dict) -> str:
text, cls = money_cell(pos.get("pnl"))
qty = pos.get("quantity")
return (f"<tr><td>{html.escape(str(pos.get('symbol', '—')))}</td>"
f"<td class='num'>{'—' if qty is None else qty}</td>"
f"<td class='num {cls}'>{text}</td></tr>")
def page(positions, funds) -> str:
margin, margin_cls = margin_text(funds)
rows = "".join(row(p) for p in positions[:12]) or (
"<tr><td colspan='3' class='c-dim'>No open positions</td></tr>")
return f"""
<div class="page stack">
<div class="header">
<h3>Desk</h3>
<span class="badge badge-active">live</span>
<span class="stamp">refreshes every 5s</span>
</div>
<div class="tiles">
<div class="tile"><div class="k">Open positions</div>
<div class="v t-num">{len(positions)}</div></div>
<div class="tile"><div class="k">Available margin</div>
<div class="v t-num {margin_cls}">{margin}</div></div>
</div>
<div class="card">
<table class="dense">
<thead><tr><th>Symbol</th><th class="num">Qty</th><th class="num">P&L</th></tr></thead>
<tbody>{rows}</tbody>
</table>
</div>
</div>"""
class HtmlDashboard(App):
id = "html_dashboard"
label = "Desk (HTML)"
@on.schedule(every="5s")
def draw(self, ctx):
positions = while25.portfolio.positions() or []
while25.ui.push("view", page(positions, while25.portfolio.funds()))Account desk built from blocks
A whole panel described as what to show: header, tiles and a positions table in a `ui.view`, drawn by the platform in its own components. Start here for any dashboard that needs no custom layout.
Index breadth: how much of the S&P 500 or Nasdaq-100 is taking part
Pulls a year and a half of daily closes for every member of the chosen index once a day, then shows the share of members above their 50- and 200-day averages, new 52-week highs and lows, the day's advancers, and the five best and worst members. Members come from the platform's dated index list, and the panel says which date.