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.
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.
Shows: App, ui.panel, ui.layout(ui.view(...)), @on.schedule, portfolio.positions(), portfolio.funds(), Money, tones.
"""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.
Shows: App, ui.panel, ui.layout(ui.view(...)), @on.schedule, portfolio.positions(), portfolio.funds(), Money, tones.
"""
import while25
from while25 import App, on, ui
ui.panel("blocks_dashboard", "Desk")
ui.layout(ui.view("main")) # the only widget, so the view IS the panel
def figure(money):
"""A Money as the plain number a block takes; None stays None.
Blocks format every number themselves and draw the absent marker for None,
so a figure the broker did not state never reads as a zero."""
return None if money is None else float(money.value)
def position_rows(positions, limit=12):
"""The table's rows: symbol, quantity and P&L as plain numbers."""
return [{"symbol": p["symbol"],
"quantity": None if p.get("quantity") is None else float(p["quantity"]),
"pnl": figure(p.get("pnl"))}
for p in positions[:limit]]
class BlocksDashboard(App):
id = "blocks_dashboard"
label = "Desk"
@on.schedule(every="5s")
def draw(self, ctx):
positions = while25.portfolio.positions() or []
# None when the broker has not stated its funds: pass it through and
# the platform draws its absent marker. A 0 here would show a funded
# account as empty.
funds = while25.portfolio.funds()
margin = None if funds is None else figure(funds["margin_available"])
while25.ui.push("main", {"blocks": [
{"kind": "header", "title": "Desk", "stamp": "refreshes every 5s",
"badges": [{"text": "live", "tone": "active"}]},
{"kind": "tiles", "items": [
{"label": "Open positions", "value": len(positions)},
{"label": "Available margin", "tone": "money", "value": margin},
{"label": "Day P&L", "tone": "money",
"value": figure(while25.portfolio.day_pnl())},
]},
{"kind": "section", "title": "Positions", "blocks": [
{"kind": "table", "rows": position_rows(positions),
"columns": ["symbol", "quantity",
{"key": "pnl", "label": "P&L", "tone": "money"}]},
]},
]})Account overview: every figure the broker states, and the day's P&L
An HTML page of the account as the broker reports it: day, realised and unrealised P&L, each funds figure under the broker's own label (with the money it lets you put into a new trade marked), per-asset balances where the venue has them, and a small inline SVG bar per position's P&L. Any figure the broker did not state says so in words, never as 0.
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.