Skip to content

trading_agent.terminal.screens.trading_screen

The Trading page: each desk's profit and stocks, halts, positions, the desks' open orders, UBI's order book, and the kill switch.

It copies the web page's TradingPage.tsx. The kill switch halts every desk and flattens the account; like the web page, it asks for a reason and the typed word FLATTEN.

Typical usage example:

app.push_screen(TradingScreen('general'))

TradingScreen

TradingScreen(desk_name)

Bases: PageScreen

The Trading page.

Creates the page.

Parameters:

Name Type Description Default
desk_name str

The desk the chat shows.

required
Source code in src/trading_agent/terminal/screens/trading_screen.py
def __init__(self, desk_name: str):
    """Creates the page.

    Args:
        desk_name (str): The desk the chat shows.
    """
    super().__init__(desk_name)
    self._formatter = formatter.Formatter()
    self._book_kind = _ORDER_BOOK_KINDS[0]

compose_page

compose_page()

Builds the page's sections.

Yields:

Type Description
Widget

The sections, in a scroll area.

Source code in src/trading_agent/terminal/screens/trading_screen.py
def compose_page(self):
    """Builds the page's sections.

    Yields:
        Widget: The sections, in a scroll area.
    """
    with containers.VerticalScroll(classes='page-body'):
        yield widgets.Static('[b]Desks[/b]', classes='page-heading')
        yield widgets.DataTable(id='trading-desks', cursor_type='row')
        yield widgets.Static('', id='trading-halts')
        yield widgets.Static('[b]Positions[/b]', classes='page-heading')
        yield widgets.DataTable(id='trading-positions', cursor_type='row')
        yield widgets.Static(
            "[b]Desks' open orders[/b]", classes='page-heading'
        )
        yield widgets.DataTable(id='trading-orders', cursor_type='row')
        yield widgets.Static(
            '', id='trading-book-title', classes='page-heading'
        )
        yield widgets.DataTable(id='trading-book', cursor_type='row')
        yield widgets.Static(
            '[b $error]Kill switch[/]  [dim]K halts every desk and flattens the account; it asks for a reason and the word FLATTEN.[/dim]',
            classes='page-heading',
        )

on_mount

on_mount()

Sets up the tables' columns, then reads the data.

Source code in src/trading_agent/terminal/screens/trading_screen.py
def on_mount(self) -> None:
    """Sets up the tables' columns, then reads the data."""
    self.table('trading-desks').add_columns(
        'Desk',
        'Today',
        'Stocks owned',
    )
    self.table('trading-positions').add_columns(
        'Symbol',
        'Product',
        'Desk',
        'Quantity',
        'Last price',
        'Today',
    )
    self.table('trading-orders').add_columns(
        'Desk',
        'Symbol',
        'Side',
        'Quantity',
        'Price',
        'Kind',
        'State',
        'Placed',
    )
    self.table('trading-book').add_columns(
        'Order',
        'Symbol',
        'Side',
        'Status',
        'Filled',
        'Average price',
        'Message',
    )
    self._show_book_title('press l to load it')
    super().on_mount()

refresh_page async

refresh_page()

Reads the account and the desks' open orders.

Source code in src/trading_agent/terminal/screens/trading_screen.py
@work(exclusive=True, group='page')
async def refresh_page(self) -> None:
    """Reads the account and the desks' open orders."""
    gateway = self.app.gateway
    try:
        account = await gateway.account()
        desk_orders = await gateway.desk_orders()
    except gateway_client.GatewayError as error:
        self.problem(error.message)
        return
    self._show_desks(account)
    self._show_positions(account)
    self._show_orders(desk_orders)

action_load_order_book

action_load_order_book()

Loads UBI's own order book of the chosen kind.

Source code in src/trading_agent/terminal/screens/trading_screen.py
def action_load_order_book(self) -> None:
    """Loads UBI's own order book of the chosen kind."""
    self._load_book()

action_next_book_kind

action_next_book_kind()

Switches between orders, trades and open parent orders, and loads them.

Source code in src/trading_agent/terminal/screens/trading_screen.py
def action_next_book_kind(self) -> None:
    """Switches between orders, trades and open parent orders, and loads them."""
    index = _ORDER_BOOK_KINDS.index(self._book_kind)
    self._book_kind = _ORDER_BOOK_KINDS[
        (index + 1) % len(_ORDER_BOOK_KINDS)
    ]
    self._load_book()

action_kill_switch

action_kill_switch()

Asks for a reason and FLATTEN, then fires the kill switch.

Source code in src/trading_agent/terminal/screens/trading_screen.py
def action_kill_switch(self) -> None:
    """Asks for a reason and FLATTEN, then fires the kill switch."""

    def fire(reason: str | None) -> None:
        """Fires once confirmed.

        Args:
            reason (str | None): The reason, or None when cancelled.
        """
        if reason is not None:
            self._fire(reason)

    self.app.push_screen(
        confirm_dialog.ConfirmDialog(
            'Fire the kill switch',
            'Every desk is halted and the whole account is flattened: open orders are cancelled and positions closed. This cannot be undone from here.',
            'Fire',
            word='FLATTEN',
        ),
        fire,
    )