-
Notifications
You must be signed in to change notification settings - Fork 155
/
trader_main.py
executable file
·143 lines (101 loc) · 4.34 KB
/
trader_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
# encoding:utf-8
"""
(Copyright) 2018, Winton Wang <365504029@qq.com>
ctpwrapper is free software: you can redistribute it and/or modify
it under the terms of the GNU LGPLv3 as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ctpwrapper. If not, see <http://www.gnu.org/licenses/>.
"""
import socket
import urllib.parse
from contextlib import closing
from ctpwrapper import ApiStructure
from ctpwrapper import TraderApiPy
def check_address_port(tcp):
"""
:param tcp:
:return:
"""
host_schema = urllib.parse.urlparse(tcp)
ip = host_schema.hostname
port = host_schema.port
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
if sock.connect_ex((ip, port)) == 0:
return True # OPEN
else:
return False # closed
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)
def main():
investor_id = "089303"
broker_id = "9999"
password = "198759"
server = "tcp://180.168.146.187:10001"
if check_address_port(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()
else:
print("trader server down")
if __name__ == "__main__":
main()