Git Product home page Git Product logo

webctp's Introduction

webctp

webctp是一个基于 openctp-ctp 开发的提供websocket接口的CTP服务。



安装及运行

环境搭建

  1. 准备Python环境(3.10, 其他版本未测试)

  2. 克隆 webctp

    $ git clone https://github.com/openctp/webctp.git
    $ cd webctp
  3. 安装依赖库

    $ pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host=pypi.tuna.tsinghua.edu.cn

    📌 默认使用 openctp-ctp 6.7.2.*, 可通过 requirements.txt 进行修改。

  4. 自定义配置文件
    参考示例 config.example.yaml

    📌 示例中行情和交易前置地址,默认配置的是 SimNow 7x24 环境, 更多 SimNow 环境参考 openctp环境监控,可根据需要变更为其他支持CTPAPI(官方实现)的柜台环境。

    创建自己的行情配置 config_md.yaml :

    TdFrontAddress: tcp://180.168.146.187:10130 # 交易前置地址
    MdFrontAddress: tcp://180.168.146.187:10131 # 行情前置地址
    BrokerID: "9999"
    AuthCode: "0000000000000000"
    AppID: simnow_client_test
    Port: 8080         # the listening port, default 8080
    Host: 0.0.0.0      # the bind ip address, default 0.0.0.0
    LogLevel: INFO     # NOTSET, DEBUG, INFO, WARN, ERROR, CRITICAL

    创建自己的交易配置 config_td.yaml :

    TdFrontAddress: tcp://180.168.146.187:10130 # 交易前置地址
    MdFrontAddress: tcp://180.168.146.187:10131 # 行情前置地址
    BrokerID: "9999"
    AuthCode: "0000000000000000"
    AppID: simnow_client_test
    Port: 8081         # the listening port, default 8081
    Host: 0.0.0.0      # the bind ip address, default 0.0.0.0
    LogLevel: INFO     # NOTSET, DEBUG, INFO, WARN, ERROR, CRITICAL

运行

# 启动交易服务
$ python main.py --config=config_td.yaml --app_type=td
# 启动行情服务
$ python main.py --config=config_md.yaml --app_type=md

请求示例

TODO: 添加postman的请求样例

Apifox 示例(部分)

示例是基于 SimNow 电信1环境, 不同环境的数据存在差异,以下示例数据未必可全部通过, 根据环境调整即可。

登录 login
请求查询成交 qry_trade
请求查询投资者持仓 qry_investor_position
请求查询资金账户 qry_trading_account
请求查询投资者 qry_investor
请求查询交易编码 qry_trading_code
查询合约保证金率 qry_instrument_margin_rate
请求查询合约手续费率 qry_instrument_commission_rate
查询期权合约手续费
查询期权交易成本
查询报单手续费率
查询交易所保证金率 qry_exchange_margin_rate
查询投资者持仓明细 qry_investor_position_detail
查询行情 qry_depth_market_data
查询产品 qry_product
查询交易所 qry_exchange
查询合约 qry_instrument
查询报单 qr_order
查询最大报单数量 qry_max_order_volume
用户口令变更 update_password
报单录入(限价单) order_insert
报单撤销 order_action

协议

通用协议格式

# 请求
{
  "MsgType": "{method_name}",
  "{request_field}": {
    "filed1": {value1},
    "...": "...",
    "fieldn": {valuen}
  },
  "RequestID": 1
}

# 响应
{
    "MsgType": "{rsp_of_method}",
    "RspInfo": {
        "ErrorID": 0,
        "ErrorMsg": "OK"
    },
    "IsLast": true,
    "RequestID": 1
    "{response_filed}": {response_body}  # 具体参见详细文档
}

部分通用错误码说明

