Skip to main content

tgsdk-python

面向期货、期权策略开发的 Python SDK,提供合约查询、实时行情、期货 K 线和直连 CTP 交易。 通过简洁的同步接口,策略可以持续读取行情、计算指标并响应数据变化。

快速开始

安装:

pip install tgsdk-python

获取行情和 K 线:

from tgsdk import TgApi

with TgApi() as api:
    # 请替换为当前有效的合约代码。
    symbol = "SHFE.au2610"
    quote = api.watch_quote(symbol)
    klines = api.watch_kline(symbol, "1m", data_length=200)

    while api.wait_update():
        if quote.is_changing("last_price"):
            print("最新价", quote.data().last_price)

        if klines.is_changing("timestamp"):
            print("新 K 线", klines.data().iloc[-1].to_dict())

连接交易账户并读取资金:

from tgsdk import TgApi, CtpAccount

ctp_account = CtpAccount(
    broker_id="期货公司代码",
    user_id="资金账号",
    password="交易密码",
    front_url="tcp://期货公司提供的地址:端口",
    app_id="期货公司提供的 AppID",
    auth_code="期货公司提供的授权码",
)

with TgApi(ctp_account) as api:
    account = api.watch_account()
    while api.wait_update():
        if api.trading_status() == "READY" and account.is_changing("available"):
            print("柜台可用资金", account.data().available)

交易账户同步完成后才进入 READY。行情或交易连接中断时,data() 保留最近一次快照;策略使用前应检查交易状态和行情时间戳。

系统架构

flowchart TD
    Broker[期货公司交易柜台]
    Market[官方期货、期权行情系统]
    SDK[tgsdk]

    Broker <-->|直连 CTP| SDK
    Market -->|行情接口| SDK

    classDef source fill:#f3f4f6,stroke:#9ca3af,color:#111;
    classDef sdk fill:#dbeafe,stroke:#598bea,stroke-width:2px,color:#111;

    class Broker,Market source;
    class SDK sdk;
  • 策略始终运行在用户本地环境中,策略代码与交易信号计算均保留在本地。
  • 交易连接由本地 SDK 直接建立到期货公司柜台,交易账号、密码和委托不经过行情服务。

主要功能

  • 合约查询:按交易所、品种、期货/期权和是否主力筛选合约,获取合约详细信息。
  • 实时行情:通过 Quote 对象读取期货、期权的最新价格、成交量、持仓量和盘口。
  • 期货 K 线序列:通过 pandas DataFrame 获取历史与实时 K 线,支持多种周期;期权暂不提供 K 线。
  • CTP 交易:读取账户资金及期货、期权的持仓、订单与成交,支持普通投机限价下单与撤单。
  • 统一更新:使用 wait_update() 推进更新,通过观察句柄的 is_changing() 判断变化、data() 读取快照。
  • 连接管理:自动连接官方数据服务,支持断线重连和订阅恢复。

完整接口、周期取值、数据字段和使用约定见 SDK 使用指南。使用前请阅读免责声明。

Release files for tgsdk-python 1.0.13

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for tgsdk-python 1.0.13
File
tgsdk_python-1.0.13-cp314-cp314-win_amd64.whl CPython 3.14 CPython 3.14 Windows x86-64 Details
tgsdk_python-1.0.13-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details
tgsdk_python-1.0.13-cp314-cp314-macosx_11_0_arm64.whl CPython 3.14 CPython 3.14 macOS 11.0+ ARM64 Details
tgsdk_python-1.0.13-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details
tgsdk_python-1.0.13-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details
tgsdk_python-1.0.13-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details
tgsdk_python-1.0.13-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
tgsdk_python-1.0.13-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.28+ x86-64, Linux glibc 2.24+ x86-64 Details
tgsdk_python-1.0.13-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details

Total release size: 41.6 MB

Release files / tgsdk_python-1.0.13-cp314-cp314-win_amd64.whl

Download URL tgsdk_python-1.0.13-cp314-cp314-win_amd64.whl
Size 2.2 MB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
87735d230ec40bdbae989a2de1b42ea8cad23700a0c1804b2d481746719308da
BLAKE2b-256 checksum
How to use checksums
a489592a6f9101d0ff09a22fdb41eeaf406ba2cb15279b87b2a7b426d7750d9c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.

