{"id":20317360,"url":"https://github.com/openctp/webctp","last_synced_at":"2025-08-21T03:32:54.750Z","repository":{"id":115824038,"uuid":"603033127","full_name":"openctp/webctp","owner":"openctp","description":"将CTP接口转换成websocket+json协议对外通讯，适合web类应用。","archived":false,"fork":false,"pushed_at":"2024-11-24T10:05:21.000Z","size":305,"stargazers_count":45,"open_issues_count":10,"forks_count":13,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-12-09T00:12:26.644Z","etag":null,"topics":["ctp","ctpapi","finance","fintech","futures","openctp","options","quant","stocks","trader"],"latest_commit_sha":null,"homepage":"http://www.openctp.cn","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/openctp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-17T13:34:14.000Z","updated_at":"2024-12-08T15:51:12.000Z","dependencies_parsed_at":null,"dependency_job_id":"a3a36105-9fb8-4168-a027-c7707270cce0","html_url":"https://github.com/openctp/webctp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openctp%2Fwebctp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openctp%2Fwebctp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openctp%2Fwebctp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openctp%2Fwebctp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/openctp","download_url":"https://codeload.github.com/openctp/webctp/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230487844,"owners_count":18233865,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ctp","ctpapi","finance","fintech","futures","openctp","options","quant","stocks","trader"],"created_at":"2024-11-14T18:31:25.687Z","updated_at":"2024-12-19T19:08:01.114Z","avatar_url":"https://github.com/openctp.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# webctp\n\nwebctp是一个基于 [openctp-ctp](https://github.com/openctp/openctp-ctp-python) 开发的提供websocket+json接口的CTP服务。\n\n---\n\n* [安装及运行](#安装及运行)\n    * [环境搭建](#环境搭建)\n    * [运行](#运行)\n* [请求示例](#请求示例)\n* [Web Demo](#Web-Demo)\n* [协议](#协议)\n    * [通用协议格式](#通用协议格式)\n    * [部分通用错误码说明](#部分通用错误码说明)\n* [开发说明](#开发说明)\n* [其他说明](#其他说明)\n\n---\n\n## 安装及运行\n\n### 环境搭建\n\n1. 准备Python环境(3.10, 其他版本未测试)\n2. 克隆 webctp\n   ```bash\n   $ git clone https://github.com/openctp/webctp.git\n   $ cd webctp\n   ```\n3. 安装依赖库\n   ```bash\n   $ pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host=pypi.tuna.tsinghua.edu.cn\n   ```\n   \u003e :pushpin: 默认使用 openctp-ctp 6.7.2.*, 可通过 requirements.txt 进行修改。\n\n4. 自定义配置文件  \n   参考示例 config.example.yaml\n   \u003e :pushpin: 示例中行情和交易前置地址，默认配置的是 SimNow 7x24 环境， 更多 SimNow\n   环境参考 [openctp环境监控](http://121.37.80.177)，可根据需要变更为其他支持CTPAPI(官方实现)的柜台环境。\n\n   创建自己的行情配置 config_md.yaml :\n   ```yaml \n   TdFrontAddress: tcp://180.168.146.187:10130 # 交易前置地址\n   MdFrontAddress: tcp://180.168.146.187:10131 # 行情前置地址\n   BrokerID: \"9999\"\n   AuthCode: \"0000000000000000\"\n   AppID: simnow_client_test\n   Port: 8080         # the listening port, default 8080\n   Host: 0.0.0.0      # the bind ip address, default 0.0.0.0\n   LogLevel: INFO     # NOTSET, DEBUG, INFO, WARN, ERROR, CRITICAL\n   ```\n   创建自己的交易配置 config_td.yaml :\n   ```yaml \n   TdFrontAddress: tcp://180.168.146.187:10130 # 交易前置地址\n   MdFrontAddress: tcp://180.168.146.187:10131 # 行情前置地址\n   BrokerID: \"9999\"\n   AuthCode: \"0000000000000000\"\n   AppID: simnow_client_test\n   Port: 8081         # the listening port, default 8081\n   Host: 0.0.0.0      # the bind ip address, default 0.0.0.0\n   LogLevel: INFO     # NOTSET, DEBUG, INFO, WARN, ERROR, CRITICAL\n   ```\n\n### 运行\n\n```bash\n# 启动交易服务\n$ python main.py --config=config_td.yaml --app_type=td\n# 启动行情服务\n$ python main.py --config=config_md.yaml --app_type=md\n```\n\n## 请求示例\n\nTODO: 添加postman的请求样例\n\n### Apifox 示例（部分）\n\n示例是基于 [SimNow 电信1环境](http://openctp.cn/report/Simnow%A3%A8CTP%A3%A9%B7%C2%D5%E6%BB%B7%BE%B31-%B5%E7%D0%C5process%D0%D0%C7%E9.html),\n不同环境的数据存在差异，以下示例数据未必可全部通过, 根据环境调整即可。\n\n\u003cdetails\u003e\n\u003csummary\u003e登录\u003c/summary\u003e\n\n\u003cimg width=\"900\" alt=\"login\" src=\"https://github.com/openctp/webctp/assets/17944025/de9f2cd4-d3eb-4b6d-b150-d274cf4d1a01\"\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e请求查询成交\u003c/summary\u003e\n\n\u003cimg width=\"973\" alt=\"qry_trade\" src=\"https://github.com/openctp/webctp/assets/17944025/a754788d-5eaa-429f-81bb-d444502ee89a\"\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e请求查询投资者持仓\u003c/summary\u003e\n\n\u003cimg width=\"978\" alt=\"qry_investor_position\" src=\"https://github.com/openctp/webctp/assets/17944025/e219ccdd-d48b-4567-bbb2-6a3aa2c68ae2\"\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e请求查询资金账户\u003c/summary\u003e\n\n\u003cimg width=\"976\" alt=\"qry_trading_account\" src=\"https://github.com/openctp/webctp/assets/17944025/42858761-c3fe-411c-9af7-fd248c238a77\"\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e请求查询投资者\u003c/summary\u003e\n\n\u003cimg width=\"977\" alt=\"qry_investor\" src=\"https://github.com/openctp/webctp/assets/17944025/d8a7cb1f-a997-48e7-ad77-81d5f261c880\"\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e请求查询交易编码\u003c/summary\u003e\n\n\u003cimg width=\"975\" alt=\"qry_trading_code\" src=\"https://github.com/openctp/webctp/assets/17944025/ee46abc5-8481-4643-98a1-e6d923e003e8\"\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e查询合约保证金率\u003c/summary\u003e\n\n\u003cimg width=\"973\" alt=\"qry_instrument_margin_rate\" src=\"https://github.com/openctp/webctp/assets/17944025/96707a3f-4fef-4118-aa46-373968dac3fa\"\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e请求查询合约手续费率\u003c/summary\u003e\n\n\u003cimg width=\"975\" alt=\"qry_instrument_commission_rate\" src=\"https://github.com/openctp/webctp/assets/17944025/b5e84d60-bfb9-4efd-8e4b-e50bd2e3873d\"\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e查询期权合约手续费\u003c/summary\u003e\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e查询期权交易成本\u003c/summary\u003e\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e查询报单手续费率\u003c/summary\u003e\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e查询交易所保证金率\u003c/summary\u003e\n\n\u003cimg width=\"976\" alt=\"qry_exchange_margin_rate\" src=\"https://github.com/openctp/webctp/assets/17944025/2b790cc3-8521-4713-92cb-b9a2cf1f2a48\"\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e查询投资者持仓明细\u003c/summary\u003e\n\n\u003cimg width=\"977\" alt=\"qry_investor_position_detail\" src=\"https://github.com/openctp/webctp/assets/17944025/8bef07b8-5024-4499-b6a7-aa754b1d69cd\"\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e查询行情\u003c/summary\u003e\n\n\u003cimg width=\"974\" alt=\"qry_depth_market_data\" src=\"https://github.com/openctp/webctp/assets/17944025/1efe7bca-7166-4c22-8d4a-750054890d6a\"\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e查询产品\u003c/summary\u003e\n\n\u003cimg width=\"977\" alt=\"qry_product\" src=\"https://github.com/openctp/webctp/assets/17944025/398fc449-a6b4-487e-a62c-37314d0cee52\"\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e查询交易所\u003c/summary\u003e\n\n\u003cimg width=\"974\" alt=\"qry_exchange\" src=\"https://github.com/openctp/webctp/assets/17944025/24ac101d-fadb-48e8-b0e2-8698ec802f84\"\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e查询合约\u003c/summary\u003e\n\n\u003cimg width=\"979\" alt=\"qry_instrument\" src=\"https://github.com/openctp/webctp/assets/17944025/12860cda-aa82-44ce-a05b-4f666c27b3ab\"\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e查询报单\u003c/summary\u003e\n\n\u003cimg width=\"897\" alt=\"qr_order\" src=\"https://github.com/openctp/webctp/assets/17944025/faad7bbb-f5fa-40c3-a4b8-f74334d3bc2a\"\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e查询最大报单数量\u003c/summary\u003e\n\n\u003cimg width=\"898\" alt=\"qry_max_order_volume\" src=\"https://github.com/openctp/webctp/assets/17944025/dbb71d38-55c9-472c-9ad7-58280d894292\"\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e用户口令变更\u003c/summary\u003e\n\n\u003cimg width=\"899\" alt=\"update_password\" src=\"https://github.com/openctp/webctp/assets/17944025/3c3fa526-acf0-407c-9975-488f5c2c446d\"\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e报单录入（限价单）\u003c/summary\u003e\n\n\u003cimg width=\"900\" alt=\"order_insert\" src=\"https://github.com/openctp/webctp/assets/17944025/5d7edf22-e15b-4f38-9aef-6341f2d2b165\"\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e报单撤销\u003c/summary\u003e\n\n\u003cimg width=\"898\" alt=\"order_action\" src=\"https://github.com/openctp/webctp/assets/17944025/a0f8117a-ec7a-4793-854e-54595c8ba885\"\u003e\n\u003c/details\u003e\n\n## Web Demo\n\n提供了一个基本 [Web 客户端示例](web/index.html)，展示了对 webctp 的基本应用。\n\u003cdetails\u003e\n\u003csummary\u003eweb客户端截图\u003c/summary\u003e\n\n\u003cimg width=\"898\" alt=\"3847705585b4f6cfb09b19b33584c4a\" src=\"https://github.com/user-attachments/assets/740e3635-f5f7-4a49-9afb-c4ff5c20f23c\"\u003e\n\u003cimg width=\"1278\" alt=\"76001cbd587ea4464fdd56ac6312304\" src=\"https://github.com/user-attachments/assets/263ca2ba-036e-4216-bf2d-0e36222a59f3\"\u003e\n\u003c/details\u003e\n\n## 协议\n\n### 通用协议格式\n\n``` python\n# 请求\n{\n  \"MsgType\": \"{method_name}\",\n  \"{request_field}\": {\n    \"filed1\": {value1},\n    \"...\": \"...\",\n    \"fieldn\": {valuen}\n  },\n  \"RequestID\": 1\n}\n\n# 响应\n{\n    \"MsgType\": \"{rsp_of_method}\",\n    \"RspInfo\": {\n        \"ErrorID\": 0,\n        \"ErrorMsg\": \"OK\"\n    },\n    \"IsLast\": true,\n    \"RequestID\": 1\n    \"{response_filed}\": {response_body}  # 具体参见详细文档\n}\n```\n\n### 部分通用错误码说明\n\n```bash\nErrorID=\"-400\" ErrorMsg=\"参数有误\"\nErrorID=\"-401\" ErrorMsg=\"未登录\"\nErrorID=\"-404\" ErrorMsg=\"Webctp还未实现该方法\"\nErrorID=\"-1\" ErrorMsg=\"CTP:请求失败\"\nErrorID=\"-2\" ErrorMsg=\"CTP:未处理请求超过许可数\"\nErrorID=\"-3\" ErrorMsg=\"CTP:每秒发送请求数超过许可数\"\nErrorID=\"0\" ErrorMsg=\"CTP:正确\"\nErrorID=\"1\" ErrorMsg=\"CTP:不在已同步状态\"\nErrorID=\"2\" ErrorMsg=\"CTP:会话信息不一致\"\nErrorID=\"3\" ErrorMsg=\"CTP:不合法的登录\"\nErrorID=\"4\" ErrorMsg=\"CTP:用户不活跃\"\nErrorID=\"5\" ErrorMsg=\"CTP:重复的登录\"\nErrorID=\"6\" ErrorMsg=\"CTP:还没有登录\"\nErrorID=\"7\" ErrorMsg=\"CTP:还没有初始化\"\nErrorID=\"8\" ErrorMsg=\"CTP:前置不活跃\"\nErrorID=\"9\" ErrorMsg=\"CTP:无此权限\"\nErrorID=\"10\" ErrorMsg=\"CTP:修改别人的口令\"\nErrorID=\"11\" ErrorMsg=\"CTP:找不到该用户\"\nErrorID=\"12\" ErrorMsg=\"CTP:找不到该经纪公司\"\nErrorID=\"13\" ErrorMsg=\"CTP:找不到投资者\"\nErrorID=\"14\" ErrorMsg=\"CTP:原口令不匹配\"\nErrorID=\"15\" ErrorMsg=\"CTP:报单字段有误\"\nErrorID=\"16\" ErrorMsg=\"CTP:找不到合约\"\n```\n\n### 详细接口文档\n\n[交易服务协议文档](./docs/td_protocol.md)\n\n[行情服务协议文档](./docs/md_protocol.md)\n\n# 开发说明\n\nTODO\n\n# 其他说明\n\n* 由于精力有限，只进行了SimNow平台的简单的测试，请自行充分测试后再接入生产环境。\n* 使用webctp进行实盘交易的后果完全有使用者自行承担。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenctp%2Fwebctp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopenctp%2Fwebctp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenctp%2Fwebctp/lists"}