def test_trader_process_create_and_clean(): """ 测试trader转换器进程的创建和清理 """ global frontAddress, mdFrontAddress, brokerID, userID, password process = psutil.Process() # 没有创建Trader对象前应该没有trader进程 # assert 'trader' not in [child.name() for child in process.children() ] # 创建后可以找到一个trader进程 trader = Trader(frontAddress, brokerID, userID, password) pid = trader.getConverterPid() assert pid and pid != 0 assert pid in [child.pid for child in process.children()] # 将变量指向None迫使垃圾回收,确认进程被清理了 trader = None sleep(1) assert pid not in [child.pid for child in process.children()]
def test_qry_trading_account(): # 创建trader对象 global frontAddress, mdFrontAddress, brokerID, userID, password trader = Trader(frontAddress, brokerID, userID, password) # 定义测试标志 f1 = [] f2 = [] # 定义回调函数,并将其绑定 def OnRspQryTradingAccount1(RequestID, RspInfo, Data, IsLast): # print 'OnRspQryTradingAccount1 is called' # print kwargs.keys() f1.append(1) trader.bind(callback.OnRspQryTradingAccount, OnRspQryTradingAccount1) def OnRspQryTradingAccount2(**kwargs): # print 'OnRspQryTradingAccount2 is called' # print kwargs.keys() if 'ResponseMethod' in kwargs.keys(): f2.append(1) f1.append(1) trader.bind(callback.OnRspQryTradingAccount, OnRspQryTradingAccount2) # 发送一个请求并等待回调函数被调用 data = struct.CThostFtdcQryTradingAccountField() result = trader.ReqQryTradingAccount(data) assert result[0] == 0 # 等待回调函数被调用 i = 0 while len(f1) < 2: sleep(.01) i += 1 if i > 300: raise Exception(u'等待回调超时...') assert len(f2) > 0
def test_open_and_close_position(): """ 测试开仓和平仓 """ def settlement_info_confirm(): """ 交易结果确认 """ result = [] def OnRspSettlementInfoConfirm(**kwargs): print 'OnRspSettlementInfoConfirm() is called ...' result.append(kwargs) trader.bind(callback.OnRspSettlementInfoConfirm, OnRspSettlementInfoConfirm) data = struct.CThostFtdcSettlementInfoConfirmField() data.BrokerID = brokerID data.InvestorID = userID data.ConfirmDate = '' data.ConfirmTime = '' trader.ReqSettlementInfoConfirm(data) while len(result) == 0: sleep(.01) print result sequence = [] onRspOrderInsertResult = [] def OnRspOrderInsert(**kwargs): print 'OnRspOrderInsert() is called ...' onRspOrderInsertResult.append(kwargs) sequence.append('onRspOrderInsert') onErrRtnOrderInsertResult = [] def OnErrRtnOrderInsert(**kwargs): print 'OnErrRtnOrderInsert() is called ...' onErrRtnOrderInsertResult.append(kwargs) sequence.append('onErrRtnOrderInsert') onRtnOrderResult = [] def OnRtnOrder(**kwargs): print 'OnRtnOrder() is called ...' onRtnOrderResult.append(kwargs) sequence.append('onRtnOrder') OnRtnTradeResult = [] def OnRtnTrade(**kwargs): print 'OnRtnTrade() is called ...' OnRtnTradeResult.append(kwargs) sequence.append('OnRtnTrade') global frontAddress, mdFrontAddress, brokerID, userID, password trader = Trader(frontAddress, brokerID, userID, password) trader.bind(callback.OnRspOrderInsert, OnRspOrderInsert) trader.bind(callback.OnErrRtnOrderInsert, OnErrRtnOrderInsert) trader.bind(callback.OnRtnOrder, OnRtnOrder) trader.bind(callback.OnRtnTrade, OnRtnTrade) # 交易结果确认 settlement_info_confirm() # 进行开仓测试 data = getInsertOrderField('buy', 'open') trader.ReqOrderInsert(data) sleep(1) assert len(onRspOrderInsertResult) == 0 assert len(onErrRtnOrderInsertResult) == 0 assert len(onRtnOrderResult) > 0 assert len(OnRtnTradeResult) == 1 # 清理数据 onRspOrderInsertResult = [] onErrRtnOrderInsertResult = [] onRtnOrderResult = [] OnRtnTradeResult = [] # 进程平仓测试 data = getInsertOrderField('buy', 'close') trader.ReqOrderInsert(data) sleep(1) assert len(onRspOrderInsertResult) == 0 assert len(onErrRtnOrderInsertResult) == 0 assert len(onRtnOrderResult) > 0 assert len(OnRtnTradeResult) == 1 print sequence assert sequence[3] == 'OnRtnTrade' assert sequence[7] == 'OnRtnTrade'