From b043f45f302544b98cb7a388b8be33de0d528ef9 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Mon, 15 May 2017 16:26:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9BarData=E5=92=8CTickData?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + vnpy/trader/ctaStrategy/ctaBacktesting.py | 9 ++- vnpy/trader/ctaStrategy/ctaBase.py | 2 - vnpy/trader/ctaStrategy/ctaEngine.py | 18 ++--- vnpy/trader/ctaStrategy/ctaHistoryData.py | 14 ++-- .../ctaStrategy/strategy/strategyAtrRsi.py | 3 +- .../strategy/strategyDualThrust.py | 3 +- .../ctaStrategy/strategy/strategyEmaDemo.py | 4 +- .../strategy/strategyKingKeltner.py | 5 +- vnpy/trader/ctaStrategy/tools/ctaLineBar.py | 4 +- .../multiTimeFrame/ctaBacktestMultiTF.py | 7 +- .../multiTimeFrame/ctaStrategyMultiTF.py | 1 + vnpy/trader/ctaStrategy/uiCtaWidget.py | 47 +++++------ vnpy/trader/dataRecorder/drBase.py | 80 ------------------- vnpy/trader/dataRecorder/drEngine.py | 64 +++++++-------- vnpy/trader/dataRecorder/uiDrWidget.py | 35 ++++---- vnpy/trader/vtEngine.py | 2 +- 17 files changed, 111 insertions(+), 188 deletions(-) diff --git a/.gitignore b/.gitignore index 1c245f5d..6c09c8e3 100644 --- a/.gitignore +++ b/.gitignore @@ -39,6 +39,7 @@ Release/ *.local *.temp *.vt +*.log vn.ctp/build/* vn.lts/build/* diff --git a/vnpy/trader/ctaStrategy/ctaBacktesting.py b/vnpy/trader/ctaStrategy/ctaBacktesting.py index 5b67f875..7c3efba5 100644 --- a/vnpy/trader/ctaStrategy/ctaBacktesting.py +++ b/vnpy/trader/ctaStrategy/ctaBacktesting.py @@ -13,6 +13,7 @@ import multiprocessing import pymongo from vnpy.trader.vtGlobal import globalSetting +from vnpy.trader.vtObject import VtTickData, VtBarData from vnpy.trader.vtConstant import * from vnpy.trader.vtGateway import VtOrderData, VtTradeData @@ -127,10 +128,10 @@ class BacktestingEngine(object): # 首先根据回测模式,确认要使用的数据类 if self.mode == self.BAR_MODE: - dataClass = CtaBarData + dataClass = VtBarData func = self.newBar else: - dataClass = CtaTickData + dataClass = VtTickData func = self.newTick # 载入初始化需要用的数据 @@ -163,10 +164,10 @@ class BacktestingEngine(object): # 首先根据回测模式,确认要使用的数据类 if self.mode == self.BAR_MODE: - dataClass = CtaBarData + dataClass = VtBarData func = self.newBar else: - dataClass = CtaTickData + dataClass = VtTickData func = self.newTick self.output(u'开始回测') diff --git a/vnpy/trader/ctaStrategy/ctaBase.py b/vnpy/trader/ctaStrategy/ctaBase.py index f00a8374..1bc92b79 100644 --- a/vnpy/trader/ctaStrategy/ctaBase.py +++ b/vnpy/trader/ctaStrategy/ctaBase.py @@ -35,8 +35,6 @@ ENGINETYPE_TRADING = 'trading' # 实盘 # CTA引擎中涉及的数据类定义 from vnpy.trader.vtConstant import EMPTY_UNICODE, EMPTY_STRING, EMPTY_FLOAT, EMPTY_INT -from vnpy.trader.vtObject import VtBarData as CtaBarData -from vnpy.trader.vtObject import VtTickData as CtaTickData ######################################################################## diff --git a/vnpy/trader/ctaStrategy/ctaEngine.py b/vnpy/trader/ctaStrategy/ctaEngine.py index fb244eec..70d4b969 100644 --- a/vnpy/trader/ctaStrategy/ctaEngine.py +++ b/vnpy/trader/ctaStrategy/ctaEngine.py @@ -27,6 +27,7 @@ from datetime import datetime, timedelta from vnpy.event import Event from vnpy.trader.vtEvent import * from vnpy.trader.vtConstant import * +from vnpy.trader.vtObject import VtTickData, VtBarData from vnpy.trader.vtGateway import VtSubscribeReq, VtOrderReq, VtCancelOrderReq, VtLogData from vnpy.trader.vtFunction import todayDate @@ -252,19 +253,14 @@ class CtaEngine(object): # 推送tick到对应的策略实例进行处理 if tick.vtSymbol in self.tickStrategyDict: - # 将vtTickData数据转化为ctaTickData - ctaTick = CtaTickData() - d = ctaTick.__dict__ - for key in d.keys(): - if key != 'datetime': - d[key] = tick.__getattribute__(key) # 添加datetime字段 - ctaTick.datetime = datetime.strptime(' '.join([tick.date, tick.time]), '%Y%m%d %H:%M:%S.%f') + if not tick.datetime: + tick.datetime = datetime.strptime(' '.join([tick.date, tick.time]), '%Y%m%d %H:%M:%S.%f') # 逐个推送到策略实例中 l = self.tickStrategyDict[tick.vtSymbol] for strategy in l: - self.callStrategyFunc(strategy, strategy.onTick, ctaTick) + self.callStrategyFunc(strategy, strategy.onTick, tick) #---------------------------------------------------------------------- def processOrderEvent(self, event): @@ -330,7 +326,7 @@ class CtaEngine(object): #---------------------------------------------------------------------- def insertData(self, dbName, collectionName, data): - """插入数据到数据库(这里的data可以是CtaTickData或者CtaBarData)""" + """插入数据到数据库(这里的data可以是VtTickData或者VtBarData)""" self.mainEngine.dbInsert(dbName, collectionName, data.__dict__) #---------------------------------------------------------------------- @@ -343,7 +339,7 @@ class CtaEngine(object): l = [] for d in barData: - bar = CtaBarData() + bar = VtBarData() bar.__dict__ = d l.append(bar) return l @@ -358,7 +354,7 @@ class CtaEngine(object): l = [] for d in tickData: - tick = CtaTickData() + tick = VtTickData() tick.__dict__ = d l.append(tick) return l diff --git a/vnpy/trader/ctaStrategy/ctaHistoryData.py b/vnpy/trader/ctaStrategy/ctaHistoryData.py index 5e478159..cfa01cba 100644 --- a/vnpy/trader/ctaStrategy/ctaHistoryData.py +++ b/vnpy/trader/ctaStrategy/ctaHistoryData.py @@ -15,8 +15,8 @@ import pymongo from vnpy.trader.vtGlobal import globalSetting from vnpy.trader.vtConstant import * +from vnpy.trader.vtObject import VtBarData -from vnpy.trader.ctaStrategy.ctaBase import * from vnpy.trader.ctaStrategy.datayesClient import DatayesClient @@ -137,7 +137,7 @@ class HistoryDataEngine(object): unique=True) for d in data: - bar = CtaBarData() + bar = VtBarData() bar.vtSymbol = symbol bar.symbol = symbol try: @@ -205,7 +205,7 @@ class HistoryDataEngine(object): unique=True) for d in data: - bar = CtaBarData() + bar = VtBarData() bar.vtSymbol = symbol bar.symbol = symbol try: @@ -289,7 +289,7 @@ class HistoryDataEngine(object): unique=True) for d in data: - bar = CtaBarData() + bar = VtBarData() bar.vtSymbol = symbol bar.symbol = symbol try: @@ -340,7 +340,7 @@ def downloadEquityDailyBarts(self, symbol): unique=True) for index, d in data.iterrows(): - bar = CtaBarData() + bar = VtBarData() bar.vtSymbol = symbol bar.symbol = symbol try: @@ -378,7 +378,7 @@ def loadMcCsv(fileName, dbName, symbol): # 读取数据和插入到数据库 reader = csv.DictReader(file(fileName, 'r')) for d in reader: - bar = CtaBarData() + bar = VtBarData() bar.vtSymbol = symbol bar.symbol = symbol bar.open = float(d['Open']) @@ -412,7 +412,7 @@ def loadTdxCsv(fileName, dbName, symbol): # 读取数据和插入到数据库 reader = csv.reader(file(fileName, 'r')) for d in reader: - bar = CtaBarData() + bar = VtBarData() bar.vtSymbol = symbol bar.symbol = symbol bar.open = float(d[2]) diff --git a/vnpy/trader/ctaStrategy/strategy/strategyAtrRsi.py b/vnpy/trader/ctaStrategy/strategy/strategyAtrRsi.py index a81bc28d..a4f3bb5f 100644 --- a/vnpy/trader/ctaStrategy/strategy/strategyAtrRsi.py +++ b/vnpy/trader/ctaStrategy/strategy/strategyAtrRsi.py @@ -13,6 +13,7 @@ import talib import numpy as np +from vnpy.trader.vtObject import VtBarData from vnpy.trader.vtConstant import EMPTY_STRING from vnpy.trader.ctaStrategy.ctaTemplate import CtaTemplate @@ -124,7 +125,7 @@ class AtrRsiStrategy(CtaTemplate): if self.bar: self.onBar(self.bar) - bar = CtaBarData() + bar = VtBarData() bar.vtSymbol = tick.vtSymbol bar.symbol = tick.symbol bar.exchange = tick.exchange diff --git a/vnpy/trader/ctaStrategy/strategy/strategyDualThrust.py b/vnpy/trader/ctaStrategy/strategy/strategyDualThrust.py index 46f8c74b..e9ae82e3 100644 --- a/vnpy/trader/ctaStrategy/strategy/strategyDualThrust.py +++ b/vnpy/trader/ctaStrategy/strategy/strategyDualThrust.py @@ -6,6 +6,7 @@ DualThrust交易策略 from datetime import time +from vnpy.trader.vtObject import VtBarData from vnpy.trader.vtConstant import EMPTY_STRING from vnpy.trader.ctaStrategy.ctaTemplate import CtaTemplate @@ -100,7 +101,7 @@ class DualThrustStrategy(CtaTemplate): if self.bar: self.onBar(self.bar) - bar = CtaBarData() + bar = VtBarData() bar.vtSymbol = tick.vtSymbol bar.symbol = tick.symbol bar.exchange = tick.exchange diff --git a/vnpy/trader/ctaStrategy/strategy/strategyEmaDemo.py b/vnpy/trader/ctaStrategy/strategy/strategyEmaDemo.py index 6f8d64dd..09e5928a 100644 --- a/vnpy/trader/ctaStrategy/strategy/strategyEmaDemo.py +++ b/vnpy/trader/ctaStrategy/strategy/strategyEmaDemo.py @@ -11,7 +11,9 @@ 也希望社区能做出一个解决了以上潜在风险的Demo出来。 """ +from __future__ import division +from vnpy.trader.vtObject import VtBarData from vnpy.trader.vtConstant import EMPTY_STRING, EMPTY_FLOAT from vnpy.trader.ctaStrategy.ctaTemplate import CtaTemplate @@ -101,7 +103,7 @@ class EmaDemoStrategy(CtaTemplate): if self.bar: self.onBar(self.bar) - bar = CtaBarData() + bar = VtBarData() bar.vtSymbol = tick.vtSymbol bar.symbol = tick.symbol bar.exchange = tick.exchange diff --git a/vnpy/trader/ctaStrategy/strategy/strategyKingKeltner.py b/vnpy/trader/ctaStrategy/strategy/strategyKingKeltner.py index 57e37ce3..594c1d9b 100644 --- a/vnpy/trader/ctaStrategy/strategy/strategyKingKeltner.py +++ b/vnpy/trader/ctaStrategy/strategy/strategyKingKeltner.py @@ -15,6 +15,7 @@ from __future__ import division import talib import numpy as np +from vnpy.trader.vtObject import VtBarData from vnpy.trader.vtConstant import EMPTY_STRING from vnpy.trader.ctaStrategy.ctaTemplate import CtaTemplate @@ -110,7 +111,7 @@ class KkStrategy(CtaTemplate): if self.bar: self.onBar(self.bar) - bar = CtaBarData() + bar = VtBarData() bar.vtSymbol = tick.vtSymbol bar.symbol = tick.symbol bar.exchange = tick.exchange @@ -154,7 +155,7 @@ class KkStrategy(CtaTemplate): else: # 如果没有缓存则新建 if not self.fiveBar: - fiveBar = CtaBarData() + fiveBar = VtBarData() fiveBar.vtSymbol = bar.vtSymbol fiveBar.symbol = bar.symbol diff --git a/vnpy/trader/ctaStrategy/tools/ctaLineBar.py b/vnpy/trader/ctaStrategy/tools/ctaLineBar.py index dfae9217..a741c362 100644 --- a/vnpy/trader/ctaStrategy/tools/ctaLineBar.py +++ b/vnpy/trader/ctaStrategy/tools/ctaLineBar.py @@ -4,7 +4,7 @@ # WeChat/QQ: 28888502 from vtConstant import * -from ctaBase import * +from vnpy.trader.vtObject import VtBarData from datetime import datetime @@ -240,7 +240,7 @@ class CtaLineBar(object): def __firstTick(self,tick): """ K线的第一个Tick数据""" - self.bar = CtaBarData() # 创建新的K线 + self.bar = VtBarData() # 创建新的K线 self.bar.vtSymbol = tick.vtSymbol self.bar.symbol = tick.symbol diff --git a/vnpy/trader/ctaStrategy/tools/multiTimeFrame/ctaBacktestMultiTF.py b/vnpy/trader/ctaStrategy/tools/multiTimeFrame/ctaBacktestMultiTF.py index 91bde1ce..ab492b79 100644 --- a/vnpy/trader/ctaStrategy/tools/multiTimeFrame/ctaBacktestMultiTF.py +++ b/vnpy/trader/ctaStrategy/tools/multiTimeFrame/ctaBacktestMultiTF.py @@ -9,6 +9,7 @@ same as CTA engine. Real trading code can be directly used for backtesting. from __future__ import division +from vnpy.trader.vtObject import VtTickData, VtBarData from ctaBacktesting import * class BacktestEngineMultiTF(BacktestingEngine): @@ -84,10 +85,10 @@ class BacktestEngineMultiTF(BacktestingEngine): # 首先根据回测模式,确认要使用的数据类 # Choose data type based on backtest mode if self.mode == self.BAR_MODE: - self.dataClass = CtaBarData + self.dataClass = VtBarData self.func = self.newBar else: - self.dataClass = CtaTickData + self.dataClass = VtTickData self.func = self.newTick # Load initializing data @@ -164,7 +165,7 @@ class BacktestEngineMultiTF(BacktestingEngine): if (data is not None) and (data['datetime'] <= self.dt): try: - temp[info_symbol] = CtaBarData() + temp[info_symbol] = VtBarData() temp[info_symbol].__dict__ = data self.infobar[info_symbol] = next(self.InfoCursor[info_symbol]) except StopIteration: diff --git a/vnpy/trader/ctaStrategy/tools/multiTimeFrame/ctaStrategyMultiTF.py b/vnpy/trader/ctaStrategy/tools/multiTimeFrame/ctaStrategyMultiTF.py index 21eb627e..d12ca378 100644 --- a/vnpy/trader/ctaStrategy/tools/multiTimeFrame/ctaStrategyMultiTF.py +++ b/vnpy/trader/ctaStrategy/tools/multiTimeFrame/ctaStrategyMultiTF.py @@ -3,6 +3,7 @@ This file tweaks ctaTemplate Module to suit multi-TimeFrame strategies. """ + from strategyAtrRsi import * from ctaBase import * from ctaTemplate import CtaTemplate diff --git a/vnpy/trader/ctaStrategy/uiCtaWidget.py b/vnpy/trader/ctaStrategy/uiCtaWidget.py index 8032aff6..d3d3064e 100644 --- a/vnpy/trader/ctaStrategy/uiCtaWidget.py +++ b/vnpy/trader/ctaStrategy/uiCtaWidget.py @@ -6,6 +6,7 @@ CTA模块相关的GUI控制组件 from vnpy.event import Event +from vnpy.trader.vtEvent import * from vnpy.trader.uiBasicWidget import QtGui, QtCore, QtWidgets, BasicCell from vnpy.trader.ctaStrategy.language import text @@ -44,7 +45,7 @@ class CtaValueMonitor(QtWidgets.QTableWidget): col = 0 for k, v in data.items(): - cell = QtGui.QTableWidgetItem(unicode(v)) + cell = QtWidgets.QTableWidgetItem(unicode(v)) self.keyCellDict[k] = cell self.setItem(0, col, cell) col += 1 @@ -86,26 +87,26 @@ class CtaStrategyManager(QtWidgets.QGroupBox): self.paramMonitor.setFixedHeight(height) self.varMonitor.setFixedHeight(height) - buttonInit = QtGui.QPushButton(text.INIT) - buttonStart = QtGui.QPushButton(text.START) - buttonStop = QtGui.QPushButton(text.STOP) + buttonInit = QtWidgets.QPushButton(text.INIT) + buttonStart = QtWidgets.QPushButton(text.START) + buttonStop = QtWidgets.QPushButton(text.STOP) buttonInit.clicked.connect(self.init) buttonStart.clicked.connect(self.start) buttonStop.clicked.connect(self.stop) - hbox1 = QtGui.QHBoxLayout() + hbox1 = QtWidgets.QHBoxLayout() hbox1.addWidget(buttonInit) hbox1.addWidget(buttonStart) hbox1.addWidget(buttonStop) hbox1.addStretch() - hbox2 = QtGui.QHBoxLayout() + hbox2 = QtWidgets.QHBoxLayout() hbox2.addWidget(self.paramMonitor) - hbox3 = QtGui.QHBoxLayout() + hbox3 = QtWidgets.QHBoxLayout() hbox3.addWidget(self.varMonitor) - vbox = QtGui.QVBoxLayout() + vbox = QtWidgets.QVBoxLayout() vbox.addLayout(hbox1) vbox.addLayout(hbox2) vbox.addLayout(hbox3) @@ -172,11 +173,11 @@ class CtaEngineManager(QtWidgets.QWidget): self.setWindowTitle(text.CTA_STRATEGY) # 按钮 - loadButton = QtGui.QPushButton(text.LOAD_STRATEGY) - initAllButton = QtGui.QPushButton(text.INIT_ALL) - startAllButton = QtGui.QPushButton(text.START_ALL) - stopAllButton = QtGui.QPushButton(text.STOP_ALL) - savePositionButton = QtGui.QPushButton(text.SAVE_POSITION_DATA) + loadButton = QtWidgets.QPushButton(text.LOAD_STRATEGY) + initAllButton = QtWidgets.QPushButton(text.INIT_ALL) + startAllButton = QtWidgets.QPushButton(text.START_ALL) + stopAllButton = QtWidgets.QPushButton(text.STOP_ALL) + savePositionButton = QtWidgets.QPushButton(text.SAVE_POSITION_DATA) loadButton.clicked.connect(self.load) initAllButton.clicked.connect(self.initAll) @@ -185,16 +186,16 @@ class CtaEngineManager(QtWidgets.QWidget): savePositionButton.clicked.connect(self.ctaEngine.savePosition) # 滚动区域,放置所有的CtaStrategyManager - self.scrollArea = QtGui.QScrollArea() + self.scrollArea = QtWidgets.QScrollArea() self.scrollArea.setWidgetResizable(True) # CTA组件的日志监控 - self.ctaLogMonitor = QtGui.QTextEdit() + self.ctaLogMonitor = QtWidgets.QTextEdit() self.ctaLogMonitor.setReadOnly(True) self.ctaLogMonitor.setMaximumHeight(200) # 设置布局 - hbox2 = QtGui.QHBoxLayout() + hbox2 = QtWidgets.QHBoxLayout() hbox2.addWidget(loadButton) hbox2.addWidget(initAllButton) hbox2.addWidget(startAllButton) @@ -202,7 +203,7 @@ class CtaEngineManager(QtWidgets.QWidget): hbox2.addWidget(savePositionButton) hbox2.addStretch() - vbox = QtGui.QVBoxLayout() + vbox = QtWidgets.QVBoxLayout() vbox.addLayout(hbox2) vbox.addWidget(self.scrollArea) vbox.addWidget(self.ctaLogMonitor) @@ -211,8 +212,8 @@ class CtaEngineManager(QtWidgets.QWidget): #---------------------------------------------------------------------- def initStrategyManager(self): """初始化策略管理组件界面""" - w = QtGui.QWidget() - vbox = QtGui.QVBoxLayout() + w = QtWidgets.QWidget() + vbox = QtWidgets.QVBoxLayout() for name in self.ctaEngine.strategyDict.keys(): strategyManager = CtaStrategyManager(self.ctaEngine, self.eventEngine, name) @@ -266,11 +267,11 @@ class CtaEngineManager(QtWidgets.QWidget): #---------------------------------------------------------------------- def closeEvent(self, event): """关闭窗口时的事件""" - reply = QtGui.QMessageBox.question(self, text.SAVE_POSITION_DATA, - text.SAVE_POSITION_QUESTION, QtGui.QMessageBox.Yes | - QtGui.QMessageBox.No, QtGui.QMessageBox.No) + reply = QtWidgets.QMessageBox.question(self, text.SAVE_POSITION_DATA, + text.SAVE_POSITION_QUESTION, QtWidgets.QMessageBox.Yes | + QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No) - if reply == QtGui.QMessageBox.Yes: + if reply == QtWidgets.QMessageBox.Yes: self.ctaEngine.savePosition() event.accept() diff --git a/vnpy/trader/dataRecorder/drBase.py b/vnpy/trader/dataRecorder/drBase.py index 697bdfcf..cddfd90a 100644 --- a/vnpy/trader/dataRecorder/drBase.py +++ b/vnpy/trader/dataRecorder/drBase.py @@ -6,12 +6,6 @@ from __future__ import division - -# 把vn.trader根目录添加到python环境变量中 -import sys -sys.path.append('..') - - # 数据库名称 SETTING_DB_NAME = 'VnTrader_Setting_Db' TICK_DB_NAME = 'VnTrader_Tick_Db' @@ -21,77 +15,3 @@ MINUTE_DB_NAME = 'VnTrader_1Min_Db' # CTA引擎中涉及的数据类定义 from vnpy.trader.vtConstant import EMPTY_UNICODE, EMPTY_STRING, EMPTY_FLOAT, EMPTY_INT - - -######################################################################## -class DrBarData(object): - """K线数据""" - - #---------------------------------------------------------------------- - def __init__(self): - """Constructor""" - self.vtSymbol = EMPTY_STRING # vt系统代码 - self.symbol = EMPTY_STRING # 代码 - self.exchange = EMPTY_STRING # 交易所 - - self.open = EMPTY_FLOAT # OHLC - self.high = EMPTY_FLOAT - self.low = EMPTY_FLOAT - self.close = EMPTY_FLOAT - - self.date = EMPTY_STRING # bar开始的时间,日期 - self.time = EMPTY_STRING # 时间 - self.datetime = None # python的datetime时间对象 - - self.volume = EMPTY_INT # 成交量 - self.openInterest = EMPTY_INT # 持仓量 - - -######################################################################## -class DrTickData(object): - """Tick数据""" - - #---------------------------------------------------------------------- - def __init__(self): - """Constructor""" - self.vtSymbol = EMPTY_STRING # vt系统代码 - self.symbol = EMPTY_STRING # 合约代码 - self.exchange = EMPTY_STRING # 交易所代码 - - # 成交数据 - self.lastPrice = EMPTY_FLOAT # 最新成交价 - self.volume = EMPTY_INT # 最新成交量 - self.openInterest = EMPTY_INT # 持仓量 - - self.upperLimit = EMPTY_FLOAT # 涨停价 - self.lowerLimit = EMPTY_FLOAT # 跌停价 - - # tick的时间 - self.date = EMPTY_STRING # 日期 - self.time = EMPTY_STRING # 时间 - self.datetime = None # python的datetime时间对象 - - # 五档行情 - self.bidPrice1 = EMPTY_FLOAT - self.bidPrice2 = EMPTY_FLOAT - self.bidPrice3 = EMPTY_FLOAT - self.bidPrice4 = EMPTY_FLOAT - self.bidPrice5 = EMPTY_FLOAT - - self.askPrice1 = EMPTY_FLOAT - self.askPrice2 = EMPTY_FLOAT - self.askPrice3 = EMPTY_FLOAT - self.askPrice4 = EMPTY_FLOAT - self.askPrice5 = EMPTY_FLOAT - - self.bidVolume1 = EMPTY_INT - self.bidVolume2 = EMPTY_INT - self.bidVolume3 = EMPTY_INT - self.bidVolume4 = EMPTY_INT - self.bidVolume5 = EMPTY_INT - - self.askVolume1 = EMPTY_INT - self.askVolume2 = EMPTY_INT - self.askVolume3 = EMPTY_INT - self.askVolume4 = EMPTY_INT - self.askVolume5 = EMPTY_INT \ No newline at end of file diff --git a/vnpy/trader/dataRecorder/drEngine.py b/vnpy/trader/dataRecorder/drEngine.py index 7fa89fcf..c4905f51 100644 --- a/vnpy/trader/dataRecorder/drEngine.py +++ b/vnpy/trader/dataRecorder/drEngine.py @@ -17,7 +17,7 @@ from threading import Thread from vnpy.event import Event from vnpy.trader.vtEvent import * from vnpy.trader.vtFunction import todayDate -from vnpy.trader.vtGateway import VtSubscribeReq, VtLogData +from vnpy.trader.vtObject import VtSubscribeReq, VtLogData, VtBarData, VtTickData from vnpy.trader.dataRecorder.drBase import * from vnpy.trader.dataRecorder.language import text @@ -90,8 +90,8 @@ class DrEngine(object): self.mainEngine.subscribe(req, setting[1]) - drTick = DrTickData() # 该tick实例可以用于缓存部分数据(目前未使用) - self.tickDict[vtSymbol] = drTick + tick = VtTickData() # 该tick实例可以用于缓存部分数据(目前未使用) + self.tickDict[vtSymbol] = tick if 'bar' in drSetting: l = drSetting['bar'] @@ -113,7 +113,7 @@ class DrEngine(object): self.mainEngine.subscribe(req, setting[1]) - bar = DrBarData() + bar = VtBarData() self.barDict[vtSymbol] = bar if 'active' in drSetting: @@ -136,34 +136,30 @@ class DrEngine(object): vtSymbol = tick.vtSymbol # 转化Tick格式 - drTick = DrTickData() - d = drTick.__dict__ - for key in d.keys(): - if key != 'datetime': - d[key] = tick.__getattribute__(key) - drTick.datetime = datetime.strptime(' '.join([tick.date, tick.time]), '%Y%m%d %H:%M:%S.%f') + if not tick.datetime: + tick.datetime = datetime.strptime(' '.join([tick.date, tick.time]), '%Y%m%d %H:%M:%S.%f') # 更新Tick数据 if vtSymbol in self.tickDict: - self.insertData(TICK_DB_NAME, vtSymbol, drTick) + self.insertData(TICK_DB_NAME, vtSymbol, tick) if vtSymbol in self.activeSymbolDict: activeSymbol = self.activeSymbolDict[vtSymbol] - self.insertData(TICK_DB_NAME, activeSymbol, drTick) + self.insertData(TICK_DB_NAME, activeSymbol, tick) # 发出日志 - self.writeDrLog(text.TICK_LOGGING_MESSAGE.format(symbol=drTick.vtSymbol, - time=drTick.time, - last=drTick.lastPrice, - bid=drTick.bidPrice1, - ask=drTick.askPrice1)) + self.writeDrLog(text.TICK_LOGGING_MESSAGE.format(symbol=tick.vtSymbol, + time=tick.time, + last=tick.lastPrice, + bid=tick.bidPrice1, + ask=tick.askPrice1)) # 更新分钟线数据 if vtSymbol in self.barDict: bar = self.barDict[vtSymbol] # 如果第一个TICK或者新的一分钟 - if not bar.datetime or bar.datetime.minute != drTick.datetime.minute: + if not bar.datetime or bar.datetime.minute != tick.datetime.minute: if bar.vtSymbol: newBar = copy.copy(bar) self.insertData(MINUTE_DB_NAME, vtSymbol, newBar) @@ -179,25 +175,25 @@ class DrEngine(object): low=bar.low, close=bar.close)) - bar.vtSymbol = drTick.vtSymbol - bar.symbol = drTick.symbol - bar.exchange = drTick.exchange + bar.vtSymbol = tick.vtSymbol + bar.symbol = tick.symbol + bar.exchange = tick.exchange - bar.open = drTick.lastPrice - bar.high = drTick.lastPrice - bar.low = drTick.lastPrice - bar.close = drTick.lastPrice + bar.open = tick.lastPrice + bar.high = tick.lastPrice + bar.low = tick.lastPrice + bar.close = tick.lastPrice - bar.date = drTick.date - bar.time = drTick.time - bar.datetime = drTick.datetime - bar.volume = drTick.volume - bar.openInterest = drTick.openInterest + bar.date = tick.date + bar.time = tick.time + bar.datetime = tick.datetime + bar.volume = tick.volume + bar.openInterest = tick.openInterest # 否则继续累加新的K线 else: - bar.high = max(bar.high, drTick.lastPrice) - bar.low = min(bar.low, drTick.lastPrice) - bar.close = drTick.lastPrice + bar.high = max(bar.high, tick.lastPrice) + bar.low = min(bar.low, tick.lastPrice) + bar.close = tick.lastPrice #---------------------------------------------------------------------- def registerEvent(self): @@ -206,7 +202,7 @@ class DrEngine(object): #---------------------------------------------------------------------- def insertData(self, dbName, collectionName, data): - """插入数据到数据库(这里的data可以是CtaTickData或者CtaBarData)""" + """插入数据到数据库(这里的data可以是VtTickData或者VtBarData)""" self.queue.put((dbName, collectionName, data.__dict__)) #---------------------------------------------------------------------- diff --git a/vnpy/trader/dataRecorder/uiDrWidget.py b/vnpy/trader/dataRecorder/uiDrWidget.py index 114d7ea9..7732c6a9 100644 --- a/vnpy/trader/dataRecorder/uiDrWidget.py +++ b/vnpy/trader/dataRecorder/uiDrWidget.py @@ -6,8 +6,11 @@ import json +from qtpy import QtWidgets, QtGui, QtCore + from vnpy.event import Event -from vnpy.trader.uiBasicWidget import QtWidgets, QtGui, QtCore +from vnpy.trader.vtEvent import * + from vnpy.trader.dataRecorder.language import text @@ -56,40 +59,40 @@ class DrEngineManager(QtWidgets.QWidget): self.setWindowTitle(text.DATA_RECORDER) # 记录合约配置监控 - tickLabel = QtGui.QLabel(text.TICK_RECORD) - self.tickTable = QtGui.QTableWidget() + tickLabel = QtWidgets.QLabel(text.TICK_RECORD) + self.tickTable = QtWidgets.QTableWidget() self.tickTable.setColumnCount(2) self.tickTable.verticalHeader().setVisible(False) - self.tickTable.setEditTriggers(QtGui.QTableWidget.NoEditTriggers) - self.tickTable.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch) + self.tickTable.setEditTriggers(QtWidgets.QTableWidget.NoEditTriggers) + self.tickTable.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch) self.tickTable.setAlternatingRowColors(True) self.tickTable.setHorizontalHeaderLabels([text.CONTRACT_SYMBOL, text.GATEWAY]) - barLabel = QtGui.QLabel(text.BAR_RECORD) - self.barTable = QtGui.QTableWidget() + barLabel = QtWidgets.QLabel(text.BAR_RECORD) + self.barTable = QtWidgets.QTableWidget() self.barTable.setColumnCount(2) self.barTable.verticalHeader().setVisible(False) - self.barTable.setEditTriggers(QtGui.QTableWidget.NoEditTriggers) - self.barTable.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch) + self.barTable.setEditTriggers(QtWidgets.QTableWidget.NoEditTriggers) + self.barTable.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch) self.barTable.setAlternatingRowColors(True) self.barTable.setHorizontalHeaderLabels([text.CONTRACT_SYMBOL, text.GATEWAY]) - activeLabel = QtGui.QLabel(text.DOMINANT_CONTRACT) - self.activeTable = QtGui.QTableWidget() + activeLabel = QtWidgets.QLabel(text.DOMINANT_CONTRACT) + self.activeTable = QtWidgets.QTableWidget() self.activeTable.setColumnCount(2) self.activeTable.verticalHeader().setVisible(False) - self.activeTable.setEditTriggers(QtGui.QTableWidget.NoEditTriggers) - self.activeTable.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch) + self.activeTable.setEditTriggers(QtWidgets.QTableWidget.NoEditTriggers) + self.activeTable.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch) self.activeTable.setAlternatingRowColors(True) self.activeTable.setHorizontalHeaderLabels([text.DOMINANT_SYMBOL, text.CONTRACT_SYMBOL]) # 日志监控 - self.logMonitor = QtGui.QTextEdit() + self.logMonitor = QtWidgets.QTextEdit() self.logMonitor.setReadOnly(True) self.logMonitor.setMinimumHeight(600) # 设置布局 - grid = QtGui.QGridLayout() + grid = QtWidgets.QGridLayout() grid.addWidget(tickLabel, 0, 0) grid.addWidget(barLabel, 0, 1) @@ -98,7 +101,7 @@ class DrEngineManager(QtWidgets.QWidget): grid.addWidget(self.barTable, 1, 1) grid.addWidget(self.activeTable, 1, 2) - vbox = QtGui.QVBoxLayout() + vbox = QtWidgets.QVBoxLayout() vbox.addLayout(grid) vbox.addWidget(self.logMonitor) self.setLayout(vbox) diff --git a/vnpy/trader/vtEngine.py b/vnpy/trader/vtEngine.py index 9f1ad7b2..6c73ca58 100644 --- a/vnpy/trader/vtEngine.py +++ b/vnpy/trader/vtEngine.py @@ -85,7 +85,7 @@ class MainEngine(object): gateway.connect() # 接口连接后自动执行数据库连接的任务 - #self.dbConnect() + self.dbConnect() #---------------------------------------------------------------------- def subscribe(self, subscribeReq, gatewayName):