|
| 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