diff --git a/vn.event/eventEngine.py b/vn.event/eventEngine.py index 2ceada00..9be61010 100644 --- a/vn.event/eventEngine.py +++ b/vn.event/eventEngine.py @@ -2,13 +2,18 @@ # 系统模块 from Queue import Queue, Empty +print u'eventEngine.py import Queue.Queue/Empty success.' + from threading import Thread +print u'eventEngine.py import threading.Thread success.' # 第三方模块 from PyQt4.QtCore import QTimer +print u'eventEngine.py import PyQt4.QtCore.QTimer success.' # 自己开发的模块 from eventType import * +print u'eventEngine.py import eventType.* success.' ######################################################################## @@ -52,6 +57,9 @@ class EventEngine: #---------------------------------------------------------------------- def __init__(self): """初始化事件引擎""" + + print u'eventEngine.py EventEngine.__init__ begin.' + # 事件队列 self.__queue = Queue() @@ -68,10 +76,14 @@ class EventEngine: # 这里的__handlers是一个字典,用来保存对应的事件调用关系 # 其中每个键对应的值是一个列表,列表中保存了对该事件进行监听的函数功能 self.__handlers = {} + print u'eventEngine.py EventEngine.__init__ end.' #---------------------------------------------------------------------- def __run(self): """引擎运行""" + + print u'eventEngine.py EventEngine.__run begin.' + while self.__active == True: try: event = self.__queue.get(block = True, timeout = 1) # 获取事件的阻塞时间设为1秒 @@ -82,18 +94,22 @@ class EventEngine: #---------------------------------------------------------------------- def __process(self, event): """处理事件""" + + print u'eventEngine.py EventEngine.__process begin.' # 检查是否存在对该事件进行监听的处理函数 if event.type_ in self.__handlers: # 若存在,则按顺序将事件传递给处理函数执行 [handler(event) for handler in self.__handlers[event.type_]] - + # 以上语句为Python列表解析方式的写法,对应的常规循环写法为: #for handler in self.__handlers[event.type_]: #handler(event) - + print u'eventEngine.py EventEngine.__process end.' + #---------------------------------------------------------------------- def __onTimer(self): """向事件队列中存入计时器事件""" + # 创建计时器事件 event = Event(type_=EVENT_TIMER) @@ -103,6 +119,8 @@ class EventEngine: #---------------------------------------------------------------------- def start(self): """引擎启动""" + + print u'eventEngine.py EventEngine.start begin.' # 将引擎设为启动 self.__active = True @@ -111,10 +129,14 @@ class EventEngine: # 启动计时器,计时器事件间隔默认设定为1秒 self.__timer.start(1000) + + print u'eventEngine.py EventEngine.start end.' #---------------------------------------------------------------------- def stop(self): """停止引擎""" + + print u'eventEngine.py EventEngine.stop begin.' # 将引擎设为停止 self.__active = False @@ -123,10 +145,15 @@ class EventEngine: # 等待事件处理线程退出 self.__thread.join() - + + print u'eventEngine.py EventEngine.stop end.' + #---------------------------------------------------------------------- def register(self, type_, handler): """注册事件处理函数监听""" + + print u'eventEngine.py EventEngine.register begin.' + # 尝试获取该事件类型对应的处理函数列表,若无则创建 try: handlerList = self.__handlers[type_] @@ -137,10 +164,15 @@ class EventEngine: # 若要注册的处理器不在该事件的处理器列表中,则注册该事件 if handler not in handlerList: handlerList.append(handler) - + + print u'eventEngine.py EventEngine.register end.' + #---------------------------------------------------------------------- def unregister(self, type_, handler): """注销事件处理函数监听""" + + print u'eventEngine.py EventEngine.unregister begin.' + # 尝试获取该事件类型对应的处理函数列表,若无则忽略该次注销请求 try: handlerList = self.handlers[type_] @@ -153,7 +185,9 @@ class EventEngine: if not handlerList: del self.handlers[type_] except KeyError: - pass + pass + + print u'eventEngine.py EventEngine.unregister end.' #---------------------------------------------------------------------- def put(self, event): @@ -176,8 +210,11 @@ class Event: def test(): """测试函数""" import sys + print u'eventEngine.py test() import sys success.' from datetime import datetime + print u'eventEngine.py test() import datetime.datetime success.' from PyQt4.QtCore import QCoreApplication + print u'eventEngine.py test() import PyQt4.QtCore.QCoreApplication success.' def simpletest(event): print u'处理每秒触发的计时器事件:%s' % str(datetime.now()) @@ -185,11 +222,14 @@ def test(): app = QCoreApplication(sys.argv) ee = EventEngine() + + #注册测试函数simpletest ee.register(EVENT_TIMER, simpletest) + ee.start() app.exec_() - + # 直接运行脚本可以进行测试 if __name__ == '__main__': diff --git a/vn.strategy/strategyEngine.py b/vn.strategy/strategyEngine.py index 78c778c2..1ea38ff7 100644 --- a/vn.strategy/strategyEngine.py +++ b/vn.strategy/strategyEngine.py @@ -2,7 +2,7 @@ from datetime import datetime -from pymongo import Connection +from pymongo import MongoClient as Connection from pymongo.errors import * from eventEngine import * diff --git a/vn.strategy/strategydemo/backtestingEngine.py b/vn.strategy/strategydemo/backtestingEngine.py index f94f5068..a4147d76 100644 --- a/vn.strategy/strategydemo/backtestingEngine.py +++ b/vn.strategy/strategydemo/backtestingEngine.py @@ -3,7 +3,7 @@ import shelve from eventEngine import * -from pymongo import Connection +from pymongo import MongoClient as Connection from pymongo.errors import * from strategyEngine import * diff --git a/vn.strategy/strategydemo/demoApi.py b/vn.strategy/strategydemo/demoApi.py index fddcfb52..c490f740 100644 --- a/vn.strategy/strategydemo/demoApi.py +++ b/vn.strategy/strategydemo/demoApi.py @@ -38,7 +38,10 @@ class DemoMdApi(MdApi): """ API对象的初始化函数 """ + print u'DemoApi.py DemoMdApi.__init__() begin' + super(DemoMdApi, self).__init__() + print u'DemoApi.py DemoMdApi.__init__() super success' # 事件引擎,所有数据都推送到其中,再由事件引擎进行分发 self.__eventEngine = eventEngine @@ -52,14 +55,17 @@ class DemoMdApi(MdApi): self.__brokerid = '' # 以下集合用于重连后自动订阅之前已订阅的合约,使用集合为了防止重复 - self.__setSubscribed = set() - + self.__setSubscribed = set() # 初始化.con文件的保存目录为\mdconnection,注意这个目录必须已存在,否则会报错 - self.createFtdcMdApi(os.getcwd() + '\\mdconnection\\') + self.createFtdcMdApi(os.getcwd() + '\\mdconnection\\') + print u'DemoApi.py DemoMdApi.__init__() createFtdcMdApi success' + + print u'DemoApi.py DemoMdApi.__init__() end' #---------------------------------------------------------------------- def onFrontConnected(self): """服务器连接""" + print u'DemoApi.py DemoMdApi.onFrontConnected() begin.' event = Event(type_=EVENT_LOG) event.dict_['log'] = u'行情服务器连接成功' self.__eventEngine.put(event) @@ -72,31 +78,41 @@ class DemoMdApi(MdApi): req['BrokerID'] = self.__brokerid self.__reqid = self.__reqid + 1 self.reqUserLogin(req, self.__reqid) + + print u'DemoApi.py DemoMdApi.onFrontConnected() end.' #---------------------------------------------------------------------- def onFrontDisconnected(self, n): """服务器断开""" + print u'DemoApi.py DemoMdApi.onFrontDisconnected() begin.' event = Event(type_=EVENT_LOG) event.dict_['log'] = u'行情服务器连接断开' self.__eventEngine.put(event) + print u'DemoApi.py DemoMdApi.onFrontDisconnected() end.' #---------------------------------------------------------------------- def onHeartBeatWarning(self, n): """心跳报警""" # 因为API的心跳报警比较常被触发,且与API工作关系不大,因此选择忽略 + print u'DemoApi.py DemoMdApi.onHeartBeatWarning()' pass #---------------------------------------------------------------------- def onRspError(self, error, n, last): """错误回报""" + print u'DemoApi.py DemoMdApi.onRspError() begin' event = Event(type_=EVENT_LOG) log = u'行情错误回报,错误代码:' + unicode(error['ErrorID']) + u',' + u'错误信息:' + error['ErrorMsg'].decode('gbk') event.dict_['log'] = log self.__eventEngine.put(event) + print u'DemoApi.py DemoMdApi.onRspError() end' #---------------------------------------------------------------------- def onRspUserLogin(self, data, error, n, last): """登陆回报""" + + print u'DemoApi.py DemoMdApi.onRspUserLogin() begin' + event = Event(type_=EVENT_LOG) if error['ErrorID'] == 0: @@ -105,16 +121,24 @@ class DemoMdApi(MdApi): log = u'登陆回报,错误代码:' + unicode(error['ErrorID']) + u',' + u'错误信息:' + error['ErrorMsg'].decode('gbk') event.dict_['log'] = log + self.__eventEngine.put(event) ## 重连后自动订阅之前已经订阅过的合约 #if self.__setSubscribed: #for instrument in self.__setSubscribed: #self.subscribe(instrument[0], instrument[1]) - + + onRspUserLogin + + print u'DemoApi.py DemoMdApi.onRspUserLogin() end' + #---------------------------------------------------------------------- def onRspUserLogout(self, data, error, n, last): """登出回报""" + + print u'DemoApi.py DemoMdApi.onRspUserLogout() begin' + event = Event(type_=EVENT_LOG) if error['ErrorID'] == 0: @@ -123,17 +147,24 @@ class DemoMdApi(MdApi): log = u'登出回报,错误代码:' + unicode(error['ErrorID']) + u',' + u'错误信息:' + error['ErrorMsg'].decode('gbk') event.dict_['log'] = log + self.__eventEngine.put(event) + + print u'DemoApi.py DemoMdApi.onRspUserLogout() end' #---------------------------------------------------------------------- def onRspSubMarketData(self, data, error, n, last): """订阅合约回报""" # 通常不在乎订阅错误,选择忽略 + print u'DemoApi.py DemoMdApi.onRspSubMarketData()' pass #---------------------------------------------------------------------- def onRspUnSubMarketData(self, data, error, n, last): """退订合约回报""" + + onRspUnSubMarketData + # 同上 pass @@ -141,7 +172,8 @@ class DemoMdApi(MdApi): def onRtnDepthMarketData(self, data): """行情推送""" # 行情推送收到后,同时触发常规行情事件,以及特定合约行情事件,用于满足不同类型的监听 - + print u'DemoApi.py DemoMdApi.onRtnDepthMarketData() begin' + # 常规行情事件 event1 = Event(type_=EVENT_MARKETDATA) event1.dict_['data'] = data @@ -152,6 +184,8 @@ class DemoMdApi(MdApi): event2.dict_['data'] = data self.__eventEngine.put(event2) + print u'DemoApi.py DemoMdApi.onRtnDepthMarketData() end' + #---------------------------------------------------------------------- def onRspSubForQuoteRsp(self, data, error, n, last): """订阅期权询价""" @@ -160,26 +194,35 @@ class DemoMdApi(MdApi): #---------------------------------------------------------------------- def onRspUnSubForQuoteRsp(self, data, error, n, last): """退订期权询价""" + print u'DemoApi.py DemoMdApi.onRspUnSubForQuoteRsp()' pass #---------------------------------------------------------------------- def onRtnForQuoteRsp(self, data): """期权询价推送""" + print u'DemoApi.py DemoMdApi.onRtnForQuoteRsp()' pass #---------------------------------------------------------------------- def login(self, address, userid, password, brokerid): """连接服务器""" + + print u'DemoApi.py DemoMdApi.login() begin' + self.__userid = userid self.__password = password self.__brokerid = brokerid # 注册服务器地址 self.registerFront(address) + print u'DemoApi.py DemoMdApi.login() registerFront success' # 初始化连接,成功会调用onFrontConnected self.init() - + print u'DemoApi.py DemoMdApi.login() init() success' + + print u'DemoApi.py DemoMdApi.login() end' + #---------------------------------------------------------------------- def subscribe(self, instrumentid, exchangeid): """订阅合约""" @@ -206,7 +249,12 @@ class DemoTdApi(TdApi): #---------------------------------------------------------------------- def __init__(self, eventEngine): """API对象的初始化函数""" + + print u'DemoApi.py DemoTdApi.__init__() begin' + super(DemoTdApi, self).__init__() + + print u'DemoApi.py DemoTdApi.__init__() call super() success' # 事件引擎,所有数据都推送到其中,再由事件引擎进行分发 self.__eventEngine = eventEngine @@ -227,10 +275,19 @@ class DemoTdApi(TdApi): # 初始化.con文件的保存目录为\tdconnection self.createFtdcTraderApi(os.getcwd() + '\\tdconnection\\') + + print u'DemoApi.py DemoTdApi.__init__() createFtdcTraderApi() success.' + + print u'DemoApi.py DemoTdApi.__init__() end.' + + #---------------------------------------------------------------------- def onFrontConnected(self): """服务器连接""" + + print u'DemoApi.py DemoTdApi.onFrontConnected() begin.' + event = Event(type_=EVENT_LOG) event.dict_['log'] = u'交易服务器连接成功' self.__eventEngine.put(event) @@ -243,27 +300,39 @@ class DemoTdApi(TdApi): req['BrokerID'] = self.__brokerid self.__reqid = self.__reqid + 1 self.reqUserLogin(req, self.__reqid) + + print u'DemoApi.py DemoTdApi.onFrontConnected() begin.' #---------------------------------------------------------------------- def onFrontDisconnected(self, n): """服务器断开""" + + print u'DemoApi.py DemoTdApi.onFrontDisconnected() begin.' + event = Event(type_=EVENT_LOG) event.dict_['log'] = u'交易服务器连接断开' self.__eventEngine.put(event) + + print u'DemoApi.py DemoTdApi.onFrontDisconnected() end.' #---------------------------------------------------------------------- def onHeartBeatWarning(self, n): - """""" + """心跳""" + print u'DemoApi.py DemoTdApi.onHeartBeatWarning().' pass #---------------------------------------------------------------------- def onRspAuthenticate(self, data, error, n, last): - """""" + """鉴权""" + print u'DemoApi.py DemoTdApi.onRspAuthenticate().' pass #---------------------------------------------------------------------- def onRspUserLogin(self, data, error, n, last): """登陆回报""" + + print u'DemoApi.py DemoTdApi.onRspUserLogin() begin.' + event = Event(type_=EVENT_LOG) if error['ErrorID'] == 0: @@ -275,10 +344,16 @@ class DemoTdApi(TdApi): self.__eventEngine.put(event) self.getSettlement() # 登录完成后立即查询结算信息 + print u'DemoApi.py DemoTdApi.onRspUserLogin() getSettlement success.' + + print u'DemoApi.py DemoTdApi.onRspUserLogin() end.' #---------------------------------------------------------------------- def onRspUserLogout(self, data, error, n, last): """登出回报""" + + print u'DemoApi.py DemoTdApi.onRspUserLogout() begin.' + event = Event(type_=EVENT_LOG) if error['ErrorID'] == 0: @@ -288,107 +363,137 @@ class DemoTdApi(TdApi): event.dict_['log'] = log self.__eventEngine.put(event) + + print u'DemoApi.py DemoTdApi.onRspUserLogout() end.' #---------------------------------------------------------------------- def onRspUserPasswordUpdate(self, data, error, n, last): - """""" + """用户登录密码更新事件""" + print u'DemoApi.py DemoTdApi.onRspUserPasswordUpdate().' pass #---------------------------------------------------------------------- def onRspTradingAccountPasswordUpdate(self, data, error, n, last): - """""" + """用户交易密码更新事件""" + print u'DemoApi.py DemoTdApi.onRspTradingAccountPasswordUpdate().' pass #---------------------------------------------------------------------- def onRspOrderInsert(self, data, error, n, last): """发单错误(柜台)""" + + print u'DemoApi.py DemoTdApi.onRspOrderInsert() begin.' event = Event(type_=EVENT_LOG) log = u' 发单错误回报,错误代码:' + unicode(error['ErrorID']) + u',' + u'错误信息:' + error['ErrorMsg'].decode('gbk') event.dict_['log'] = log self.__eventEngine.put(event) + + print u'DemoApi.py DemoTdApi.onRspOrderInsert() end.' #---------------------------------------------------------------------- def onRspParkedOrderInsert(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspParkedOrderInsert().' pass #---------------------------------------------------------------------- def onRspParkedOrderAction(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspParkedOrderInsert().' pass #---------------------------------------------------------------------- def onRspOrderAction(self, data, error, n, last): """撤单错误(柜台)""" + + print u'DemoApi.py DemoTdApi.onRspOrderAction() begin.' event = Event(type_=EVENT_LOG) log = u'撤单错误回报,错误代码:' + unicode(error['ErrorID']) + u',' + u'错误信息:' + error['ErrorMsg'].decode('gbk') event.dict_['log'] = log self.__eventEngine.put(event) + + print u'DemoApi.py DemoTdApi.onRspOrderAction() end.' #---------------------------------------------------------------------- def onRspQueryMaxOrderVolume(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQueryMaxOrderVolume().' pass #---------------------------------------------------------------------- def onRspSettlementInfoConfirm(self, data, error, n, last): """确认结算信息回报""" + + print u'DemoApi.py DemoTdApi.onRspSettlementInfoConfirm() begin.' event = Event(type_=EVENT_LOG) log = u'结算信息确认完成' event.dict_['log'] = log self.__eventEngine.put(event) event = Event(type_=EVENT_TDLOGIN) - self.__eventEngine.put(event) + self.__eventEngine.put(event) + print u'DemoApi.py DemoTdApi.onRspSettlementInfoConfirm() end.' #---------------------------------------------------------------------- def onRspRemoveParkedOrder(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspRemoveParkedOrder().' pass #---------------------------------------------------------------------- def onRspRemoveParkedOrderAction(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspRemoveParkedOrderAction().' pass #---------------------------------------------------------------------- def onRspExecOrderInsert(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspExecOrderInsert().' pass #---------------------------------------------------------------------- def onRspExecOrderAction(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspExecOrderAction().' pass #---------------------------------------------------------------------- def onRspForQuoteInsert(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspForQuoteInsert().' pass #---------------------------------------------------------------------- def onRspQuoteInsert(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQuoteInsert().' pass #---------------------------------------------------------------------- def onRspQuoteAction(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQuoteAction().' pass #---------------------------------------------------------------------- def onRspQryOrder(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryOrder().' pass #---------------------------------------------------------------------- def onRspQryTrade(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryTrade().' pass #---------------------------------------------------------------------- def onRspQryInvestorPosition(self, data, error, n, last): """持仓查询回报""" + + print u'DemoApi.py DemoTdApi.onRspQryInvestorPosition() begin.' + if error['ErrorID'] == 0: event = Event(type_=EVENT_POSITION) event.dict_['data'] = data @@ -398,10 +503,14 @@ class DemoTdApi(TdApi): log = u'持仓查询回报,错误代码:' + unicode(error['ErrorID']) + u',' + u'错误信息:' + error['ErrorMsg'].decode('gbk') event.dict_['log'] = log self.__eventEngine.put(event) + + print u'DemoApi.py DemoTdApi.onRspQryInvestorPosition() end.' #---------------------------------------------------------------------- def onRspQryTradingAccount(self, data, error, n, last): """资金账户查询回报""" + + print u'DemoApi.py DemoTdApi.onRspQryTradingAccount() begin.' if error['ErrorID'] == 0: event = Event(type_=EVENT_ACCOUNT) event.dict_['data'] = data @@ -411,10 +520,14 @@ class DemoTdApi(TdApi): log = u'账户查询回报,错误代码:' + unicode(error['ErrorID']) + u',' + u'错误信息:' + error['ErrorMsg'].decode('gbk') event.dict_['log'] = log self.__eventEngine.put(event) + + print u'DemoApi.py DemoTdApi.onRspQryTradingAccount() end.' #---------------------------------------------------------------------- def onRspQryInvestor(self, data, error, n, last): """投资者查询回报""" + + print u'DemoApi.py DemoTdApi.onRspQryInvestor() begin.' if error['ErrorID'] == 0: event = Event(type_=EVENT_INVESTOR) event.dict_['data'] = data @@ -424,30 +537,37 @@ class DemoTdApi(TdApi): log = u'合约投资者回报,错误代码:' + unicode(error['ErrorID']) + u',' + u'错误信息:' + error['ErrorMsg'].decode('gbk') event.dict_['log'] = log self.__eventEngine.put(event) - + print u'DemoApi.py DemoTdApi.onRspQryInvestor() end.' + + #---------------------------------------------------------------------- def onRspQryTradingCode(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryTradingCode() ' pass #---------------------------------------------------------------------- def onRspQryInstrumentMarginRate(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryInstrumentMarginRate() ' pass #---------------------------------------------------------------------- def onRspQryInstrumentCommissionRate(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryInstrumentCommissionRate() ' pass #---------------------------------------------------------------------- def onRspQryExchange(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryExchange() ' pass #---------------------------------------------------------------------- def onRspQryProduct(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryProduct() ' pass #---------------------------------------------------------------------- @@ -458,6 +578,8 @@ class DemoTdApi(TdApi): 选择先储存在一个本地字典中,全部收集完毕后再推送到队列中 (由于耗时过长目前使用其他进程读取) """ + + print u'DemoApi.py DemoTdApi.onRspQryInstrument() begin.' if error['ErrorID'] == 0: event = Event(type_=EVENT_INSTRUMENT) event.dict_['data'] = data @@ -469,14 +591,19 @@ class DemoTdApi(TdApi): event.dict_['log'] = log self.__eventEngine.put(event) + print u'DemoApi.py DemoTdApi.onRspQryInstrument() end.' + #---------------------------------------------------------------------- def onRspQryDepthMarketData(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryDepthMarketData().' pass #---------------------------------------------------------------------- def onRspQrySettlementInfo(self, data, error, n, last): """查询结算信息回报""" + + print u'DemoApi.py DemoTdApi.onRspQrySettlementInfo() begin.' if last: event = Event(type_=EVENT_LOG) log = u'结算信息查询完成' @@ -484,113 +611,140 @@ class DemoTdApi(TdApi): self.__eventEngine.put(event) self.confirmSettlement() # 查询完成后立即确认结算信息 + + print u'DemoApi.py DemoTdApi.onRspQrySettlementInfo() end.' #---------------------------------------------------------------------- def onRspQryTransferBank(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryTransferBank() ' pass #---------------------------------------------------------------------- def onRspQryInvestorPositionDetail(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryInvestorPositionDetail() ' pass #---------------------------------------------------------------------- def onRspQryNotice(self, data, error, n, last): + print u'DemoApi.py DemoTdApi.onRspQryNotice() ' """""" pass #---------------------------------------------------------------------- def onRspQrySettlementInfoConfirm(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQrySettlementInfoConfirm() ' pass #---------------------------------------------------------------------- def onRspQryInvestorPositionCombineDetail(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryInvestorPositionCombineDetail() ' pass #---------------------------------------------------------------------- def onRspQryCFMMCTradingAccountKey(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryCFMMCTradingAccountKey() ' pass #---------------------------------------------------------------------- def onRspQryEWarrantOffset(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryEWarrantOffset() ' pass #---------------------------------------------------------------------- def onRspQryInvestorProductGroupMargin(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryInvestorProductGroupMargin() ' pass #---------------------------------------------------------------------- def onRspQryExchangeMarginRate(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryExchangeMarginRate() ' pass #---------------------------------------------------------------------- def onRspQryExchangeMarginRateAdjust(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryExchangeMarginRateAdjust() ' pass #---------------------------------------------------------------------- def onRspQryExchangeRate(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryExchangeRate() ' pass #---------------------------------------------------------------------- def onRspQrySecAgentACIDMap(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQrySecAgentACIDMap() ' pass #---------------------------------------------------------------------- def onRspQryOptionInstrTradeCost(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryOptionInstrTradeCost() ' pass #---------------------------------------------------------------------- def onRspQryOptionInstrCommRate(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryOptionInstrCommRate() ' pass #---------------------------------------------------------------------- def onRspQryExecOrder(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryExecOrder() ' pass #---------------------------------------------------------------------- def onRspQryForQuote(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryForQuote() ' pass #---------------------------------------------------------------------- def onRspQryQuote(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryQuote() ' pass #---------------------------------------------------------------------- def onRspQryTransferSerial(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryTransferSerial() ' pass #---------------------------------------------------------------------- def onRspQryAccountregister(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryAccountregister() ' pass #---------------------------------------------------------------------- def onRspError(self, error, n, last): """错误回报""" + + print u'DemoApi.py DemoTdApi.onRspError() begin.' + event = Event(type_=EVENT_LOG) log = u'交易错误回报,错误代码:' + unicode(error['ErrorID']) + u',' + u'错误信息:' + error['ErrorMsg'].decode('gbk') event.dict_['log'] = log self.__eventEngine.put(event) + + print u'DemoApi.py DemoTdApi.onRspError() end.' #---------------------------------------------------------------------- def onRtnOrder(self, data): """报单回报""" + print u'DemoApi.py DemoTdApi.onRtnOrder() begin.' # 更新最大报单编号 newref = data['OrderRef'] self.__orderref = max(self.__orderref, int(newref)) @@ -604,10 +758,13 @@ class DemoTdApi(TdApi): event2 = Event(type_=(EVENT_ORDER_ORDERREF+data['OrderRef'])) event2.dict_['data'] = data self.__eventEngine.put(event2) + print u'DemoApi.py DemoTdApi.onRtnOrder() end.' #---------------------------------------------------------------------- def onRtnTrade(self, data): """成交回报""" + + print u'DemoApi.py DemoTdApi.onRtnTrade() begin.' # 常规成交事件 event1 = Event(type_=EVENT_TRADE) event1.dict_['data'] = data @@ -617,221 +774,272 @@ class DemoTdApi(TdApi): event2 = Event(type_=(EVENT_TRADE_CONTRACT+data['InstrumentID'])) event2.dict_['data'] = data self.__eventEngine.put(event2) + + print u'DemoApi.py DemoTdApi.onRtnTrade() end.' #---------------------------------------------------------------------- def onErrRtnOrderInsert(self, data, error): """发单错误回报(交易所)""" + print u'DemoApi.py DemoTdApi.onErrRtnOrderInsert() begin.' + event = Event(type_=EVENT_LOG) log = u'发单错误回报,错误代码:' + unicode(error['ErrorID']) + u',' + u'错误信息:' + error['ErrorMsg'].decode('gbk') event.dict_['log'] = log self.__eventEngine.put(event) + + print u'DemoApi.py DemoTdApi.onErrRtnOrderInsert() end.' #---------------------------------------------------------------------- def onErrRtnOrderAction(self, data, error): """撤单错误回报(交易所)""" + + print u'DemoApi.py DemoTdApi.onErrRtnOrderAction() begin.' event = Event(type_=EVENT_LOG) log = u'撤单错误回报,错误代码:' + unicode(error['ErrorID']) + u',' + u'错误信息:' + error['ErrorMsg'].decode('gbk') event.dict_['log'] = log self.__eventEngine.put(event) + + print u'DemoApi.py DemoTdApi.onErrRtnOrderAction() end.' #---------------------------------------------------------------------- def onRtnInstrumentStatus(self, data): """""" + print u'DemoApi.py DemoTdApi.onRtnInstrumentStatus() ' pass #---------------------------------------------------------------------- def onRtnTradingNotice(self, data): """""" + print u'DemoApi.py DemoTdApi.onRtnTradingNotice() ' pass #---------------------------------------------------------------------- def onRtnErrorConditionalOrder(self, data): """""" + print u'DemoApi.py DemoTdApi.onRtnErrorConditionalOrder() ' pass #---------------------------------------------------------------------- def onRtnExecOrder(self, data): """""" + print u'DemoApi.py DemoTdApi.onRtnExecOrder() ' pass #---------------------------------------------------------------------- def onErrRtnExecOrderInsert(self, data, error): """""" + print u'DemoApi.py DemoTdApi.onErrRtnExecOrderInsert() ' pass #---------------------------------------------------------------------- def onErrRtnExecOrderAction(self, data, error): """""" + print u'DemoApi.py DemoTdApi.onErrRtnExecOrderAction() ' pass #---------------------------------------------------------------------- def onErrRtnForQuoteInsert(self, data, error): """""" + print u'DemoApi.py DemoTdApi.onErrRtnForQuoteInsert() ' pass #---------------------------------------------------------------------- def onRtnQuote(self, data): """""" + print u'DemoApi.py DemoTdApi.onRtnQuote() ' pass #---------------------------------------------------------------------- def onErrRtnQuoteInsert(self, data, error): """""" + print u'DemoApi.py DemoTdApi.onErrRtnQuoteInsert() ' pass #---------------------------------------------------------------------- def onErrRtnQuoteAction(self, data, error): """""" + print u'DemoApi.py DemoTdApi.onErrRtnQuoteAction() ' pass #---------------------------------------------------------------------- def onRtnForQuoteRsp(self, data): """""" + print u'DemoApi.py DemoTdApi.onRtnForQuoteRsp() ' pass #---------------------------------------------------------------------- def onRspQryContractBank(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryContractBank() ' pass #---------------------------------------------------------------------- def onRspQryParkedOrder(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryParkedOrder() ' pass #---------------------------------------------------------------------- def onRspQryParkedOrderAction(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryParkedOrderAction() ' pass #---------------------------------------------------------------------- def onRspQryTradingNotice(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryTradingNotice() ' pass #---------------------------------------------------------------------- def onRspQryBrokerTradingParams(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryBrokerTradingParams() ' pass #---------------------------------------------------------------------- def onRspQryBrokerTradingAlgos(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQryBrokerTradingAlgos() ' pass #---------------------------------------------------------------------- def onRtnFromBankToFutureByBank(self, data): """""" + print u'DemoApi.py DemoTdApi.onRtnFromBankToFutureByBank() ' pass #---------------------------------------------------------------------- def onRtnFromFutureToBankByBank(self, data): """""" + print u'DemoApi.py DemoTdApi.onRtnFromFutureToBankByBank() ' pass #---------------------------------------------------------------------- def onRtnRepealFromBankToFutureByBank(self, data): """""" + print u'DemoApi.py DemoTdApi.onRtnRepealFromBankToFutureByBank() ' pass #---------------------------------------------------------------------- def onRtnRepealFromFutureToBankByBank(self, data): """""" + print u'DemoApi.py DemoTdApi.onRtnRepealFromFutureToBankByBank() ' pass #---------------------------------------------------------------------- def onRtnFromBankToFutureByFuture(self, data): """""" + print u'DemoApi.py DemoTdApi.onRtnFromBankToFutureByFuture() ' pass #---------------------------------------------------------------------- def onRtnFromFutureToBankByFuture(self, data): """""" + print u'DemoApi.py DemoTdApi.onRtnFromFutureToBankByFuture() ' pass #---------------------------------------------------------------------- def onRtnRepealFromBankToFutureByFutureManual(self, data): """""" + print u'DemoApi.py DemoTdApi.onRtnRepealFromBankToFutureByFutureManual() ' pass #---------------------------------------------------------------------- def onRtnRepealFromFutureToBankByFutureManual(self, data): """""" + print u'DemoApi.py DemoTdApi.onRtnRepealFromFutureToBankByFutureManual() ' pass #---------------------------------------------------------------------- def onRtnQueryBankBalanceByFuture(self, data): """""" + print u'DemoApi.py DemoTdApi.onRtnQueryBankBalanceByFuture() ' pass #---------------------------------------------------------------------- def onErrRtnBankToFutureByFuture(self, data, error): """""" + print u'DemoApi.py DemoTdApi.onErrRtnBankToFutureByFuture() ' pass #---------------------------------------------------------------------- def onErrRtnFutureToBankByFuture(self, data, error): """""" + print u'DemoApi.py DemoTdApi.onErrRtnFutureToBankByFuture() ' pass #---------------------------------------------------------------------- def onErrRtnRepealBankToFutureByFutureManual(self, data, error): """""" + print u'DemoApi.py DemoTdApi.onErrRtnRepealBankToFutureByFutureManual() ' pass #---------------------------------------------------------------------- def onErrRtnRepealFutureToBankByFutureManual(self, data, error): """""" + print u'DemoApi.py DemoTdApi.onErrRtnRepealFutureToBankByFutureManual() ' pass #---------------------------------------------------------------------- def onErrRtnQueryBankBalanceByFuture(self, data, error): """""" + print u'DemoApi.py DemoTdApi.onErrRtnQueryBankBalanceByFuture() ' pass #---------------------------------------------------------------------- def onRtnRepealFromBankToFutureByFuture(self, data): """""" + print u'DemoApi.py DemoTdApi.onRtnRepealFromBankToFutureByFuture() ' pass #---------------------------------------------------------------------- def onRtnRepealFromFutureToBankByFuture(self, data): """""" + print u'DemoApi.py DemoTdApi.onRtnRepealFromFutureToBankByFuture() ' pass #---------------------------------------------------------------------- def onRspFromBankToFutureByFuture(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspFromBankToFutureByFuture() ' pass #---------------------------------------------------------------------- def onRspFromFutureToBankByFuture(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspFromFutureToBankByFuture() ' pass #---------------------------------------------------------------------- def onRspQueryBankAccountMoneyByFuture(self, data, error, n, last): """""" + print u'DemoApi.py DemoTdApi.onRspQueryBankAccountMoneyByFuture() ' pass #---------------------------------------------------------------------- def onRtnOpenAccountByBank(self, data): """""" + print u'DemoApi.py DemoTdApi.onRtnOpenAccountByBank() ' pass #---------------------------------------------------------------------- def onRtnCancelAccountByBank(self, data): """""" + print u'DemoApi.py DemoTdApi.onRtnCancelAccountByBank() ' pass #---------------------------------------------------------------------- def onRtnChangeAccountByBank(self, data): """""" + print u'DemoApi.py DemoTdApi.onRtnChangeAccountByBank() ' pass #---------------------------------------------------------------------- def login(self, address, userid, password, brokerid): """连接服务器""" + + print u'DemoApi.py DemoTdApi.login() begin.' self.__userid = userid self.__password = password self.__brokerid = brokerid @@ -845,37 +1053,48 @@ class DemoTdApi(TdApi): # 初始化连接,成功会调用onFrontConnected self.init() + + print u'DemoApi.py DemoTdApi.login() end.' #---------------------------------------------------------------------- def getInstrument(self): """查询合约""" + print u'DemoApi.py DemoTdApi.getInstrument() begin.' self.__reqid = self.__reqid + 1 self.reqQryInstrument({}, self.__reqid) + print u'DemoApi.py DemoTdApi.getInstrument() end.' #---------------------------------------------------------------------- def getAccount(self): """查询账户""" + print u'DemoApi.py DemoTdApi.getAccount() begin.' self.__reqid = self.__reqid + 1 self.reqQryTradingAccount({}, self.__reqid) + print u'DemoApi.py DemoTdApi.getAccount() end.' #---------------------------------------------------------------------- def getInvestor(self): """查询投资者""" + print u'DemoApi.py DemoTdApi.getInvestor() begin.' self.__reqid = self.__reqid + 1 self.reqQryInvestor({}, self.__reqid) + print u'DemoApi.py DemoTdApi.getInvestor() end.' #---------------------------------------------------------------------- def getPosition(self): """查询持仓""" + print u'DemoApi.py DemoTdApi.getPosition() begin.' self.__reqid = self.__reqid + 1 req = {} req['BrokerID'] = self.__brokerid req['InvestorID'] = self.__userid self.reqQryInvestorPosition(req, self.__reqid) + print u'DemoApi.py DemoTdApi.getPosition() end.' #---------------------------------------------------------------------- def sendOrder(self, instrumentid, exchangeid, price, pricetype, volume, direction, offset): """发单""" + print u'DemoApi.py DemoTdApi.sendOrder() begin.' self.__reqid = self.__reqid + 1 req = {} @@ -902,12 +1121,17 @@ class DemoTdApi(TdApi): self.reqOrderInsert(req, self.__reqid) + print u'DemoApi.py DemoTdApi.sendOrder() end.' + # 返回订单号,便于某些算法进行动态管理 return self.__orderref + #---------------------------------------------------------------------- def cancelOrder(self, instrumentid, exchangeid, orderref, frontid, sessionid): """撤单""" + + print u'DemoApi.py DemoTdApi.cancelOrder() begin. ' self.__reqid = self.__reqid + 1 req = {} @@ -922,10 +1146,14 @@ class DemoTdApi(TdApi): req['InvestorID'] = self.__userid self.reqOrderAction(req, self.__reqid) + + print u'DemoApi.py DemoTdApi.cancelOrder() end.' #---------------------------------------------------------------------- def getSettlement(self): """查询结算信息""" + + print u'DemoApi.py DemoTdApi.getSettlement() begin ' self.__reqid = self.__reqid + 1 req = {} @@ -933,14 +1161,19 @@ class DemoTdApi(TdApi): req['InvestorID'] = self.__userid self.reqQrySettlementInfo(req, self.__reqid) + + print u'DemoApi.py DemoTdApi.getSettlement() end.' #---------------------------------------------------------------------- def confirmSettlement(self): """确认结算信息""" + + print u'DemoApi.py DemoTdApi.confirmSettlement() begin.' self.__reqid = self.__reqid + 1 req = {} req['BrokerID'] = self.__brokerid req['InvestorID'] = self.__userid - self.reqSettlementInfoConfirm(req, self.__reqid) \ No newline at end of file + self.reqSettlementInfoConfirm(req, self.__reqid) + print u'DemoApi.py DemoTdApi.confirmSettlement() end.' \ No newline at end of file diff --git a/vn.strategy/strategydemo/demoEngine.py b/vn.strategy/strategydemo/demoEngine.py index 25f28592..cb55ec8d 100644 --- a/vn.strategy/strategydemo/demoEngine.py +++ b/vn.strategy/strategydemo/demoEngine.py @@ -27,10 +27,14 @@ class MainEngine: def __init__(self): """Constructor""" self.ee = EventEngine() # 创建事件驱动引擎 - + print u'demoEngine.MainEngine create EventEngine() success' + self.md = DemoMdApi(self.ee) # 创建API接口 + print u'demoEngine.MainEngine create DemoMdApi() success' + self.td = DemoTdApi(self.ee) - + print u'demoEngine.MainEngine create DemoTdApi() success' + self.ee.start() # 启动事件驱动引擎 # 循环查询持仓和账户相关 diff --git a/vn.strategy/strategydemo/demoStrategy.py b/vn.strategy/strategydemo/demoStrategy.py index 95561ac2..b5d46748 100644 --- a/vn.strategy/strategydemo/demoStrategy.py +++ b/vn.strategy/strategydemo/demoStrategy.py @@ -2,15 +2,21 @@ # 首先写系统内置模块 import sys +print u'demoStrategy.py import sys success' from datetime import datetime, timedelta, time +print u'demoStrategy.py import datetime.datetime/timedelta/time success' from time import sleep +print u'demoStrategy.py import time.sleep success' # 然后是第三方库模块(如PyQt4等) import sip +print u'demoStrategy.py import sip success' from PyQt4 import QtCore +print u'demoStrategy.py import PyQt4.QtCore success' # 然后是自己编写的模块 from demoEngine import MainEngine +print u'demoStrategy.py import demoEngine.MainEngine success' from strategyEngine import * @@ -265,9 +271,12 @@ def main(): """运行在CMD中的演示程度""" # 创建PyQt4应用对象 app = QtCore.QCoreApplication(sys.argv) - + print u'demoStrategy.py Main call QtCore.QCoreApplication(sys.argv) success' + # 创建主引擎对象 me = MainEngine() + print u'demoStrategy.py Main call MainEngine() success' + # 注册事件监听 me.ee.register(EVENT_LOG, print_log) diff --git a/vn.strategy/strategydemo/eventEngine.py b/vn.strategy/strategydemo/eventEngine.py index c0fc249a..9e693c96 100644 --- a/vn.strategy/strategydemo/eventEngine.py +++ b/vn.strategy/strategydemo/eventEngine.py @@ -2,13 +2,20 @@ # 系统模块 from Queue import Queue, Empty +print u'eventEngine.py import Queue.Queue/Empty success' + from threading import Thread +print u'eventEngine.py import threading.Thread success' # 第三方模块 from PyQt4.QtCore import QTimer +print u'eventEngine.py import PyQt4.QtCore.QTimer success' # 自己开发的模块 from eventType import * +print u'eventEngine.py import eventType.* success' + + ######################################################################## diff --git a/vn.strategy/strategydemo/strategyEngine.py b/vn.strategy/strategydemo/strategyEngine.py index d30a1b09..2a7bcf43 100644 --- a/vn.strategy/strategydemo/strategyEngine.py +++ b/vn.strategy/strategydemo/strategyEngine.py @@ -1,11 +1,18 @@ # encoding: UTF-8 from datetime import datetime +print u'StragegyEngine.py import datetime.datetime success' + +from pymongo import MongoClient as Connection +print u'demoStrategy.py import pymongo.Connection success' -from pymongo import Connection from pymongo.errors import * +print u'demoStrategy.py import pymongo.errors.* success' from eventEngine import * +print u'demoStrategy.py import eventEngine.* success' + + # 常量定义 diff --git a/vnpy.pyproj b/vnpy.pyproj new file mode 100644 index 00000000..21da358c --- /dev/null +++ b/vnpy.pyproj @@ -0,0 +1,178 @@ + + + + Debug + 2.0 + {5c9c864b-1736-4189-81b1-d32b67b54740} + + vn.event\eventEngine.py + + . + . + {888888a0-9f3d-457c-b088-3a5042f75d52} + Standard Python launcher + {2af0f10d-7135-4994-9156-5d01c9c11b7e} + 2.7 + + + + + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file