Skip to main content

PeopleQuant

CTP接口的Python版,跨平台的交易接口,精而美的Python SDK。
CTP接口是连接交易柜台的门户,本项目力图解决交易前的风控、交易后的数据反馈、各种交易场景下的数据查询,解决交易上的“最后一公里问题”,并能嵌入其他交易平台,借助其他平台的优势,以及结合Python支持的科学计算、机器学习、AI等现代技术,开发各种高效策略。

ThostFtdcTraderApi.h C++头文件,包含交易相关的指令,如报单。
ThostFtdcMdApi.h C++头文件,包含获取行情相关的指令。
ThostFtdcUserApiStruct.h 包含了所有用到的数据结构。
ThostFtdcUserApiDataType.h 包含了所有用到的数据类型。
thosttraderapi_se.dll 交易部分的动态链接库。
thosttraderapi_se.lib 交易部分的静态链接库。
thostmduserapi_se.dll 行情部分的动态链接库。
thostmduserapi_se.lib行情部分的静态链接库。
error.dtd 包含所有可能的错误信息。
error.xml包含所有可能的错误信息

本项目基于openctp编译的Python版CTP接口开发。
CTP原生接口的交易接口和行情接口编译成Python版接口后形成了如下文件列表:

_thostmduserapi.pyd
_thosttraderapi.pyd
thostmduserapi.py
thostmduserapi_se.dll
thosttraderapi.py
thosttraderapi_se.dll

本项目采用openctp项目中编译好的Python接口。接口地址:openctp接口文件

框架的层次结构图如下:
3FFC60F5F890D0FDBC2B8E0B2F937DE9

CTP框架的设计思路:
一、 流控
客户端主动发起的指令,如果频率过高,可能对CTP柜台造成压力,所以CTP对指令设了流量控制,流控分为4类:
1、 查询流:
在途1笔,在途的含义是指令发出到响应收完之前(bIsLast为False),只有响应收完(bIsLast为True),才可以发送下一笔查询。但1秒只能查询1次,时间上也做了频率流控。
2、 报单/撤单流:
报单/撤单设置频率流控,一般是1秒6笔,报单和撤单分开算,且不同合约分开算。
3、 FTD报文流控:
报文流控为综合流控,包括所有的指令(登录、查询、报单等),一般是1秒6笔。
4、 会话数流控:
一个登录连接称为一个会话session,一般会话数为6,即只允许账户同时登录6个会话,包括手机、电脑端的程序。同时登录账户数不超过6,超过次数的登录会被拒绝。

查询流和FTD报文流控是会话session级别的,表示一个会话的最大流量,可以通过增加登录会话突破流控,但又会受最大会话数限制。  
报单/撤单流控是投资者级别的,对所有的会话的报撤单做汇总流控,只能通过分账户突破该流控。根据最新的程序化交易管理规定,“在1个交易日内出现10次(含)以上1秒钟内10笔(含)以上报撤单的,具有实际控制关系的账户按照一个账户管理”,可能要被认定高频交易,除了需要报备,还可能取消手续费返还。  
查询流在CTP本端流控,超过流控时会被直接拒绝,不会通过网络发出,其他指令则是发向柜台后被流控,指令发向柜台后如果超过了流控,会进入排队,不会被直接拒绝,但若频率过高总是超过柜台流控,可能会被期货公司拒绝接入,所以最好也在本地做好这些流控。  
接口参数中默认流控为:_FTDmaxsize={"Session":6, "ReqQry":1,"OrderInsert":6,"OrderAction":6}  
在CTP6.7.9及以上版本中可以查询到FTD和查询流控,后台会根据查询到的数据更新Session和ReqQry  

二、 持仓、资金
策略层若没有其他持仓数据的接口来源,只能通过CTP底层读取,则需要跟随行情实时计算,跟随委托单、成交单实时计算。

三、 事前风控
策略层不能拿到持仓数据做风控,就需要在CTP底层做风控,对报单参数检查,价格是最小变动价位整数,对可用资金检查,等等。

四、 事后反馈
结果记录日志,并向策略层反馈。

五、 多线程/多进程
CTP底层、策略层、数据分析层采用多线程或多进程设计,各层分工,避免相互阻塞。

