Skip to main content

Python wrapper for CTP API

Project description

tradebest-ctp-python

tradebest-ctp-python 是一个功能完善、使用便捷的中国金融期货交易平台(CTP)的Python接口封装。

特点

  • 功能完善:完整支持CTP交易和行情接口的所有功能
  • 使用便捷:提供简洁直观的API,降低学习和使用门槛
  • 易于迭代:模块化设计,当CTP接口变更时可以轻松适配
  • 跨平台支持:支持Windows、Linux和MacOS系统
  • 多版本兼容:支持多个CTP API版本
  • 类型提示:完整的类型注解,提升开发体验
  • 异常处理:健壮的错误处理机制
  • 详细文档:简洁易懂的使用文档和示例

安装

pip install tradebest-ctp-python

快速开始

行情接口示例

from tradebest_ctp import mdapi

class CMdImpl(mdapi.CThostFtdcMdSpi):
    def __init__(self, md_front):
        mdapi.CThostFtdcMdSpi.__init__(self)
        self.md_front = md_front
        self.api = None

    def Run(self):
        self.api = mdapi.CThostFtdcMdApi.CreateFtdcMdApi()
        self.api.RegisterFront(self.md_front)
        self.api.RegisterSpi(self)
        self.api.Init()

    def OnFrontConnected(self):
        print("OnFrontConnected")
        
        # 登录
        req = mdapi.CThostFtdcReqUserLoginField()
        self.api.ReqUserLogin(req, 0)

    def OnRspUserLogin(self, pRspUserLogin, pRspInfo, nRequestID, bIsLast):
        if pRspInfo is not None and pRspInfo.ErrorID != 0:
            print(f"Login failed. {pRspInfo.ErrorMsg}")
            return
        print(f"Login succeed.{pRspUserLogin.TradingDay}")
        
        # 订阅行情
        self.api.SubscribeMarketData(["au2406".encode('utf-8')], 1)

    def OnRtnDepthMarketData(self, pDepthMarketData):
        print(f"{pDepthMarketData.InstrumentID} - {pDepthMarketData.LastPrice} - {pDepthMarketData.Volume}")

if __name__ == '__main__':
    md = CMdImpl("tcp://180.168.146.187:10131")
    md.Run()
    
    input("Press enter key to exit.")

交易接口示例

from tradebest_ctp import tdapi

class TdImpl(tdapi.CThostFtdcTraderSpi):
    def __init__(self, host, broker, user, password, appid, authcode):
        super().__init__()
        
        self.broker = broker
        self.user = user
        self.password = password
        self.appid = appid
        self.authcode = authcode
        
        self.api = tdapi.CThostFtdcTraderApi.CreateFtdcTraderApi()
        self.api.RegisterSpi(self)
        self.api.RegisterFront(host)
        self.api.SubscribePrivateTopic(tdapi.THOST_TERT_QUICK)
        self.api.SubscribePublicTopic(tdapi.THOST_TERT_QUICK)
    
    def Run(self):
        self.api.Init()
    
    def OnFrontConnected(self):
        print("OnFrontConnected")
        
        # 认证
        req = tdapi.CThostFtdcReqAuthenticateField()
        req.BrokerID = self.broker
        req.UserID = self.user
        req.AppID = self.appid
        req.AuthCode = self.authcode
        self.api.ReqAuthenticate(req, 0)
    
    def OnRspAuthenticate(self, pRspAuthenticateField, pRspInfo, nRequestID, bIsLast):
        if pRspInfo and pRspInfo.ErrorID != 0:
            print("认证失败:{}".format(pRspInfo.ErrorMsg))
            return
        print("Authenticate succeed.")
        
        # 登录
        req = tdapi.CThostFtdcReqUserLoginField()
        req.BrokerID = self.broker
        req.UserID = self.user
        req.Password = self.password
        self.api.ReqUserLogin(req, 0)
    
    def OnRspUserLogin(self, pRspUserLogin, pRspInfo, nRequestID, bIsLast):
        if pRspInfo is not None and pRspInfo.ErrorID != 0:
            print(f"Login failed. {pRspInfo.ErrorMsg}")
            return
        print(f"Login succeed. TradingDay: {pRspUserLogin.TradingDay}")
        
        # 查询账户资金
        self.QryAccount()
    
    def QryAccount(self):
        req = tdapi.CThostFtdcQryTradingAccountField()
        req.BrokerID = self.broker
        req.InvestorID = self.user
        self.api.ReqQryTradingAccount(req, 0)
    
    def OnRspQryTradingAccount(self, pTradingAccount, pRspInfo, nRequestID, bIsLast):
        if pRspInfo is not None and pRspInfo.ErrorID != 0:
            print(f"查询账户资金失败: {pRspInfo.ErrorMsg}")
            return
        
        if pTradingAccount is not None:
            print(f"账户资金: 可用资金={pTradingAccount.Available}, "
                  f"当前保证金={pTradingAccount.CurrMargin}, "
                  f"平仓盈亏={pTradingAccount.CloseProfit}, "
                  f"持仓盈亏={pTradingAccount.PositionProfit}")

if __name__ == '__main__':
    td = TdImpl(
        "tcp://180.168.146.187:10101",  # 交易前置地址
        "9999",                         # 经纪公司代码
        "123456",                       # 投资者代码
        "password",                     # 密码
        "simnow_client_test",           # AppID
        "0000000000000000"              # 授权码
    )
    td.Run()
    
    input("Press enter key to exit.")

支持的CTP版本

tradebest-ctp-python当前版本支持以下CTP API版本:

  • 6.7.7 (当前版本)

未来将只支持最新的CTP API版本,不会支持旧版本。

支持的Python版本

tradebest-ctp-python支持以下Python版本:

  • Python 3.7
  • Python 3.8
  • Python 3.9
  • Python 3.10
  • Python 3.11
  • Python 3.12

许可证

BSD 3-Clause License

Project details


Download files

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

Source Distribution

tradebest_ctp_python-6.7.8.tar.gz (28.6 MB view details)

Uploaded Source

Built Distribution

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

tradebest_ctp_python-6.7.8-py3-none-any.whl (14.4 MB view details)

Uploaded Python 3

File details

Details for the file tradebest_ctp_python-6.7.8.tar.gz.

File metadata

  • Download URL: tradebest_ctp_python-6.7.8.tar.gz
  • Upload date:
  • Size: 28.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for tradebest_ctp_python-6.7.8.tar.gz
Algorithm Hash digest
SHA256 d2d88bd11ac36f5e7738dd996cb38309f775816dd9324a0e290aca73f1289d7a
MD5 1cb5aa7fa083f85a726ad4c51ef631c0
BLAKE2b-256 e839de7af33503e84d310559a845efd817333757c5302e03224d8335dfe752ce

See more details on using hashes here.

File details

Details for the file tradebest_ctp_python-6.7.8-py3-none-any.whl.

File metadata

File hashes

Hashes for tradebest_ctp_python-6.7.8-py3-none-any.whl
Algorithm Hash digest
SHA256 2462c4e78ed8768f0581900844a7bc4df8de54ba8cebaf1f9fb4be127cd78429
MD5 e65e2ff6cf83b832e14337dfc4a6fc3a
BLAKE2b-256 3084af6970e7dbb4839d993a0765c2e675dbe31e1fb1b7d8002087fd1a3d70be

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page