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()读取快照。 - 连接管理:自动连接官方数据服务,支持断线重连和订阅恢复。
Release files for tgsdk-python 1.0.12
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
Total release size: 40.7 MB
Release files / tgsdk_python-1.0.12-cp314-cp314-win_amd64.whl
| Download URL | tgsdk_python-1.0.12-cp314-cp314-win_amd64.whl |
|---|---|
| Size | 2.1 MB |
| Tags | CPython 3.14 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
c356e25f9854ac86d509c62b9d7e2e797746bc25831bc653e51fa60e72398a46
|
|
BLAKE2b-256 checksum How to use checksums |
94cda9b392d99871f53ac88b9e2033e0678bb260c1103b5b2ca4f22f0fa45753
|
| 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 23, 2026.
Transparency logRelease files / tgsdk_python-1.0.12-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
| Download URL | tgsdk_python-1.0.12-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 6.4 MB |
| Tags | CPython 3.14 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
2728b92fdcb355f518c941e92b2225219191740b4e53e66acd1ca1355d2324d3
|
|
BLAKE2b-256 checksum How to use checksums |
c32dfa582b992c1b091abcdd8defc550d5a161990d8c14e753054bf1f8005a3d
|
| 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 23, 2026.
Transparency logRelease files / tgsdk_python-1.0.12-cp314-cp314-macosx_11_0_arm64.whl
| Download URL | tgsdk_python-1.0.12-cp314-cp314-macosx_11_0_arm64.whl |
|---|---|
| Size | 5.0 MB |
| Tags | CPython 3.14 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
8f886ae218b7563d830291f8660f92c8b58bde60b7ba660eccc79c1adbf0be2c
|
|
BLAKE2b-256 checksum How to use checksums |
df3325c98b4af4b8a4553c6a06f91fbef35be4d2048f2455f73fb7b8cfef2639
|
| 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 23, 2026.
Transparency logRelease files / tgsdk_python-1.0.12-cp313-cp313-win_amd64.whl
| Download URL | tgsdk_python-1.0.12-cp313-cp313-win_amd64.whl |
|---|---|
| Size | 2.1 MB |
| Tags | CPython 3.13 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
dac8dd9e9ae988c9fa291ce3fdde4fe7f34f60f5af938b981dfc498be2b907e0
|
|
BLAKE2b-256 checksum How to use checksums |
908a7d1b8513daa5953d6590c4ab5ed78837986a9223e57009bf03d716f49204
|
| 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 23, 2026.
Transparency logRelease files / tgsdk_python-1.0.12-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
| Download URL | tgsdk_python-1.0.12-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 6.5 MB |
| Tags | CPython 3.13 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
3f78e16197de702cc40903ab73d8b369d72375ef6a88bbf96af3d7738fb8de03
|
|
BLAKE2b-256 checksum How to use checksums |
b954a3b34b56ed43eb5ff95be17554484604339da35d9b6fcd5eada0db31687b
|
| 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 23, 2026.
Transparency logRelease files / tgsdk_python-1.0.12-cp313-cp313-macosx_11_0_arm64.whl
| Download URL | tgsdk_python-1.0.12-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 |
36b7703056291f387bfa018c8bb731310df8cbcc68fc93f36492c28c9e42249d
|
|
BLAKE2b-256 checksum How to use checksums |
c2d9fe958bfb964b395c27370d6054d2b0d0d45e895233b22172e51670c6c700
|
| 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 23, 2026.
Transparency logRelease files / tgsdk_python-1.0.12-cp312-cp312-win_amd64.whl
| Download URL | tgsdk_python-1.0.12-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 2.1 MB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
be756dc4681b1df10e1946db67ce9ab5b8b5a2800ab49677add5402f0b3c4ddb
|
|
BLAKE2b-256 checksum How to use checksums |
e15698df93c7c672abc0afc68558eb088ed44918f65d6f4fb17e6ba2266f9c03
|
| 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 23, 2026.
Transparency logRelease files / tgsdk_python-1.0.12-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
| Download URL | tgsdk_python-1.0.12-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 6.5 MB |
| Tags | CPython 3.12 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
9227d1a260fe96ee3b57574292ffbe8f79a90502027ddaaaf5190f954782789e
|
|
BLAKE2b-256 checksum How to use checksums |
05ab6e59801a174402899bb4304017961eca88cb2038f458d2166aa1e613c1a4
|
| 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 23, 2026.
Transparency logRelease files / tgsdk_python-1.0.12-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | tgsdk_python-1.0.12-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 |
945deeb55f8d669712a2250d1b5cf19d469a265231cccd6e7d17ca1a4fece04b
|
|
BLAKE2b-256 checksum How to use checksums |
da94143d595dfe5d0bfe5af8203895362fdc6396ace8d11a3769bb826a326bfa
|
| 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 23, 2026.
Transparency log