본문으로 건너뛰기

계정

연결의 주문, 포지션, 잔고, 체결 내역을 읽고 변경을 따라갑니다.

이 페이지의 모든 호출에는 account:read 권한과 connection_id가 필요합니다. 계정에는 기본 연결이 없습니다. ID는 connections()에서 가져옵니다. 가격과 수량은 십진수 문자열이고, 시간은 UTC 밀리초입니다.

미체결 주문

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에는 미체결 주문이, triggers에는 스톱로스, 테이크프로핏, 스톱 주문이 들어 있습니다. key는 터미널이 주문에 붙인 키이고, exchange_id는 거래소의 ID입니다. 둘 다 값이 알려질 때까지 비어 있습니다.

포지션

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

size의 단위는 기준 코인입니다. MOEX에서는 랏 단위이며, 1랏은 기준 단위 lot_size개입니다.

unrealized_pnl과 mark_price는 터미널이 자체 계산한 값입니다. 마크는 롱이면 최우선 매수 호가, 숏이면 최우선 매도 호가입니다.

잔고

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

터미널은 API를 켠 순간부터 잔고를 수집합니다. 그 뒤로 변하지 않은 통화는 목록에 없습니다.

체결 내역

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)

체결 내역은 오래된 것부터 옵니다. limit은 1부터 1000까지이며 기본값은 100입니다. since_ts는 이 거래소 시각부터의 체결 내역만 남깁니다.

터미널은 연결마다 마지막 체결 내역 1000건을 보관하며, API를 켠 순간부터 수집합니다. 연결을 끄면 그 연결의 체결 내역과 잔고가 지워집니다.

변경 따라가기

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())
토픽프레임에 담긴 것
account.orders한 종목의 모든 주문과 트리거. 가지고 있는 목록을 이것으로 교체합니다.
account.positions한 종목의 포지션. 포지션이 청산되면 비어 있습니다.
account.balances한 통화의 잔고.
account.fills체결 내역 한 건.

구독은 시작 상태를 보내지 않습니다. 먼저 위의 호출로 읽습니다.

너무 느리게 읽는 클라이언트는 체결 내역을 놓칠 수 있습니다. since_ts로 체결 내역을 다시 읽어 놓친 부분을 채웁니다. 자세한 내용은 스트리밍에 있습니다.

이 페이지의 내용