diff --git a/vn.trader/dataRecorder/drEngine.py b/vn.trader/dataRecorder/drEngine.py index c4565fc4..fbcdc92a 100644 --- a/vn.trader/dataRecorder/drEngine.py +++ b/vn.trader/dataRecorder/drEngine.py @@ -6,18 +6,15 @@ 使用DR_setting.json来配置需要收集的合约,以及主力合约代码。 ''' +import copy import json import os -import copy -from collections import OrderedDict -from datetime import datetime, timedelta -from Queue import Queue -from threading import Thread +from datetime import datetime -from eventEngine import * -from vtGateway import VtSubscribeReq, VtLogData from drBase import * +from eventEngine import * from vtFunction import todayDate +from vtGateway import VtSubscribeReq, VtLogData ######################################################################## @@ -53,6 +50,14 @@ class DrEngine(object): # 载入设置,订阅行情 self.loadSetting() + + def saveSetting(self, setting): + """保存设置""" + setting['working'] = self.working + with open(self.settingFileName, 'w') as f: + str = json.dumps(setting) + f.write(str) + return True #---------------------------------------------------------------------- def loadSetting(self): @@ -61,8 +66,8 @@ class DrEngine(object): drSetting = json.load(f) # 如果working设为False则不启动行情记录功能 - working = drSetting['working'] - if not working: + self.working = drSetting['working'] + if not self.working: return if 'tick' in drSetting: diff --git a/vn.trader/dataRecorder/uiDrEdit.py b/vn.trader/dataRecorder/uiDrEdit.py index 0756f69c..b04a1749 100644 --- a/vn.trader/dataRecorder/uiDrEdit.py +++ b/vn.trader/dataRecorder/uiDrEdit.py @@ -206,11 +206,14 @@ class DrEditWidget(QtGui.QWidget): vline.setSpacing(2) btnTickAll = QtGui.QPushButton(u"全部记录tick", self) btnBarAll = QtGui.QPushButton(u'全部记录bar', self) + btnSaveAll = QtGui.QPushButton(u'保存设置', self) btnTickAll.clicked.connect(self.selectAllTick) btnBarAll.clicked.connect(self.selectAllBar) + btnSaveAll.clicked.connect(self.saveSetting) vline.addWidget(btnTickAll) vline.addWidget(btnBarAll) + vline.addWidget(btnSaveAll) vbox.addLayout(vline) @@ -233,36 +236,60 @@ class DrEditWidget(QtGui.QWidget): child = [] contractDict = {} - # contracts = self.mainEngine.getAllContracts() - # for contract in contracts: - # contractName = self.getContractChineseName(contract.name) - # if contractDict.has_key(contractName): - # parentItem = contractDict[contractName] - # item = TreeItem([contract.symbol, False, False, False, contract.exchange, "CTP"], parentItem) - # parentItem.appendChild(item) - # else: - # item = TreeItem([contractName, False, False, False, contract.exchange, "CTP"], self.model.rootItem) - # contractDict[contractName] = item - # child.append(item) - # subItem = TreeItem([contract.symbol, False, False, False, contract.exchange, "CTP"], item) - # item.appendChild(subItem) + contracts = self.mainEngine.getAllContracts() + for contract in contracts: + contractName = self.getContractChineseName(contract.name) + if contractDict.has_key(contractName): + parentItem = contractDict[contractName] + item = TreeItem([contract.symbol, False, False, False, contract.exchange, "CTP"], parentItem) + parentItem.appendChild(item) + else: + item = TreeItem([contractName, False, False, False, contract.exchange, "CTP"], self.model.rootItem) + contractDict[contractName] = item + child.append(item) + subItem = TreeItem([contract.symbol, False, False, False, contract.exchange, "CTP"], item) + item.appendChild(subItem) - yumi = TreeItem([u"玉米", False, False, False, "SH", "CTP"], self.model.rootItem) - yumi.appendChild(TreeItem([u"c1705", False, False, False, "SH", "CTP"], yumi)) - yumi.appendChild(TreeItem([u"c1703", False, False, False, "SH", "CTP"], yumi)) - yumi.appendChild(TreeItem([u"c1707", False, False, False, "SH", "CTP"], yumi)) - yumi.appendChild(TreeItem([u"c1709", False, False, False, "SH", "CTP"], yumi)) - dianfen = TreeItem([u"淀粉", False, False, False, "SH", "CTP"], self.model.rootItem) - dianfen.appendChild(TreeItem([u"d1705", False, False, False, "SH", "CTP"], dianfen)) - dianfen.appendChild(TreeItem([u"d1703", False, False, False, "SH", "CTP"], dianfen)) - dianfen.appendChild(TreeItem([u"d1707", False, False, False, "SH", "CTP"], dianfen)) - dianfen.appendChild(TreeItem([u"d1709", False, False, False, "SH", "CTP"], dianfen)) - - child.append(yumi) - child.append(dianfen) + # yumi = TreeItem([u"玉米", False, False, False, "SH", "CTP"], self.model.rootItem) + # yumi.appendChild(TreeItem([u"c1705", False, False, False, "SH", "CTP"], yumi)) + # yumi.appendChild(TreeItem([u"c1703", False, False, False, "SH", "CTP"], yumi)) + # yumi.appendChild(TreeItem([u"c1707", False, False, False, "SH", "CTP"], yumi)) + # yumi.appendChild(TreeItem([u"c1709", False, False, False, "SH", "CTP"], yumi)) + # dianfen = TreeItem([u"淀粉", False, False, False, "SH", "CTP"], self.model.rootItem) + # dianfen.appendChild(TreeItem([u"d1705", False, False, False, "SH", "CTP"], dianfen)) + # dianfen.appendChild(TreeItem([u"d1703", False, False, False, "SH", "CTP"], dianfen)) + # dianfen.appendChild(TreeItem([u"d1707", False, False, False, "SH", "CTP"], dianfen)) + # dianfen.appendChild(TreeItem([u"d1709", False, False, False, "SH", "CTP"], dianfen)) + # + # child.append(yumi) + # child.append(dianfen) self.model.setDataSource(child) self.qTreeView.expandAll() + def saveSetting(self): + setting = {} + setting["tick"] = [] + setting["bar"] = [] + setting["active"] = {} + queue = Queue() + queue.put(self.model.rootItem) + while queue.qsize() > 0: + item = queue.get() + for child in item.childItems: + queue.put(child) + if item.parentItem is not None and item.parentItem != self.model.rootItem: + name = item.data(0) + interface = item.data(5) + if item.data(1): + setting["tick"].append([name, interface]) + if item.data(2): + setting["bar"].append([name, interface]) + if item.data(3): + setting["active"][item.parentItem.data(0)] = name + if self.mainEngine.drEngine.saveSetting(setting): + self.close() + + def selectAllTick(self): self.selectAll(True, False, True) diff --git a/vn.trader/dataRecorder/uiDrWidget.py b/vn.trader/dataRecorder/uiDrWidget.py index 06ffd80c..41ba66ea 100644 --- a/vn.trader/dataRecorder/uiDrWidget.py +++ b/vn.trader/dataRecorder/uiDrWidget.py @@ -6,6 +6,9 @@ import json +from PyQt4.QtCore import QObject + +from dataRecorder.drEngine import DrEngine from dataRecorder.uiDrEdit import DrEditWidget from eventEngine import * from uiBasicWidget import QtGui, QtCore @@ -99,15 +102,22 @@ class DrEngineManager(QtGui.QWidget): grid.addWidget(self.barTable, 1, 1) grid.addWidget(self.activeTable, 1, 2) - btn = QtGui.QPushButton(u'编辑', self) + btnEdit = QtGui.QPushButton(u'编辑', self) + btnRestart = QtGui.QPushButton(u'更新数据', self) vbox = QtGui.QVBoxLayout() vbox.addLayout(grid) - vbox.addWidget(btn) + + vline = QtGui.QHBoxLayout() + vline.addWidget(btnEdit) + vline.addWidget(btnRestart) + + vbox.addLayout(vline) vbox.addWidget(self.logMonitor) self.setLayout(vbox) - btn.clicked.connect(self.openDr) + btnEdit.clicked.connect(self.openDr) + btnRestart.clicked.connect(self.restart) #---------------------------------------------------------------------- def updateLog(self, event): @@ -125,6 +135,11 @@ class DrEngineManager(QtGui.QWidget): #---------------------------------------------------------------------- def updateSetting(self): """显示引擎行情记录配置""" + + self.tickTable.clearContents() + self.barTable.clearContents() + self.activeTable.clearContents() + with open(self.drEngine.settingFileName) as f: drSetting = json.load(f) @@ -158,3 +173,8 @@ class DrEngineManager(QtGui.QWidget): self.mDrEditWidget = DrEditWidget(self.mainEngine, self.eventEngine) self.mDrEditWidget.setFixedSize(500, 500) self.mDrEditWidget.show() + QObject.connect(self.mDrEditWidget, QtCore.SIGNAL('closed()'), self, QtCore.SIGNAL('restart()')) + + def restart(self): + self.updateSetting() + self.mainEngine.drEngine = DrEngine(self.mainEngine, self.mainEngine.eventEngine)