六、 业务数据及业务函数阻塞逻辑
PeopleQuant中的业务数据为持仓、委托单、成交单、账户资金、行情快照,均为字典格式,CTP推送业务数据时,后台会完成数据的更新,字典是可变数据类型,业务函数返回的数据会自动跟着更新,因此无需重复使用业务函数获取数据。
PeopleQuant中的业务函数设计为阻塞模式,即调用业务函数向CTP查询数据时,需要等待结果反馈,若查询成功,返回值会得到业务数据,若查询失败,则返回None或缺省值。
调用业务函数时,首先会从内存中查询是否已存在数据,例如合约的行情已经订阅,就直接从内存中取,无需重复订阅,持仓数据已在内存中更新,就直接返回相应持仓,无需重复向CTP查询持仓,这会大大增加执行效率,因为向CTP查询有1秒1次的查询流控,频繁查询会造成CTP更新滞后。
调用下单函数时,也会得到下单成功发出或失败的结果,下单成功发出时会返回委托单数据,下单失败会返回None,下单发到交易柜台再从柜台收到回报需要时间,这会增加执行耗时,对高频交易有影响,但及时获取到下单成败的结果,可以避免持续错误下单。
下单发向交易柜台之前,也会在本端对价格、可用手数、可用资金做下事前风控判断,避免错误的参数发向柜台。

目前本项目完成了CTP底层的基础开发,实现了交易所需的基本功能,可嵌入到其他交易平台中,利用其他平台计算的交易信号,调用本接口交易。
项目文件:

_thostmduserapi.pyd
_thosttraderapi.pyd
thostmduserapi.py
thostmduserapi_se.dll
thosttraderapi.py
thosttraderapi_se.dll
__init__.py    #包标识
ctpapi.cp38-win_amd64.pyd   #ctp接口编译文件
trade_mdforopenctp.py      #实际应用接口文件
zhuchannel.py              #接口所需的队列、线程类
zhustruct.py               #业务数据结构和K线生成类
example.py                 #策略示例
6.7.11_API接口说明.chm     #CTP接口说明

本项目推出两个Python版:旧版Python3.8,支持win7以上系统;最新版Python3.13,支持Win10以上系统。
对应两个CTP版本:6.7.2;最新版6.7.11。

安装教程

  1. 可从本项目仓库中下载所需版本。
  2. 从本项目下载ctpapi.cp38-win_amd64.pyd(ctpapi.cp313-win_amd64.pyd)、trade_mdforopenctp.py、zhuchannel.py、example.py,CTP接口文件从openctp官网下载。
    下载的所有文件放在一个文件夹即可

使用说明

  1. 字段说明:
    CTP接口中已有的字段保持原字段使用,CTP中的字段一般是大写字母开头(如InstrumentID表示合约代码),项目中新增的字段,例如持仓浮动盈亏(float_profit_long),采用小写字母开头,以做区分。
    CTP接口的字段含义可从API接口说明.chm和.h头文件中查询。

  2. 业务数据说明:
    持仓、行情、账户资金、委托单、成交单为字典格式,CTP底层推送数据时自动更新这些业务数据,使用时无需重复调用查询函数查询。

  3. 实例化:

if __name__ == '__main__':
    pqapi = PeopleQuantApi(BrokerID,UserID,PassWord,AppID, AuthCode,TradeFrontAddr,MdFrontAddr,s) #实例化
    account = pqapi.get_account()  #查询账户资金
    position1 = pqapi.get_position("FG601")  #查询持仓
    quote1 = pqapi.get_quote("FG601")    #查询行情
    position2 = pqapi.get_position("rb2601")  #查询持仓
    quote2 = pqapi.get_quote("rb2601")  #查询行情
    #print(position)
    #print(quote)
    #margin1 = p.get_symbol_marginrate("IH2509")  #查询保证金率
    margin2 = p.get_symbol_marginrate("TF2512")  #查询保证金率
    #print(margin1,"IH2509")
    print(margin2,"TF2512")
    UpdateTime = 0  #行情推送时间
    
    while True:
        #业务数据自动更新,无需重复调用查询函数查询
        if UpdateTime != quote1["UpdateTime"]:  #行情时间变动(UpdateTime字段精确到秒值)
            print(quote1["InstrumentID"],quote1["LastPrice"],quote1["UpdateTime"],)
            UpdateTime = quote1["UpdateTime"]
            print(position1 )
            print(quote2["InstrumentID"],quote2["LastPrice"],quote2["UpdateTime"],)
            print(position2 )
            print(account)
  1. 接口说明