ErrorID="-400" ErrorMsg="参数有误"
ErrorID="-401" ErrorMsg="未登录"
ErrorID="-404" ErrorMsg="Webctp还未实现该方法"
ErrorID="-1" ErrorMsg="CTP:请求失败"
ErrorID="-2" ErrorMsg="CTP:未处理请求超过许可数"
ErrorID="-3" ErrorMsg="CTP:每秒发送请求数超过许可数"
ErrorID="0" ErrorMsg="CTP:正确"
ErrorID="1" ErrorMsg="CTP:不在已同步状态"
ErrorID="2" ErrorMsg="CTP:会话信息不一致"
ErrorID="3" ErrorMsg="CTP:不合法的登录"
ErrorID="4" ErrorMsg="CTP:用户不活跃"
ErrorID="5" ErrorMsg="CTP:重复的登录"
ErrorID="6" ErrorMsg="CTP:还没有登录"
ErrorID="7" ErrorMsg="CTP:还没有初始化"
ErrorID="8" ErrorMsg="CTP:前置不活跃"
ErrorID="9" ErrorMsg="CTP:无此权限"
ErrorID="10" ErrorMsg="CTP:修改别人的口令"
ErrorID="11" ErrorMsg="CTP:找不到该用户"
ErrorID="12" ErrorMsg="CTP:找不到该经纪公司"
ErrorID="13" ErrorMsg="CTP:找不到投资者"
ErrorID="14" ErrorMsg="CTP:原口令不匹配"
ErrorID="15" ErrorMsg="CTP:报单字段有误"
ErrorID="16" ErrorMsg="CTP:找不到合约"

详细接口文档

交易服务协议文档

行情服务协议文档

开发说明

TODO

其他说明

  • 由于精力有限,只进行了SimNow平台的简单的测试,请自行充分测试后再接入生产环境。
  • 使用webctp进行实盘交易的后果完全有使用者自行承担。

webctp's People

Contributors

dennisxie avatar hcisgood avatar jedore avatar krenx1983 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

webctp's Issues

通过pypi进行发布

通过pip install webctp进行安装,用户可以引用webctp中的app进行自定义的启动。

添加启动脚本

添加各个平台的启动脚本,方便一键启动分为如下几个脚本

# Windows
webctp_md.ps1 -c configure_file
webctp_td.ps1 -c configure_file
# Linux
webctp_md.sh -c configure_file
webctp_td.sh -c configure_file
# MacOS
webctp_md.sh -c configure_file
webctp_td.sh -c configure_file

订阅行情和取消订阅行情的应答数据格式有误

期望为

{
  "MsgType": "OnRspUnSubscribeMarketData",
  "SpecificInstrument": {
    "InstrumentID": "au2305"
  },
  "RspInfo": {
    "ErrorID": 0,
    "ErrorMsg": ""
  },
  "RequestID": 0,
  "IsLast": true
}

实际是

{
    "MsgType": "OnRspSubMarketData",
    "RspInfo": {
        "ErrorID": 0,
        "ErrorMsg": "CTP:No Error"
    },
    "RequestID": 0,
    "IsLast": true,
    "SpecificInstrument": "ag2306"
}

Unit test

  • For clients package.
  • For service package.

补充行情接口的相关API和SPI

请另外建一个issue说明在补充哪些API和SPI,新建的issue请引用本issue并在本issue下comment,然后把开发中的API和SPI填写在开发中一栏,merge以后再改到已有中。

已有

  • login
  • SubscribeMarketData
  • OnRspSubscribeMarketData
  • UnSubscribeMarketData
  • OnRspUnSubMarketData
  • OnRtnDepthMarketData

开发中

未开发

  • GetTradingDay
  • RegisterFensUserInfo
  • RegisterNameServer
  • ReqQryMulticastInstrument
  • ReqUserLogout
  • SubscribeForQuoteRsp
  • UnSubscribeForQuoteRsp
  • OnHeartBeatWarning
  • OnRspError
  • OnRspQryMulticastInstrument
  • OnRspSubForQuoteRsp
  • OnRspUnSubForQuoteRsp
  • OnRspUserLogout
  • OnRtnForQuoteRsp

查询成交、持仓、资金账户、投资者、交易编码、保证金率和手续费率的API和SPI函数

相关issue #5
///请求查询成交
virtual int ReqQryTrade(CThostFtdcQryTradeField *pQryTrade, int nRequestID) = 0;

///请求查询投资者持仓
virtual int ReqQryInvestorPosition(CThostFtdcQryInvestorPositionField *pQryInvestorPosition, int nRequestID) = 0;

///请求查询资金账户
virtual int ReqQryTradingAccount(CThostFtdcQryTradingAccountField *pQryTradingAccount, int nRequestID) = 0;

///请求查询投资者
virtual int ReqQryInvestor(CThostFtdcQryInvestorField *pQryInvestor, int nRequestID) = 0;

