2015-03-03 06:11:00 +00:00
|
|
|
|
# encoding: UTF-8
|
2015-03-02 05:05:53 +00:00
|
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
from time import sleep
|
|
|
|
|
|
|
|
|
|
from PyQt4 import QtGui
|
|
|
|
|
|
|
|
|
|
from vnltsmd import *
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def print_dict(d):
|
2015-03-03 06:11:00 +00:00
|
|
|
|
"""按照键值打印一个字典"""
|
2015-03-02 05:05:53 +00:00
|
|
|
|
for key,value in d.items():
|
|
|
|
|
print key + ':' + str(value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def simple_log(func):
|
2015-03-03 06:11:00 +00:00
|
|
|
|
"""简单装饰器用于输出函数名"""
|
2015-03-02 05:05:53 +00:00
|
|
|
|
def wrapper(*args, **kw):
|
|
|
|
|
print ""
|
|
|
|
|
print str(func.__name__)
|
|
|
|
|
return func(*args, **kw)
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
|
class TestMdApi(MdApi):
|
2015-03-03 06:11:00 +00:00
|
|
|
|
"""测试用实例"""
|
2015-03-02 05:05:53 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def __init__(self):
|
|
|
|
|
"""Constructor"""
|
|
|
|
|
super(TestMdApi, self).__init__()
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
@simple_log
|
|
|
|
|
def onFrontConnected(self):
|
2015-03-03 06:11:00 +00:00
|
|
|
|
"""服务器连接"""
|
2015-03-02 05:05:53 +00:00
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
@simple_log
|
|
|
|
|
def onFrontDisconnected(self, n):
|
2015-03-03 06:11:00 +00:00
|
|
|
|
"""服务器断开"""
|
2015-03-02 05:05:53 +00:00
|
|
|
|
print n
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
@simple_log
|
|
|
|
|
def onHeartBeatWarning(self, n):
|
2015-03-03 06:11:00 +00:00
|
|
|
|
"""心跳报警"""
|
2015-03-02 05:05:53 +00:00
|
|
|
|
print n
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
@simple_log
|
|
|
|
|
def onRspError(self, error, n, last):
|
2015-03-03 06:11:00 +00:00
|
|
|
|
"""错误"""
|
2015-03-02 05:05:53 +00:00
|
|
|
|
print_dict(error)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspUserLogin(self, data, error, n, last):
|
2015-03-03 06:11:00 +00:00
|
|
|
|
"""登陆回报"""
|
2015-03-02 05:05:53 +00:00
|
|
|
|
print_dict(data)
|
|
|
|
|
print_dict(error)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
@simple_log
|
|
|
|
|
def onRspUserLogout(self, data, error, n, last):
|
2015-03-03 06:11:00 +00:00
|
|
|
|
"""登出回报"""
|
2015-03-02 05:05:53 +00:00
|
|
|
|
print_dict(data)
|
|
|
|
|
print_dict(error)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
@simple_log
|
|
|
|
|
def onRspSubMarketData(self, data, error, n, last):
|
2015-03-03 06:11:00 +00:00
|
|
|
|
"""订阅合约回报"""
|
2015-03-02 05:05:53 +00:00
|
|
|
|
print_dict(data)
|
|
|
|
|
print_dict(error)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
@simple_log
|
|
|
|
|
def onRspUnSubMarketData(self, data, error, n, last):
|
2015-03-03 06:11:00 +00:00
|
|
|
|
"""退订合约回报"""
|
2015-03-02 05:05:53 +00:00
|
|
|
|
print_dict(data)
|
|
|
|
|
print_dict(error)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
@simple_log
|
|
|
|
|
def onRtnDepthMarketData(self, data):
|
2015-03-03 06:11:00 +00:00
|
|
|
|
"""行情推送"""
|
2015-03-02 05:05:53 +00:00
|
|
|
|
print_dict(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def main():
|
2015-03-03 06:11:00 +00:00
|
|
|
|
"""主测试函数,出现堵塞时可以考虑使用sleep"""
|
2015-03-02 05:05:53 +00:00
|
|
|
|
reqid = 0
|
|
|
|
|
|
2015-03-03 06:11:00 +00:00
|
|
|
|
# 创建Qt应用对象,用于事件循环
|
2015-03-02 05:05:53 +00:00
|
|
|
|
app = QtGui.QApplication(sys.argv)
|
|
|
|
|
|
2015-03-03 06:11:00 +00:00
|
|
|
|
# 创建API对象
|
2015-03-02 05:05:53 +00:00
|
|
|
|
api = TestMdApi()
|
|
|
|
|
|
2015-03-03 06:11:00 +00:00
|
|
|
|
# 在C++环境中创建MdApi对象,传入参数是希望用来保存.con文件的地址
|
2015-03-02 05:05:53 +00:00
|
|
|
|
api.createFtdcMdApi('')
|
|
|
|
|
|
2015-03-03 06:11:00 +00:00
|
|
|
|
# 注册前置机地址
|
|
|
|
|
api.registerFront("tcp://211.144.195.163:34513")
|
2015-03-02 05:05:53 +00:00
|
|
|
|
|
2015-03-03 06:11:00 +00:00
|
|
|
|
# 初始化api,连接前置机
|
2015-03-02 05:05:53 +00:00
|
|
|
|
api.init()
|
|
|
|
|
sleep(0.5)
|
|
|
|
|
|
2015-03-03 06:11:00 +00:00
|
|
|
|
# 登陆
|
|
|
|
|
loginReq = {} # 创建一个空字典
|
|
|
|
|
loginReq['UserID'] = '' # 参数作为字典键值的方式传入
|
|
|
|
|
loginReq['Password'] = '' # 键名和C++中的结构体成员名对应
|
|
|
|
|
loginReq['BrokerID'] = ''
|
|
|
|
|
reqid = reqid + 1 # 请求数必须保持唯一性
|
2015-03-02 05:05:53 +00:00
|
|
|
|
i = api.reqUserLogin(loginReq, 1)
|
|
|
|
|
sleep(0.5)
|
|
|
|
|
|
2015-03-03 06:11:00 +00:00
|
|
|
|
## 登出
|
2015-03-02 05:05:53 +00:00
|
|
|
|
#reqid = reqid + 1
|
|
|
|
|
#i = api.reqUserLogout({}, 1)
|
|
|
|
|
#sleep(0.5)
|
|
|
|
|
|
2015-03-03 06:11:00 +00:00
|
|
|
|
# 获取交易日,目前输出为空
|
2015-03-02 05:05:53 +00:00
|
|
|
|
#day = api.getTradingDay()
|
|
|
|
|
#print 'Trading Day is:' + str(day)
|
|
|
|
|
#sleep(0.5)
|
|
|
|
|
|
2015-03-03 06:11:00 +00:00
|
|
|
|
# 订阅合约
|
2015-03-02 05:05:53 +00:00
|
|
|
|
subReq = {}
|
2015-03-03 06:11:00 +00:00
|
|
|
|
subReq['InstrumentID'] = '11000061'
|
2015-03-02 05:05:53 +00:00
|
|
|
|
subReq['ExchangeID'] = 'SSE'
|
|
|
|
|
i = api.subscribeMarketData(subReq)
|
|
|
|
|
|
2015-03-03 06:11:00 +00:00
|
|
|
|
## 退订合约
|
2015-03-02 05:05:53 +00:00
|
|
|
|
#i = api.unSubscribeMarketData(subReq)
|
|
|
|
|
|
2015-03-03 06:11:00 +00:00
|
|
|
|
# 连续运行,用于输出行情
|
2015-03-02 05:05:53 +00:00
|
|
|
|
app.exec_()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|