Skip to Content

Account

Read the orders, positions, balances and fills of a connection, and follow their changes.

Every call on this page needs the account:read scope and a connection_id. The account has no default connection: take the id from connections(). Prices and sizes are decimal strings, times are UTC milliseconds.

Open orders

orders = terminal.account_orders(12)
for order in orders.data:
    print(order.key, order.exchange_id, order.side, order.price, order.remaining, order.status)
for trigger in orders.triggers:
    print(trigger.id, trigger.trigger_type, trigger.trigger_price)

data holds the open orders, triggers the stop-loss, take-profit and stop orders. key is the terminal's key of an order, exchange_id is the exchange's. Either is empty until it is known.

Positions

positions = terminal.account_positions(12)
for position in positions.data:
    print(position.instrument, position.side, position.size, position.entry_price, position.unrealized_pnl)

size is in the base coin. On MOEX it is in lots of lot_size base units.

unrealized_pnl and mark_price are the terminal's own. The mark is the best bid for a long and the best ask for a short.

Balances

balances = terminal.account_balances(12)
for balance in balances.data:
    print(balance.currency, balance.total, balance.available, balance.locked)

The terminal collects balances from the moment the API is switched on. A currency that has not changed since then is missing from the list.

Fills

fills = terminal.account_fills(12, limit=50)
for fill in fills.data:
    print(fill.ts, fill.instrument, fill.side, fill.price, fill.qty, fill.fee)
if fills.data:
    newer = terminal.account_fills(12, since_ts=fills.data[-1].ts)

Fills come oldest first. limit is from 1 to 1000, 100 by default. since_ts keeps the fills from this exchange time on.

The terminal keeps the last 1000 fills of a connection, collected from the moment the API is switched on. Switching the connection off clears its fills and balances.

Follow changes

import asyncio
from spectre_terminal import Event

async def main():
    async with terminal.stream() as stream:
        await stream.subscribe("account.orders", connection_id=12)
        await stream.subscribe("account.fills", connection_id=12)
        async for frame in stream:
            if not isinstance(frame, Event):
                continue
            if frame.topic == "account.orders":
                print(frame.data.instrument, len(frame.data.orders), len(frame.data.triggers))
            elif frame.topic == "account.fills":
                print(frame.data.instrument, frame.data.side, frame.data.price, frame.data.qty)

asyncio.run(main())
TopicWhat a frame carries
account.ordersAll orders and triggers of one instrument. Replace your list with it.
account.positionsThe position of one instrument. It is empty when the position is closed.
account.balancesThe balance of one currency.
account.fillsOne fill.

A subscription sends no starting state. Read it with the calls above first.

A client that reads too slowly can miss a fill. Read the fills again with since_ts to catch up. More in Streaming.

On this page