///请求查询交易编码
virtual int ReqQryTradingCode(CThostFtdcQryTradingCodeField *pQryTradingCode, int nRequestID) = 0;

///请求查询合约保证金率
virtual int ReqQryInstrumentMarginRate(CThostFtdcQryInstrumentMarginRateField *pQryInstrumentMarginRate, int nRequestID) = 0;

///请求查询合约手续费率
virtual int ReqQryInstrumentCommissionRate(CThostFtdcQryInstrumentCommissionRateField *pQryInstrumentCommissionRate, int nRequestID) = 0;

Repeated client

Now one connection(websocket) has one client(md/td).
If the same user login by multi clients, then server will create multi same clients which are repeated.
Can webctp avoid the repeated resources ?

添加报单相关以及口令更新的API和SPI方法

关联单#5

///用户口令更新请求
virtual int ReqUserPasswordUpdate(CThostFtdcUserPasswordUpdateField *pUserPasswordUpdate, int nRequestID) = 0;
///报单录入请求
virtual int ReqOrderInsert(CThostFtdcInputOrderField *pInputOrder, int nRequestID) = 0;
///报单操作请求
virtual int ReqOrderAction(CThostFtdcInputOrderActionField *pInputOrderAction, int nRequestID) = 0;
///查询最大报单数量请求
virtual int ReqQueryMaxOrderVolume(CThostFtdcQueryMaxOrderVolumeField *pQueryMaxOrderVolume, int nRequestID) = 0;
///请求查询报单
virtual int ReqQryOrder(CThostFtdcQryOrderField *pQryOrder, int nRequestID) = 0;

补充交易接口的API和SPI

请另外建一个issue说明在补充哪些API和SPI,新建的issue请引用本issue并在本issue下comment,然后把开发中的API和SPI填写在开发中一栏,merge以后再改到已有中。

已有

  • login
  • ReqQryInstrument
  • OnRspReqQryInstrument

开发中

  • ReqUserPasswordUpdate
  • OnRspUserPasswordUpdate
  • ReqOrderInsert
  • OnRspOrderInsert
  • OnErrRtnOrderInsert
  • ReqOrderAction
  • OnRspOrderAction
  • OnErrRtnOrderAction
  • ReqQryMaxOrderVolume
  • OnRspQryMaxOrderVolume
  • ReqQryOrder
  • OnRspQryOrder

