3. Dữ liệu K-line (OHLC)

Lấy dữ liệu nến cho biểu đồ và phân tích kỹ thuật

Mục tiêu

Cung cấp dữ liệu OHLCV (Open/High/Low/Close/Volume) cho biểu đồ nến và phân tích kỹ thuật. Hỗ trợ các timeframe: 1m, 3m, 5m, 15m, 1h, 1d (nến tuần/tháng không được hỗ trợ).

Luồng xử lý

Chart/Strategy Module → Market Data API (OHLC) → Chart Engine
  1. Client gửi symbol, interval, from_date, to_date
  2. API trả về mảng OHLCV theo mốc thời gian
  3. Chuẩn hóa dữ liệu (time/open/high/low/close/volume)
  4. Nếu lịch sử dài thì lặp theo paging, hoặc dùng get_ohlc_all / iter_ohlc (SDK 3.3.0) để SDK tự phân trang

Sample Code — Sync

python/sample_03_ohlc.py
from ssi_sdk import Auth, Data, Config
from ssi_sdk.enums import Timeframe
from ssi_sdk.exceptions import ValidationError
from auth_helper import ensure_auth

config = Config(
    client_id="<your_client_id>",
    api_key="<your_api_key>",
    api_secret="<your_api_secret>",
    private_key="<your_private_key>",
)

SYMBOL = "SSI"

with Auth(config) as auth:
    ensure_auth(auth)

    with Data(auth) as data:
        # Bước 1: OHLC ngày gần nhất
        print(f"--- OHLC 1 ngày ({SYMBOL}) ---")
        daily = data.market_data.get_ohlc_1day_historical(
            SYMBOL, from_date="2026/03/01 00:00:00", to_date="2026/03/27 23:59:59"
        )
        for bar in daily[:5]:
            print(
                f"  {bar.trading_date} | "
                f"O:{bar.open_price:>10} H:{bar.high_price:>10} "
                f"L:{bar.low_price:>10} C:{bar.close_price:>10} "
                f"V:{bar.volume:>12}"
            )

        # Bước 2: OHLC lịch sử với paging
        hist = data.market_data.get_ohlc_1day_historical(
            symbol=SYMBOL,
            from_date="2026/01/01 00:00:00",
            to_date="2026/03/27 23:59:59",
            page=1,
            size=20,
        )

        # Bước 3: OHLC theo timeframe khác (1h)
        hourly = data.market_data.get_ohlc_1hour(SYMBOL)
        for bar in hourly[:5]:
            print(
                f"  {bar.trading_date} | O:{bar.open_price} H:{bar.high_price} "
                f"L:{bar.low_price} C:{bar.close_price} V:{bar.volume}"
            )

        # Bước 4: Phân trang cho dữ liệu lớn (1 phút)
        page = 1
        total_bars = 0
        while True:
            bars = data.market_data.get_ohlc_1minute_historical(
                symbol=SYMBOL,
                from_date="2026/03/25 09:00:00",
                to_date="2026/03/25 15:00:00",
                page=page,
                size=100,
            )
            if not bars:
                break
            total_bars += len(bars)
            page += 1
        print(f"Tổng cộng {total_bars} nến 1 phút.")

        # Bước 5 (SDK 3.3.0): lấy TOÀN BỘ nến trong khoảng, SDK tự phân trang
        # Timeframe hỗ trợ: 1d, 1m, 3m, 5m, 15m, 1h
        all_bars = data.market_data.get_ohlc_all(
            SYMBOL, Timeframe.DAY_1,
            from_date="2026/01/01 00:00:00", to_date="2026/03/27 23:59:59",
            ascending=True,  # server trả newest first; ascending=True đảo lại cũ -> mới
        )
        print(f"get_ohlc_all 1 ngày: {len(all_bars)} nến")
        # iter_ohlc (generator): duyệt từng nến, tự phân trang, newest first
        count = sum(
            1
            for _ in data.market_data.iter_ohlc(
                SYMBOL, Timeframe.MINUTE_5,
                from_date="2026/03/25 09:00:00", to_date="2026/03/25 15:00:00", size=100,
            )
        )
        print(f"iter_ohlc 5 phút: {count} nến")

        # Nến tuần (1w) / tháng (1M) KHÔNG được server hỗ trợ (lỗi 400210).
        # Từ SDK 3.3.0 get_ohlc_1week_historical / get_ohlc_1month_historical raise
        # ValidationError ngay phía client, không gửi request. Tự gộp nến ngày nếu cần.
        try:
            data.market_data.get_ohlc_1week_historical(
                SYMBOL, from_date="2026/01/01 00:00:00", to_date="2026/03/27 23:59:59"
            )
        except ValidationError as e:
            print(f"1 tuần không hỗ trợ: {e}")

Sample Code — Async

python/sample_03_ohlc_async.py
import asyncio
from ssi_sdk import AsyncAuth, AsyncData, Config
from ssi_sdk.enums import Timeframe
from auth_helper import ensure_auth_async

config = Config(...)  # Tương tự config ở trên

SYMBOL = "SSI"

async def main():
    async with AsyncAuth(config) as auth:
        await ensure_auth_async(auth)
        async with AsyncData(auth) as data:
            daily = await data.market_data.get_ohlc_1day_historical(
                SYMBOL, from_date="2026/03/01 00:00:00", to_date="2026/03/27 23:59:59"
            )
            for bar in daily[:5]:
                print(f"  {bar.trading_date} | C:{bar.close_price} V:{bar.volume}")

            hourly = await data.market_data.get_ohlc_1hour(SYMBOL)

            # SDK 3.3.0: lấy toàn bộ nến, tự phân trang
            all_bars = await data.market_data.get_ohlc_all(
                SYMBOL, Timeframe.DAY_1,
                from_date="2026/01/01 00:00:00", to_date="2026/03/27 23:59:59",
                ascending=True,
            )
            # iter_ohlc là async generator
            count = 0
            async for bar in data.market_data.iter_ohlc(
                SYMBOL, Timeframe.MINUTE_5,
                from_date="2026/03/25 09:00:00", to_date="2026/03/25 15:00:00", size=100,
            ):
                count += 1

asyncio.run(main())

API Methods

MethodTimeframeMô tả
get_ohlc_1day_historical(symbol, from_date, to_date)1 ngàyNến ngày lịch sử
get_ohlc_1hour(symbol)1 giờNến 1 giờ gần nhất
get_ohlc_1minute_historical(symbol, from_date, to_date, page, size)1 phútNến phút lịch sử (có paging)
get_ohlc_3minute_historical(...)3 phútNến 3 phút lịch sử
get_ohlc_5minute_historical(...)5 phútNến 5 phút lịch sử
get_ohlc_15minute_historical(...)15 phútNến 15 phút lịch sử
get_ohlc_all(symbol, timeframe, from_date, to_date, size, ascending)1d, 1m, 3m, 5m, 15m, 1hLấy toàn bộ nến trong khoảng (tự phân trang); ascending=True trả cũ -> mới
iter_ohlc(symbol, timeframe, from_date, to_date, size)1d, 1m, 3m, 5m, 15m, 1hGenerator duyệt từng nến, tự phân trang, newest first (async: async for)
get_ohlc_1week_historical / get_ohlc_1month_historical1 tuần / 1 thángKhông được hỗ trợ: raise ValidationError (server trả lỗi 400210)

Trên trang này