Skip to content

Commit cafbddb

Browse files
committed
添加股票期权Python接口示例
1 parent 0574893 commit cafbddb

6 files changed

Lines changed: 140 additions & 8 deletions

File tree

‎languages/Python/config_default.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf-8 -*-
33
configs = {
44
'root_dir': r'd:' + '\\test_ctp',
5-
'api': {
5+
'md': {
66
'Address': br'tcp://218.202.237.33:10012',
77
'BrokerID': b'9999',
88
'UserID': b'654321',

‎languages/Python/config_override.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33
configs = {
4-
'api': {
4+
'md': {
55
'UserID': b'037505',
66
'Password': b'123456',
77
},

‎languages/Python/test_ctp/某用户/config_ctp.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
configs = {
1818
'root_dir': r'D:\Users\Kan\Documents\GitHub\XAPI3\languages\Python\test_ctp\某用户',
19-
'api': {
19+
'md': {
2020
'Address': br'tcp://180.168.146.187:10110',
2121
'BrokerID': b'9999',
2222
'UserID': b'654321',
@@ -28,7 +28,7 @@
2828
'AppID': b'8500342533',
2929
'AuthCode': b'0000000000000000',
3030
'UserID': b'14078611',
31-
'Password': b'zaq1xsw2',
31+
'Password': b'123456',
3232
},
3333
}
3434

‎languages/Python/test_ctp_api.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ def init(config):
1616
td = XApi(r'C:\Program Files\SmartQuant Ltd\OpenQuant 2014\XAPI\x64\XAPI_CPP_x64.dll')
1717
td.ServerInfo.Address = config['td']['Address']
1818
td.ServerInfo.BrokerID = config['td']['BrokerID']
19+
# td.ServerInfo.AppID = config['td']['AppID']
20+
# td.ServerInfo.AuthCode = config['td']['AuthCode']
1921
td.UserInfo.UserID = config['td']['UserID']
2022
td.UserInfo.Password = config['td']['Password']
2123

2224
# 行情连接
2325
md = XApi(r'C:\Program Files\SmartQuant Ltd\OpenQuant 2014\XAPI\x64\XAPI_CPP_x64.dll')
24-
md.ServerInfo.Address = config['api']['Address']
25-
md.ServerInfo.BrokerID = config['api']['BrokerID']
26-
md.UserInfo.UserID = config['api']['UserID']
27-
md.UserInfo.Password = config['api']['Password']
26+
md.ServerInfo.Address = config['md']['Address']
27+
md.ServerInfo.BrokerID = config['md']['BrokerID']
28+
md.UserInfo.UserID = config['md']['UserID']
29+
md.UserInfo.Password = config['md']['Password']
2830

2931
ret = td.init(br'C:\Program Files\SmartQuant Ltd\OpenQuant 2014\XAPI\x64\CTP_SE\CTP_SE_Trade_x64.dll')
3032
if not ret:
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
import sys
4+
5+
sys.path.append(r'D:\Users\Kan\Documents\GitHub\XAPI3\languages\Python')
6+
7+
# 解决Python 3.6的pandas不支持中文路径的问题
8+
print(sys.getfilesystemencoding()) # 查看修改前的
9+
try:
10+
sys._enablelegacywindowsfsencoding() # 修改
11+
print(sys.getfilesystemencoding()) # 查看修改后的
12+
except:
13+
pass
14+
15+
from test_ctp_sopt_api import main
16+
17+
configs = {
18+
'root_dir': r'D:\Users\Kan\Documents\GitHub\XAPI3\languages\Python\test_ctp_sopt\某用户',
19+
'md': {
20+
'Address': br'tcp://124.74.247.142:18213',
21+
'BrokerID': b'2001',
22+
'UserID': b'00000020781',
23+
'Password': b'123456',
24+
},
25+
'td': {
26+
'Address': br'tcp://124.74.247.142:18205',
27+
'BrokerID': b'2001',
28+
'AppID': b'1.3.0',
29+
'AuthCode': b'EG9FWWGUYV2U292T',
30+
'UserID': b'00000020781',
31+
'Password': b'123456',
32+
},
33+
}
34+
35+
if __name__ == '__main__':
36+
main(configs)
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
调用CTP接口进行目标持仓调整示例
5+
"""
6+
import getopt
7+
from MyXSpi import MyXSpi
8+
from xapi import *
9+
10+
11+
# 从当前config类中加载配置文件
12+
# import config
13+
14+
def init(config):
15+
# 交易连接
16+
td = XApi(r'C:\Program Files\SmartQuant Ltd\OpenQuant 2014\XAPI\x64\XAPI_CPP_x64.dll')
17+
td.ServerInfo.Address = config['td']['Address']
18+
td.ServerInfo.BrokerID = config['td']['BrokerID']
19+
td.ServerInfo.AppID = config['td']['AppID']
20+
td.ServerInfo.AuthCode = config['td']['AuthCode']
21+
td.UserInfo.UserID = config['td']['UserID']
22+
td.UserInfo.Password = config['td']['Password']
23+
24+
# 行情连接
25+
md = XApi(r'C:\Program Files\SmartQuant Ltd\OpenQuant 2014\XAPI\x64\XAPI_CPP_x64.dll')
26+
md.ServerInfo.Address = config['md']['Address']
27+
md.ServerInfo.BrokerID = config['md']['BrokerID']
28+
md.UserInfo.UserID = config['md']['UserID']
29+
md.UserInfo.Password = config['md']['Password']
30+
31+
ret = td.init(br'C:\Program Files\SmartQuant Ltd\OpenQuant 2014\XAPI\x64\CTP_SOPT_SE\CTP_SOPT_SE_Trade_x64.dll')
32+
if not ret:
33+
print(td.get_last_error())
34+
exit(-1)
35+
print(ord(td.get_api_type()))
36+
print(td.get_api_name())
37+
print(td.get_api_version())
38+
39+
ret = md.init(br'C:\Program Files\SmartQuant Ltd\OpenQuant 2014\XAPI\x64\CTP_SOPT_SE\CTP_SOPT_SE_Quote_x64.dll')
40+
if not ret:
41+
print(md.get_last_error())
42+
exit(-1)
43+
44+
print(ord(md.get_api_type()))
45+
print(md.get_api_name())
46+
print(md.get_api_version())
47+
48+
return td, md
49+
50+
51+
def main(configs):
52+
print(sys.stdin.encoding)
53+
td, md = init(configs)
54+
spi = MyXSpi(root_dir=configs['root_dir'],
55+
portfolios=['portfolio_1.csv', 'portfolio_2.csv', 'portfolio_3.csv'],
56+
td=td, md=md, is_stock=False)
57+
spi.connect()
58+
# 在这最好等那么一会儿
59+
spi.wait_logined(15, 2)
60+
print("登录等待结束")
61+
62+
# 参数的依次执行,由于等待的问题存在,1-6怎么处理
63+
opts, args = getopt.getopt(sys.argv[1:], "", ["input="])
64+
for op, value in opts:
65+
print("参数", op, value)
66+
if op == "--input":
67+
input_str = value
68+
input_list = input_str.split(';')
69+
for x in input_list:
70+
print(x)
71+
i = int(x)
72+
spi.input(i)
73+
# exit(0)
74+
75+
while True:
76+
spi.usage()
77+
x = input('请输入:\r\n')
78+
79+
# 输入数字时,python3是str,python2是int,需要处理一下
80+
print(x, type(x))
81+
82+
if x == "q":
83+
break
84+
85+
# 处理输入是回车或别符号的可能性
86+
try:
87+
i = int(x)
88+
except:
89+
continue
90+
91+
spi.input(i)
92+
93+
# 循环外,退出
94+
spi.disconnect()

0 commit comments

Comments
 (0)