未开发

  • GetApiVersion
  • GetTradingDay
  • RegisterFensUserInfo
  • RegisterNameServer
  • RegisterUserSystemInfo
  • ReqBatchOrderAction
  • ReqCombActionInsert
  • ReqExecOrderAction
  • ReqExecOrderInsert
  • ReqForQuoteInsert
  • ReqFromBankToFutureByFuture
  • ReqFromFutureToBankByFuture
  • ReqGenUserCaptcha
  • ReqGenUserText
  • ReqOptionSelfCloseAction
  • ReqOptionSelfCloseInsert
  • ReqParkedOrderAction
  • ReqParkedOrderInsert
  • ReqQryAccountregister
  • ReqQryBrokerTradingAlgos
  • ReqQryBrokerTradingParams
  • ReqQryCFMMCTradingAccountKey
  • ReqQryClassifiedInstrument
  • ReqQryCombAction
  • ReqQryCombInstrumentGuard
  • ReqQryCombPromotionParam
  • ReqQryContractBank
  • ReqQryDepthMarketData
  • ReqQryEWarrantOffset
  • ReqQryExchange
  • ReqQryExchangeMarginRate
  • ReqQryExchangeMarginRateAdjust
  • ReqQryExchangeRate
  • ReqQryExecOrder
  • ReqQryForQuote
  • ReqQryInstrumentCommissionRate
  • ReqQryInstrumentMarginRate
  • ReqQryInstrumentOrderCommRate
  • ReqQryInvestUnit
  • ReqQryInvestor
  • ReqQryInvestorPosition
  • ReqQryInvestorPositionCombineDetail
  • ReqQryInvestorPositionDetail
  • ReqQryInvestorProductGroupMargin
  • ReqQryMMInstrumentCommissionRate
  • ReqQryMMOptionInstrCommRate
  • ReqQryNotice
  • ReqQryOptionInstrCommRate
  • ReqQryOptionInstrTradeCost
  • ReqQryOptionSelfClose
  • ReqQryParkedOrder
  • ReqQryParkedOrderAction
  • ReqQryProduct
  • ReqQryProductExchRate
  • ReqQryProductGroup
  • ReqQryQuote
  • ReqQryRiskSettleInvstPosition
  • ReqQryRiskSettleProductStatus
  • ReqQrySecAgentACIDMap
  • ReqQrySecAgentCheckMode
  • ReqQrySecAgentTradeInfo
  • ReqQrySecAgentTradingAccount
  • ReqQrySettlementInfo
  • ReqQrySettlementInfoConfirm
  • ReqQryTrade
  • ReqQryTraderOffer
  • ReqQryTradingAccount
  • ReqQryTradingCode
  • ReqQryTradingNotice
  • ReqQryTransferBank
  • ReqQryTransferSerial
  • ReqQueryBankAccountMoneyByFuture
  • ReqQueryCFMMCTradingAccountToken
  • ReqQuoteAction
  • ReqQuoteInsert
  • ReqRemoveParkedOrder
  • ReqRemoveParkedOrderAction
  • ReqSettlementInfoConfirm
  • ReqTradingAccountPasswordUpdate
  • ReqUserAuthMethod
  • ReqUserLoginWithCaptcha
  • ReqUserLoginWithOTP
  • ReqUserLoginWithText
  • ReqUserLogout
  • SubmitUserSystemInfo
  • SubscribePrivateTopic
  • SubscribePublicTopic

  • OnErrRtnBankToFutureByFuture
  • OnErrRtnBatchOrderAction
  • OnErrRtnCombActionInsert
  • OnErrRtnExecOrderAction
  • OnErrRtnExecOrderInsert
  • OnErrRtnForQuoteInsert
  • OnErrRtnFutureToBankByFuture
  • OnErrRtnOptionSelfCloseAction
  • OnErrRtnOptionSelfCloseInsert
  • OnErrRtnQueryBankBalanceByFuture
  • OnErrRtnQuoteAction
  • OnErrRtnQuoteInsert
  • OnErrRtnRepealBankToFutureByFutureManual
  • OnErrRtnRepealFutureToBankByFutureManual
  • OnHeartBeatWarning
  • OnRspBatchOrderAction
  • OnRspCombActionInsert
  • OnRspError
  • OnRspExecOrderAction
  • OnRspExecOrderInsert
  • OnRspForQuoteInsert
  • OnRspFromBankToFutureByFuture
  • OnRspFromFutureToBankByFuture
  • OnRspGenUserCaptcha
  • OnRspGenUserText
  • OnRspOptionSelfCloseAction
  • OnRspOptionSelfCloseInsert
  • OnRspParkedOrderAction
  • OnRspParkedOrderInsert
  • OnRspQryAccountregister
  • OnRspQryBrokerTradingAlgos
  • OnRspQryBrokerTradingParams
  • OnRspQryCFMMCTradingAccountKey
  • OnRspQryClassifiedInstrument
  • OnRspQryCombAction
  • OnRspQryCombInstrumentGuard
  • OnRspQryCombPromotionParam
  • OnRspQryContractBank
  • OnRspQryDepthMarketData
  • OnRspQryEWarrantOffset
  • OnRspQryExchange
  • OnRspQryExchangeMarginRate
  • OnRspQryExchangeMarginRateAdjust
  • OnRspQryExchangeRate
  • OnRspQryExecOrder
  • OnRspQryForQuote
  • OnRspQryInstrumentCommissionRate
  • OnRspQryInstrumentMarginRate
  • OnRspQryInstrumentOrderCommRate
  • OnRspQryInvestUnit
  • OnRspQryInvestor
  • OnRspQryInvestorPosition
  • OnRspQryInvestorPositionCombineDetail
  • OnRspQryInvestorPositionDetail
  • OnRspQryInvestorProductGroupMargin
  • OnRspQryMMInstrumentCommissionRate
  • OnRspQryMMOptionInstrCommRate
  • OnRspQryNotice
  • OnRspQryOptionInstrCommRate
  • OnRspQryOptionInstrTradeCost
  • OnRspQryOptionSelfClose
  • OnRspQryParkedOrder
  • OnRspQryParkedOrderAction
  • OnRspQryProduct
  • OnRspQryProductExchRate
  • OnRspQryProductGroup
  • OnRspQryQuote
  • OnRspQryRiskSettleInvstPosition
  • OnRspQryRiskSettleProductStatus
  • OnRspQrySecAgentACIDMap
  • OnRspQrySecAgentCheckMode
  • OnRspQrySecAgentTradeInfo
  • OnRspQrySecAgentTradingAccount
  • OnRspQrySettlementInfo
  • OnRspQrySettlementInfoConfirm
  • OnRspQryTrade
  • OnRspQryTraderOffer
  • OnRspQryTradingAccount
  • OnRspQryTradingCode
  • OnRspQryTradingNotice
  • OnRspQryTransferBank
  • OnRspQryTransferSerial
  • OnRspQueryBankAccountMoneyByFuture
  • OnRspQueryCFMMCTradingAccountToken
  • OnRspQuoteAction
  • OnRspQuoteInsert
  • OnRspRemoveParkedOrder
  • OnRspRemoveParkedOrderAction
  • OnRspSettlementInfoConfirm
  • OnRspTradingAccountPasswordUpdate
  • OnRspUserAuthMethod
  • OnRspUserLogout
  • OnRtnBulletin
  • OnRtnCFMMCTradingAccountToken
  • OnRtnCancelAccountByBank
  • OnRtnChangeAccountByBank
  • OnRtnCombAction
  • OnRtnErrorConditionalOrder
  • OnRtnExecOrder
  • OnRtnForQuoteRsp
  • OnRtnFromBankToFutureByBank
  • OnRtnFromBankToFutureByFuture
  • OnRtnFromFutureToBankByBank
  • OnRtnFromFutureToBankByFuture
  • OnRtnInstrumentStatus
  • OnRtnOpenAccountByBank
  • OnRtnOptionSelfClose
  • OnRtnOrder
  • OnRtnQueryBankBalanceByFuture
  • OnRtnQuote
  • OnRtnRepealFromBankToFutureByBank
  • OnRtnRepealFromBankToFutureByFuture
  • OnRtnRepealFromBankToFutureByFutureManual
  • OnRtnRepealFromFutureToBankByBank
  • OnRtnRepealFromFutureToBankByFuture
  • OnRtnRepealFromFutureToBankByFutureManual
  • OnRtnTrade
  • OnRtnTradingNotice

