Skip to main content

py_ctp

上期技术期货交易 api 之 python 封装,实现接口调用。支持 windows(x86/x64) linux(x64).

更新

v6.7.10.20250422 全函数封装

ctp 接口封装由 ctp_generate 生成

安装

pip install py-ctp==6.7.10.20260815

示例

#!/usr/bin/env python
# -*- coding: utf-8 -*-
__title__ = 'test py ctp of se'
__author__ = 'HaiFeng'
__mtime__ = '20190506'

from py_ctp.trade import CtpTrade
from py_ctp.quote import CtpQuote
from py_ctp.enums import *
import time


class TestTrade(object):
    def __init__(self, addr: str, broker: str, investor: str, pwd: str, appid: str, auth_code: str, proc: str):
        self.front = addr
        self.broker = broker
        self.investor = investor
        self.pwd = pwd
        self.appid = appid
        self.authcode = auth_code
        self.proc = proc

        self.t = CtpTrade()
        self.t.OnConnected = self.on_connect
        self.t.OnUserLogin = lambda o, x: print('Trade logon:', x)
        self.t.OnDisConnected = lambda o, x: print(x)
        self.t.OnRtnNotice = lambda obj, time, msg: print(f'OnNotice: {time}:{msg}')
        self.t.OnErrRtnQuote = lambda obj, quote, info: None
        self.t.OnErrRtnQuoteInsert = lambda obj, o: None
        self.t.OnOrder = lambda obj, o: None
        self.t.OnErrOrder = lambda obj, f, info: None
        self.t.OnTrade = lambda obj, o: None
        self.t.OnInstrumentStatus = lambda obj, inst, stat: None

    def on_connect(self, obj):
        self.t.ReqUserLogin(self.investor, self.pwd, self.broker, self.proc, self.appid, self.authcode)

    def run(self):
        self.t.ReqConnect(self.front)
        # self.t.ReqConnect('tcp://192.168.52.4:41205')

    def release(self):
        self.t.ReqUserLogout()


class TestQuote(object):
    """TestQuote"""

    def __init__(self, addr: str, broker: str, investor: str, pwd: str):
        """"""
        self.front = addr
        self.broker = broker
        self.investor = investor
        self.pwd = pwd

        self.q = CtpQuote()
        self.q.OnConnected = lambda x: self.q.ReqUserLogin(self.investor, self.pwd, self.broker)
        self.q.OnUserLogin = lambda o, i: self.q.ReqSubscribeMarketData('rb2409')

    def run(self):
        self.q.ReqConnect(self.front)

    def release(self):
        self.q.ReqUserLogout()


if __name__ == "__main__":
    front_trade = 'tcp://180.168.146.187:10202'
    front_quote = 'tcp://180.168.146.187:10212'
    broker = '9999'
    investor = ''
    pwd = ''
    appid = ''
    auth_code = ''
    proc = ''
    if investor == '':
        investor = input('invesotr:')
        pwd = input('password:')
        appid = input('appid:')
        auth_code = input('auth code:')
        proc = input('product info:')
    tt = TestTrade(front_trade, broker, investor, pwd, appid, auth_code, proc)

发布到 PyPI

从 2023 年起,PyPI 不再支持用户名/密码认证,必须使用 API Token 或 Trusted Publishers。

方法一:使用 API Token(推荐)

  1. 访问 PyPI 并登录
  2. 进入 Account Settings 页面
  3. 找到 "API tokens" 部分并点击 "Add API token"
  4. 为 Token 添加描述(例如:py_ctp release)
  5. 选择作用域(Scope),通常选择 "Upload packages"
  6. 点击 "Add token" 并复制生成的 Token

有了 API Token 后,有两种使用方式:

选项 1:使用 .pypirc 配置文件

创建或编辑 ~/.pypirc 文件:

[pypi]
username = __token__
password = pypi-*******************************

pypi-******************************* 替换为你刚刚生成的实际 API Token。

然后正常执行发布命令:

rm dist -rf && python setup.py sdist && twine upload dist/*
rm dist -rf && python setup.py bdist_wheel && twine upload dist/*.whl

选项 2:使用命令行参数

rm dist -rf && python setup.py sdist && twine upload -u __token__ -p pypi-******************************* dist/*

方法二:使用 Trusted Publishers(适用于 GitHub Actions 等 CI/CD)

如果你使用 GitHub Actions 或其他 CI/CD 系统,可以配置 Trusted Publishers 实现自动发布。

详细信息请参考官方文档:

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_ctp-6.7.10.20260818.tar.gz (10.6 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_ctp-6.7.10.20260818-py3-none-any.whl (10.7 MB view details)

Uploaded Python 3

File details

Details for the file py_ctp-6.7.10.20260818.tar.gz.

File metadata

  • Download URL: py_ctp-6.7.10.20260818.tar.gz
  • Upload date:
  • Size: 10.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for py_ctp-6.7.10.20260818.tar.gz
Algorithm Hash digest
SHA256 a07e990f79f549c61351a4e6d2f04894353b36599dd16381c08cd7e875026640
MD5 626c6f2e719a844383748daf23c8c281
BLAKE2b-256 c6c35c968e4274915f563f84b2ee8246b8231a643eed49d912f0219fdae1bd2c

See more details on using hashes here.

File details

Details for the file py_ctp-6.7.10.20260818-py3-none-any.whl.

File metadata

File hashes

Hashes for py_ctp-6.7.10.20260818-py3-none-any.whl
Algorithm Hash digest
SHA256 a292b09059d648c526f357fbfacea33f5e7d0997770514e2dbc15545cf6dbbf4
MD5 133a1da172b8d67750bcf5ab4eb99ed1
BLAKE2b-256 9bd8b42725f6c50ac2b0d87d2cdaae27598771859933dc62866b14851cf70dfd

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

6.7.10.20260818 This release

2 files

6.7.10.20250425

1 file

6.7.10.20250424

1 file

6.7.10.20250423

1 file

6.7.10.20250422

1 file

6.7.2.20240315

2 files

6.7.2.20230913

2 files

6.6.8.20230422

2 files

6.6.8.20220915

2 files

6.6.8.20220911

1 file

6.5.1.119

2 files

6.5.1.115

2 files

6.3.15.1126

1 file

6.3.15.1106

2 files

2.3.15

1 file

2.3.4

1 file

2.3.3

3 files

2.3.2

2 files

2.3.1

1 file

2.3.0

1 file

2.2.9

1 file

2.2.8

1 file

2.2.7

1 file

2.2.5

1 file

2.2.4

1 file

2.2.3

1 file

2.2.2

1 file

2.2.1

1 file

2.2.0

1 file

2.1.2

1 file

2.1.1

1 file

2.0.0

1 file

1.1.7

1 file

1.1.6

1 file

1.1.5.3

1 file

1.1.5.2

1 file

1.1.5.1

1 file

1.1.5

1 file

1.1.4.3

1 file

1.1.4.2

1 file

1.1.4

1 file

1.1.3

1 file

1.1.2

1 file

1.1.1

1 file

1.1.0

1 file

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