diff --git a/examples/VnTrader/run.py b/examples/VnTrader/run.py index e174bd53..db82fbee 100644 --- a/examples/VnTrader/run.py +++ b/examples/VnTrader/run.py @@ -5,6 +5,13 @@ import sys reload(sys) sys.setdefaultencoding('utf8') +import os +# 将repostory的目录i,作为根目录,添加到系统环境中。 +ROOT_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..','..')) +sys.path.append(ROOT_PATH) + +print sys.path + # vn.trader模块 from vnpy.event import EventEngine from vnpy.trader.vtEngine import MainEngine diff --git a/vnpy/rpc/testClient.py b/vnpy/rpc/testClient.py index e613cad5..ea4647be 100644 --- a/vnpy/rpc/testClient.py +++ b/vnpy/rpc/testClient.py @@ -22,7 +22,7 @@ class TestClient(RpcClient): if __name__ == '__main__': reqAddress = 'tcp://localhost:2014' - subAddress = 'tcp://localhost:0602' + subAddress = 'tcp://localhost:2016' tc = TestClient(reqAddress, subAddress) tc.subscribeTopic('') diff --git a/vnpy/rpc/testServer.py b/vnpy/rpc/testServer.py index f067603b..c53c3efa 100644 --- a/vnpy/rpc/testServer.py +++ b/vnpy/rpc/testServer.py @@ -25,7 +25,7 @@ class TestServer(RpcServer): if __name__ == '__main__': repAddress = 'tcp://*:2014' - pubAddress = 'tcp://*:0602' + pubAddress = 'tcp://*:2016' ts = TestServer(repAddress, pubAddress) ts.start() diff --git a/vnpy/trader/noUiMain.py b/vnpy/trader/noUiMain.py index db4064cf..0a037246 100644 --- a/vnpy/trader/noUiMain.py +++ b/vnpy/trader/noUiMain.py @@ -144,12 +144,10 @@ def run_noui(): try: log_file_name = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), - 'logs', - u'noUiMain_{0}.log'.format(datetime.now().strftime('%m%d_%H%M')))) + 'logs', u'noUiMain.log')) except Exception as ex: print u'Use local dict:{0}'.format(os.getcwd()) - log_file_name = os.path.abspath(os.path.join(os.getcwd(),'logs', - u'noUiMain_{0}.log'.format(datetime.now().strftime('%m%d_%H%M')))) + log_file_name = os.path.abspath(os.path.join(os.getcwd(), 'logs', u'noUiMain.log')) setup_logger(filename=log_file_name, debug=False) noUi = NoUiMain() diff --git a/vnpy/trader/uiBasicWidget.py b/vnpy/trader/uiBasicWidget.py index 595035a9..53a61b2d 100644 --- a/vnpy/trader/uiBasicWidget.py +++ b/vnpy/trader/uiBasicWidget.py @@ -5,28 +5,16 @@ import csv import os from collections import OrderedDict -from PyQt4 import QtGui, QtCore +#from PyQt4 import QtGui, QtCore from vnpy.trader.vtEvent import * from vnpy.trader.vtFunction import * from vnpy.trader.vtGateway import * import vtText +from vnpy.trader.uiQt import QtGui, QtCore, BASIC_FONT - -#---------------------------------------------------------------------- -def loadFont(): - """载入字体设置""" - - try: - from vnpy.trader.vtGlobal import globalSetting - family = globalSetting['fontFamily'] - size = globalSetting['fontSize'] - font = QtGui.QFont(family, size) - except: - font = QtGui.QFont(u'微软雅黑', 12) - return font - -BASIC_FONT = loadFont() +QCOLOR_RED = QtGui.QColor('red') +QCOLOR_GREEN = QtGui.QColor('green') ######################################################################## @@ -168,7 +156,34 @@ class AskCell(QtGui.QTableWidgetItem): """设置内容""" self.setText(text) +######################################################################## +class PnlCell(QtGui.QTableWidgetItem): + """显示盈亏的单元格""" + # ---------------------------------------------------------------------- + def __init__(self, text=None, mainEngine=None): + """Constructor""" + super(PnlCell, self).__init__() + self.data = None + self.color = '' + if text: + self.setContent(text) + + # ---------------------------------------------------------------------- + def setContent(self, text): + """设置内容""" + self.setText(text) + + try: + value = float(text) + if value >= 0 and self.color != 'red': + self.color = 'red' + self.setForeground(QCOLOR_RED) + elif value < 0 and self.color != 'green': + self.color = 'green' + self.setForeground(QCOLOR_GREEN) + except ValueError: + pass ######################################################################## class BasicMonitor(QtGui.QTableWidget): """ diff --git a/vnpy/trader/uiMainWindow.py b/vnpy/trader/uiMainWindow.py index 32caee85..f5539530 100644 --- a/vnpy/trader/uiMainWindow.py +++ b/vnpy/trader/uiMainWindow.py @@ -161,8 +161,7 @@ class MainWindow(QtGui.QMainWindow): sysMenu.addAction(connectCtpJRAction) sysMenu.addAction(connectCtpJR2Action) - if 'CTP' in self.mainEngine.gatewayDict: - sysMenu.addAction(connectCtpAction) + sysMenu.addAction(connectCtpAction) #if 'LTS' in self.mainEngine.gatewayDict: # sysMenu.addAction(connectLtsAction) #if 'FEMAS' in self.mainEngine.gatewayDict: diff --git a/vnpy/trader/uiQt.py b/vnpy/trader/uiQt.py new file mode 100644 index 00000000..3efd3d69 --- /dev/null +++ b/vnpy/trader/uiQt.py @@ -0,0 +1,36 @@ +# encoding: UTF-8 + +import platform + +from PyQt4 import QtGui, QtCore + +from vnpy.trader.vtGlobal import globalSetting +from vnpy.trader.vtFunction import loadIconPath + +# 创建Qt应用对象 +qApp = QtGui.QApplication([]) + +# 设置Qt的皮肤 +if globalSetting['darkStyle']: + try: + import qdarkstyle + qApp.setStyleSheet(qdarkstyle.load_stylesheet(pyside=False)) + except ImportError: + pass + +# 设置Windows底部任务栏图标 +if 'Windows' in platform.uname(): + import ctypes + ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID('vn.trader') + +# 设置Qt字体 +try: + family = globalSetting['fontFamily'] + size = globalSetting['fontSize'] + BASIC_FONT = QtGui.QFont(family, size) +except: + BASIC_FONT = QtGui.QFont(u'微软雅黑', 12) +qApp.setFont(BASIC_FONT) + +# 设置Qt图标 +qApp.setWindowIcon(QtGui.QIcon(loadIconPath('vnpy.ico'))) \ No newline at end of file diff --git a/vnpy/trader/vtClient.py b/vnpy/trader/vtClient.py index fd1ae9da..8324476d 100644 --- a/vnpy/trader/vtClient.py +++ b/vnpy/trader/vtClient.py @@ -142,6 +142,21 @@ class ClientEngine(object): """查询所有的接口名称""" return self.client.getAllGatewayNames() + def getAccountInfo(self): + """读取风控的账号与仓位数据 + # Added by IncenseLee + 仅支持一个账号。不支持多账号 + 以后支持跨市场套利才更新吧。 + """ + return self.rmEngine.getAccountInfo() + + def clearData(self): + """清空数据引擎的数据""" + self.dataEngine.clearData() + self.ctaEngine.clearData() + + def saveData(self): + self.ctaEngine.saveStrategyData() #---------------------------------------------------------------------- def main(): @@ -160,7 +175,7 @@ def main(): # 创建客户端 reqAddress = 'tcp://localhost:2014' - subAddress = 'tcp://localhost:0602' + subAddress = 'tcp://localhost:2016' client = VtClient(reqAddress, subAddress, eventEngine) client.subscribeTopic('') diff --git a/vnpy/trader/vtEngine.py b/vnpy/trader/vtEngine.py index 65e65d85..6dc312d3 100644 --- a/vnpy/trader/vtEngine.py +++ b/vnpy/trader/vtEngine.py @@ -148,7 +148,7 @@ class MainEngine(object): #---------------------------------------------------------------------- - def qryAccont(self, gatewayName): + def qryAccount(self, gatewayName): """查询特定接口的账户""" if gatewayName in self.gatewayDict: gateway = self.gatewayDict[gatewayName] diff --git a/vnpy/trader/vtFunction.py b/vnpy/trader/vtFunction.py index b33335b4..561960df 100644 --- a/vnpy/trader/vtFunction.py +++ b/vnpy/trader/vtFunction.py @@ -47,6 +47,31 @@ def loadMongoSetting(): #---------------------------------------------------------------------- def todayDate(): """获取当前本机电脑时间的日期""" - return datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) + return datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) + +# 图标路径 +iconPathDict = {} + +path = os.path.abspath(os.path.join(os.path.dirname(__file__),'ico')) + +for root, subdirs, files in os.walk(path): + for fileName in files: + if '.ico' in fileName: + iconPathDict[fileName] = os.path.join(root, fileName) + +# ---------------------------------------------------------------------- +def loadIconPath(iconName): + """加载程序图标路径""" + global iconPathDict + return iconPathDict.get(iconName, '') + +# ---------------------------------------------------------------------- +def getTempPath(name): + """获取存放临时文件的路径""" + tempPath = os.path.join(os.getcwd(), 'temp') + if not os.path.exists(tempPath): + os.makedirs(tempPath) + + path = os.path.join(tempPath, name) + return path - diff --git a/vnpy/trader/vtMain.py b/vnpy/trader/vtMain.py index fc995264..2661ec40 100644 --- a/vnpy/trader/vtMain.py +++ b/vnpy/trader/vtMain.py @@ -1,21 +1,20 @@ # encoding: UTF-8 import sys - import os import ctypes import platform -# 将repostory的目录i,作为根目录,添加到系统环境中。 -root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..' , '..')) -sys.path.append(root_path) -print sys.path +# 将repostory的目录,作为根目录,添加到系统环境中。 +ROOT_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..' , '..')) +sys.path.append(ROOT_PATH) from vnpy.trader.vtEngine import MainEngine +from vnpy.trader.uiQt import qApp from vnpy.trader.uiMainWindow import * -# 加载底层接口 -from gateway import ctpGateway +# 加载底层接口 +from vnpy.trader.gateway import ctpGateway # 初始化的接口模块,以及其指定的名称,CTP是模块,value,是该模块下的多个连接配置文件,如 CTP_JR_connect.json init_gateway_names = {'CTP': ['CTP', 'CTP_Prod', 'CTP_Post', 'CTP_EBF', 'CTP_JR', 'CTP_JR2']} @@ -24,7 +23,7 @@ path = os.path.abspath(os.path.dirname(__file__)) ICON_FILENAME = 'vnpy.ico' ICON_FILENAME = os.path.join(path, ICON_FILENAME) -from setup_logger import setup_logger +from vnpy.trader.setup_logger import setup_logger setup_logger(filename='logs/vnpy_{0}.log'.format(datetime.now().strftime('%m%d_%H%M')), debug=False) # ---------------------------------------------------------------------- @@ -34,6 +33,7 @@ def main(): reload(sys) sys.setdefaultencoding('utf8') + """ # 设置Windows底部任务栏图标 if 'Windows' in platform.uname(): ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID('vn.trader') @@ -52,7 +52,7 @@ def main(): except: pass - + """ # 初始化主引擎和主窗口对象 mainEngine = MainEngine() @@ -64,7 +64,7 @@ def main(): mainWindow.showMaximized() # 在主线程中启动Qt事件循环 - sys.exit(app.exec_()) + sys.exit(qApp.exec_()) if __name__ == '__main__': main() diff --git a/vnpy/trader/vtServer.py b/vnpy/trader/vtServer.py index 35ebe813..dace151c 100644 --- a/vnpy/trader/vtServer.py +++ b/vnpy/trader/vtServer.py @@ -7,11 +7,12 @@ from datetime import datetime from time import sleep from threading import Thread - import vtEvent -from vnrpc import RpcServer -from vtEngine import MainEngine +from vnpy.rpc import RpcServer +from vnpy.trader.vtEngine import MainEngine +from vnpy.trader.gateway import ctpGateway +init_gateway_names = {'CTP': ['CTP', 'CTP_Prod', 'CTP_Post', 'CTP_EBF', 'CTP_JR', 'CTP_JR2']} ######################################################################## class VtServer(RpcServer): @@ -25,7 +26,11 @@ class VtServer(RpcServer): # 创建主引擎对象 self.engine = MainEngine() - + + for gw_name in init_gateway_names['CTP']: + print 'add {0}'.format(gw_name) + self.engine.addGateway(ctpGateway, gw_name) + # 注册主引擎的方法到服务器的RPC函数 self.register(self.engine.connect) self.register(self.engine.subscribe) @@ -33,6 +38,8 @@ class VtServer(RpcServer): self.register(self.engine.cancelOrder) self.register(self.engine.qryAccount) self.register(self.engine.qryPosition) + self.register(self.engine.checkGatewayStatus) # 检测gateway的连接状态 + self.register(self.engine.qryStatus) # 检测ctaEngine的状态 self.register(self.engine.exit) self.register(self.engine.writeLog) self.register(self.engine.dbConnect) @@ -44,6 +51,7 @@ class VtServer(RpcServer): self.register(self.engine.getOrder) self.register(self.engine.getAllWorkingOrders) self.register(self.engine.getAllGatewayNames) + self.register(self.engine.saveData) # 注册事件引擎发送的事件处理监听 self.engine.eventEngine.registerGeneralHandler(self.eventHandler) @@ -73,7 +81,7 @@ def printLog(content): def runServer(): """运行服务器""" repAddress = 'tcp://*:2014' - pubAddress = 'tcp://*:0602' + pubAddress = 'tcp://*:2016' # 创建并启动服务器 server = VtServer(repAddress, pubAddress)