# Portfolio

> Balance, positions, fills, settlements, per-market stats and account history.

Source: https://docs.stxapp.io/sdks/python/portfolio/

All amounts are dollar strings and all contract counts are quantity strings, as the API sends them. Every field is described in the [API reference](/api/rest/).

## Balance

`balance()` calls `GET /api/v1/account/balance`: cash, what is available, liabilities, lifetime totals and the fee schedule. It is the same object the [`balances` channel](/sdks/python/websockets#balances) pushes.

```python
from stx import STX

with STX() as client:
    b = client.balance()
    print("available", b.available_balance, "cash", b.account_balance)
    print("buy liability", b.buy_order_liability, "sell liability", b.sell_order_liability)
    print("fees", b.fee_schedule, b.taker_factor, b.maker_factor, "tier", b.loyalty_tier)
```

`available_balance` is rounded down to the cent and the liabilities up, so they need not reconcile to the cent; treat each as authoritative.

## Positions

`positions()` calls `GET /api/v1/positions` and returns your positions (`market_ids=` narrows them), the same objects the [`positions` channel](/sdks/python/websockets#positions) sends on join.

```python
from stx import STX

with STX() as client:
    for p in client.positions():
        print(p.market_id, "net", p.position, "premium", p.premium, "open risk", p.open_risk)
```

`position` is positive when long and negative when short. Positions are not marked to market: value them against the order book yourself.

## Fills

A fill is one of your executions. `fills()` filters on `market_ids`, `order_ids` and `status` (`created`, `open`, `settled`, `cancelled`):

```python
from stx import STX

with STX() as client:
    for f in client.fills(limit=5):
        print(f.trade_id, f.order_id, f.action, f.filled, "@", f.price, "fee", f.total_fee)

    orders = client.orders(status="filled", limit=1)
    if orders.items:
        mine = client.fills(order_ids=[orders[0].id])
        print("fills for", orders[0].id, [f.filled for f in mine])
```

`total_fee` is the all-in fee: the trade fee plus settlement fees so far. `unrounded_trade_fee` can carry up to nine decimals, so parse with `Decimal`.

## Settlements and history

```python
from stx import STX

with STX() as client:
    for s in client.settlements(limit=5):
        print(s.type, s.quantity, s.opening_price, "->", s.closing_price, "pnl", s.realized_pnl)

    for name in ("deposits", "withdrawals", "adjustments", "fees", "loyalty"):
        page = getattr(client, name)(limit=3)
        print(name, [(t.type, t.amount) for t in page])
```

`settlements()` takes `market_ids` and `type` (`closed_short`, `closed_long`, `expired_short`, `expired_long`). Every history method pages by cursor and has an `iter_` twin.

## Per-market statistics

`account_market_stats()` returns your position, exposure and P&L broken out per market (`GET /api/v1/account/market_stats`). It is unrelated to the public `market_stats` channel, which carries prices.

```python
from stx import STX

with STX() as client:
    for row in client.account_market_stats(exclude_zero_settlements=True, limit=5):
        print(row.market_id, "position", row.position, "net pnl", row.total_net_pnl)
```

It filters on `market_ids`, `event_ids`, `sports`, `competitions`, `from_time` and `to_time` (Unix microseconds) and `exclude_zero_settlements`.

## Keeping it current

These methods return a snapshot. To stay current, join `orders`, `fills`, `positions`, `settlements` and `balances` (or `account` for all of them) on the socket, and call `orders()` and the other methods you rely on again after a reconnect. See [WebSockets](/sdks/python/websockets/).