Add request validation code.

Requirements:

  • Can easily define the validation strategy.
  • Easy to use.
  • Better to throw an exception with the response info, then we can catch it and return it in the connection.

查询交易所、产品、行情、持仓明细、保证金率、手续费、交易成本和合约手续费API及SPI函数

相关issue #5

///请求查询交易所
virtual int ReqQryExchange(CThostFtdcQryExchangeField *pQryExchange, int nRequestID) = 0;

///请求查询产品
virtual int ReqQryProduct(CThostFtdcQryProductField *pQryProduct, int nRequestID) = 0;

///请求查询合约
virtual int ReqQryInstrument(CThostFtdcQryInstrumentField *pQryInstrument, int nRequestID) = 0;

///请求查询行情
virtual int ReqQryDepthMarketData(CThostFtdcQryDepthMarketDataField *pQryDepthMarketData, int nRequestID) = 0;
///请求查询投资者持仓明细
virtual int ReqQryInvestorPositionDetail(CThostFtdcQryInvestorPositionDetailField *pQryInvestorPositionDetail, int nRequestID) = 0;
///请求查询交易所保证金率
virtual int ReqQryExchangeMarginRate(CThostFtdcQryExchangeMarginRateField *pQryExchangeMarginRate, int nRequestID) = 0;
///请求查询报单手续费
virtual int ReqQryInstrumentOrderCommRate(CThostFtdcQryInstrumentOrderCommRateField *pQryInstrumentOrderCommRate, int nRequestID) = 0;
///请求查询期权交易成本
virtual int ReqQryOptionInstrTradeCost(CThostFtdcQryOptionInstrTradeCostField *pQryOptionInstrTradeCost, int nRequestID) = 0;

///请求查询期权合约手续费
virtual int ReqQryOptionInstrCommRate(CThostFtdcQryOptionInstrCommRateField *pQryOptionInstrCommRate, int nRequestID) = 0;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.