Skip to content

trading_agent.terminal.widgets.profit_card

The side panel's Profit and loss card: the desk's and the account's profit, and any halts in force.

Typical usage example:

card = ProfitCard() card.show('general', account)

ProfitCard

ProfitCard(collapsed=False)

Bases: SideCard

The Profit and loss card.

Creates the card.

Parameters:

Name Type Description Default
collapsed bool

Whether it starts folded.

False
Source code in src/trading_agent/terminal/widgets/profit_card.py
def __init__(self, collapsed: bool = False):
    """Creates the card.

    Args:
        collapsed (bool): Whether it starts folded.
    """
    super().__init__('pnl', 'Profit and loss', collapsed=collapsed)
    self._formatter = formatter.Formatter()

show

show(desk_name, account)

Shows the profit and loss.

Parameters:

Name Type Description Default
desk_name str

The desk the chat shows.

required
account dict[str, Any] | None

The account, or None when it could not be read.

required
Source code in src/trading_agent/terminal/widgets/profit_card.py
def show(self, desk_name: str, account: dict[str, Any] | None) -> None:
    """Shows the profit and loss.

    Args:
        desk_name (str): The desk the chat shows.
        account (dict[str, Any] | None): The account, or None when it could not be read.
    """
    if account is None:
        self.show_body(self.note('Loading…'))
        return
    desk_pnl = float((account.get('desk_pnl') or {}).get(desk_name) or 0)
    lines = [
        self.rows(
            [
                (
                    'This desk today',
                    self._signed(desk_pnl),
                ),
                (
                    'Account today',
                    self._signed(float(account.get('day_pnl') or 0)),
                ),
                (
                    'Account this week',
                    self._signed(float(account.get('week_pnl') or 0)),
                ),
            ]
        ),
    ]
    for halt in account.get('halts') or []:
        scope = halt.get('scope')
        if scope not in (desk_name, 'account'):
            continue
        who = 'Desk'
        if scope == 'account':
            who = 'Account'
        lines.append(
            Content.from_markup(
                '[$error]$who halted until $until: $reason[/]',
                who=who,
                until=str(halt.get('until_date', '')),
                reason=str(halt.get('reason', '')),
            )
        )
    self.show_body(Content('\n').join(lines))