Python

Client classes

Auth, Data, Trading, Stream — khởi tạo và sử dụng

SDK cung cấp 4 client chuyên biệt, mỗi client có phiên bản sync và async.

Auth / AsyncAuth

Client gốc — quản lý REST client, xác thực và token. Tất cả client khác đều nhận Auth làm tham số.

from ssi_sdk import Auth, Config

config = Config(
    client_id="YOUR_CLIENT_ID",
    api_key="YOUR_API_KEY",
    api_secret="YOUR_API_SECRET",
    private_key="YOUR_PRIVATE_KEY",
)

with Auth(config) as auth:
    # Xác thực với OTP
    token = auth.authenticate(otp="222222")
    print(f"Access token: {token.access_token}")
    print(f"Expires at: {token.expires_at}")

Các method xác thực

# Yêu cầu gửi OTP
result = auth.request_otp()

# Xác thực với OTP
token = auth.authenticate(otp="222222")

# Làm mới token
token = auth.refresh()

# Tự động refresh nếu token hết hạn, hoặc yêu cầu OTP nếu cần
access_token = auth.ensure_authenticated(otp="222222")

Kiểm tra trạng thái token

print(auth.token)              # Token object hoặc None
print(auth.access_token)       # Access token string hoặc None
print(auth.is_token_expired)   # True/False
print(auth.has_refresh_token)  # True/False

Data / AsyncData

Client dữ liệu thị trường. Không cần OTP — chỉ cần auth.authenticate().

from ssi_sdk import Auth, Data, Config

with Auth(config) as auth:
    auth.authenticate()  # Không cần OTP cho market data

    with Data(auth) as data:
        # Truy cập qua service
        ohlc = data.market_data.get_ohlc_1minute("SSI")
        indices = data.market_data.get_indexes()
        info = data.market_data.get_securities_info("SSI")

Service: data.market_data (MarketDataService) — OHLC, chỉ số, chứng khoán, securities summary.

Trading / AsyncTrading

Client giao dịch, tài khoản và danh mục. Cần OTP.

from ssi_sdk import Auth, Trading, Config
from ssi_sdk.enums import OrderSide

with Auth(config) as auth:
    auth.authenticate(otp="222222")

    with Trading(auth) as trading:
        # Tài khoản
        accounts = trading.account.get_account_info()

        # Danh mục
        balance = trading.portfolio.get_equity_balance("1234561")
        positions = trading.portfolio.get_equity_positions("1234561")
        orders = trading.portfolio.get_today_orders("1234561")

        # Giao dịch
        result = trading.trading.place_limit_order(
            account_no="1234561",
            symbol="SSI",
            side=OrderSide.BUY,
            quantity=100,
            price=66000,
        )

Services:

  • trading.account (AccountService) — thông tin tài khoản.
  • trading.portfolio (PortfolioService) — số dư, vị thế, sổ lệnh, PPMMR.
  • trading.trading (TradingService) — đặt/sửa/huỷ lệnh, sức mua/bán.

Stream / AsyncStream

Client streaming realtime qua WebSocket. Cần OTP.

from ssi_sdk import Auth, Stream, Config

with Auth(config) as auth:
    auth.authenticate(otp="222222")

    with Stream(auth) as stream:
        stream.streaming.connect()

        # Đăng ký callback
        stream.streaming.on_data = lambda msg: print(f"Data: {msg}")
        stream.streaming.on_trading = lambda msg: print(f"Trading: {msg}")

        # Subscribe
        stream.streaming.subscribe_symbol(["SSI", "HPG"])
        stream.streaming.subscribe_order_status()

        # Chờ nhận dữ liệu
        stream.streaming.wait()

Service: stream.streaming (StreamingService) — subscribe/unsubscribe realtime data.

Callbacks:

PropertyKiểuMô tả
on_dataCallable[[message], Any]Nhận market data (tự parse thành typed message)
on_tradingCallable[[OrderStatusMessage | PortfolioMessage], Any]Nhận trading events
on_heartbeatCallable[[HeartbeatMessage], Any]Nhận heartbeat

Async example hoàn chỉnh

import asyncio
from ssi_sdk import AsyncAuth, AsyncData, AsyncTrading, AsyncStream, Config

async def main():
    config = Config(
        client_id="YOUR_CLIENT_ID",
        api_key="YOUR_API_KEY",
        api_secret="YOUR_API_SECRET",
        private_key="YOUR_PRIVATE_KEY",
    )

    async with AsyncAuth(config) as auth:
        await auth.authenticate(otp="222222")

        async with AsyncData(auth) as data:
            ohlc = await data.market_data.get_ohlc_1minute("SSI")
            print(ohlc)

        async with AsyncTrading(auth) as trading:
            accounts = await trading.account.get_account_info()
            print(accounts)

        async with AsyncStream(auth) as stream:
            await stream.streaming.connect()
            stream.streaming.on_data = lambda msg: print(msg)
            await stream.streaming.subscribe_symbol_trade(["SSI"])
            await stream.streaming.wait()

asyncio.run(main())

Ghi chú

  • Nhóm Async* dùng async withawait.
  • Nhóm sync (Auth, Data, Trading, Stream) dùng with — phù hợp luồng xử lý tuần tự.
  • Tất cả client chia sẻ HTTP connection qua Auth.
  • Lỗi thường gặp: AuthenticationError (xác thực), WebSocketError (stream), APIError (HTTP lỗi nghiệp vụ).

Trên trang này