diff --git a/examples/VnTrader/VT_setting.json b/examples/VnTrader/VT_setting.json new file mode 100644 index 00000000..c9bdcc0b --- /dev/null +++ b/examples/VnTrader/VT_setting.json @@ -0,0 +1,11 @@ +{ + "fontFamily": "微软雅黑", + "fontSize": 12, + + "mongoHost": "localhost", + "mongoPort": 27017, + "mongoLogging": true, + + "darkStyle": true, + "language": "chinese" +} \ No newline at end of file diff --git a/vnpy/trader/app/dataRecorder/drEngine.py b/vnpy/trader/app/dataRecorder/drEngine.py index ec4641c2..63a20e57 100644 --- a/vnpy/trader/app/dataRecorder/drEngine.py +++ b/vnpy/trader/app/dataRecorder/drEngine.py @@ -218,7 +218,9 @@ class DrEngine(object): bar = self.barDict[vtSymbol] # 如果第一个TICK或者新的一分钟 - if not bar.datetime or bar.datetime.minute != tick.datetime.minute: + if (not bar.datetime or + bar.datetime.minute != tick.datetime.minute or + bar.datetime.hour != tick.datetime.hour): if bar.vtSymbol: newBar = copy.copy(bar) self.insertData(MINUTE_DB_NAME, vtSymbol, newBar) diff --git a/vnpy/trader/gateway/cshshlpGateway/cshshlpGateway.py b/vnpy/trader/gateway/cshshlpGateway/cshshlpGateway.py index 63fdf361..8e8689cb 100644 --- a/vnpy/trader/gateway/cshshlpGateway/cshshlpGateway.py +++ b/vnpy/trader/gateway/cshshlpGateway/cshshlpGateway.py @@ -92,15 +92,15 @@ class CshshlpGateway(VtGateway): self.qryEnabled = False # 是否要启动循环查询 + self.fileName = self.gatewayName + '_connect.json' + self.filePath = getJsonPath(self.fileName, __file__) + #---------------------------------------------------------------------- def connect(self): """连接""" # 载入json文件 - fileName = self.gatewayName + '_connect.json' - filePath = getJsonPath(fileName, __file__) - try: - f = file(filePath) + f = file(self.filePath) except IOError: log = VtLogData() log.gatewayName = self.gatewayName diff --git a/vnpy/trader/gateway/ctpGateway/ctpGateway.py b/vnpy/trader/gateway/ctpGateway/ctpGateway.py index 129bcd30..f9390b2a 100644 --- a/vnpy/trader/gateway/ctpGateway/ctpGateway.py +++ b/vnpy/trader/gateway/ctpGateway/ctpGateway.py @@ -92,15 +92,14 @@ class CtpGateway(VtGateway): self.qryEnabled = False # 循环查询 + self.fileName = self.gatewayName + '_connect.json' + self.filePath = getJsonPath(self.fileName, __file__) + #---------------------------------------------------------------------- def connect(self): """连接""" - # 载入json文件 - fileName = self.gatewayName + '_connect.json' - filePath = getJsonPath(fileName, __file__) - try: - f = file(filePath) + f = file(self.filePath) except IOError: log = VtLogData() log.gatewayName = self.gatewayName diff --git a/vnpy/trader/gateway/femasGateway/femasGateway.py b/vnpy/trader/gateway/femasGateway/femasGateway.py index 497f146b..c5962a49 100644 --- a/vnpy/trader/gateway/femasGateway/femasGateway.py +++ b/vnpy/trader/gateway/femasGateway/femasGateway.py @@ -72,15 +72,15 @@ class FemasGateway(VtGateway): self.qryEnabled = False # 是否要启动循环查询 + self.fileName = self.gatewayName + '_connect.json' + self.filePath = getJsonPath(self.fileName, __file__) + #---------------------------------------------------------------------- def connect(self): """连接""" # 载入json文件 - fileName = self.gatewayName + '_connect.json' - filePath = getJsonPath(fileName, __file__) - try: - f = file(filePath) + f = file(self.filePath) except IOError: log = VtLogData() log.gatewayName = self.gatewayName diff --git a/vnpy/trader/gateway/huobiGateway/huobiGateway.py b/vnpy/trader/gateway/huobiGateway/huobiGateway.py index dd7b6224..cb36f3c2 100644 --- a/vnpy/trader/gateway/huobiGateway/huobiGateway.py +++ b/vnpy/trader/gateway/huobiGateway/huobiGateway.py @@ -60,15 +60,15 @@ class HuobiGateway(VtGateway): self.tradeApi = HuobiTradeApi(self) self.dataApi = HuobiDataApi(self) + self.fileName = self.gatewayName + '_connect.json' + self.filePath = getJsonPath(self.fileName, __file__) + #---------------------------------------------------------------------- def connect(self): """连接""" # 载入json文件 - fileName = self.gatewayName + '_connect.json' - filePath = getJsonPath(fileName, __file__) - try: - f = file(filePath) + f = file(self.filePath) except IOError: log = VtLogData() log.gatewayName = self.gatewayName diff --git a/vnpy/trader/gateway/ibGateway/ibGateway.py b/vnpy/trader/gateway/ibGateway/ibGateway.py index 51c12e5e..6104f002 100644 --- a/vnpy/trader/gateway/ibGateway/ibGateway.py +++ b/vnpy/trader/gateway/ibGateway/ibGateway.py @@ -138,16 +138,16 @@ class IbGateway(VtGateway): self.connected = False # 连接状态 self.api = IbWrapper(self) # API接口 + + self.fileName = self.gatewayName + '_connect.json' + self.filePath = getJsonPath(self.fileName, __file__) #---------------------------------------------------------------------- def connect(self): """连接""" # 载入json文件 - fileName = self.gatewayName + '_connect.json' - filePath = getJsonPath(fileName, __file__) - try: - f = file(filePath) + f = file(self.filePath) except IOError: log = VtLogData() log.gatewayName = self.gatewayName diff --git a/vnpy/trader/gateway/ksgoldGateway/ksgoldGateway.py b/vnpy/trader/gateway/ksgoldGateway/ksgoldGateway.py index a23fa1cf..d7ebb69e 100644 --- a/vnpy/trader/gateway/ksgoldGateway/ksgoldGateway.py +++ b/vnpy/trader/gateway/ksgoldGateway/ksgoldGateway.py @@ -46,15 +46,15 @@ class KsgoldGateway(VtGateway): self.orderInited = False # 委托初始化查询 self.tradeInited = False # 成交初始化查询 + self.fileName = self.gatewayName + '_connect.json' + self.filePath = getJsonPath(self.fileName, __file__) + #---------------------------------------------------------------------- def connect(self): """连接""" # 载入json文件 - fileName = self.gatewayName + '_connect.json' - filePath = getJsonPath(fileName, __file__) - try: - f = file(filePath) + f = file(self.filePath) except IOError: log = VtLogData() log.gatewayName = self.gatewayName diff --git a/vnpy/trader/gateway/ksotpGateway/ksotpGateway.py b/vnpy/trader/gateway/ksotpGateway/ksotpGateway.py index fdfead52..c5b0a465 100644 --- a/vnpy/trader/gateway/ksotpGateway/ksotpGateway.py +++ b/vnpy/trader/gateway/ksotpGateway/ksotpGateway.py @@ -68,15 +68,15 @@ class KsotpGateway(VtGateway): self.qryEnabled = False # 是否要启动循环查询 + self.fileName = self.gatewayName + '_connect.json' + self.filePath = getJsonPath(self.fileName, __file__) + #---------------------------------------------------------------------- def connect(self): """连接""" # 载入json文件 - fileName = self.gatewayName + '_connect.json' - filePath = getJsonPath(fileName, __file__) - try: - f = file(filePath) + f = file(self.filePath) except IOError: log = VtLogData() log.gatewayName = self.gatewayName diff --git a/vnpy/trader/gateway/lhangGateway/lhangGateway.py b/vnpy/trader/gateway/lhangGateway/lhangGateway.py index 3948482a..174906e6 100644 --- a/vnpy/trader/gateway/lhangGateway/lhangGateway.py +++ b/vnpy/trader/gateway/lhangGateway/lhangGateway.py @@ -47,15 +47,15 @@ class LhangGateway(VtGateway): self.api = LhangApi(self) + self.fileName = self.gatewayName + '_connect.json' + self.filePath = getJsonPath(self.fileName, __file__) + #---------------------------------------------------------------------- def connect(self): """连接""" # 载入json文件 - fileName = self.gatewayName + '_connect.json' - filePath = getJsonPath(fileName, __file__) - try: - f = file(filePath) + f = file(self.filePath) except IOError: log = VtLogData() log.gatewayName = self.gatewayName diff --git a/vnpy/trader/gateway/ltsGateway/ltsGateway.py b/vnpy/trader/gateway/ltsGateway/ltsGateway.py index 693e9542..83610ac2 100644 --- a/vnpy/trader/gateway/ltsGateway/ltsGateway.py +++ b/vnpy/trader/gateway/ltsGateway/ltsGateway.py @@ -68,16 +68,16 @@ class LtsGateway(VtGateway): self.qryConnected = False self.qryEnabled = False # 是否要启动循环查询 + + self.fileName = self.gatewayName + '_connect.json' + self.filePath = getJsonPath(self.fileName, __file__) #---------------------------------------------------------------------- def connect(self): """连接""" # 载入json 文件 - fileName = self.gatewayName + '_connect.json' - filePath = getJsonPath(fileName, __file__) - try: - f = file(filePath) + f = file(self.filePath) except IOError: log = VtLogData() log.gatewayName = self.gatewayName diff --git a/vnpy/trader/gateway/oandaGateway/oandaGateway.py b/vnpy/trader/gateway/oandaGateway/oandaGateway.py index e6cb9aff..642c81bf 100644 --- a/vnpy/trader/gateway/oandaGateway/oandaGateway.py +++ b/vnpy/trader/gateway/oandaGateway/oandaGateway.py @@ -54,15 +54,15 @@ class OandaGateway(VtGateway): self.qryEnabled = False # 是否要启动循环查询 + self.fileName = self.gatewayName + '_connect.json' + self.filePath = getJsonPath(self.fileName, __file__) + #---------------------------------------------------------------------- def connect(self): """连接""" # 载入json文件 - fileName = self.gatewayName + '_connect.json' - filePath = getJsonPath(fileName, __file__) - try: - f = file(filePath) + f = file(self.filePath) except IOError: log = VtLogData() log.gatewayName = self.gatewayName diff --git a/vnpy/trader/gateway/okcoinGateway/okcoinGateway.py b/vnpy/trader/gateway/okcoinGateway/okcoinGateway.py index 1b73dc3c..0fd23e7d 100644 --- a/vnpy/trader/gateway/okcoinGateway/okcoinGateway.py +++ b/vnpy/trader/gateway/okcoinGateway/okcoinGateway.py @@ -118,15 +118,15 @@ class OkcoinGateway(VtGateway): self.leverage = 0 self.connected = False + self.fileName = self.gatewayName + '_connect.json' + self.filePath = getJsonPath(self.fileName, __file__) + #---------------------------------------------------------------------- def connect(self): """连接""" # 载入json文件 - fileName = self.gatewayName + '_connect.json' - filePath = getJsonPath(fileName, __file__) - try: - f = file(filePath) + f = file(self.filePath) except IOError: log = VtLogData() log.gatewayName = self.gatewayName diff --git a/vnpy/trader/gateway/qdpGateway/qdpGateway.py b/vnpy/trader/gateway/qdpGateway/qdpGateway.py index d9443c75..1c621518 100644 --- a/vnpy/trader/gateway/qdpGateway/qdpGateway.py +++ b/vnpy/trader/gateway/qdpGateway/qdpGateway.py @@ -78,15 +78,14 @@ class QdpGateway(VtGateway): self.qryEnabled = False # 是否要启动循环查询 + self.fileName = self.gatewayName + '_connect.json' + self.filePath = getJsonPath(self.fileName, __file__) + #---------------------------------------------------------------------- def connect(self): - """连接""" - # 载入json文件 - fileName = self.gatewayName + '_connect.json' - filePath = getJsonPath(fileName, __file__) - + """连接""" try: - f = file(filePath) + f = file(self.filePath) except IOError: log = VtLogData() log.gatewayName = self.gatewayName diff --git a/vnpy/trader/gateway/sgitGateway/sgitGateway.py b/vnpy/trader/gateway/sgitGateway/sgitGateway.py index ecee3026..d46501a3 100644 --- a/vnpy/trader/gateway/sgitGateway/sgitGateway.py +++ b/vnpy/trader/gateway/sgitGateway/sgitGateway.py @@ -89,15 +89,14 @@ class SgitGateway(VtGateway): self.qryEnabled = False # 是否要启动循环查询 + self.fileName = self.gatewayName + '_connect.json' + self.filePath = getJsonPath(self.fileName, __file__) + #---------------------------------------------------------------------- def connect(self): """连接""" - # 载入json文件 - fileName = self.gatewayName + '_connect.json' - filePath = getJsonPath(fileName, __file__) - try: - f = file(filePath) + f = file(self.filePath) except IOError: log = VtLogData() log.gatewayName = self.gatewayName diff --git a/vnpy/trader/gateway/shzdGateway/shzdGateway.py b/vnpy/trader/gateway/shzdGateway/shzdGateway.py index ae192271..3ed9f305 100644 --- a/vnpy/trader/gateway/shzdGateway/shzdGateway.py +++ b/vnpy/trader/gateway/shzdGateway/shzdGateway.py @@ -69,15 +69,14 @@ class ShzdGateway(VtGateway): self.qryEnabled = False # 是否要启动循环查询 + self.fileName = self.gatewayName + '_connect.json' + self.filePath = getJsonPath(self.fileName, __file__) + #---------------------------------------------------------------------- def connect(self): """连接""" - # 载入json文件 - fileName = self.gatewayName + '_connect.json' - filePath = getJsonPath(fileName, __file__) - try: - f = file(filePath) + f = file(self.filePath) except IOError: log = VtLogData() log.gatewayName = self.gatewayName diff --git a/vnpy/trader/gateway/xspeedGateway/xspeedGateway.py b/vnpy/trader/gateway/xspeedGateway/xspeedGateway.py index 091d4693..dd46ffef 100644 --- a/vnpy/trader/gateway/xspeedGateway/xspeedGateway.py +++ b/vnpy/trader/gateway/xspeedGateway/xspeedGateway.py @@ -78,15 +78,14 @@ class XspeedGateway(VtGateway): self.qryEnabled = False # 是否要启动循环查询 + self.fileName = self.gatewayName + '_connect.json' + self.filePath = getJsonPath(self.fileName, __file__) + #---------------------------------------------------------------------- def connect(self): - """连接""" - # 载入json文件 - fileName = self.gatewayName + '_connect.json' - filePath = getJsonPath(fileName, __file__) - + """连接""" try: - f = file(filePath) + f = file(self.filePath) except IOError: log = VtLogData() log.gatewayName = self.gatewayName diff --git a/vnpy/trader/ico/editor.ico b/vnpy/trader/ico/editor.ico new file mode 100644 index 00000000..e5227376 Binary files /dev/null and b/vnpy/trader/ico/editor.ico differ diff --git a/vnpy/trader/language/chinese/text.py b/vnpy/trader/language/chinese/text.py index 4627da96..6029c21a 100644 --- a/vnpy/trader/language/chinese/text.py +++ b/vnpy/trader/language/chinese/text.py @@ -121,6 +121,9 @@ RESTORE = u'还原窗口' ABOUT = u'关于' TEST = u'测试' CONNECT = u'连接' +EDIT_SETTING = u'编辑配置' +LOAD = u'读取' +SAVE = u'保存' CPU_MEMORY_INFO = u'CPU使用率:{cpu}% 内存使用率:{memory}%' CONFIRM_EXIT = u'确认退出?' diff --git a/vnpy/trader/language/english/text.py b/vnpy/trader/language/english/text.py index bf2c14b3..17b307d6 100644 --- a/vnpy/trader/language/english/text.py +++ b/vnpy/trader/language/english/text.py @@ -121,6 +121,9 @@ RESTORE = u'Restore Window' ABOUT = u'About' TEST = u'Test' CONNECT = u'Connect ' +EDIT_SETTING = 'Edit Setting' +LOAD = 'Load' +SAVE = 'Save' CPU_MEMORY_INFO = u'CPU Usage:{cpu}% Memory Usage:{memory}%' CONFIRM_EXIT = u'Confirm Exit?' diff --git a/vnpy/trader/uiBasicWidget.py b/vnpy/trader/uiBasicWidget.py index 0a793642..c88cadb9 100644 --- a/vnpy/trader/uiBasicWidget.py +++ b/vnpy/trader/uiBasicWidget.py @@ -7,11 +7,12 @@ import platform from collections import OrderedDict from vnpy.event import * -from vnpy.trader.vtEvent import * -from vnpy.trader.vtFunction import * -from vnpy.trader.vtGateway import * -from vnpy.trader import vtText -from vnpy.trader.uiQt import QtGui, QtWidgets, QtCore, BASIC_FONT +from .vtEvent import * +from .vtFunction import * +from .vtGateway import * +from . import vtText +from .uiQt import QtGui, QtWidgets, QtCore, BASIC_FONT +from .vtFunction import jsonPathDict COLOR_RED = QtGui.QColor('red') @@ -1229,3 +1230,90 @@ class ContractManager(QtWidgets.QWidget): self.monitor.refresh() + +######################################################################## +class SettingEditor(QtWidgets.QWidget): + """配置编辑器""" + + #---------------------------------------------------------------------- + def __init__(self, mainEngine, parent=None): + """Constructor""" + super(SettingEditor, self).__init__(parent) + + self.mainEngine = mainEngine + self.currentFileName = '' + + self.initUi() + + #---------------------------------------------------------------------- + def initUi(self): + """初始化界面""" + self.setWindowTitle(vtText.EDIT_SETTING) + + self.comboFileName = QtWidgets.QComboBox() + self.comboFileName.addItems(jsonPathDict.keys()) + + buttonLoad = QtWidgets.QPushButton(vtText.LOAD) + buttonSave = QtWidgets.QPushButton(vtText.SAVE) + buttonLoad.clicked.connect(self.loadSetting) + buttonSave.clicked.connect(self.saveSetting) + + self.editSetting = QtWidgets.QTextEdit() + self.labelPath = QtWidgets.QLabel() + + hbox = QtWidgets.QHBoxLayout() + hbox.addWidget(self.comboFileName) + hbox.addWidget(buttonLoad) + hbox.addWidget(buttonSave) + hbox.addStretch() + + vbox = QtWidgets.QVBoxLayout() + vbox.addLayout(hbox) + vbox.addWidget(self.editSetting) + vbox.addWidget(self.labelPath) + + self.setLayout(vbox) + + #---------------------------------------------------------------------- + def loadSetting(self): + """加载配置""" + self.currentFileName = str(self.comboFileName.currentText()) + filePath = jsonPathDict[self.currentFileName] + self.labelPath.setText(filePath) + + with open(filePath) as f: + self.editSetting.clear() + + for line in f: + line = line.replace('\n', '') # 移除换行符号 + line = line.decode('UTF-8') + self.editSetting.append(line) + + #---------------------------------------------------------------------- + def saveSetting(self): + """保存配置""" + if not self.currentFileName: + return + + filePath = jsonPathDict[self.currentFileName] + + with open(filePath, 'w') as f: + content = self.editSetting.toPlainText() + content = content.encode('UTF-8') + f.write(content) + + #---------------------------------------------------------------------- + def show(self): + """显示""" + self.comboFileName.clear() + self.comboFileName.addItems(jsonPathDict.keys()) + + super(SettingEditor, self).showMaximized() + + + + + + + + diff --git a/vnpy/trader/uiMainWindow.py b/vnpy/trader/uiMainWindow.py index 27c3f5e7..2fd2769f 100644 --- a/vnpy/trader/uiMainWindow.py +++ b/vnpy/trader/uiMainWindow.py @@ -117,6 +117,7 @@ class MainWindow(QtWidgets.QMainWindow): # 帮助 helpMenu = menubar.addMenu(vtText.HELP) helpMenu.addAction(self.createAction(vtText.CONTRACT_SEARCH, self.openContract, loadIconPath('contract.ico'))) + helpMenu.addAction(self.createAction(vtText.EDIT_SETTING, self.openSettingEditor, loadIconPath('editor.ico'))) helpMenu.addSeparator() helpMenu.addAction(self.createAction(vtText.RESTORE, self.restoreWindow, loadIconPath('restore.ico'))) helpMenu.addAction(self.createAction(vtText.ABOUT, self.openAbout, loadIconPath('about.ico'))) @@ -219,6 +220,15 @@ class MainWindow(QtWidgets.QMainWindow): except KeyError: self.widgetDict['contractM'] = ContractManager(self.mainEngine) self.widgetDict['contractM'].show() + + #---------------------------------------------------------------------- + def openSettingEditor(self): + """打开配置编辑""" + try: + self.widgetDict['settingEditor'].show() + except KeyError: + self.widgetDict['settingEditor'] = SettingEditor(self.mainEngine) + self.widgetDict['settingEditor'].show() #---------------------------------------------------------------------- def closeEvent(self, event): diff --git a/vnpy/trader/vtFunction.py b/vnpy/trader/vtFunction.py index 32bc99a4..acd7405c 100644 --- a/vnpy/trader/vtFunction.py +++ b/vnpy/trader/vtFunction.py @@ -63,7 +63,11 @@ def getTempPath(name): path = os.path.join(tempPath, name) return path - + + +# JSON配置文件路径 +jsonPathDict = {} + #---------------------------------------------------------------------- def getJsonPath(name, moduleFile): """ @@ -74,10 +78,14 @@ def getJsonPath(name, moduleFile): currentFolder = os.getcwd() currentJsonPath = os.path.join(currentFolder, name) if os.path.isfile(currentJsonPath): + jsonPathDict[name] = currentJsonPath return currentJsonPath moduleFolder = os.path.abspath(os.path.dirname(moduleFile)) moduleJsonPath = os.path.join(moduleFolder, '.', name) + jsonPathDict[name] = moduleJsonPath return moduleJsonPath + + diff --git a/vnpy/trader/vtGlobal.py b/vnpy/trader/vtGlobal.py index ab5a6f8d..0b4bc72c 100644 --- a/vnpy/trader/vtGlobal.py +++ b/vnpy/trader/vtGlobal.py @@ -7,14 +7,13 @@ import os import traceback import json - +from vtFunction import getJsonPath globalSetting = {} # 全局配置字典 settingFileName = "VT_setting.json" -path = os.path.abspath(os.path.dirname(__file__)) -settingFileName = os.path.join(path, settingFileName) +settingFilePath = getJsonPath(settingFileName, __file__) try: f = file(settingFileName)