diff --git a/vn.trader/ctaAlgo/ctaEngine.py b/vn.trader/ctaAlgo/ctaEngine.py index 2f0fea08..16015a7e 100644 --- a/vn.trader/ctaAlgo/ctaEngine.py +++ b/vn.trader/ctaAlgo/ctaEngine.py @@ -33,7 +33,8 @@ from vtFunction import todayDate class CtaEngine(object): """CTA策略引擎""" settingFileName = 'CTA_setting.json' - settingFileName = os.getcwd() + '/ctaAlgo/' + settingFileName + path = os.path.abspath(os.path.dirname(__file__)) + settingFileName = os.path.join(path, settingFileName) #---------------------------------------------------------------------- def __init__(self, mainEngine, eventEngine): diff --git a/vn.trader/ctaAlgo/datayesClient.py b/vn.trader/ctaAlgo/datayesClient.py index 2659d897..4f0ceba7 100644 --- a/vn.trader/ctaAlgo/datayesClient.py +++ b/vn.trader/ctaAlgo/datayesClient.py @@ -31,6 +31,8 @@ class DatayesClient(object): def loadSetting(self): """载入配置""" try: + path = os.path.abspath(os.path.dirname(__file__)) + FILENAME = os.path.join(path, FILENAME) f = file(FILENAME) except IOError: print u'%s无法打开配置文件' % self.name diff --git a/vn.trader/ctpGateway/ctpGateway.py b/vn.trader/ctpGateway/ctpGateway.py index 731ab5f3..ff1ba72f 100644 --- a/vn.trader/ctpGateway/ctpGateway.py +++ b/vn.trader/ctpGateway/ctpGateway.py @@ -87,7 +87,8 @@ class CtpGateway(VtGateway): """连接""" # 载入json文件 fileName = self.gatewayName + '_connect.json' - fileName = os.getcwd() + '/ctpGateway/' + fileName + path = os.path.abspath(os.path.dirname(__file__)) + fileName = os.path.join(path, fileName) try: f = file(fileName) diff --git a/vn.trader/dataRecorder/drEngine.py b/vn.trader/dataRecorder/drEngine.py index 76ea1df1..c4565fc4 100644 --- a/vn.trader/dataRecorder/drEngine.py +++ b/vn.trader/dataRecorder/drEngine.py @@ -25,7 +25,8 @@ class DrEngine(object): """数据记录引擎""" settingFileName = 'DR_setting.json' - settingFileName = os.getcwd() + '/dataRecorder/' + settingFileName + path = os.path.abspath(os.path.dirname(__file__)) + settingFileName = os.path.join(path, settingFileName) #---------------------------------------------------------------------- def __init__(self, mainEngine, eventEngine): diff --git a/vn.trader/femasGateway/femasGateway.py b/vn.trader/femasGateway/femasGateway.py index 5ff9e22c..f49e47ce 100644 --- a/vn.trader/femasGateway/femasGateway.py +++ b/vn.trader/femasGateway/femasGateway.py @@ -78,8 +78,9 @@ class FemasGateway(VtGateway): """连接""" # 载入json文件 fileName = self.gatewayName + '_connect.json' - fileName = os.getcwd() + '/femasGateway/' + fileName - + path = os.path.abspath(os.path.dirname(__file__)) + fileName = os.path.join(path, fileName) + try: f = file(fileName) except IOError: diff --git a/vn.trader/ibGateway/ibGateway.py b/vn.trader/ibGateway/ibGateway.py index 66142aa3..0f4747de 100644 --- a/vn.trader/ibGateway/ibGateway.py +++ b/vn.trader/ibGateway/ibGateway.py @@ -143,7 +143,8 @@ class IbGateway(VtGateway): """连接""" # 载入json文件 fileName = self.gatewayName + '_connect.json' - fileName = os.getcwd() + '/ibGateway/' + fileName + path = os.path.abspath(os.path.dirname(__file__)) + fileName = os.path.join(path, fileName) try: f = file(fileName) diff --git a/vn.trader/ksgoldGateway/ksgoldGateway.py b/vn.trader/ksgoldGateway/ksgoldGateway.py index 81a210f0..43b66d92 100644 --- a/vn.trader/ksgoldGateway/ksgoldGateway.py +++ b/vn.trader/ksgoldGateway/ksgoldGateway.py @@ -51,7 +51,8 @@ class KsgoldGateway(VtGateway): """连接""" # 载入json文件 fileName = self.gatewayName + '_connect.json' - fileName = os.getcwd() + '/ksgoldGateway/' + fileName + path = os.path.abspath(os.path.dirname(__file__)) + fileName = os.path.join(path, fileName) try: f = file(fileName) diff --git a/vn.trader/ksotpGateway/ksotpGateway.py b/vn.trader/ksotpGateway/ksotpGateway.py index dd1b4b48..a8dcb4af 100644 --- a/vn.trader/ksotpGateway/ksotpGateway.py +++ b/vn.trader/ksotpGateway/ksotpGateway.py @@ -74,7 +74,8 @@ class KsotpGateway(VtGateway): """连接""" # 载入json文件 fileName = self.gatewayName + '_connect.json' - fileName = os.getcwd() + '/ksotpGateway/' + fileName + path = os.path.abspath(os.path.dirname(__file__)) + fileName = os.path.join(path, fileName) try: f = file(fileName) diff --git a/vn.trader/ltsGateway/ltsGateway.py b/vn.trader/ltsGateway/ltsGateway.py index 77eccdc9..4ce649c6 100644 --- a/vn.trader/ltsGateway/ltsGateway.py +++ b/vn.trader/ltsGateway/ltsGateway.py @@ -76,7 +76,8 @@ class LtsGateway(VtGateway): """连接""" # 载入json 文件 fileName = self.gatewayName + '_connect.json' - fileName = os.getcwd() + '/ltsGateway/' + fileName + path = os.path.abspath(os.path.dirname(__file__)) + fileName = os.path.join(path, fileName) try: f = file(fileName) diff --git a/vn.trader/oandaGateway/oandaGateway.py b/vn.trader/oandaGateway/oandaGateway.py index 6bc6f50d..1c524d63 100644 --- a/vn.trader/oandaGateway/oandaGateway.py +++ b/vn.trader/oandaGateway/oandaGateway.py @@ -58,7 +58,8 @@ class OandaGateway(VtGateway): """连接""" # 载入json文件 fileName = self.gatewayName + '_connect.json' - fileName = os.getcwd() + '/oandaGateway/' + fileName + path = os.path.abspath(os.path.dirname(__file__)) + fileName = os.path.join(path, fileName) try: f = file(fileName) diff --git a/vn.trader/okcoinGateway/okcoinGateway.py b/vn.trader/okcoinGateway/okcoinGateway.py index ee1d86aa..4c2f38c0 100644 --- a/vn.trader/okcoinGateway/okcoinGateway.py +++ b/vn.trader/okcoinGateway/okcoinGateway.py @@ -107,7 +107,8 @@ class OkcoinGateway(VtGateway): """连接""" # 载入json文件 fileName = self.gatewayName + '_connect.json' - fileName = os.getcwd() + '/okcoinGateway/' + fileName + path = os.path.abspath(os.path.dirname(__file__)) + fileName = os.path.join(path, fileName) try: f = file(fileName) diff --git a/vn.trader/riskManager/rmEngine.py b/vn.trader/riskManager/rmEngine.py index 15982ca7..d90e2a23 100644 --- a/vn.trader/riskManager/rmEngine.py +++ b/vn.trader/riskManager/rmEngine.py @@ -20,7 +20,8 @@ from vtGateway import VtLogData class RmEngine(object): """风控引擎""" settingFileName = 'RM_setting.json' - settingFileName = os.getcwd() + '/riskManager/' + settingFileName + path = os.path.abspath(os.path.dirname(__file__)) + settingFileName = os.path.join(path, settingFileName) name = u'风控模块' diff --git a/vn.trader/sgitGateway/sgitGateway.py b/vn.trader/sgitGateway/sgitGateway.py index 8ab1b22b..8747f4bd 100644 --- a/vn.trader/sgitGateway/sgitGateway.py +++ b/vn.trader/sgitGateway/sgitGateway.py @@ -94,7 +94,8 @@ class SgitGateway(VtGateway): """连接""" # 载入json文件 fileName = self.gatewayName + '_connect.json' - fileName = os.getcwd() + '/sgitGateway/' + fileName + path = os.path.abspath(os.path.dirname(__file__)) + fileName = os.path.join(path, fileName) try: f = file(fileName) diff --git a/vn.trader/shzdGateway/shzdGateway.py b/vn.trader/shzdGateway/shzdGateway.py index cd3352e8..eaea2303 100644 --- a/vn.trader/shzdGateway/shzdGateway.py +++ b/vn.trader/shzdGateway/shzdGateway.py @@ -77,8 +77,9 @@ class ShzdGateway(VtGateway): """连接""" # 载入json文件 fileName = self.gatewayName + '_connect.json' - fileName = os.getcwd() + '/shzdGateway/' + fileName - + path = os.path.abspath(os.path.dirname(__file__)) + fileName = os.path.join(path, fileName) + try: f = file(fileName) except IOError: diff --git a/vn.trader/uiBasicWidget.py b/vn.trader/uiBasicWidget.py index be38fae4..43c8005c 100644 --- a/vn.trader/uiBasicWidget.py +++ b/vn.trader/uiBasicWidget.py @@ -2,6 +2,7 @@ import json import csv +import os from collections import OrderedDict from PyQt4 import QtGui, QtCore @@ -14,8 +15,12 @@ from vtGateway import * #---------------------------------------------------------------------- def loadFont(): """载入字体设置""" + fileName = 'VT_setting.json' + path = os.path.abspath(os.path.dirname(__file__)) + fileName = os.path.join(path, fileName) + try: - f = file("VT_setting.json") + f = file(fileName) setting = json.load(f) family = setting['fontFamily'] size = setting['fontSize'] diff --git a/vn.trader/vtFunction.py b/vn.trader/vtFunction.py index 46f0b27c..c14286e0 100644 --- a/vn.trader/vtFunction.py +++ b/vn.trader/vtFunction.py @@ -1,9 +1,10 @@ # encoding: UTF-8 """ -包含一些开放中常用的函数 +包含一些开发中常用的函数 """ +import os import decimal import json from datetime import datetime @@ -30,8 +31,12 @@ def safeUnicode(value): #---------------------------------------------------------------------- def loadMongoSetting(): """载入MongoDB数据库的配置""" + fileName = 'VT_setting.json' + path = os.path.abspath(os.path.dirname(__file__)) + fileName = os.path.join(path, fileName) + try: - f = file("VT_setting.json") + f = file(fileName) setting = json.load(f) host = setting['mongoHost'] port = setting['mongoPort'] diff --git a/vn.trader/vtMain.py b/vn.trader/vtMain.py index a673a963..d6098a6b 100644 --- a/vn.trader/vtMain.py +++ b/vn.trader/vtMain.py @@ -1,12 +1,22 @@ # encoding: UTF-8 import sys +import os import ctypes import platform +import vtPath from vtEngine import MainEngine from uiMainWindow import * +# 文件路径名 +path = os.path.abspath(os.path.dirname(__file__)) +ICON_FILENAME = 'vnpy.ico' +ICON_FILENAME = os.path.join(path, ICON_FILENAME) + +SETTING_FILENAME = 'VT_setting.json' +SETTING_FILENAME = os.path.join(path, SETTING_FILENAME) + #---------------------------------------------------------------------- def main(): """主程序入口""" @@ -17,15 +27,15 @@ def main(): # 设置Windows底部任务栏图标 if 'Windows' in platform.uname() : ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID('vn.trader') - + # 初始化Qt应用对象 app = QtGui.QApplication(sys.argv) - app.setWindowIcon(QtGui.QIcon('vnpy.ico')) + app.setWindowIcon(QtGui.QIcon(ICON_FILENAME)) app.setFont(BASIC_FONT) # 设置Qt的皮肤 try: - f = file("VT_setting.json") + f = file(SETTING_FILENAME) setting = json.load(f) if setting['darkStyle']: import qdarkstyle diff --git a/vn.trader/vtPath.py b/vn.trader/vtPath.py new file mode 100644 index 00000000..e3da26fa --- /dev/null +++ b/vn.trader/vtPath.py @@ -0,0 +1,24 @@ +# encoding: UTF-8 + +""" +用于vn.trader的运行目录环境设置 +""" + +import os +import sys + +# 将根目录路径添加到环境变量中 +ROOT_PATH = os.path.abspath(os.path.dirname(__file__)) +sys.path.append(ROOT_PATH) + +# 将功能模块的目录路径添加到环境变量中 +# 若各目录下存在同名文件可能导致异常,请注意测试 +MODULE_PATH = {} +MODULE_PATH['CTA'] = os.path.join(ROOT_PATH, 'ctaAlgo') +MODULE_PATH['RM'] = os.path.join(ROOT_PATH, 'riskManager') +MODULE_PATH['DR'] = os.path.join(ROOT_PATH, 'dataRecorder') + +# 添加到环境变量中 +for path in MODULE_PATH.values(): + if path not in sys.path: + sys.path.append(path) diff --git a/vn.trader/xspeedGateway/xspeedGateway.py b/vn.trader/xspeedGateway/xspeedGateway.py index 670ff236..fb3de747 100644 --- a/vn.trader/xspeedGateway/xspeedGateway.py +++ b/vn.trader/xspeedGateway/xspeedGateway.py @@ -76,7 +76,8 @@ class XspeedGateway(VtGateway): """连接""" # 载入json文件 fileName = self.gatewayName + '_connect.json' - fileName = os.getcwd() + '/xspeedGateway/' + fileName + path = os.path.abspath(os.path.dirname(__file__)) + fileName = os.path.join(path, fileName) try: f = file(fileName)