完成了LTS TD的测试脚本,并在封装的API调用python回调函数时添加了try...catch...语法,用于实现捕捉python异常,防止状态未知的API崩溃

This commit is contained in:
WOLF 2015-03-03 14:11:00 +08:00
parent a9ff04a808
commit 32c8061852
11 changed files with 3221 additions and 265 deletions

View File

@ -505,56 +505,130 @@ struct MdApiWrap : MdApi, wrapper < MdApi >
{
virtual void onFrontConnected()
{
//在向python环境中调用回调函数推送数据前需要先获取全局锁GIL防止解释器崩溃
PyLock lock;
//以下的try...catch...可以实现捕捉python环境中错误的功能防止C++直接出现原因未知的崩溃
try
{
this->get_override("onFrontConnected")();
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onFrontDisconnected(int i)
{
PyLock lock;
try
{
this->get_override("onFrontDisconnected")(i);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onHeartBeatWarning(int i)
{
PyLock lock;
try
{
this->get_override("onHeartBeatWarning")(i);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspError(dict data, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspError")(data, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspUserLogin(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspUserLogin")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspUserLogout(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspUserLogout")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspSubMarketData(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspSubMarketData")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspUnSubMarketData(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspUnSubMarketData")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRtnDepthMarketData(dict data)
{
PyLock lock;
try
{
this->get_override("onRtnDepthMarketData")(data);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
};

View File

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

Binary file not shown.

View File

@ -1,83 +0,0 @@
# encoding = UTF-8
from PyQt4 import QtGui
import sys
from time import sleep
from vnltstd import *
def printdict(error):
for key, value in error.items():
print key + ':' + str(value)
class MyTdApi(TdApi):
def __init__(self):
super(MyTdApi, self).__init__()
def onFrontConnected(self):
print 'connected'
def onFrontDisconnected(self, i):
print 'disconnected'
def onHeartBeatWarning(self, i):
print 'heartbeat'
def onRspError(self, data, id_, last):
print 'error'
print data
def onRspUserLogin(self, data, error, id_, last) :
print 'login'
printdict(data)
printdict(error)
def onRspUserLogout(self, data, error, id_, last):
print 'logout'
printdict(data)
printdict(error)
#----------------------------------------------------------------------
def onRspQryInstrument(self, data, error, id_, last):
""""""
printdict(data)
#----------------------------------------------------------------------
def onRspQryInvestor(self, data, error, id_, last):
""""""
printdict(data)
def main():
app = QtGui.QApplication(sys.argv)
api = MyTdApi()
api.createFtdcTraderApi('')
api.registerFront("tcp://101.231.210.1:24505")
api.init()
sleep(1.0)
lgreq = {}
lgreq['UserID'] = '010000058678'
lgreq['Password'] = '0656032'
lgreq['BrokerID'] = '2011'
i = api.reqUserLogin(lgreq, 1)
print i
sleep(1.0)
#req2 = {}
#req2['InstrumentID'] = '600600'
#req2['ExchangeID'] = 'SSE'
#api.subscribeMarketData(req2)
ireq = {}
#a = api.reqQryInstrument(ireq, 3)
b = api.reqQryInvestor(ireq, 3)
#print 'a is:'+ str(a)
app.exec_()
if __name__ == '__main__':
main()

Binary file not shown.

View File

@ -322,6 +322,8 @@ void TdApi::OnRspQryTradingAccount(CSecurityFtdcTradingAccountField *pTradingAcc
};
void TdApi::OnRspQryDepthMarketData(CSecurityFtdcDepthMarketDataField *pDepthMarketData, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if (pDepthMarketData) //手动修改因为有可能出现pDepthMarketData为空指针的情况
{
Task task = Task();
task.task_name = ONRSPQRYDEPTHMARKETDATA;
@ -339,6 +341,7 @@ void TdApi::OnRspQryDepthMarketData(CSecurityFtdcDepthMarketDataField *pDepthMar
task.task_id = nRequestID;
task.task_last = bIsLast;
this->task_queue.push(task);
}
};
void TdApi::OnRspQryBondInterest(CSecurityFtdcBondInterestField *pBondInterest, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
@ -382,6 +385,8 @@ void TdApi::OnRspQryMarketRationInfo(CSecurityFtdcMarketRationInfoField *pMarket
};
void TdApi::OnRspQryInstrumentCommissionRate(CSecurityFtdcInstrumentCommissionRateField *pInstrumentCommissionRate, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if (pInstrumentCommissionRate) //手动修改,因为有可能出现空指针的情况
{
Task task = Task();
task.task_name = ONRSPQRYINSTRUMENTCOMMISSIONRATE;
@ -399,6 +404,7 @@ void TdApi::OnRspQryInstrumentCommissionRate(CSecurityFtdcInstrumentCommissionRa
task.task_id = nRequestID;
task.task_last = bIsLast;
this->task_queue.push(task);
}
};
void TdApi::OnRspQryETFInstrument(CSecurityFtdcETFInstrumentField *pETFInstrument, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
@ -482,6 +488,8 @@ void TdApi::OnRspQrySFInstrument(CSecurityFtdcSFInstrumentField *pSFInstrument,
};
void TdApi::OnRspQryOrder(CSecurityFtdcOrderField *pOrder, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if (pOrder) //手动修改因为有可能出现pOrder为空指针的情况
{
Task task = Task();
task.task_name = ONRSPQRYORDER;
@ -499,9 +507,12 @@ void TdApi::OnRspQryOrder(CSecurityFtdcOrderField *pOrder, CSecurityFtdcRspInfoF
task.task_id = nRequestID;
task.task_last = bIsLast;
this->task_queue.push(task);
}
};
void TdApi::OnRspQryTrade(CSecurityFtdcTradeField *pTrade, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if (pTrade) //手动修改因为有可能出现pTrade为空指针的情况
{
Task task = Task();
task.task_name = ONRSPQRYTRADE;
@ -519,9 +530,12 @@ void TdApi::OnRspQryTrade(CSecurityFtdcTradeField *pTrade, CSecurityFtdcRspInfoF
task.task_id = nRequestID;
task.task_last = bIsLast;
this->task_queue.push(task);
}
};
void TdApi::OnRspQryInvestorPosition(CSecurityFtdcInvestorPositionField *pInvestorPosition, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if (pInvestorPosition) //手动修改,防止空指针
{
Task task = Task();
task.task_name = ONRSPQRYINVESTORPOSITION;
@ -539,6 +553,7 @@ void TdApi::OnRspQryInvestorPosition(CSecurityFtdcInvestorPositionField *pInvest
task.task_id = nRequestID;
task.task_last = bIsLast;
this->task_queue.push(task);
}
};
void TdApi::OnRtnOrder(CSecurityFtdcOrderField *pOrder)
@ -648,6 +663,8 @@ void TdApi::OnRtnFundInByBank(CSecurityFtdcFundTransferField *pFundTransfer)
};
void TdApi::OnRspQryFundTransferSerial(CSecurityFtdcFundTransferField *pFundTransfer, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if (pFundTransfer) //手动修改,防止空指针
{
Task task = Task();
task.task_name = ONRSPQRYFUNDTRANSFERSERIAL;
@ -665,6 +682,7 @@ void TdApi::OnRspQryFundTransferSerial(CSecurityFtdcFundTransferField *pFundTran
task.task_id = nRequestID;
task.task_last = bIsLast;
this->task_queue.push(task);
}
};
void TdApi::OnRspFundInterTransfer(CSecurityFtdcFundInterTransferField *pFundInterTransfer, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
@ -688,6 +706,8 @@ void TdApi::OnRspFundInterTransfer(CSecurityFtdcFundInterTransferField *pFundInt
};
void TdApi::OnRspQryFundInterTransferSerial(CSecurityFtdcFundInterTransferSerialField *pFundInterTransferSerial, CSecurityFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if (pFundInterTransferSerial) //手动修改,防止空指针
{
Task task = Task();
task.task_name = ONRSPQRYFUNDINTERTRANSFERSERIAL;
@ -705,6 +725,7 @@ void TdApi::OnRspQryFundInterTransferSerial(CSecurityFtdcFundInterTransferSerial
task.task_id = nRequestID;
task.task_last = bIsLast;
this->task_queue.push(task);
}
};
void TdApi::OnRtnFundInterTransferSerial(CSecurityFtdcFundInterTransferSerialField *pFundInterTransferSerial)
@ -2208,14 +2229,13 @@ int TdApi::reqUserLogout(dict req, int nRequestID)
int TdApi::reqOrderInsert(dict req, int nRequestID)
{
//该函数进行了手动编辑主要因为Direction和CombOffsetFlag两个字段的特殊性
CSecurityFtdcInputOrderField myreq = CSecurityFtdcInputOrderField();
memset(&myreq, 0, sizeof(myreq));
getChar(req, "ContingentCondition", &myreq.ContingentCondition);
getChar(req, "CombOffsetFlag", myreq.CombOffsetFlag);
getChar(req, "UserID", myreq.UserID);
getChar(req, "LimitPrice", myreq.LimitPrice);
getInt(req, "UserForceClose", &myreq.UserForceClose);
getChar(req, "Direction", &myreq.Direction);
getInt(req, "VolumeTotalOriginal", &myreq.VolumeTotalOriginal);
getChar(req, "OrderPriceType", &myreq.OrderPriceType);
getChar(req, "TimeCondition", &myreq.TimeCondition);
@ -2233,6 +2253,33 @@ int TdApi::reqOrderInsert(dict req, int nRequestID)
getChar(req, "InvestorID", myreq.InvestorID);
getChar(req, "VolumeCondition", &myreq.VolumeCondition);
getInt(req, "RequestID", &myreq.RequestID);
//处理Direction
if (req.has_key("Direction"))
{
object o1 = req["Direction"];
extract<string> x1(o1);
if (x1.check())
{
string s1 = x1();
const char *buffer1 = s1.c_str();
myreq.Direction = *buffer1;
}
}
//处理CombOffsetFlag
if (req.has_key("CombOffsetFlag"))
{
object o2 = req["CombOffsetFlag"];
extract<string> x2(o2);
if (x2.check())
{
string s2 = x2();
const char *buffer2 = s2.c_str();
myreq.CombOffsetFlag[0] = *buffer2;
}
}
int i = this->api->ReqOrderInsert(&myreq, nRequestID);
return i;
};
@ -2464,6 +2511,7 @@ int TdApi::reqQryInvestorPosition(dict req, int nRequestID)
int TdApi::reqFundOutByLiber(dict req, int nRequestID)
{
//手动修改,结构体生成错误
CSecurityFtdcInputFundTransferField myreq = CSecurityFtdcInputFundTransferField();
memset(&myreq, 0, sizeof(myreq));
getChar(req, "UserID", myreq.UserID);
@ -2471,7 +2519,7 @@ int TdApi::reqFundOutByLiber(dict req, int nRequestID)
getChar(req, "BrokerID", myreq.BrokerID);
getChar(req, "AccountType", &myreq.AccountType);
getChar(req, "Password", myreq.Password);
getChar(req, "Password", myreq.Password);
getDouble(req, "TradeAmount", &myreq.TradeAmount);
getChar(req, "Digest", myreq.Digest);
getChar(req, "AccountID", myreq.AccountID);
int i = this->api->ReqFundOutByLiber(&myreq, nRequestID);
@ -2524,236 +2572,550 @@ struct TdApiWrap : TdApi, wrapper < TdApi >
{
virtual void onFrontConnected()
{
//在向python环境中调用回调函数推送数据前需要先获取全局锁GIL防止解释器崩溃
PyLock lock;
//以下的try...catch...可以实现捕捉python环境中错误的功能防止C++直接出现原因未知的崩溃
try
{
this->get_override("onFrontConnected")();
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onFrontDisconnected(int i)
{
PyLock lock;
try
{
this->get_override("onFrontDisconnected")(i);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onHeartBeatWarning(int i)
{
PyLock lock;
try
{
this->get_override("onHeartBeatWarning")(i);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspError(dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspError")(error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspUserLogin(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspUserLogin")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspUserLogout(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspUserLogout")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspOrderInsert(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspOrderInsert")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspOrderAction(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspOrderAction")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspUserPasswordUpdate(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspUserPasswordUpdate")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspTradingAccountPasswordUpdate(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspTradingAccountPasswordUpdate")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspQryExchange(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspQryExchange")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspQryInstrument(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspQryInstrument")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspQryInvestor(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspQryInvestor")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspQryTradingCode(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspQryTradingCode")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspQryTradingAccount(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspQryTradingAccount")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspQryDepthMarketData(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspQryDepthMarketData")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspQryBondInterest(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspQryBondInterest")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspQryMarketRationInfo(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspQryMarketRationInfo")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspQryInstrumentCommissionRate(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspQryInstrumentCommissionRate")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspQryETFInstrument(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspQryETFInstrument")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspQryETFBasket(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspQryETFBasket")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspQryOFInstrument(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspQryOFInstrument")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspQrySFInstrument(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspQrySFInstrument")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspQryOrder(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspQryOrder")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspQryTrade(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspQryTrade")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspQryInvestorPosition(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspQryInvestorPosition")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRtnOrder(dict data)
{
PyLock lock;
try
{
this->get_override("onRtnOrder")(data);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRtnTrade(dict data)
{
PyLock lock;
try
{
this->get_override("onRtnTrade")(data);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onErrRtnOrderInsert(dict data, dict error)
{
PyLock lock;
try
{
this->get_override("onErrRtnOrderInsert")(data, error);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onErrRtnOrderAction(dict data, dict error)
{
PyLock lock;
try
{
this->get_override("onErrRtnOrderAction")(data, error);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspFundOutByLiber(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspFundOutByLiber")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRtnFundOutByLiber(dict data)
{
PyLock lock;
try
{
this->get_override("onRtnFundOutByLiber")(data);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onErrRtnFundOutByLiber(dict data, dict error)
{
PyLock lock;
try
{
this->get_override("onErrRtnFundOutByLiber")(data, error);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRtnFundInByBank(dict data)
{
PyLock lock;
try
{
this->get_override("onRtnFundInByBank")(data);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspQryFundTransferSerial(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspQryFundTransferSerial")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspFundInterTransfer(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspFundInterTransfer")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRspQryFundInterTransferSerial(dict data, dict error, int id, bool last)
{
PyLock lock;
try
{
this->get_override("onRspQryFundInterTransferSerial")(data, error, id, last);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onRtnFundInterTransferSerial(dict data)
{
PyLock lock;
try
{
this->get_override("onRtnFundInterTransferSerial")(data);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
virtual void onErrRtnFundInterTransfer(dict data, dict error)
{
PyLock lock;
try
{
this->get_override("onErrRtnFundInterTransfer")(data, error);
}
catch (error_already_set const &)
{
PyErr_Print();
}
};
};

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,527 @@
# encoding: UTF-8
import sys
from time import sleep
from PyQt4 import QtGui
from vnltstd import *
from lts_data_type import defineDict
#----------------------------------------------------------------------
def print_dict(d):
"""按照键值打印一个字典"""
for key,value in d.items():
print key + ':' + str(value)
#----------------------------------------------------------------------
def simple_log(func):
"""简单装饰器用于输出函数名"""
def wrapper(*args, **kw):
print ""
print str(func.__name__)
return func(*args, **kw)
return wrapper
########################################################################
class TestTdApi(TdApi):
"""测试用实例"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
super(TestTdApi, self).__init__()
#----------------------------------------------------------------------
@simple_log
def onFrontConnected(self):
"""服务器连接"""
pass
#----------------------------------------------------------------------
@simple_log
def onFrontDisconnected(self, n):
"""服务器断开"""
print n
#----------------------------------------------------------------------
@simple_log
def onHeartBeatWarning(self, n):
"""心跳报警"""
print n
#----------------------------------------------------------------------
@simple_log
def onRspError(self, error, n, last):
"""错误"""
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRspUserLogin(self, data, error, n, last):
"""登陆回报"""
print_dict(data)
print_dict(error)
self.brokerID = data['BrokerID']
self.userID = data['UserID']
self.frontID = data['FrontID']
self.sessionID = data['SessionID']
#----------------------------------------------------------------------
@simple_log
def onRspUserLogout(self, data, error, n, last):
"""登出回报"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRspOrderInsert(self, data, error, n, last):
"""发单错误(柜台)"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRspOrderAction(self, data, error, n, last):
"""撤单错误(柜台)"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRspUserPasswordUpdate(self, data, error, n, last):
"""用户密码更新错误"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRspTradingAccountPasswordUpdate(self, data, error, n, last):
"""交易账户密码更新错误"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRspQryExchange(self, data, error, n, last):
"""交易所查询回报"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRspQryInstrument(self, data, error, n, last):
"""合约查询回报"""
if len(data['InstrumentID']) == 8 and '510180' in data['ExchangeInstID']:
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRspQryInvestor(self, data, error, n, last):
"""投资者查询回报"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRspQryTradingCode(self, data, error, n, last):
"""交易编码查询回报"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRspQryTradingAccount(self, data, error, n, last):
"""资金账户查询回报"""
print_dict(data)
print_dict(error)
self.accountID = data['AccountID']
#----------------------------------------------------------------------
@simple_log
def onRspQryDepthMarketData(self, data, error, n, last):
"""行情查询回报"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRspQryBondInterest(self, data, error, n, last):
"""债券利息查询回报"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRspQryMarketRationInfo(self, data, error, n, last):
"""市值配售查询回报"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRspQryInstrumentCommissionRate(self, data, error, n, last):
"""合约手续费查询回报"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRspQryETFInstrument(self, data, error, n, last):
"""ETF基金查询回报"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRspQryETFBasket(self, data, error, n, last):
"""ETF股票篮查询回报"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRspQryOFInstrument(self, data, error, n, last):
"""OF合约查询回报"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRspQrySFInstrument(self, data, error, n, last):
"""SF合约查询回报"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRspQryOrder(self, data, error, n, last):
"""报单查询回报"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRspQryTrade(self, data, error, n, last):
"""成交查询回报"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRspQryInvestorPosition(self, data, error, n, last):
"""持仓查询回报"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRtnOrder(self, data):
"""报单回报"""
print_dict(data)
#----------------------------------------------------------------------
@simple_log
def onRtnTrade(self, data):
"""成交回报"""
print_dict(data)
#----------------------------------------------------------------------
@simple_log
def onErrRtnOrderInsert(self, data, error):
"""发单错误回报(交易所)"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onErrRtnOrderAction(self, data, error):
"""撤单错误回报(交易所)"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRspFundOutByLiber(self, data, error, n, last):
"""LTS发起出金应答"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRtnFundOutByLiber(self, data):
"""LTS发起出金通知"""
print_dict(data)
#----------------------------------------------------------------------
@simple_log
def onErrRtnFundOutByLiber(self, data, error):
"""LTS发起出金错误回报"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRtnFundInByBank(self, data):
"""银行发起入金通知"""
print_dict(data)
#----------------------------------------------------------------------
@simple_log
def onRspQryFundTransferSerial(self, data, error, n, last):
"""资金转账查询应答"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRspFundInterTransfer(self, data, error, n, last):
"""资金内转应答"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRspQryFundInterTransferSerial(self, data, error, n, last):
"""资金内转流水查询应答"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
@simple_log
def onRtnFundInterTransferSerial(self, data):
"""资金内转流水通知"""
print_dict(data)
#----------------------------------------------------------------------
@simple_log
def onErrRtnFundInterTransfer(self, data, error):
"""资金内转错误回报"""
print_dict(data)
print_dict(error)
#----------------------------------------------------------------------
def main():
"""主测试函数出现堵塞时可以考虑使用sleep"""
reqid = 0
# 创建Qt应用对象用于事件循环
app = QtGui.QApplication(sys.argv)
# 创建API对象测试通过
api = TestTdApi()
# 在C++环境中创建MdApi对象传入参数是希望用来保存.con文件的地址测试通过
api.createFtdcTraderApi('')
# 设置数据流重传方式,测试通过
api.subscribePrivateTopic(1)
api.subscribePublicTopic(1)
# 注册前置机地址,测试通过
api.registerFront("tcp://211.144.195.163:34505")
# 初始化api连接前置机测试通过
api.init()
sleep(0.5)
# 登陆,测试通过
loginReq = {} # 创建一个空字典
loginReq['UserID'] = '' # 参数作为字典键值的方式传入
loginReq['Password'] = '' # 键名和C++中的结构体成员名对应
loginReq['BrokerID'] = ''
reqid = reqid + 1 # 请求数必须保持唯一性
i = api.reqUserLogin(loginReq, reqid)
sleep(0.5)
## 登出,测试通过
#reqid = reqid + 1
#i = api.reqUserLogout({}, 1)
#sleep(0.5)
## 获取交易日,目前输出为空,测试通过
#day = api.getTradingDay()
#print 'Trading Day is:' + str(day)
#sleep(0.5)
##########################################################
## 查询交易所,测试通过
#reqid = reqid + 1
#i = api.reqQryExchange({}, reqid)
## 查询合约列表,测试通过
#reqid = reqid + 1
#i = api.reqQryInstrument({}, reqid)
## 查询投资者,测试通过
#reqid = reqid + 1
#i = api.reqQryInvestor({}, reqid)
## 查询交易编码,测试通过
#reqid = reqid + 1
#i = api.reqQryTradingCode({}, reqid)
## 查询交易账户,测试通过
#reqid = reqid + 1
#i = api.reqQryTradingAccount({}, reqid)
#sleep(1.0)
## 查询行情, 测试失败C++环境中返回空指针
#reqid = reqid + 1
#req = {}
#req['InstrumentID'] = '600000'
#i = api.reqQryDepthMarketData(req, reqid)
## 查询债券利率,测试通过
#reqid = reqid + 1
#i = api.reqQryBondInterest({}, reqid)
## 查询市值配售,测试无反应
#reqid = reqid + 1
#i = api.reqQryMarketRationInfo({}, reqid)
## 查询手续费率, 测试失败C++环境中返回空指针
#reqid = reqid + 1
#req = {}
#req['InstrumentID'] = '600000'
#req['ExchangeID'] = 'SSE'
#req['InvestorID'] = api.userID
#req['BrokerID'] = api.brokerID
#i = api.reqQryInstrumentCommissionRate(req, reqid)
## 查询ETF合约测试通过
#reqid = reqid + 1
#i = api.reqQryETFInstrument({}, reqid)
## 查询ETF组合测试通过
#reqid = reqid + 1
#i = api.reqQryETFBasket({}, reqid)
## 查询OF合约测试通过
#reqid = reqid + 1
#i = api.reqQryOFInstrument({}, reqid)
## 查询SF合约测试通过
#reqid = reqid + 1
#i = api.reqQrySFInstrument({}, reqid)
######################################################
# 以下三个未测试
## 查询发单, 测试通过
#reqid = reqid + 1
#req = {}
#req['BrokerID'] = api.brokerID
#req['InvestorID'] = api.userID
#i = api.reqQryOrder(req, reqid)
#sleep(2.0)
## 查询成交, 测试通过
#reqid = reqid + 1
#req = {}
#req['BrokerID'] = api.brokerID
#req['InvestorID'] = api.userID
#i = api.reqQryTrade(req, reqid)
#sleep(2.0)
## 查询持仓,测试通过
#reqid = reqid + 1
#req = {}
#req['BrokerID'] = api.brokerID
#req['InvestorID'] = api.userID
#i = api.reqQryInvestorPosition(req, reqid)
#sleep(2.0)
################################################
## 更新行情密码,测试通过
#reqid = reqid + 1
#req = {}
#req['UserID'] = api.userID
#req['BrokerID'] = api.brokerID
#req['NewPassword'] = "624001X"
#req['OldPassword'] = "624001X"
#i = api.reqUserPasswordUpdate(req, reqid)
## 更新交易密码,测试有反应,但报错
## 应当无需更新因为登陆直接使用user密码
## 获取api.accountID需要先查询交易账户reqQryTradingAccount
#reqid = reqid + 1
#req = {}
#req['AccountID'] = api.accountID
#req['BrokerID'] = api.brokerID
#req['NewPassword'] = "624001X"
#req['OldPassword'] = "624001X"
#i = api.reqTradingAccountPasswordUpdate(req, reqid)
################################################
## 发单测试, 测试通过
#reqid = reqid + 1
#req = {}
#req['InvestorID'] = api.userID
#req['UserID'] = api.userID
#req['BrokerID'] = api.brokerID
#req['InstrumentID'] = '11000061'
#req['ExchangeID'] = 'SSE'
#req['OrderPriceType'] = defineDict['SECURITY_FTDC_OPT_LimitPrice']
#req['LimitPrice'] = '0.1850'
#req['VolumeTotalOriginal'] = 1
#req['Direction'] = defineDict['SECURITY_FTDC_D_Buy']
#req['CombOffsetFlag'] = defineDict['SECURITY_FTDC_OF_Open']
#req['OrderRef'] = '1'
#req['CombHedgeFlag'] = defineDict['SECURITY_FTDC_HF_Speculation']
#req['ContingentCondition'] = defineDict['SECURITY_FTDC_CC_Immediately']
#req['ForceCloseReason'] = defineDict['SECURITY_FTDC_FCC_NotForceClose']
#req['IsAutoSuspend'] = 0
#req['UserForceClose'] = 0
#req['TimeCondition'] = defineDict['SECURITY_FTDC_TC_GFD']
#req['VolumeCondition'] = defineDict['SECURITY_FTDC_VC_AV']
#req['MinVolume'] = 1
#i = api.reqOrderInsert(req, reqid)
#sleep(1.0)
## 撤单测试,测试通过
#reqid = reqid + 1
#req = {}
#req['InstrumentID'] = '11000061'
#req['ExchangeID'] = 'SSE'
#req['OrderRef'] = '1'
#req['ActionFlag'] = defineDict['SECURITY_FTDC_AF_Delete']
#req['BrokerID'] = api.brokerID
#req['InvestorID'] = api.userID
#req['FrontID'] = api.frontID
#req['SessionID'] = api.sessionID
#i = api.reqOrderAction(req, reqid)
################################################
## 测试资金转出,未测试
#reqid = reqid + 1
#i = api.reqFundOutByLiber({}, reqid)
## 测试资金内转,未测试
#reqid = reqid + 1
#i = api. reqFundInterTransfer({}, reqid)
## 测试查询账户转账流水,未测试
#reqid = reqid + 1
#i = api.reqQryFundTransferSerial({}, reqid)
## 测试查询内部账户转账流水,未测试
#reqid = reqid + 1
#i = api.reqQryFundInterTransferSerial({}, reqid)
# 连续运行
app.exec_()
if __name__ == '__main__':
main()

Binary file not shown.