get_account #查询账户资金
get_position #查询账户持仓
get_quote    #订阅行情
get_symbol_trade  #查询合约的成交单
get_symbol_order  #查询合约的委托单,活动单、结束单,开仓单、平仓单
get_id_order      #根据委托单编号查询委托单
get_trade_of_order  #查询委托单对应的成交单
get_order_risk      #查询订单风控,报单次数、撤单次数、开仓成交手数、自成交数、信息量、报单成交比
get_symbol_info    #查询合约属性,合约乘数、最小跳、到期日…
query_symbol_option  #查询合约对应的期权,按不同到期日分类,例如同一白糖合约有不同到期日的期权
get_option          #根据标的价格查询某档位的期权
open_close        #实用的开平仓下单及结果处理
"更多功能敬请期待……"

Metadata

Release files for peoplequant 1.8.4

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 peoplequant 1.8.4
File
peoplequant-1.8.4-cp314-none-win_amd64.whl CPython 3.14 none Windows x86-64 Details
peoplequant-1.8.4-cp314-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.14 none Linux glibc 2.17+ x86-64 Details
peoplequant-1.8.4-cp313-none-win_amd64.whl CPython 3.13 none Windows x86-64 Details
peoplequant-1.8.4-cp313-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.13 none Linux glibc 2.17+ x86-64 Details
peoplequant-1.8.4-cp312-none-win_amd64.whl CPython 3.12 none Windows x86-64 Details
peoplequant-1.8.4-cp312-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.12 none Linux glibc 2.17+ x86-64 Details
peoplequant-1.8.4-cp311-none-win_amd64.whl CPython 3.11 none Windows x86-64 Details
peoplequant-1.8.4-cp311-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.11 none Linux glibc 2.17+ x86-64 Details
peoplequant-1.8.4-cp310-none-win_amd64.whl CPython 3.10 none Windows x86-64 Details
peoplequant-1.8.4-cp310-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.10 none Linux glibc 2.17+ x86-64 Details

Total release size: 159.5 MB

Release files / peoplequant-1.8.4-cp314-none-win_amd64.whl

Download URL peoplequant-1.8.4-cp314-none-win_amd64.whl
Size 16.2 MB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
4297dcf1baafe3a760d9ae49dce9bcbbce5f361cd439636f5cf8734031eec76e
BLAKE2b-256 checksum
How to use checksums
e30359218ffe88b85436c44b63a8f5a43c20aa2b75415650b203972099354a90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.0

Release files / peoplequant-1.8.4-cp314-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL peoplequant-1.8.4-cp314-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 16.5 MB
Tags CPython 3.14 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
eff2924cca2020950352acad5e7daa7559cd6e5d86fc422a8f1c1c209abfae5f
BLAKE2b-256 checksum
How to use checksums
3991b208de1bb3fa9562f6b2ec5e0d50c16beb51a348a0ffbe65d9db69d54609
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.0

Release files / peoplequant-1.8.4-cp313-none-win_amd64.whl

Download URL peoplequant-1.8.4-cp313-none-win_amd64.whl
Size 16.1 MB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
6d409bd3c761ff50e577b3010b9eaebadf530ddb81f16aaf9ba32ba5790d8e7f
BLAKE2b-256 checksum
How to use checksums
f9a0cf51687c118ec91eab053050e7856840f03d8849ba6e7b9e130b793cb345
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.0

Release files / peoplequant-1.8.4-cp313-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL peoplequant-1.8.4-cp313-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 16.6 MB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
abd55dd18afef422a45e267d6fcb4d61dd3dc1fdd96f22b19f9f90c2a46bc55e
BLAKE2b-256 checksum
How to use checksums
b136d424204b8a31db7e9ff9cf099d994281d0977e9ad1bc473cc9d7835dffb4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.0

