From 005df0bcd623a7b36c44ed2f2d27752e63ce87e7 Mon Sep 17 00:00:00 2001 From: chenxy123 Date: Mon, 13 Jun 2016 22:48:23 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E4=BF=AE=E5=A4=8DCTA=E5=9B=9E=E6=B5=8B?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E9=87=8CTICK=E5=9B=9E=E6=B5=8B=E7=9A=84?= =?UTF-8?q?=E6=92=AE=E5=90=88bug=EF=BC=88=E4=B9=8B=E5=89=8D=E5=81=B7?= =?UTF-8?q?=E6=87=92=E5=BF=98=E8=AE=B0=E4=BA=86=EF=BC=89=202.=20=E8=A1=8C?= =?UTF-8?q?=E6=83=85=E8=AE=B0=E5=BD=95=E5=BC=95=E6=93=8E=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=8D=95=E7=8B=AC=E7=9A=84=E6=95=B0=E6=8D=AE=E6=8F=92=E5=85=A5?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E5=92=8C=E9=98=9F=E5=88=97=EF=BC=8C=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E8=A7=A3=E5=86=B3=E4=BA=8B=E4=BB=B6=E5=BC=95=E6=93=8E?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E9=98=BB=E5=A1=9E=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vn.trader/ctaAlgo/ctaBacktesting.py | 18 ++++++------ vn.trader/dataRecorder/DR_setting.json | 2 +- vn.trader/dataRecorder/drEngine.py | 38 ++++++++++++++++++++++++-- vn.trader/vtEngine.py | 3 ++ 4 files changed, 49 insertions(+), 12 deletions(-) diff --git a/vn.trader/ctaAlgo/ctaBacktesting.py b/vn.trader/ctaAlgo/ctaBacktesting.py index 35220f1b..753c7afc 100644 --- a/vn.trader/ctaAlgo/ctaBacktesting.py +++ b/vn.trader/ctaAlgo/ctaBacktesting.py @@ -279,13 +279,15 @@ class BacktestingEngine(object): """基于最新数据撮合限价单""" # 先确定会撮合成交的价格 if self.mode == self.BAR_MODE: - buyCrossPrice = self.bar.low # 若买入方向限价单价格高于该价格,则会成交 - sellCrossPrice = self.bar.high # 若卖出方向限价单价格低于该价格,则会成交 - bestCrossPrice = self.bar.open # 在当前时间点前发出的委托可能的最优成交价 + buyCrossPrice = self.bar.low # 若买入方向限价单价格高于该价格,则会成交 + sellCrossPrice = self.bar.high # 若卖出方向限价单价格低于该价格,则会成交 + buyBestCrossPrice = self.bar.open # 在当前时间点前发出的买入委托可能的最优成交价 + sellBestCrossPrice = self.bar.open # 在当前时间点前发出的卖出委托可能的最优成交价 else: - buyCrossPrice = self.tick.lastPrice - sellCrossPrice = self.tick.lastPrice - bestCrossPrice = self.tick.lastPrice + buyCrossPrice = self.tick.askPrice + sellCrossPrice = self.tick.bidPrice + buyBestCrossPrice = self.tick.askPrice + sellBestCrossPrice = self.tick.bidPrice # 遍历限价单字典中的所有限价单 for orderID, order in self.workingLimitOrderDict.items(): @@ -312,10 +314,10 @@ class BacktestingEngine(object): # 2. 假设在上一根K线结束(也是当前K线开始)的时刻,策略发出的委托为限价105 # 3. 则在实际中的成交价会是100而不是105,因为委托发出时市场的最优价格是100 if buyCross: - trade.price = min(order.price, bestCrossPrice) + trade.price = min(order.price, buyBestCrossPrice) self.strategy.pos += order.totalVolume else: - trade.price = max(order.price, bestCrossPrice) + trade.price = max(order.price, sellBestCrossPrice) self.strategy.pos -= order.totalVolume trade.volume = order.totalVolume diff --git a/vn.trader/dataRecorder/DR_setting.json b/vn.trader/dataRecorder/DR_setting.json index 38c4dd35..f7c297c5 100644 --- a/vn.trader/dataRecorder/DR_setting.json +++ b/vn.trader/dataRecorder/DR_setting.json @@ -3,7 +3,7 @@ "tick": [ - ["IF1605", "SGIT"], + ["m1609", "XSPEED"], ["IF1606", "SGIT"], ["IH1606", "SGIT"], ["IH1606", "SGIT"], diff --git a/vn.trader/dataRecorder/drEngine.py b/vn.trader/dataRecorder/drEngine.py index 644dc0da..76ea1df1 100644 --- a/vn.trader/dataRecorder/drEngine.py +++ b/vn.trader/dataRecorder/drEngine.py @@ -11,6 +11,8 @@ import os import copy from collections import OrderedDict from datetime import datetime, timedelta +from Queue import Queue +from threading import Thread from eventEngine import * from vtGateway import VtSubscribeReq, VtLogData @@ -43,6 +45,11 @@ class DrEngine(object): # K线对象字典 self.barDict = {} + # 负责执行数据库插入的单独线程相关 + self.active = False # 工作状态 + self.queue = Queue() # 队列 + self.thread = Thread(target=self.run) # 线程 + # 载入设置,订阅行情 self.loadSetting() @@ -111,9 +118,12 @@ class DrEngine(object): # 注意这里的vtSymbol对于IB和LTS接口,应该后缀.交易所 for activeSymbol, vtSymbol in d.items(): self.activeSymbolDict[vtSymbol] = activeSymbol - + + # 启动数据插入线程 + self.start() + # 注册事件监听 - self.registerEvent() + self.registerEvent() #---------------------------------------------------------------------- def procecssTickEvent(self, event): @@ -187,7 +197,29 @@ class DrEngine(object): #---------------------------------------------------------------------- def insertData(self, dbName, collectionName, data): """插入数据到数据库(这里的data可以是CtaTickData或者CtaBarData)""" - self.mainEngine.dbInsert(dbName, collectionName, data.__dict__) + self.queue.put((dbName, collectionName, data.__dict__)) + + #---------------------------------------------------------------------- + def run(self): + """运行插入线程""" + while self.active: + try: + dbName, collectionName, d = self.queue.get(block=True, timeout=1) + self.mainEngine.dbInsert(dbName, collectionName, d) + except Empty: + pass + #---------------------------------------------------------------------- + def start(self): + """启动""" + self.active = True + self.thread.start() + + #---------------------------------------------------------------------- + def stop(self): + """退出""" + if self.active: + self.active = False + self.thread.join() #---------------------------------------------------------------------- def writeDrLog(self, content): diff --git a/vn.trader/vtEngine.py b/vn.trader/vtEngine.py index b947ac8a..0ce0bd66 100644 --- a/vn.trader/vtEngine.py +++ b/vn.trader/vtEngine.py @@ -188,6 +188,9 @@ class MainEngine(object): # 停止事件引擎 self.eventEngine.stop() + # 停止数据记录引擎 + self.drEngine.stop() + # 保存数据引擎里的合约数据到硬盘 self.dataEngine.saveContracts()