Transparency log

Release files / tgsdk_python-1.0.13-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL tgsdk_python-1.0.13-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 6.6 MB
Tags CPython 3.14 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
7bffb57958ef2134843aa24c78040f67deda187ba47d1b3dc2393775232beed0
BLAKE2b-256 checksum
How to use checksums
5bfa8785df87227e199c091de1ad6d19279f225195625a24890d9bcb76264048
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.

Transparency log

Release files / tgsdk_python-1.0.13-cp314-cp314-macosx_11_0_arm64.whl

Download URL tgsdk_python-1.0.13-cp314-cp314-macosx_11_0_arm64.whl
Size 5.1 MB
Tags CPython 3.14 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
fc78a4a2c737015c7f28e5f741cbf9a7ad5926bed369f39b33a24125fb4f691a
BLAKE2b-256 checksum
How to use checksums
07fe1fb354e56d7309f2bbb9fb68fb5dbebcd81a4b7ab485ca45bb0c6f5341e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.

Transparency log

Release files / tgsdk_python-1.0.13-cp313-cp313-win_amd64.whl

Download URL tgsdk_python-1.0.13-cp313-cp313-win_amd64.whl
Size 2.1 MB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
eab9b85286f905cd9842a08bef7671ed68e4ecf8fbd13d29a182e6b4cef37b54
BLAKE2b-256 checksum
How to use checksums
bb1e3f8ef2587ae0dfae2521e4c92ac6a6b8abad39897a4206dfc8ab7b52504f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.

Transparency log

Release files / tgsdk_python-1.0.13-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL tgsdk_python-1.0.13-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 6.7 MB
Tags CPython 3.13 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
ef9c4a79b914b5807fdd30c2109b03f2ee3ca4b5bacf6d1a86c5a7be8ce0865a
BLAKE2b-256 checksum
How to use checksums
210c4cefa1cca2aa84cdaa0320bbaa4d48fd7675136688f347b937182bcee25c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.

Transparency log

Release files / tgsdk_python-1.0.13-cp313-cp313-macosx_11_0_arm64.whl

Download URL tgsdk_python-1.0.13-cp313-cp313-macosx_11_0_arm64.whl
Size 5.0 MB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
8d28812df77f50d46ecf0505eed231b57ec2670f485d468fdefce317c135e6ee
BLAKE2b-256 checksum
How to use checksums
eac0036b4e6fd749e960d5bec6b25f5dad0a341a7994948a7cffa6b84eba44d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.

Transparency log

Release files / tgsdk_python-1.0.13-cp312-cp312-win_amd64.whl

Download URL tgsdk_python-1.0.13-cp312-cp312-win_amd64.whl
Size 2.1 MB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
abda244d9b2a34da9cb0033cb46dc83df991d9f72af99862c61a50020fa177a5
BLAKE2b-256 checksum
How to use checksums
ce8042519a1f286373fba0e0a167d01243c8b70765fd282daa22e4fb42b17454
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.

Transparency log

Release files / tgsdk_python-1.0.13-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL tgsdk_python-1.0.13-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 6.7 MB
Tags CPython 3.12 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
d4da7626f8166879fddf9de6101b3ce85d32a2d4b39b121dc1a229a7caa7ecb8
BLAKE2b-256 checksum
How to use checksums
cff9728b0157ae8d262dbd2bad5f2ef266e5fb64386f353576313bbc2ec2486f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.

Transparency log

Release files / tgsdk_python-1.0.13-cp312-cp312-macosx_11_0_arm64.whl

Download URL tgsdk_python-1.0.13-cp312-cp312-macosx_11_0_arm64.whl
Size 5.0 MB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
f9c98ff0ea3f7ed45d11c441e026af5ba123b45440b9fefb327d62b23dc1324a
BLAKE2b-256 checksum
How to use checksums
bdefc5054427f36c43183caf1413032f72908863809a23de28f90e0fcd604a51
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.13 This release

9 release files

1.0.12

9 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page