From ca4a0cdafbad7049db8004d50c7307e14770250c Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Tue, 24 Jul 2018 09:16:46 +0800 Subject: [PATCH] =?UTF-8?q?[Mod]=E8=B0=83=E6=95=B4=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E4=BA=A4=E6=98=93=E7=BB=84=E4=BB=B6=E7=9B=AE=E5=BD=95=E7=BB=93?= =?UTF-8?q?=E6=9E=84=EF=BC=8C=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=8A=A8=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E8=BF=90=E8=A1=8C=E6=97=B6=E7=9B=AE=E5=BD=95=E4=B8=8B?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/trader/app/algoTrading/algo/__init__.py | 59 +++++++++++++++++++ .../app/algoTrading/{ => algo}/dmaAlgo.py | 5 +- .../app/algoTrading/{ => algo}/stAlgo.py | 5 +- .../app/algoTrading/{ => algo}/stopAlgo.py | 5 +- .../app/algoTrading/{ => algo}/twapAlgo.py | 5 +- vnpy/trader/app/algoTrading/algoEngine.py | 37 +----------- vnpy/trader/app/algoTrading/uiAlgoManager.py | 5 +- 7 files changed, 71 insertions(+), 50 deletions(-) create mode 100644 vnpy/trader/app/algoTrading/algo/__init__.py rename vnpy/trader/app/algoTrading/{ => algo}/dmaAlgo.py (97%) rename vnpy/trader/app/algoTrading/{ => algo}/stAlgo.py (97%) rename vnpy/trader/app/algoTrading/{ => algo}/stopAlgo.py (98%) rename vnpy/trader/app/algoTrading/{ => algo}/twapAlgo.py (98%) diff --git a/vnpy/trader/app/algoTrading/algo/__init__.py b/vnpy/trader/app/algoTrading/algo/__init__.py new file mode 100644 index 00000000..d6fa977a --- /dev/null +++ b/vnpy/trader/app/algoTrading/algo/__init__.py @@ -0,0 +1,59 @@ +# encoding: UTF-8 + +''' +动态载入所有的策略类 +''' +from __future__ import print_function + +import os +import importlib +import traceback + + +# 用来保存算法类和控件类的字典 +ALGO_DICT = {} +WIDGET_DICT = {} + + +#---------------------------------------------------------------------- +def loadAlgoModule(path, prefix): + """使用importlib动态载入算法""" + for root, subdirs, files in os.walk(path): + for name in files: + # 只有文件名以Algo.py结尾的才是算法文件 + if len(name)>7 and name[-7:] == 'Algo.py': + try: + # 模块名称需要模块路径前缀 + moduleName = prefix + name.replace('.py', '') + module = importlib.import_module(moduleName) + + # 获取算法类和控件类 + algo = None + widget = None + + for k in dir(module): + # 以Algo结尾的类,是算法 + if k[-4:] == 'Algo': + algo = module.__getattribute__(k) + + # 以Widget结尾的类,是控件 + if k[-6:] == 'Widget': + widget = module.__getattribute__(k) + + # 保存到字典中 + if algo and widget: + ALGO_DICT[algo.templateName] = algo + WIDGET_DICT[algo.templateName] = widget + except: + print ('-' * 20) + print ('Failed to import strategy file %s:' %moduleName) + traceback.print_exc() + + +# 遍历algo目录下的文件 +path1 = os.path.abspath(os.path.dirname(__file__)) +loadAlgoModule(path1, 'vnpy.trader.app.algoTrading.algo.') + +# 遍历工作目录下的文件 +path2 = os.getcwd() +loadAlgoModule(path2, '') \ No newline at end of file diff --git a/vnpy/trader/app/algoTrading/dmaAlgo.py b/vnpy/trader/app/algoTrading/algo/dmaAlgo.py similarity index 97% rename from vnpy/trader/app/algoTrading/dmaAlgo.py rename to vnpy/trader/app/algoTrading/algo/dmaAlgo.py index 54281d7d..d0fec233 100644 --- a/vnpy/trader/app/algoTrading/dmaAlgo.py +++ b/vnpy/trader/app/algoTrading/algo/dmaAlgo.py @@ -8,9 +8,8 @@ from vnpy.trader.vtConstant import (DIRECTION_LONG, DIRECTION_SHORT, PRICETYPE_LIMITPRICE, PRICETYPE_MARKETPRICE, STATUS_REJECTED, STATUS_CANCELLED, STATUS_ALLTRADED) from vnpy.trader.uiQt import QtWidgets - -from .algoTemplate import AlgoTemplate -from .uiAlgoWidget import AlgoWidget, QtWidgets +from vnpy.trader.app.algoTrading.algoTemplate import AlgoTemplate +from vnpy.trader.app.algoTrading.uiAlgoWidget import AlgoWidget, QtWidgets from six import text_type diff --git a/vnpy/trader/app/algoTrading/stAlgo.py b/vnpy/trader/app/algoTrading/algo/stAlgo.py similarity index 97% rename from vnpy/trader/app/algoTrading/stAlgo.py rename to vnpy/trader/app/algoTrading/algo/stAlgo.py index 9d0d6893..1bbadd27 100644 --- a/vnpy/trader/app/algoTrading/stAlgo.py +++ b/vnpy/trader/app/algoTrading/algo/stAlgo.py @@ -6,9 +6,8 @@ from collections import OrderedDict from vnpy.trader.vtConstant import (DIRECTION_LONG, DIRECTION_SHORT, OFFSET_OPEN, OFFSET_CLOSE) from vnpy.trader.uiQt import QtWidgets - -from .algoTemplate import AlgoTemplate -from .uiAlgoWidget import AlgoWidget, QtWidgets +from vnpy.trader.app.algoTrading.algoTemplate import AlgoTemplate +from vnpy.trader.app.algoTrading.uiAlgoWidget import AlgoWidget, QtWidgets diff --git a/vnpy/trader/app/algoTrading/stopAlgo.py b/vnpy/trader/app/algoTrading/algo/stopAlgo.py similarity index 98% rename from vnpy/trader/app/algoTrading/stopAlgo.py rename to vnpy/trader/app/algoTrading/algo/stopAlgo.py index 3cbdcdf3..901a8fac 100644 --- a/vnpy/trader/app/algoTrading/stopAlgo.py +++ b/vnpy/trader/app/algoTrading/algo/stopAlgo.py @@ -9,9 +9,8 @@ from vnpy.trader.vtConstant import (DIRECTION_LONG, DIRECTION_SHORT, OFFSET_OPEN, OFFSET_CLOSE, STATUS_ALLTRADED, STATUS_CANCELLED, STATUS_REJECTED) from vnpy.trader.uiQt import QtWidgets - -from .algoTemplate import AlgoTemplate -from .uiAlgoWidget import AlgoWidget, QtWidgets +from vnpy.trader.app.algoTrading.algoTemplate import AlgoTemplate +from vnpy.trader.app.algoTrading.uiAlgoWidget import AlgoWidget, QtWidgets diff --git a/vnpy/trader/app/algoTrading/twapAlgo.py b/vnpy/trader/app/algoTrading/algo/twapAlgo.py similarity index 98% rename from vnpy/trader/app/algoTrading/twapAlgo.py rename to vnpy/trader/app/algoTrading/algo/twapAlgo.py index c31aceb6..799a623d 100644 --- a/vnpy/trader/app/algoTrading/twapAlgo.py +++ b/vnpy/trader/app/algoTrading/algo/twapAlgo.py @@ -7,9 +7,8 @@ from six import text_type from vnpy.trader.vtConstant import (DIRECTION_LONG, DIRECTION_SHORT) from vnpy.trader.uiQt import QtWidgets - -from .algoTemplate import AlgoTemplate -from .uiAlgoWidget import AlgoWidget, QtWidgets +from vnpy.trader.app.algoTrading.algoTemplate import AlgoTemplate +from vnpy.trader.app.algoTrading.uiAlgoWidget import AlgoWidget, QtWidgets ######################################################################## diff --git a/vnpy/trader/app/algoTrading/algoEngine.py b/vnpy/trader/app/algoTrading/algoEngine.py index 4dde2028..678774aa 100644 --- a/vnpy/trader/app/algoTrading/algoEngine.py +++ b/vnpy/trader/app/algoTrading/algoEngine.py @@ -15,13 +15,7 @@ from vnpy.trader.vtConstant import (DIRECTION_LONG, DIRECTION_SHORT, OFFSET_CLOSETODAY, OFFSET_CLOSEYESTERDAY) from vnpy.trader.vtObject import VtSubscribeReq, VtOrderReq, VtCancelOrderReq, VtLogData - -from .twapAlgo import TwapAlgo -from .dmaAlgo import DmaAlgo -from .stopAlgo import StopAlgo -from .stAlgo import StAlgo -from .mmAlgo import MmAlgo - +from .algo import ALGO_DICT EVENT_ALGO_LOG = 'eAlgoLog' # 算法日志事件 @@ -328,32 +322,3 @@ class AlgoEngine(object): event = Event(EVENT_ALGO_SETTING) event.dict_['data'] = algoSetting self.eventEngine.put(event) - - -# 加载目录下的算法模板 -ALGO_DICT = {} -WIDGET_DICT = {} - -path = os.path.abspath(os.path.dirname(__file__)) - -for root, subdirs, files in os.walk(path): - for name in files: - # 只有文件名以Algo.py结尾的才是算法文件 - if len(name)>7 and name[-7:] == 'Algo.py': - # 模块名称需要模块路径前缀 - moduleName = 'vnpy.trader.app.algoTrading.' + name.replace('.py', '') - module = importlib.import_module(moduleName) - - # 获取算法类和控件类 - for k in dir(module): - # 以Algo结尾的类,是算法 - if k[-4:] == 'Algo': - algo = module.__getattribute__(k) - - # 以Widget结尾的类,是控件 - if k[-6:] == 'Widget': - widget = module.__getattribute__(k) - - # 保存到字典中 - ALGO_DICT[algo.templateName] = algo - WIDGET_DICT[algo.templateName] = widget diff --git a/vnpy/trader/app/algoTrading/uiAlgoManager.py b/vnpy/trader/app/algoTrading/uiAlgoManager.py index 4385aa81..a04eae7b 100644 --- a/vnpy/trader/app/algoTrading/uiAlgoManager.py +++ b/vnpy/trader/app/algoTrading/uiAlgoManager.py @@ -10,8 +10,9 @@ from vnpy.event import Event from vnpy.trader.uiQt import QtCore, QtWidgets from .algoEngine import (EVENT_ALGO_LOG, EVENT_ALGO_PARAM, - EVENT_ALGO_VAR, EVENT_ALGO_SETTING, - WIDGET_DICT) + EVENT_ALGO_VAR, EVENT_ALGO_SETTING) +from .algo import WIDGET_DICT + ########################################################################