Skip to Content
While25SDK docs
ExamplesApps and panels

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.

View on GitHub

apps/blocks_dashboard.py
"""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"}]},
            ]},
        ]})