Skip to content

Commit

Permalink
Merge pull request #47 from nooperpudd/bugfix/test
Browse files Browse the repository at this point in the history
bug fix and md_main test demo
  • Loading branch information
nooperpudd authored Sep 2, 2020
2 parents 4ea1e4f + 0ef9083 commit fd285e7
Show file tree
Hide file tree
Showing 24 changed files with 1,504 additions and 1,237 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
__pycache__/
*.py[cod]
*$py.class
*.cpp
*.con
ctpwrapper/*.so


# Distribution / packaging
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ language: python
matrix:
include:
# others still work and don't install python3.7
- python: pypy3
- python: pypy3.6-7.3.1
sudo: required
- python: 3.5
sudo: required
Expand Down
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM python:3.8
RUN pip3 install cython pip setuptools --upgrade
WORKDIR /code
198 changes: 9 additions & 189 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
[![implementation](https://img.shields.io/pypi/implementation/ctpwrapper.svg)](https://pypi.python.org/pypi/ctpwrapper)
[![Downloads](https://pepy.tech/badge/ctpwrapper)](https://pepy.tech/project/ctpwrapper)

[CTP Website](http://www.sfit.com.cn/5_2_DocumentDown.htm)
[CTP Website](http://www.sfit.com.cn/5_2_DocumentDown_1.htm)

Version: v6.3.19 P1
Version: v6.3.19 P1(v6.3.19_P1_20200106)

Platform: Linux 64bit, Windows 64bit

Expand All @@ -34,196 +34,16 @@ already install cython package.

<img src="images/alipay.png" width="250" height="250"><img src="images/wechat.jpg" width="250" height="250">

## ⚠️⚠️ notice ⚠️⚠️
sometimes quote the market futures data, but there is no trading data from the API stream,
better just add `time.sleep(2)` func call after `Init()` method invoked, this could help to solve no data response issue.

## Demo
sample code [samples](samples/)

# Usage

MdApi Class Wrapper

```python
import sys

from ctpwrapper import ApiStructure
from ctpwrapper import MdApiPy


class Md(MdApiPy):
"""
"""

def __init__(self, broker_id, investor_id, password, request_id=1):

self.broker_id = broker_id
self.investor_id = investor_id
self.password = password
self.request_id = request_id

def OnRspError(self, pRspInfo, nRequestID, bIsLast):

self.ErrorRspInfo(pRspInfo, nRequestID)

def ErrorRspInfo(self, info, request_id):
"""
:param info:
:return:
"""
if info.ErrorID != 0:
print('request_id=%s ErrorID=%d, ErrorMsg=%s',
request_id, info.ErrorID, info.ErrorMsg.decode('gbk'))
return info.ErrorID != 0

def OnFrontConnected(self):
"""
:return:
"""

user_login = ApiStructure.ReqUserLoginField(BrokerID=self.broker_id,
UserID=self.investor_id,
Password=self.password)
self.ReqUserLogin(user_login, self.request_id)

def OnFrontDisconnected(self, nReason):

print("Md OnFrontDisconnected %s", nReason)
sys.exit()

def OnHeartBeatWarning(self, nTimeLapse):
"""心跳超时警告。当长时间未收到报文时,该方法被调用。
@param nTimeLapse 距离上次接收报文的时间
"""
print('Md OnHeartBeatWarning, time = %s', nTimeLapse)

def OnRspUserLogin(self, pRspUserLogin, pRspInfo, nRequestID, bIsLast):
"""
用户登录应答
:param pRspUserLogin:
:param pRspInfo:
:param nRequestID:
:param bIsLast:
:return:
"""
if pRspInfo.ErrorID != 0:
print("Md OnRspUserLogin failed error_id=%s msg:%s",
pRspInfo.ErrorID, pRspInfo.ErrorMsg.decode('gbk'))
else:
print("Md user login successfully")
print(pRspUserLogin)
print(pRspInfo)


BORDKER_ID = ""
INVESTOR_ID = ""
PASSWORD = ""
SERVER = ""

md = Md(BORDKER_ID, INVESTOR_ID, PASSWORD)
md.Create()
md.RegisterFront(SERVER)
md.Init()
day = md.GetTradingDay()

print(day)
print("api worker!")
```

Trader Class Wrapper

```python

from ctpwrapper import ApiStructure
from ctpwrapper import TraderApiPy

class Trader(TraderApiPy):

def __init__(self, broker_id, investor_id, password, request_id=1):
self.request_id = request_id
self.broker_id = broker_id.encode()
self.investor_id = investor_id.encode()
self.password = password.encode()

def OnRspError(self, pRspInfo, nRequestID, bIsLast):

self.ErrorRspInfo(pRspInfo, nRequestID)

def ErrorRspInfo(self, info, request_id):
"""
:param info:
:return:
"""
if info.ErrorID != 0:
print('request_id=%s ErrorID=%d, ErrorMsg=%s',
request_id, info.ErrorID, info.ErrorMsg.decode('gbk'))
return info.ErrorID != 0

def OnHeartBeatWarning(self, nTimeLapse):
"""心跳超时警告。当长时间未收到报文时,该方法被调用。
@param nTimeLapse 距离上次接收报文的时间
"""
print("on OnHeartBeatWarning time: ", nTimeLapse)

def OnFrontDisconnected(self, nReason):
print("on FrontDisConnected disconnected", nReason)

def OnFrontConnected(self):

req = ApiStructure.ReqUserLoginField(BrokerID=self.broker_id,
UserID=self.investor_id,
Password=self.password)
self.ReqUserLogin(req, self.request_id)
print("trader on front connection")

def OnRspUserLogin(self, pRspUserLogin, pRspInfo, nRequestID, bIsLast):

if pRspInfo.ErrorID != 0:
print("Md OnRspUserLogin failed error_id=%s msg:%s",
pRspInfo.ErrorID, pRspInfo.ErrorMsg.decode('gbk'))
else:
print("Md user login successfully")

inv = ApiStructure.QryInvestorField(BrokerID=self.broker_id, InvestorID=self.investor_id)

self.ReqQryInvestor(inv, self.inc_request_id())
req = ApiStructure.SettlementInfoConfirmField.from_dict({"BrokerID": self.broker_id,
"InvestorID": self.investor_id})

self.ReqSettlementInfoConfirm(req, self.inc_request_id())

def OnRspSettlementInfoConfirm(self, pSettlementInfoConfirm, pRspInfo, nRequestID, bIsLast):
print(pSettlementInfoConfirm, pRspInfo)
print(pRspInfo.ErrorMsg.decode("GBK"))

def inc_request_id(self):
self.request_id += 1
return self.request_id

def OnRspQryInvestor(self, pInvestor, pRspInfo, nRequestID, bIsLast):
print(pInvestor, pRspInfo)

if __name__ == "__main__":
investor_id = ""
broker_id = ""
password = ""
server = ""

user_trader = Trader(broker_id=broker_id, investor_id=investor_id, password=password)

user_trader.Create()
user_trader.RegisterFront(server)
user_trader.SubscribePrivateTopic(2) # 只传送登录后的流内容
user_trader.SubscribePrivateTopic(2) # 只传送登录后的流内容

user_trader.Init()

print("trader started")
print(user_trader.GetTradingDay())

user_trader.Join()

```

# Documentation
CTP's documentation can be found in the [docs](doc/ctp/)


CTP documentation can be found in the [docs](doc/ctp/)

# Contact

Expand Down
Loading

0 comments on commit fd285e7

Please sign in to comment.