Release files / peoplequant-1.8.4-cp312-none-win_amd64.whl

Download URL peoplequant-1.8.4-cp312-none-win_amd64.whl
Size 16.1 MB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
7e945ce6723c9af778e2aade259e96a15a4443e55f33c8d1ea07addab436d30d
BLAKE2b-256 checksum
How to use checksums
6daba39e9d0d80fc1895581be29cfbb2671d2a81b433c46f962f1d32bb748a65
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.0

Release files / peoplequant-1.8.4-cp312-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL peoplequant-1.8.4-cp312-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 16.0 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
34f73346e18443434043cf09531847ce90eb234d0e79114fe23427b4418e2661
BLAKE2b-256 checksum
How to use checksums
87b8531b6c3754581033c2bc6e77f8c3634e2c77e17866f6ce3b6d060e27eea3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.0

Release files / peoplequant-1.8.4-cp311-none-win_amd64.whl

Download URL peoplequant-1.8.4-cp311-none-win_amd64.whl
Size 15.8 MB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
d5819b5c46d6da7c1dd41ea151edd30e10f234f2afab7bca337b1329ee6ad71d
BLAKE2b-256 checksum
How to use checksums
0f901ef34f7b02e3a9aad3e4cc7f3be2c399621dee32fa1c3a79e0e69f27275b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.0

Release files / peoplequant-1.8.4-cp311-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL peoplequant-1.8.4-cp311-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 15.6 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
e145b92b6581d1ff95091756705f964e3cb6c8c20648aff8e46cae038256d4f8
BLAKE2b-256 checksum
How to use checksums
88e4adbd4f750d1a14ac0bc09b0c62e479aab97170c81238138d3a1855c39c60
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.0

Release files / peoplequant-1.8.4-cp310-none-win_amd64.whl

Download URL peoplequant-1.8.4-cp310-none-win_amd64.whl
Size 15.8 MB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
a4879b4aabaee26aece5676d7ac43344a4967e4f6981bd9dd7b2f140cef3e3b7
BLAKE2b-256 checksum
How to use checksums
47ffa3ea10bca5dbb45dfe63ca4bf52a47cc770da4bcac795617863bcf594315
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.0

Release files / peoplequant-1.8.4-cp310-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL peoplequant-1.8.4-cp310-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 14.8 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
909ac975a834c67abbf884d3b329b169e9fc9f8428da8150830df9dd9d5113c1
BLAKE2b-256 checksum
How to use checksums
6fa7fffecd4703f0a69f0152793e010f0df9ff740625433f7fa7f43a48519540
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.0

Release history Release notifications | RSS feed

1.8.6

10 release files

1.8.5

10 release files

This release

1.8.4 This release

10 release files

1.8.3

10 release files

1.8.2

10 release files

1.8.1

10 release files

1.7.6

10 release files

1.7.5

10 release files

1.7.4

10 release files

1.7.3

10 release files

1.7.1

5 release files

1.6.8

5 release files

1.6.7

5 release files

1.6.6

5 release files

1.6.5

6 release files

1.6.4

5 release files

1.6.3

5 release files

1.6.2

5 release files

1.6.1

5 release files

1.6.0

3 release files

1.5.9

3 release files

1.5.8

3 release files

1.5.7

3 release files

1.5.6

3 release files

1.5.5

3 release files

1.5.4

3 release files

1.5.3

3 release files

1.5.2

3 release files

1.5.1

3 release files

1.5.0

3 release files

1.4.9

3 release files

1.4.8

3 release files

1.4.7

3 release files

1.4.6

3 release files

1.4.5

3 release files

1.4.4

3 release files

1.4.3

3 release files

1.4.2

3 release files

1.4.1

3 release files

1.4.0

3 release files

1.3.9

3 release files

1.3.8

3 release files

1.3.7

3 release files

1.3.6

3 release files

1.3.5

3 release files

1.3.4

3 release files

1.3.3

3 release files

1.3.2

3 release files

1.3.1

3 release files

1.3.0

3 release files

1.2.9

3 release files

1.2.8

3 release files

1.2.7

3 release files

1.2.6

3 release files

1.2.5

3 release files

1.2.4

3 release files

1.2.3

3 release files

1.2.2

3 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