2019-04-18 23:00:23 +00:00
|
|
|
|
# 交易接口
|
|
|
|
|
|
|
|
|
|
## 如何连接
|
|
|
|
|
|
2019-05-01 03:22:39 +00:00
|
|
|
|
从gateway文件夹上引入接口程序,通过add_gateway()函数调动,最终展示到图形化操作界面VN Trader中。
|
|
|
|
|
|
|
|
|
|
在菜单栏中点击"系统"->"连接CTP”按钮会弹出如图账号配置窗口,输入账号、密码等相关信息即连接接口,并立刻进行查询工作: 如查询账号信息、查询持仓、查询委托信息、查询成交信息等。
|
|
|
|
|
|
|
|
|
|
![](https://vnpy-community.oss-cn-shanghai.aliyuncs.com/forum_experience/yazhang/gateway/login.png)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-18 23:00:23 +00:00
|
|
|
|
### 加载需要用的接口
|
|
|
|
|
|
2019-05-01 03:22:39 +00:00
|
|
|
|
加载接口示例在根目录"tests\trader"文件夹的run.py文件中。
|
|
|
|
|
- 从gateway文件夹引入接口类文件,如from vnpy.gateway.ctp import CtpGateway;
|
|
|
|
|
- 创建事件引擎对象并且通过add_gateway()函数添加接口程序;
|
|
|
|
|
- 创建图形化对象main_window,以VN Trader操作界面展示出来。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
from vnpy.gateway.ctp import CtpGateway
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
""""""
|
|
|
|
|
qapp = create_qapp()
|
|
|
|
|
main_engine = MainEngine(event_engine)
|
|
|
|
|
main_engine.add_gateway(CtpGateway)
|
|
|
|
|
main_window = MainWindow(main_engine, event_engine)
|
|
|
|
|
main_window.showMaximized()
|
|
|
|
|
qapp.exec()
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-18 23:00:23 +00:00
|
|
|
|
|
|
|
|
|
### VN Trader界面操作
|
|
|
|
|
|
2019-05-01 03:22:39 +00:00
|
|
|
|
打开cmd窗口,使用命令“Python run.py"即可进入VN Trader操作界面。在左上方的菜单栏中点击"系统"->"连接CTP”按钮会弹出账号配置窗口,输入账号、密码等相关信息即连接接口。
|
|
|
|
|
|
|
|
|
|
连接接口的流程首先是初始化账户信息,然后调用connet()函数来连接交易端口和行情端口。
|
|
|
|
|
- 交易端口:查询用户相关信息(如账户资金、持仓、委托记录、成交记录)、查询可交易合约信息、挂撤单操作;
|
|
|
|
|
- 行情端口:接收订阅的行情信息推送、接收用户相关信息(如账户资金更新、持仓更新、委托推送、成交推送)更新的回调推送。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-18 23:00:23 +00:00
|
|
|
|
|
|
|
|
|
### 修改json配置文件
|
|
|
|
|
|
2019-05-01 03:22:39 +00:00
|
|
|
|
接口配置相关保存在json文件中,放在如图C盘用户目录下的.vntrader文件夹内。
|
|
|
|
|
|
|
|
|
|
![](https://vnpy-community.oss-cn-shanghai.aliyuncs.com/forum_experience/yazhang/gateway/.vntrader.png)
|
|
|
|
|
|
|
|
|
|
所以要修改接口配置文件,用户即可以在图形化界面VN Trader内修改,也可以直接在.vntrader修改json文件。
|
|
|
|
|
另外将json配置文件分离于vnpy的好处在于:避免每次升级都要重新配置json文件。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-18 23:00:23 +00:00
|
|
|
|
|
|
|
|
|
### 可交易的合约查看
|
|
|
|
|
|
2019-05-01 03:22:39 +00:00
|
|
|
|
先登录接口,然后在菜单栏中点击"帮助"->"查询合约”按钮会空白的“查询合约”窗口。点击“查询”按钮后才会显示查询结果,如图。
|
|
|
|
|
|
|
|
|
|
![](https://vnpy-community.oss-cn-shanghai.aliyuncs.com/forum_experience/yazhang/gateway/query_contract.png)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-18 23:00:23 +00:00
|
|
|
|
|
|
|
|
|
## 接口分类
|
|
|
|
|
|
2019-05-01 03:22:39 +00:00
|
|
|
|
| 接口 |类型 |
|
|
|
|
|
| ------ | :------: |
|
|
|
|
|
| CTP | 期货 |
|
|
|
|
|
| OES | 国内股票 |
|
|
|
|
|
| IB | 外盘股票、期货、期权 |
|
|
|
|
|
| FUTU | 国内股票、港股、美股 |
|
|
|
|
|
| TIGER | 国内股票、港股、美股 |
|
|
|
|
|
| BITMEX | 数字货币 |
|
|
|
|
|
| OKEX | 数字货币 |
|
|
|
|
|
| HUOBI | 数字货币 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-18 23:00:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 接口详解
|
|
|
|
|
|
2019-05-01 03:22:39 +00:00
|
|
|
|
### CTP
|
|
|
|
|
|
|
|
|
|
#### 如何加载
|
|
|
|
|
|
|
|
|
|
run.py文件提供了接口加载示例:先从gateway上调用ctpGateway类;然后通过add_gateway()函数添加到main_engine上。
|
|
|
|
|
```
|
|
|
|
|
from vnpy.gateway.ctp import CtpGateway
|
|
|
|
|
main_engine.add_gateway(CtpGateway)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 相关字段
|
|
|
|
|
|
|
|
|
|
- 用户名:username
|
|
|
|
|
- 密码:password:
|
|
|
|
|
- 经纪商编号:brokerid
|
|
|
|
|
- 交易服务器地址:td_address
|
|
|
|
|
- 行情服务器地址:md_address
|
|
|
|
|
- 产品名称:product_info
|
|
|
|
|
- 授权编码:auth_code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 获取账号
|
|
|
|
|
|
|
|
|
|
- 仿真账号:从SimNow网站上获取。只需输入手机号码和短信验证即可。(短信验证有时只能在工作日正常工作时段收到)SimNow的用户名为6位纯数字,经纪商编号为9999,并且提供2套环境用于盘中仿真交易以及盘后的测试。
|
|
|
|
|
|
|
|
|
|
- 实盘账号:在期货公司开户,通过联系客户经理可以开通。用户名为纯数字,经纪商编号也是4位纯数字。(每个期货公司的经纪商编号都不同)另外,实盘账号也可以开通仿真交易功能,同样需要联系客户经理。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 宽睿柜台(OES)
|
2019-04-18 23:00:23 +00:00
|
|
|
|
|
|
|
|
|
#### 如何加载
|
|
|
|
|
|
2019-05-01 03:22:39 +00:00
|
|
|
|
先从gateway上调用OesGateway类;然后通过add_gateway()函数添加到main_engine上。
|
|
|
|
|
```
|
|
|
|
|
from vnpy.gateway.oes import OesGateway
|
|
|
|
|
main_engine.add_gateway(OesGateway)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-18 23:00:23 +00:00
|
|
|
|
#### 相关字段
|
|
|
|
|
|
2019-05-01 03:22:39 +00:00
|
|
|
|
- 用户名:username
|
|
|
|
|
- 密码:password
|
|
|
|
|
- 硬盘序列号:hdd_serial
|
|
|
|
|
- 交易委托服务器:td_ord_server
|
|
|
|
|
- 交易回报服务器:td_rpt_server
|
|
|
|
|
- 交易查询服务器:td_qry_server
|
|
|
|
|
- 行情推送服务器:md_tcp_server
|
|
|
|
|
- 行情查询服务器:md_qry_server
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-18 23:00:23 +00:00
|
|
|
|
#### 获取账号
|
|
|
|
|
|
2019-05-01 03:22:39 +00:00
|
|
|
|
测试账号请联系宽睿科技申请
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-18 23:00:23 +00:00
|
|
|
|
#### 其他特点
|
|
|
|
|
|
2019-05-01 03:22:39 +00:00
|
|
|
|
宽睿柜台提供内网UDP低延时组播行情以及实时成交信息推送。
|
|
|
|
|
|
|
|
|
|
|
2019-04-18 23:00:23 +00:00
|
|
|
|
|
2019-05-01 03:22:39 +00:00
|
|
|
|
### 盈透证券(IB)
|
2019-04-18 23:00:23 +00:00
|
|
|
|
|
|
|
|
|
#### 如何加载
|
|
|
|
|
|
2019-05-01 03:22:39 +00:00
|
|
|
|
先从gateway上调用IbGateway类;然后通过add_gateway()函数添加到main_engine上。
|
|
|
|
|
```
|
|
|
|
|
from vnpy.gateway.oes import IbGateway
|
|
|
|
|
main_engine.add_gateway(IbGateway)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-18 23:00:23 +00:00
|
|
|
|
#### 相关字段
|
|
|
|
|
|
2019-05-01 03:22:39 +00:00
|
|
|
|
- TWS地址:127.0.0.1
|
|
|
|
|
- TWS端口:7497
|
|
|
|
|
- 客户号:1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-18 23:00:23 +00:00
|
|
|
|
#### 获取账号
|
|
|
|
|
|
2019-05-01 03:22:39 +00:00
|
|
|
|
在盈透证券开户并且入金后可以获得API接入权限。拥有实盘账号后才可以申请开通仿真交易账号。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-18 23:00:23 +00:00
|
|
|
|
#### 其他特点
|
2019-05-01 03:22:39 +00:00
|
|
|
|
可交易品种几乎覆盖全球的股票、期权、期权;手续费相对较低。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 富途证券(FUTU)
|
|
|
|
|
|
|
|
|
|
#### 如何加载
|
|
|
|
|
|
|
|
|
|
先从gateway上调用FutuGateway类;然后通过add_gateway()函数添加到main_engine上。
|
|
|
|
|
```
|
|
|
|
|
from vnpy.gateway.oes import FutuGateway
|
|
|
|
|
main_engine.add_gateway(FutuGateway)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 相关字段
|
|
|
|
|
|
|
|
|
|
- 地址:127.0.0.1
|
|
|
|
|
- 密码:
|
|
|
|
|
- 端口:11111
|
|
|
|
|
- 市场:HK 或 US
|
|
|
|
|
- 环境:TrdEnv.REAL 或 TrdEnv.SIMULATE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 获取账号
|
|
|
|
|
|
|
|
|
|
在富途证券开户并且入金后可以获得API接入权限。拥有实盘账号后才可以申请开通仿真交易账号。
|
|
|
|
|
|
|
|
|
|
|
2019-04-18 23:00:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-05-01 03:22:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 老虎证券(TIGER)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 如何加载
|
|
|
|
|
|
|
|
|
|
先从gateway上调用TigerGateway类;然后通过add_gateway()函数添加到main_engine上。
|
|
|
|
|
```
|
|
|
|
|
from vnpy.gateway.oes import TigerGateway
|
|
|
|
|
main_engine.add_gateway(TigerGateway)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 相关字段
|
|
|
|
|
|
|
|
|
|
- 用户ID:tiger_id
|
|
|
|
|
- 环球账户:account
|
|
|
|
|
- 标准账户:standard_account
|
|
|
|
|
- 秘钥:private_key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 获取账号
|
|
|
|
|
|
|
|
|
|
在老虎证券开户并且入金后可以获得API接入权限。拥有实盘账号后才可以申请开通仿真交易账号。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# BITMEX
|
|
|
|
|
|
|
|
|
|
#### 如何加载
|
|
|
|
|
|
|
|
|
|
先从gateway上调用BitmexGateway类;然后通过add_gateway()函数添加到main_engine上。
|
|
|
|
|
```
|
|
|
|
|
from vnpy.gateway.oes import BitmexGateway
|
|
|
|
|
main_engine.add_gateway(BitmexGateway)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 相关字段
|
|
|
|
|
|
|
|
|
|
- 用户ID:ID
|
|
|
|
|
- 密码:Secret
|
|
|
|
|
- 会话数:3
|
|
|
|
|
- 服务器:REAL 或 TESTNET
|
|
|
|
|
- 代理地址:
|
|
|
|
|
- 代理端口:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 获取账号
|
|
|
|
|
|
|
|
|
|
在BITMEX官网开户并且入金后可以获得API接入权限。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-18 23:00:23 +00:00
|
|
|
|
|
|
|
|
|
### OKEX
|
|
|
|
|
|
|
|
|
|
|
2019-05-01 03:22:39 +00:00
|
|
|
|
#### 如何加载
|
|
|
|
|
|
|
|
|
|
先从gateway上调用OkexGateway类;然后通过add_gateway()函数添加到main_engine上。
|
|
|
|
|
```
|
|
|
|
|
from vnpy.gateway.oes import OkexGateway
|
|
|
|
|
main_engine.add_gateway(OkexGateway)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 相关字段
|
|
|
|
|
|
|
|
|
|
- API秘钥:API Key
|
|
|
|
|
- 密码秘钥:Secret Key
|
|
|
|
|
- 会话数:3
|
|
|
|
|
- 密码:passphrase
|
|
|
|
|
- 代理地址:
|
|
|
|
|
- 代理端口:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 获取账号
|
|
|
|
|
|
|
|
|
|
在OKEX官网开户并且入金后可以获得API接入权限。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 火币
|
|
|
|
|
|
|
|
|
|
#### 如何加载
|
|
|
|
|
|
|
|
|
|
先从gateway上调用HuobiGateway类;然后通过add_gateway()函数添加到main_engine上。
|
|
|
|
|
```
|
|
|
|
|
from vnpy.gateway.oes import HuobiGateway
|
|
|
|
|
main_engine.add_gateway(HuobiGateway)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 相关字段
|
|
|
|
|
|
|
|
|
|
- API秘钥:API Key
|
|
|
|
|
- 密码秘钥:Secret Key
|
|
|
|
|
- 会话数:3
|
|
|
|
|
- 代理地址:
|
|
|
|
|
- 代理端口:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 获取账号
|
|
|
|
|
|
|
|
|
|
在火币官网开户并且入金后可以获得API接入权限。
|